CfSocket is not a constructor

I am trying to connect a postgres database via Hyperdrive inside Cloudflare Worker with Hono. and I tried to follow this docs: https://developers.cloudflare.com/hyperdrive/get-started/#_top
const client = new Client({
connectionString: c.env.HYPERDRIVE.connectionString,
});
try {
await client.connect();
const results = await client.query(
`SELECT CEIL(COUNT(*) / $1) AS count
FROM users`,
[SITEMAP_BATCH_SIZE],
);
} catch (e) {}
const client = new Client({
connectionString: c.env.HYPERDRIVE.connectionString,
});
try {
await client.connect();
const results = await client.query(
`SELECT CEIL(COUNT(*) / $1) AS count
FROM users`,
[SITEMAP_BATCH_SIZE],
);
} catch (e) {}
when I run to run the application and test it, I got this runtime error:
✘ [ERROR] TypeError: CloudflareSocket is not a constructor
at getStream
(file:///home/main/node_modules/.pnpm/pg@8.16.2/node_modules/pg/lib/stream.js:42:12)
at new Connection
(file:///home/main/node_modules/.pnpm/pg@8.16.2/node_modules/pg/lib/connection.js:18:36)
at new Client (file:///home/main/node_modules/.pnpm/pg@8.16.2/node_modules/pg/lib/client.js:49:7)
at pgClient
(file:///home/main/apps/cf-worker-sitemap/src/utils/pg.ts:7:10)
at Array.sitemapUsers
(file:///home/main/apps/cf-worker-sitemap/src/routes/users.ts:17:20)
at async jsonError
(file:///home/main/node_modules/.pnpm/wrangler@4.20.5/node_modules/wrangler/templates/middleware/middleware-miniflare3-json-error.ts:22:10)
at async drainBody
(file:///home/main/node_modules/.pnpm/wrangler@4.20.5/node_modules/wrangler/templates/middleware/middleware-ensure-req-body-drained.ts:5:10)
✘ [ERROR] TypeError: CloudflareSocket is not a constructor
at getStream
(file:///home/main/node_modules/.pnpm/pg@8.16.2/node_modules/pg/lib/stream.js:42:12)
at new Connection
(file:///home/main/node_modules/.pnpm/pg@8.16.2/node_modules/pg/lib/connection.js:18:36)
at new Client (file:///home/main/node_modules/.pnpm/pg@8.16.2/node_modules/pg/lib/client.js:49:7)
at pgClient
(file:///home/main/apps/cf-worker-sitemap/src/utils/pg.ts:7:10)
at Array.sitemapUsers
(file:///home/main/apps/cf-worker-sitemap/src/routes/users.ts:17:20)
at async jsonError
(file:///home/main/node_modules/.pnpm/wrangler@4.20.5/node_modules/wrangler/templates/middleware/middleware-miniflare3-json-error.ts:22:10)
at async drainBody
(file:///home/main/node_modules/.pnpm/wrangler@4.20.5/node_modules/wrangler/templates/middleware/middleware-ensure-req-body-drained.ts:5:10)
1 Reply
eckoln
eckoln5mo ago
Could you try using @prisma/pg-worker instead of pg? It will give you a new Client() that is supported in the worker. (https://www.npmjs.com/package/@prisma/pg-worker)
npm
@prisma/pg-worker
Prisma's lightweight version of "pg" for workers. Latest version: 6.9.0, last published: 22 days ago. Start using @prisma/pg-worker in your project by running npm i @prisma/pg-worker. There are no other projects in the npm registry using @prisma/pg-worker.

Did you find this page helpful?