Relational and sql-like query apis in the same file

Hello! My first question here. Drizzle looks really cool and I'm trying to learn it. I have this scenario where I have my schema file like this
// schema.ts
export const proyectos = mysqlTable('my_table', { ... });
// schema.ts
export const proyectos = mysqlTable('my_table', { ... });
Then I use it in my service file
import * as schema from 'schema';

this.db.query.proyectos.findMany(...);
import * as schema from 'schema';

this.db.query.proyectos.findMany(...);
This works just fine but I also have the other kind of query type
this.db.select().from(schema.proyectos)...
this.db.select().from(schema.proyectos)...
But I don't like having to add schema. everytime so I specify in the import what I want exactly
import { proyectos } from 'schema'
this.db.select().from(proyectos)...
import { proyectos } from 'schema'
this.db.select().from(proyectos)...
This works fine but then the next stops working and it doesn't recognize it anymore
// rip
this.db.query.proyectos.findMany()... // It doesn't find 'proyectos' in 'query' anymore.
// rip
this.db.query.proyectos.findMany()... // It doesn't find 'proyectos' in 'query' anymore.
Is there a way to have both kind of queries in the same file without doing anything special?
2 Replies
Luxaritas
Luxaritas12mo ago
The key thing with the relational query builder is you need to pass the entire schema to the drizzle constructor ie db = drizzle(driver, { schema }) In most cases that should be a nonissue since your DB instance is often created in a separate file, so you can import * there But if for some reason you can’t do that, you could always destructure schema Eg
import * as schema from 'schema'
const {proyectos} = schema
import * as schema from 'schema'
const {proyectos} = schema
ericmartinezr
ericmartinezrOP12mo ago
Thanks, that helps a lot!
Want results from more Discord servers?
Add your server