question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

A few ideas and thank you

See original GitHub issue

Hi 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:26 (15 by maintainers)

github_iconTop GitHub Comments

6reactions
koskimascommented, Sep 22, 2021

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 type T | null and you want to leave to null, you need to explicitly set to null

await db.insertInto('person').values({
  id: db.generated,
  created_at: db.generated,
  name: 'Testerson',
  some_nullable_column: null,
})

I’ll revisit the optionality later.

2reactions
xiaoyu-tamucommented, Oct 4, 2021

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

import { CustomerRepository, Database, DatabaseSchema } from './database';

  const db = new Kysely<DatabaseSchema>({
    database: process.env.DATABASE_NAME!,
    host: process.env.DATABASE_HOST!,
    user: process.env.DATABASE_USER!,
    password: process.env.DATABASE_PASSWORD!,
    dialect: 'postgres',
    plugins: [new CamelCasePlugin()],
  });

  // select unique customer by primary key
  const fetchedCustomer = await customerRepo.selectOne({
    db,
    select,
    where: { customerPkey: { customerId: -1 } },
  });

  // update unique customer by unique key
  const updatedCustomer = await customerRepo.updateOne({
    db,
    select,
    where: { customerEmailUk: { email: 'lookup@email.com' } },
    data: { activebool: true },
  });	  
Read more comments on GitHub >

github_iconTop Results From Across the Web

85 Ways to Say Thank You + Printables for Your Message
Here are a few ideas for telling someone that you are grateful for their contributions: Thank you for helping me with this project....
Read more >
Thank You Messages: Thank You Card Wording Ideas
Use our thank you card wording ideas and messages to add some charm to your next thank you note. These ideas will help...
Read more >
50 Thoughtful Messages for a Meaningful Thank You Note
From birthday thank you notes to wedding thank you card wording ideas, here is what to say in a thank you note for...
Read more >
58 Best thank you notes ideas - Pinterest
Apr 17, 2019 - Explore Kimberly Chapman's board "thank you notes", followed by 1383 people ... See more ideas about thank you notes,...
Read more >
Best Thank You Card Messages & Wording Ideas
Give gratitude to your well-wishers & party guests through a thoughtful thank you greeting card. Here are dozens of thank you messages, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found