-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pg): use postgres.js driver for drizzle to avoid pg mutation
- Loading branch information
1 parent
6e59b6b
commit f6a1973
Showing
16 changed files
with
1,155 additions
and
803 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
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
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
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
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,22 +1,29 @@ | ||
import { DefaultLogger } from "drizzle-orm/logger"; | ||
import { drizzle } from "drizzle-orm/node-postgres"; | ||
import { Pool } from "pg"; | ||
import { drizzle } from "drizzle-orm/postgres-js"; | ||
import { migrate } from "drizzle-orm/postgres-js/migrator"; | ||
import postgres from "postgres"; | ||
import { container, inject } from "tsyringe"; | ||
|
||
import * as billingSchemas from "@src/billing/model-schemas"; | ||
import { config } from "@src/core/config"; | ||
import { LoggerService } from "@src/core/services/logger/logger.service"; | ||
import { PostgresLoggerService } from "@src/core/services/postgres-logger/postgres-logger.service"; | ||
|
||
const pool = new Pool({ | ||
connectionString: config.POSTGRES_DB_URI | ||
}); | ||
const logger = new LoggerService({ context: "POSTGRES" }); | ||
const migrationClient = postgres(config.POSTGRES_DB_URI, { max: 1, onnotice: logger.info.bind(logger) }); | ||
const appClient = postgres(config.POSTGRES_DB_URI); | ||
|
||
export const pgDatabase = drizzle(pool, { logger: new DefaultLogger({ writer: new PostgresLoggerService() }), schema: billingSchemas }); | ||
const drizzleOptions = { logger: new DefaultLogger({ writer: new PostgresLoggerService() }), schema: billingSchemas }; | ||
|
||
export type ApiPgSchema = typeof billingSchemas; | ||
const pgMigrationDatabase = drizzle(migrationClient, drizzleOptions); | ||
export const migratePG = () => migrate(pgMigrationDatabase, { migrationsFolder: "./drizzle" }); | ||
|
||
const pgDatabase = drizzle(appClient, drizzleOptions); | ||
|
||
export const POSTGRES_DB = "POSTGRES_DB"; | ||
container.register(POSTGRES_DB, { useValue: pgDatabase }); | ||
|
||
export const InjectPg = () => inject(POSTGRES_DB); | ||
|
||
export type ApiPgDatabase = typeof pgDatabase; | ||
export type ApiPgSchema = typeof billingSchemas; |
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,4 +1,3 @@ | ||
export * from "@src/core/services/tx/tx.service"; | ||
export * from "./logger/logger.service"; | ||
export * from "@src/core/services/http-logger/http-logger.service"; | ||
export * from "./postgres-migrator/postgres-migrator.service"; |
19 changes: 0 additions & 19 deletions
19
apps/api/src/core/services/postgres-migrator/postgres-migrator.service.ts
This file was deleted.
Oops, something went wrong.
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
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,20 @@ | ||
import "reflect-metadata"; | ||
import "./open-telemetry"; | ||
|
||
import { container } from "tsyringe"; | ||
import dotenv from "dotenv"; | ||
|
||
import { initApp } from "@src/app"; | ||
import type { PostgresMigratorService } from "@src/core"; | ||
dotenv.config({ path: ".env.local" }); | ||
dotenv.config(); | ||
|
||
const { BILLING_ENABLED } = process.env; | ||
async function bootstrap() { | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
if (process.env.BILLING_ENABLED === "true") { | ||
const pg = require("./core"); | ||
await pg.migratePG(); | ||
} | ||
|
||
const migrate = | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
BILLING_ENABLED === "true" ? container.resolve<PostgresMigratorService>(require("@src/core").PostgresMigratorService).migrate() : Promise.resolve(); | ||
const entry = require("./app"); | ||
await entry.initApp(); | ||
} | ||
|
||
migrate.then(() => initApp()); | ||
bootstrap(); |
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
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
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,11 @@ | ||
import "reflect-metadata"; | ||
|
||
import dotenv from "dotenv"; | ||
import { container } from "tsyringe"; | ||
|
||
import { PostgresMigratorService } from "@src/core"; | ||
import { migratePG } from "@src/core"; | ||
|
||
dotenv.config({ path: ".env.functional.test" }); | ||
|
||
beforeAll(async () => { | ||
await container.resolve(PostgresMigratorService).migrate(); | ||
await migratePG(); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import dotenv from "dotenv"; | ||
|
||
dotenv.config({ path: ".env.local" }); | ||
dotenv.config(); |
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
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
Oops, something went wrong.