How to share Drizzle schema in multiple projects?

Hi, I have an Express.js application running with Drizzle ORM. The schemas are defined and migration are generated in this repository, no problem. and now I'm going to have another application written in Next.js and it is going to access the same database. How can I share the Drizzle schema in multiple projects? Currently, I'm duplicating the Drizzle's schema from the previous Express.js application repository and make sure I don't run the migration. Do you have a better suggestion?
2 Replies
JipSterk
JipSterk8mo ago
A shared database package inside of a monorepo
TJ
TJOP8mo ago
Thanks, @JipSterk So this how I share the schema in monorepo: I have created two packages, db to store the schema and app to import the schema:
/
+--db
| +- schema.js
| +- index.js
| +- drizzle.config.js
|
+--app
| +- app.js
/
+--db
| +- schema.js
| +- index.js
| +- drizzle.config.js
|
+--app
| +- app.js
Note that the drizzle.config.js is created in db to generate the migration using drizzle-kit. Then the schema looks like this,
// schema.js
export const invoices = pgTable("users", { ...})
// schema.js
export const invoices = pgTable("users", { ...})
and the package main script to export the schema,
// index.js
export * from "./schema.js";
// index.js
export * from "./schema.js";
then I import the schema in the app
// app.js
import * as schema from '@app/db/schema.js'

const options = { ... }

const db = drizzle(postgres(options), { logger: true, schema })
// app.js
import * as schema from '@app/db/schema.js'

const options = { ... }

const db = drizzle(postgres(options), { logger: true, schema })
Want results from more Discord servers?
Add your server