Repository navigation
Building without available db connection #4449
Replies: 3 comments
|
Hi, did you find a better way since ? I'm having the same issue |
|
Drizzle itself doesn't open a connection at build time,
A. Read the DB URL with // db.ts
import { env } from '$env/dynamic/private';
import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
const pool = mysql.createPool(env.DATABASE_URL);
export const db = drizzle(pool);B. Make sure nothing that touches With both in place, |
|
One missing piece in the example above: For a SvelteKit 2 setup using that env API, I'd defer initialization itself with a cached getter: // src/lib/server/db.ts
import { env } from '$env/dynamic/private';
import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
function createDb() {
const url = env.DATABASE_URL;
if (!url) throw new Error('DATABASE_URL is required at runtime');
return drizzle({ client: mysql.createPool(url) });
}
let db: ReturnType<typeof createDb> | undefined;
export function getDb() {
return (db ??= createDb());
}Call Database-backed prerendering still needs a database during the build; this only addresses initialization during module analysis. This is a docs-based suggestion, not a reproduction against your app, so if it still fails, the build stack trace would help distinguish initialization from an actual build-time query. |
Uh oh!
There was an error while loading. Please reload this page.
Hi everyone, I am having a hard time making a SvelteKit app compile using drizzle-orm.
By my understanding, drizzle requires a connection to the database even at build time, as I would like to create a docker image this is quite a hassle to provide during the build stage.
Is there any way to gracefully skip the database connection ?
I tried creating a mock db (by creating an undefined client that I type as a MySQL2Client forcefully) whenever NODE_ENV is build but this only results in other errors down the line as the db is of course not functionnal.
One person on stack overflow decided to simply set the db to null and thereafter put a condition in each and everyone of his db functions eg.
Which I guess is functionnal but leaves me thinking that there must be a better way.
All reactions