-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: workaround drizzle auto-connecting to db
- Loading branch information
1 parent
43f189f
commit 0308b95
Showing
4 changed files
with
27 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
node_modules | ||
.git | ||
.gitignore | ||
.env | ||
.env* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ WORKDIR /app | |
|
||
COPY . . | ||
|
||
ENV BUILDING true | ||
|
||
RUN npm install | ||
RUN npm run build | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,23 @@ | ||
import { drizzle } from 'drizzle-orm/mysql2'; | ||
import { MySql2Client, drizzle } from 'drizzle-orm/mysql2'; | ||
import mysql from 'mysql2/promise'; | ||
import { ENV } from '@/env'; | ||
import { BUILDING, ENV } from '@/env'; | ||
import * as schema from './schema'; | ||
|
||
const connection = await mysql.createConnection({ | ||
host: ENV.DB_HOST, | ||
user: ENV.DB_USER, | ||
database: ENV.DB_DATABASE, | ||
password: ENV.DB_PASS, | ||
}); | ||
async function createDbConnection() { | ||
if (BUILDING) { | ||
const mockConnection = {} as unknown as MySql2Client; | ||
return drizzle(mockConnection, { schema, mode: 'default' }); | ||
} | ||
|
||
export const db = drizzle(connection, { schema, mode: 'default' }); | ||
const connection = await mysql.createConnection({ | ||
host: ENV.DB_HOST, | ||
user: ENV.DB_USER, | ||
database: ENV.DB_DATABASE, | ||
password: ENV.DB_PASS, | ||
}); | ||
|
||
return drizzle(connection, { schema, mode: 'default' }); | ||
} | ||
|
||
export const db = await createDbConnection(); | ||
export { schema }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
import { createEnv } from '@t3-oss/env-nextjs'; | ||
import { z } from 'zod'; | ||
|
||
export const BUILDING = | ||
z | ||
.enum(['true'], { message: 'BUILDING must be "TRUE" if set' }) | ||
.optional() | ||
.parse(process.env.BUILDING) ?? false; | ||
|
||
export const ENV = createEnv({ | ||
server: { | ||
DB_HOST: z.string(), | ||
DB_USER: z.string(), | ||
DB_PASS: z.string(), | ||
DB_DATABASE: z.string(), | ||
}, | ||
skipValidation: BUILDING, | ||
}); |