Push with schema in separate files

I have my schemas setup something like customer.ts vendor.ts inventory.ts public.ts In my config I have schema: "./app/lib/data/db/schema/*" In public.ts I am creating tables with pgTable but in the non public ts files I am creating a schema with export const customer = pgSchema("customer") and then creating tables with export const customerTable = customer.table("customer", {fieilds here}) When I run drizzle-kit push:pg all that is created is anything in the public file and pgTable What am I missing?
9 Replies
A_Dev
A_DevOP14mo ago
Tried it with the simple example in docs too and it still doesn’t work ‘import { serial, text, pgTable, pgSchema } from "drizzle-orm/pg-core"; export const mySchema = pgSchema("my_schema") export const mySchemaUsers = mySchema.table('users', { id: serial('id').primaryKey(), name: text('name'), });’
Scarcer
Scarcer13mo ago
@A_Dev I do the same thing
Scarcer
Scarcer13mo ago
No description
Scarcer
Scarcer13mo ago
database/schema/index.ts
No description
Scarcer
Scarcer13mo ago
database/index.ts looks something like this:
import { schema, relations } from "./schema"

const db = drizzle(client, {
schema: {
...schema,
...relations
},
})

export {
db,
schema,
relations,
...
}
import { schema, relations } from "./schema"

const db = drizzle(client, {
schema: {
...schema,
...relations
},
})

export {
db,
schema,
relations,
...
}
tsconfig.json
{
...,
"compilerOptions": {
...,
"paths": {
...,
"@database": [
"./src/lib/server/database"
],
}
}
{
...,
"compilerOptions": {
...,
"paths": {
...,
"@database": [
"./src/lib/server/database"
],
}
}
Basic server side useage:
import { db, schema } from "@database"

const results = await db.select().from(schema.users)
// or
const results = await db.query.users.findMany()
import { db, schema } from "@database"

const results = await db.select().from(schema.users)
// or
const results = await db.query.users.findMany()
The setup docs are a mess, so it took a lot of trial and error
A_Dev
A_DevOP13mo ago
Yeah they are. Much appreciated I’ll give that a shot!
Darth_Fuzzy
Darth_Fuzzy13mo ago
@Scarcer why redeclare the schemas and relations as additional objects? surely using a barrel export would achieve the same effect and have far less code on your part eg
export * from "./users"
export * from "./email"
....other schema definitions
export * from "./users"
export * from "./email"
....other schema definitions
@Scarcer im asking from a point of best practices and effeciency. I am genuinely curious regarding the thought process behind your implementation
Scarcer
Scarcer13mo ago
@Darth_Fuzzy The reason is lame. Simply, linters in vs code don't track that implementation as well.
ASquirrel
ASquirrel10mo ago
Waking up this post again! Let's say if I want to have a schema for auth, audit and public. If I try to delegate this to different files I get: ReferenceError: Cannot access 'authSchema' before initialization The error disappears if I put all the table definitions in the same file as the authSchema. How would I approach this problem in a structured way?
Want results from more Discord servers?
Add your server