DB type error

I have looked at other help questions for this, but they don't really help me understanding the issues I have. I can do something like:
import * as schema from "@root/models/user.model";
...
export const db = drizzle(pool, { schema });
import * as schema from "@root/models/user.model";
...
export const db = drizzle(pool, { schema });
I don't really see this on the site or in the docs, or any explanation of why I need to do this. Instead, the docs show that you can throw them into db/schemas/ as whatever files you want and that's it. I do have a drizzle.config.ts file which points towards my schemas and a migrate.ts that works fine. If I don't do the above I get errors like:
Property 'users' does not exist on type 'DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?">'.
Property 'users' does not exist on type 'DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?">'.
So, my questions are: 1. Do I need use the above method of "registering" schemas? 2. Is there a way to add multiple schema files?
2 Replies
loczek
loczek13mo ago
import * as users from "./models/users";
import * as accounts from "./models/accounts";

export const db = drizzle(pool, {
schema: {
...users,
...accounts,
},
});
import * as users from "./models/users";
import * as accounts from "./models/accounts";

export const db = drizzle(pool, {
schema: {
...users,
...accounts,
},
});
I don't think there is a better way to do it than this if you have multiple schema files
Angelelz
Angelelz13mo ago
What most people do to add multiple schema files is a what they call a barrel export. Create an index.ts file and do:
export * from "./users";
export * from "./accounts";
export * from "./users";
export * from "./accounts";
then you can import like this:
import * as schema from "@root/models";
import * as schema from "@root/models";
Want results from more Discord servers?
Add your server