Internal server error: Error connecting to database: fetch failed

I'm trying to send the following query to my db but it eventually times out, throwing an error:
const currentCity = await db.query.city.findFirst({
where: and(
eq(city.slug, citySlug),
eq(city.stateName, stateSlugToStateName(stateSlug))
),
});

const facilities = await db.query.facility.findMany({
where: and(
eq(facility.showOnListPage, true),
gte(facility.addressLat, currentCity.lat - LAT_RADIUS),
lte(facility.addressLat, currentCity.lat + LAT_RADIUS),
gte(facility.addressLng, currentCity.lng - LNG_RADIUS),
lte(facility.addressLng, currentCity.lng + LNG_RADIUS),
whereLevelOfCare(levelOfCare)
),
limit: 20,
orderBy: desc(facility.internalRating),
with: {
city: true,
apartments: true,
images: true,
amenityToFacility: {
with: {
amenity: true
}
}
},
});
const currentCity = await db.query.city.findFirst({
where: and(
eq(city.slug, citySlug),
eq(city.stateName, stateSlugToStateName(stateSlug))
),
});

const facilities = await db.query.facility.findMany({
where: and(
eq(facility.showOnListPage, true),
gte(facility.addressLat, currentCity.lat - LAT_RADIUS),
lte(facility.addressLat, currentCity.lat + LAT_RADIUS),
gte(facility.addressLng, currentCity.lng - LNG_RADIUS),
lte(facility.addressLng, currentCity.lng + LNG_RADIUS),
whereLevelOfCare(levelOfCare)
),
limit: 20,
orderBy: desc(facility.internalRating),
with: {
city: true,
apartments: true,
images: true,
amenityToFacility: {
with: {
amenity: true
}
}
},
});
If I comment out the facilities query, the city query returns just fine, so I don't think there's anything wrong with my env setup or connector code. This is also the same query that I previously sent via prisma (or at least, my best attempt at recreating it), and it worked there, so I don't think the query is failing due to its intrinsic size. Any ideas as to how to resolve this issue?
1 Reply
zerokelvin
zerokelvinOP2y ago
Here's how I'm connecting to my db:
import { neon, neonConfig } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
import * as schema from './schema';

neonConfig.fetchConnectionCache = true;

export const getDb = (connectionString: string) => {
const sql = neon(connectionString);
const db = drizzle(sql, {schema});

return { db };
};
import { neon, neonConfig } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
import * as schema from './schema';

neonConfig.fetchConnectionCache = true;

export const getDb = (connectionString: string) => {
const sql = neon(connectionString);
const db = drizzle(sql, {schema});

return { db };
};
It looks like when I remove amenityToFacility from the with block, it seems to work as intended (and as quickly as I'd expect) Here's what my amenityToFacility relation looks like:
export const amenityToFacility = pgTable(
"_AmenityToFacility",
{
a: integer("A" as string)
.notNull()
.references(() => amenity.id, {
onDelete: "cascade",
onUpdate: "cascade",
}),
b: integer("B" as string)
.notNull()
.references(() => facility.id, {
onDelete: "cascade",
onUpdate: "cascade",
}),
},
(table) => {
return {
abUnique: uniqueIndex("_AmenityToFacility_AB_unique" as string).on(
table.a,
table.b
),
bIdx: index().on(table.b),
};
}
);

export const amenityToFacilityRelations = relations(
amenityToFacility,
({ one }) => ({
amenity: one(amenity, {
fields: [amenityToFacility.a],
references: [amenity.id],
}),
facility: one(facility, {
fields: [amenityToFacility.b],
references: [facility.id],
}),
})
);
export const amenityToFacility = pgTable(
"_AmenityToFacility",
{
a: integer("A" as string)
.notNull()
.references(() => amenity.id, {
onDelete: "cascade",
onUpdate: "cascade",
}),
b: integer("B" as string)
.notNull()
.references(() => facility.id, {
onDelete: "cascade",
onUpdate: "cascade",
}),
},
(table) => {
return {
abUnique: uniqueIndex("_AmenityToFacility_AB_unique" as string).on(
table.a,
table.b
),
bIdx: index().on(table.b),
};
}
);

export const amenityToFacilityRelations = relations(
amenityToFacility,
({ one }) => ({
amenity: one(amenity, {
fields: [amenityToFacility.a],
references: [amenity.id],
}),
facility: one(facility, {
fields: [amenityToFacility.b],
references: [facility.id],
}),
})
);
(please forgive the undescriptive column names, they were autogenerated by prisma) Commenting out amenityToFacilityRelations in schema.ts dramatically speeds up the code, even compared to circumstances where I wasn't ever referencing that relation So it seems that either I didn't define my relations correctly, or Drizzle has some performance issue in handling many-to-many relations. Would appreciate any insight anyone has into this!
Want results from more Discord servers?
Add your server