using a monorepo with multiple apps and services

i would like to separate my migrations folder with sub directories for example a shared one where i could move all my shared schema and one sub directory per app name ? is there something remotely close to do that ? that would help alot for my ci/cd and avoid me to use some kind of post generate script to reorganize my migration folder
4 Replies
iska
iskaOP5mo ago
i would very much like avoid creating multiple drizzle config file which is also a solution but that looks kind of odd to have ten of those on my root level
rafar
rafar5mo ago
I have a setup working now I isolated all data access concerns into a package called data and there’s a drizzle config within Also the schema is there and all I expose to the test of the apps are repositories to be used on entities. I use turborepo for task orchestration and Vercel with neon integration so that whenever there’s a change in the data package it runs the generate and migrate into whatever env is deployed to if you expose all your db queries and entities from the data package you don’t need a drizzle config file anywhere else
iska
iskaOP5mo ago
i m not sure this answers my issues when running generate to generate your migrations files it will then create those migrations files for a single db which is fine if you dont have any shared generic models that can also apply to another DB say for example user model that is shared to app1 and app2 migrations will be created once based on the change they detect right ? so the only way atm is to filter those shared schema and then dispatch them by cloning them to your multiple db unless there is a more elegant solution ? ok found an idea if anyone ever needs something
import { defineConfig, Config } from 'drizzle-kit';

const schemaMap = {
tiktok: ['./libs/backend/services/models/src/lib/schema.ts'],
aideator: ['./libs/backend/services/models/src/lib/schema.ts'],
}


const localConfig: Config = {
schema: schemaMap[process.env.DB_NAME],
out: `./wranglers/migrations/${process.env.DB_NAME}`,
dialect: 'sqlite',
dbCredentials: {
url: process.env.LOCAL_DB_PATH,
},
};

const cloudConfig: Config = {
schema: schemaMap[process.env.DB_NAME],
out: `./wranglers/migrations/${process.env.DB_NAME}`,
dialect: 'sqlite',
driver: 'd1-http',
dbCredentials: {
accountId: process.env.ACCOUNT_ID,
databaseId:process.env.D1_DATABASE_ID,
token: process.env.D1_TOKEN,
},
};

export default defineConfig(process.env.LOCAL_DB_PATH ? localConfig : cloudConfig);
import { defineConfig, Config } from 'drizzle-kit';

const schemaMap = {
tiktok: ['./libs/backend/services/models/src/lib/schema.ts'],
aideator: ['./libs/backend/services/models/src/lib/schema.ts'],
}


const localConfig: Config = {
schema: schemaMap[process.env.DB_NAME],
out: `./wranglers/migrations/${process.env.DB_NAME}`,
dialect: 'sqlite',
dbCredentials: {
url: process.env.LOCAL_DB_PATH,
},
};

const cloudConfig: Config = {
schema: schemaMap[process.env.DB_NAME],
out: `./wranglers/migrations/${process.env.DB_NAME}`,
dialect: 'sqlite',
driver: 'd1-http',
dbCredentials: {
accountId: process.env.ACCOUNT_ID,
databaseId:process.env.D1_DATABASE_ID,
token: process.env.D1_TOKEN,
},
};

export default defineConfig(process.env.LOCAL_DB_PATH ? localConfig : cloudConfig);
this way only one drizzle config is required then using DB_NAME variable i can map them to schemaMap and manage my migration per app downside is to write manually all the schema 🙂
rafar
rafar5mo ago
ah got it, good point I wasn’t addressing multiple databases
Want results from more Discord servers?
Add your server