A few ideas and thank you
See original GitHub issueHi there, I have been trying this lib this weekend. By far, this is the most powerful and lightweight type-safe sql query builder IMO. (compares to knex, zapatos and slonik)
I’m using prisma2
in production but would love to migrate some of the queries to kysely once it become stable.
I love the amazing work you have done and I think this work could be improved if it could support stricter type.
Assuming i have the following DDL
CREATE TABLE person
(
id INTEGER GENERATED BY DEFAULT AS IDENTITY
CONSTRAINT person_pk
PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
name text NOT NULL
)
I would expect the following code throw because name non-nullable
interface Person {
id: number
created_at: string
name: string
}
interface Database {
person: Person
}
const db = new Kysely<Database>
await db.insertInto('person').values({})
The current behavior typescript does not raise exception because the method value
shape is Partial<Model>
.
export type MutationObject<DB, TB extends keyof DB> = {
[C in keyof DB[TB]]?: MutationValueExpression<DB[TB][C]>;
};
So, Instead of passing single database model definition to new Kysely<Models>
, it would be nice to allow user pass both selectable definition and insertable definition in the future. Then we could get stricter type for both insert and update mutation.
interface PersonSelectable {
id: number
created_at: string
name: string
}
interface PersonInsertable {
id?: number
created_at?: string
name: string
}
interface DatabaseSelectable {
person: PersonSelectable
}
interface DatabaseInsertable {
person: PersonInsertable
}
const db = new Kysely<DatabaseSelectable, DatabaseInsertable>
// it will throw error because name is required in PersonInsertable
await db.insertInto('person').values({})
Also, do you have any plan to build/integrate database typing generator into this library? I’m using patched version of sql-ts
for now, it works great but it has some limitations. I could help on that if you like
Keep up the good work 👍
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:26 (15 by maintainers)
Top GitHub Comments
All fields are now required for inserts in the latest 0.3.5 version and the
db.generated
placeholder has been added. I wasn’t able to make nullable fields optional, so any fields that have a typeT | null
and you want to leave to null, you need to explicitly set to nullI’ll revisit the optionality later.
I created a typings and shortcuts generator that using kysely for kysely over this weekend.
This library is still work in progress and docs and tests are still missing.
But the database introspection is ready to use.
npx ormless@latest -c ormless.config.json
https://github.com/xiaoyu-tamu/ormless/tree/main/example https://github.com/xiaoyu-tamu/ormless