Multiple files schemas with query to Access data

I want to use query but I heard you have to import every schemas to use query. Is there a way to import every files in that folder instead of doing this below:

import * as schema1 from './schema1';
import * as schema2 from './schema2';
import { drizzle } from 'drizzle-orm/...';
const db = drizzle(client, { schema: { ...schema1, ...schema2 } });
const result = await db.query.users.findMany({
with: {
posts: true
},
});

import * as schema1 from './schema1';
import * as schema2 from './schema2';
import { drizzle } from 'drizzle-orm/...';
const db = drizzle(client, { schema: { ...schema1, ...schema2 } });
const result = await db.query.users.findMany({
with: {
posts: true
},
});
According to docs. ^^^ I have drizzle. config.ts like this already :
typescript
export default defineConfig({
schema: './drizzle/schemas/*.ts',
out: './drizzle/migrations',
dialect: 'postgresql',
}
)
typescript
export default defineConfig({
schema: './drizzle/schemas/*.ts',
out: './drizzle/migrations',
dialect: 'postgresql',
}
)
Why cant I just do
const db = drizzle(client)
const db = drizzle(client)
(basically, this wont work anymore without {schema} so I cant do db.query.user) instead of importing every schemas with specific file name any way to do this?
1 Reply
Theo Ribeiro
Theo Ribeiro5mo ago
you could create a file to aggregate all your schemas. This file would simply re-export each and every separate schema you have, then all you have to do in every instance is to import this one file: schema.ts
export * from './schema1';
export * from './schema2';
export * from './schema1';
export * from './schema2';
Want results from more Discord servers?
Add your server