Generic Kysely Client

hey guys, i'm currently creating an ORM-agnostic queue engine. Currently I'm trying to integrate kysely as new adapter. While the adapter code is working as expected as "standalone" code, I have currently the challenge if I try to use the adapter in my example. It's working if I only have a Database definition which equals my package definition. But this isn't the case if an user wants to integrate it into their existing project. In my adapter I have the following definition:
constructor(private readonly db: Kysely<DB>) {
super()
}
constructor(private readonly db: Kysely<DB>) {
super()
}
The DB definition is the following:
export interface DB {
queue_jobs: QueueJobTableDefinition
}
export interface DB {
queue_jobs: QueueJobTableDefinition
}
This works inside the package and gives me all the type-safety. In my example, I have a custom DB definition:
interface DB {
queue_jobs: QueueJobTableDefinition
other_table: {
name: string
}
}
interface DB {
queue_jobs: QueueJobTableDefinition
other_table: {
name: string
}
}
This results in an typescript error that the types doesn't match ( which makes sense ๐Ÿ˜„ ) The question is now: Do you know a way to support custom DB definitions from the outside while keeping the type-safety in the package? It's also fine if it's not possible, maybe you could give me some hints how to solve it.
Note: Have seen something similar in the discord help ( from 2023 ), maybe this would work ( without the any? ) db.selectFrom(table) as SelectQueryBuilder<any, any, {}>
* Adapter code: https://github.com/noxify/vorsteh-queue/blob/adapter-kysely/packages/adapter-kysely/src/postgres-adapter.ts * Example: https://github.com/noxify/vorsteh-queue/tree/adapter-kysely/examples/kysely-postgres If you need more details/information or something else, please let me know.
GitHub
vorsteh-queue/packages/adapter-kysely/src/postgres-adapter.ts at ad...
A powerful, ORM-agnostic queue engine for PostgreSQL 12+. Handle background jobs, scheduled tasks, and recurring processes with ease. - noxify/vorsteh-queue
GitHub
vorsteh-queue/examples/kysely-postgres at adapter-kysely ยท noxify/...
A powerful, ORM-agnostic queue engine for PostgreSQL 12+. Handle background jobs, scheduled tasks, and recurring processes with ease. - noxify/vorsteh-queue
1 Reply
Marcus - noxify
Marcus - noxifyOPโ€ข3w ago
Had an idea which seems to work: Updated the constructor to:
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(private readonly db: Kysely<any>) {
super()

this.customDbClient = db as Kysely<DB>
}
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(private readonly db: Kysely<any>) {
super()

this.customDbClient = db as Kysely<DB>
}
}
and generate a custom client to get the correct typing inside the adapter. Inside my adapter, i now use this.customDbClient instead of this.db to get the type-safety. After running the build command, the type errors in my example are gone. Not sure if this is best/bad practise - any thoughts?

Did you find this page helpful?