Error querying planetscale db

Trying to get setup with drizzle and running into this error. There's not much info here so I'm not sure how to debug it
TypeError: fetch failed
at fetch (/home/kelby/work/jobs_fe/node_modules/undici/index.js:113:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async postJSON (file:///home/kelby/work/jobs_fe/node_modules/@planetscale/database/dist/index.js:115:22)
at async Connection.execute (file:///home/kelby/work/jobs_fe/node_modules/@planetscale/database/dist/index.js:75:23)
at async load (/home/kelby/work/jobs_fe/src/routes/+page.server.ts:34:14)
at async Module.load_server_data (/node_modules/@sveltejs/kit/src/runtime/server/page/load_data.js:33:17)
at async eval (/node_modules/@sveltejs/kit/src/runtime/server/page/index.js:168:13)
TypeError: fetch failed
at fetch (/home/kelby/work/jobs_fe/node_modules/undici/index.js:113:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async postJSON (file:///home/kelby/work/jobs_fe/node_modules/@planetscale/database/dist/index.js:115:22)
at async Connection.execute (file:///home/kelby/work/jobs_fe/node_modules/@planetscale/database/dist/index.js:75:23)
at async load (/home/kelby/work/jobs_fe/src/routes/+page.server.ts:34:14)
at async Module.load_server_data (/node_modules/@sveltejs/kit/src/runtime/server/page/load_data.js:33:17)
at async eval (/node_modules/@sveltejs/kit/src/runtime/server/page/index.js:168:13)
The query I'm trying to make at line 34 of +page.server.ts is:
import { db } from "$lib/server/db";
import { users } from "$lib/server/schema";
const res = await db.select().from(users);
import { db } from "$lib/server/db";
import { users } from "$lib/server/schema";
const res = await db.select().from(users);
8 Replies
Andrii Sherman
can you show planetscale database.js connection code? try to use custom fetch
Andrii Sherman
GitHub
GitHub - planetscale/database-js: A Fetch API-compatible PlanetScal...
A Fetch API-compatible PlanetScale database driver - GitHub - planetscale/database-js: A Fetch API-compatible PlanetScale database driver
Andrii Sherman
it may be a reason just a first guess
kelbs
kelbs2y ago
Here's the connection code. It's entirely possible I'm missing a required piece of config
import { drizzle } from "drizzle-orm/planetscale-serverless";
import type { PlanetScaleDatabase } from "drizzle-orm/planetscale-serverless";

import { connect } from "@planetscale/database";

// create the connection
const connection = connect({
host: process.env["DATABASE_HOST"],
username: process.env["DATABASE_USERNAME"],
password: process.env["DATABASE_PASSWORD"]
});

export const db: PlanetScaleDatabase = drizzle(connection);
import { drizzle } from "drizzle-orm/planetscale-serverless";
import type { PlanetScaleDatabase } from "drizzle-orm/planetscale-serverless";

import { connect } from "@planetscale/database";

// create the connection
const connection = connect({
host: process.env["DATABASE_HOST"],
username: process.env["DATABASE_USERNAME"],
password: process.env["DATABASE_PASSWORD"]
});

export const db: PlanetScaleDatabase = drizzle(connection);
Andrii Sherman
yeah I guess you need to use a link I've sent you there is an example for custom fetch for planetscale database.js driver
kelbs
kelbs2y ago
I can try it. The link says to use custom fetch for node versions older than 18 but I'm on 18.7.0 I'll read more of the planetscale databasejs docs because I haven't read them yet. I've only been using drizzle docs so far to get setup I tried custom fetch, didn't work. I think its because its already using undici, the one they recommend passing as a custom fetch (can see this in the stack trace) Boom got it working! Had to import "dotenv/config"; to make sure dotenv was being used. I think my DATABASE_URL env var was undefined and that's why the fetch failed. Not sure where in that stack trace the error info is being left behind. If it's in drizzle somewhere it'd definitely help to flow that all the way back to the user. For example, when I used planetscale's examples in a separate file and executed that file with ts-node, this is the more complete error:
TypeError: fetch failed
at Object.processResponse (node:internal/deps/undici/undici:6277:34)
at node:internal/deps/undici/undici:6602:42
at node:internal/process/task_queues:140:7
at AsyncResource.runInAsyncScope (node:async_hooks:203:9)
at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)
at processTicksAndRejections (node:internal/process/task_queues:95:5) {
cause: Error: getaddrinfo ENOTFOUND undefined
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:111:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'undefined'
}
}
TypeError: fetch failed
at Object.processResponse (node:internal/deps/undici/undici:6277:34)
at node:internal/deps/undici/undici:6602:42
at node:internal/process/task_queues:140:7
at AsyncResource.runInAsyncScope (node:async_hooks:203:9)
at AsyncResource.runMicrotask (node:internal/process/task_queues:137:8)
at processTicksAndRejections (node:internal/process/task_queues:95:5) {
cause: Error: getaddrinfo ENOTFOUND undefined
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:111:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'undefined'
}
}
Andrii Sherman
Yeah! great! Thanks for the suggestion, will work on proper error providing from driver drizzle is using
kelbs
kelbs2y ago
Love this lib and I appreciate your help in looking into this! 🙏
Want results from more Discord servers?
Add your server