my worker is throwing too many, error 1101.

I am migrating from Lambda, I was using this code
import pg from "pg"
import { Kysely, ParseJSONResultsPlugin, PostgresDialect } from "kysely"

import type { DB } from "../../db/src"

const { Pool } = pg

const globaldb = global as unknown as { db: Kysely<DB> }

export const db =
globaldb.db ||
new Kysely<DB>({
dialect: new PostgresDialect({ pool: new Pool({ connectionString: process.env.DATABASE_URL }) }),
plugins: [new ParseJSONResultsPlugin()],
})

if (process.env.NODE_ENV !== "production") {
globaldb.db = db
}
import pg from "pg"
import { Kysely, ParseJSONResultsPlugin, PostgresDialect } from "kysely"

import type { DB } from "../../db/src"

const { Pool } = pg

const globaldb = global as unknown as { db: Kysely<DB> }

export const db =
globaldb.db ||
new Kysely<DB>({
dialect: new PostgresDialect({ pool: new Pool({ connectionString: process.env.DATABASE_URL }) }),
plugins: [new ParseJSONResultsPlugin()],
})

if (process.env.NODE_ENV !== "production") {
globaldb.db = db
}
but in case of my worker, one request works & the other does not, not sure what's wrong here.
10 Replies
Ahmed Eid
Ahmed EidOP2mo ago
I traced it down to my db, this file exactely, something is not right here.
Walshy
Walshy2mo ago
well what do the logs say?
Ahmed Eid
Ahmed EidOP2mo ago
@Walshy
Ahmed Eid
Ahmed EidOP2mo ago
it executes fine for the first couple of times and then throws this error. I tried migrating to a D1 db and still the same error.
import { Resource } from "sst"
import { Kysely, ParseJSONResultsPlugin } from "kysely"
import { D1Dialect } from "kysely-d1"

import type { DB } from "../../db/src"

const globaldb = global as unknown as { db: Kysely<DB> }

export const db =
globaldb.db ??
new Kysely<DB>({
dialect: new D1Dialect({ database: Resource.MainDB }),
plugins: [new ParseJSONResultsPlugin()],
})

globaldb.db = db
import { Resource } from "sst"
import { Kysely, ParseJSONResultsPlugin } from "kysely"
import { D1Dialect } from "kysely-d1"

import type { DB } from "../../db/src"

const globaldb = global as unknown as { db: Kysely<DB> }

export const db =
globaldb.db ??
new Kysely<DB>({
dialect: new D1Dialect({ database: Resource.MainDB }),
plugins: [new ParseJSONResultsPlugin()],
})

globaldb.db = db
const languages = await db.selectFrom("Language").selectAll().execute()
return langauges
const languages = await db.selectFrom("Language").selectAll().execute()
return langauges
something is wrong with the global context somehow .. doing this directly inside my route handler works
const db = new Kysely<DB>({
dialect: new D1Dialect({ database: Resource.MainDB }),
plugins: [new ParseJSONResultsPlugin()],
})

const languages = await db.selectFrom("Language").selectAll().execute()
return languages
const db = new Kysely<DB>({
dialect: new D1Dialect({ database: Resource.MainDB }),
plugins: [new ParseJSONResultsPlugin()],
})

const languages = await db.selectFrom("Language").selectAll().execute()
return languages
however moving the db init to the global context breaks
const db = new Kysely<DB>({
dialect: new D1Dialect({ database: Resource.MainDB }),
plugins: [new ParseJSONResultsPlugin()],
})
const db = new Kysely<DB>({
dialect: new D1Dialect({ database: Resource.MainDB }),
plugins: [new ParseJSONResultsPlugin()],
})
no sorry I have this code living in a monorepo package
const db = new Kysely<DB>({
dialect: new D1Dialect({ database: Resource.MainDB }),
plugins: [new ParseJSONResultsPlugin()],
})
const db = new Kysely<DB>({
dialect: new D1Dialect({ database: Resource.MainDB }),
plugins: [new ParseJSONResultsPlugin()],
})
that makes it break. unfortunately I am still facing this issue. is there something wrong with exporting connections from the global scope like so ? utils/db.ts
import pg from "pg";
import { Kysely, ParseJSONResultsPlugin, PostgresDialect } from "kysely";

import type { DB } from "../types";

const { Pool } = pg;

const connectionString ="postgresql:";

export const db = new Kysely<DB>({
dialect: new PostgresDialect({ pool: new Pool({ connectionString }) }),
plugins: [new ParseJSONResultsPlugin()],
log: (event) => {
if (event.level === "error") {
console.log(event.error);
}
},
});
import pg from "pg";
import { Kysely, ParseJSONResultsPlugin, PostgresDialect } from "kysely";

import type { DB } from "../types";

const { Pool } = pg;

const connectionString ="postgresql:";

export const db = new Kysely<DB>({
dialect: new PostgresDialect({ pool: new Pool({ connectionString }) }),
plugins: [new ParseJSONResultsPlugin()],
log: (event) => {
if (event.level === "error") {
console.log(event.error);
}
},
});
could it be because of "pg" ? @Walshy ideas ? it's weird cuz 1 request works another doesn't.
Ahmed Eid
Ahmed EidOP2mo ago
Ahmed Eid
Ahmed EidOP2mo ago
it's not because of pg moving the db connection to the route and destroying it in the route. works perfectly. this works
app.get("/", async (c) => {
const db = new Kysely<DB>({
dialect: new PostgresDialect({ pool: new Pool({ connectionString }) }),
plugins: [new ParseJSONResultsPlugin()],
log: (event) => {
if (event.level === "error") {
console.log(event.error);
}
},
});

const langs = await db.selectFrom("Language").selectAll().executeTakeFirst();
return c.json(langs);
});
app.get("/", async (c) => {
const db = new Kysely<DB>({
dialect: new PostgresDialect({ pool: new Pool({ connectionString }) }),
plugins: [new ParseJSONResultsPlugin()],
log: (event) => {
if (event.level === "error") {
console.log(event.error);
}
},
});

const langs = await db.selectFrom("Language").selectAll().executeTakeFirst();
return c.json(langs);
});
but this doesn't
const db = new Kysely<DB>({
dialect: new PostgresDialect({ pool: new Pool({ connectionString }) }),
plugins: [new ParseJSONResultsPlugin()],
log: (event) => {
if (event.level === "error") {
console.log(event.error);
}
},
});

app.get("/", async (c) => {
const langs = await db.selectFrom("Language").selectAll().executeTakeFirst();
return c.json(langs);
});
const db = new Kysely<DB>({
dialect: new PostgresDialect({ pool: new Pool({ connectionString }) }),
plugins: [new ParseJSONResultsPlugin()],
log: (event) => {
if (event.level === "error") {
console.log(event.error);
}
},
});

app.get("/", async (c) => {
const langs = await db.selectFrom("Language").selectAll().executeTakeFirst();
return c.json(langs);
});
Cyb3r-Jak3
Cyb3r-Jak32mo ago
You should avoid you doing stuff like DB connections in the global scope. It is not really good for setting stuff like that up
Ahmed Eid
Ahmed EidOP2mo ago
should I init a new db connection per request, can't I cache the connection somehow to reuse it between requests ?
Cyb3r-Jak3
Cyb3r-Jak32mo ago
You could use hyperdrive which makes a pool
Ahmed Eid
Ahmed EidOP2mo ago
yeah, I am looking how to use it right now. thanks. how does this look ?
type Env = {
Bindings: { db: Hyperdrive };
Variables: { db: Kysely<DB> };
};

export const databaseMiddleware = (): MiddlewareHandler =>
createMiddleware<Env>(async (c, next) => {
const hv = c.env.db;
const db = new Kysely<DB>({
dialect: new PostgresDialect({
pool: new Pool({ connectionString: hv.connectionString }),
}),
});
c.set("db", db);
await next();
});
type Env = {
Bindings: { db: Hyperdrive };
Variables: { db: Kysely<DB> };
};

export const databaseMiddleware = (): MiddlewareHandler =>
createMiddleware<Env>(async (c, next) => {
const hv = c.env.db;
const db = new Kysely<DB>({
dialect: new PostgresDialect({
pool: new Pool({ connectionString: hv.connectionString }),
}),
});
c.set("db", db);
await next();
});
same issue thou when I try to cache the connection db
let _db: Kysely<DB> | undefined;

export const databaseMiddleware = (): MiddlewareHandler =>
createMiddleware<Env>(async (c, next) => {
const hv = c.env.db;

if (!_db) {
_db = new Kysely<DB>({
dialect: new PostgresDialect({
pool: new Pool({ connectionString: hv.connectionString }),
}),
});
}

c.set("db", _db);
await next();
});
let _db: Kysely<DB> | undefined;

export const databaseMiddleware = (): MiddlewareHandler =>
createMiddleware<Env>(async (c, next) => {
const hv = c.env.db;

if (!_db) {
_db = new Kysely<DB>({
dialect: new PostgresDialect({
pool: new Pool({ connectionString: hv.connectionString }),
}),
});
}

c.set("db", _db);
await next();
});
Want results from more Discord servers?
Add your server