Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: tables passed in delete / insert / update are not type-checked even if schema is passed in drizzle constructor. #2283

Open
sdeprez opened this issue May 9, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@sdeprez
Copy link

sdeprez commented May 9, 2024

What version of drizzle-orm are you using?

0.30.8

What version of drizzle-kit are you using?

0.20.14

Describe the Bug

(I've filed a bug report but arguably this could be considered as a missing feature).

Unless I'm missing something, the tables passed in insert, delete or update are not type-checked based on the tables (schemas) given in the drizzle constructor, at least when using Postgres (not tested with other DBs).

To reproduce, create two tables, used in two different databases.

import {drizzle} from 'drizzle-orm/node-postgres'
import {pgTable, text} from 'drizzle-orm/pg-core' 
import {Client} from 'pg'
  
const tableA = pgTable('table_a', {columnA: text('text')})
const dbA = drizzle(new Client(), {schema: {tableA}})
  
const tableB = pgTable('table_b', {columnB: text('text')})
const dbB = drizzle(new Client(), {schema: {tableB}})                                                                                                                                                                            

// No type-check error but will fail at runtime.
dbB.insert(tableA).values({columnA: 'value'}).execute()

Expected behavior

dbB.insert(tableA).values({columnA: 'value'}).execute() should have a type error, and same for update and delete.

Note

As a workaround for now, I've modified the database typing and exported a new one that have the typings for insert, delete and update scoped to the relevant tables. For instance, for dbA

import {NodePgQueryResultHKT} from 'drizzle-orm/node-postgres'
import {PgDeleteBase, PgInsertBuilder, PgUpdateBuilder} from 'drizzle-orm/pg-core

const tables = {tableA}
type Tables = (typeof tables)[keyof typeof tables]

export type DatabaseA = Omit<typeof dbA, 'delete' | 'insert' | 'update'> & {                                                                                                                                                        
  delete: <T extends Tables>(table: T) => PgDeleteBase<T, NodePgQueryResultHKT>
  insert: <T extends Tables>(table: T) => PgInsertBuilder<T, NodePgQueryResultHKT>
  update: <T extends Tables>(table: T) => PgUpdateBuilder<T, NodePgQueryResultHKT>
}
@sdeprez sdeprez added the bug Something isn't working label May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant