Skip to content

Commit

Permalink
feat(pg): use postgres.js driver for drizzle to avoid pg mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrishajev committed Jul 17, 2024
1 parent 6e59b6b commit f6a1973
Show file tree
Hide file tree
Showing 16 changed files with 1,155 additions and 803 deletions.
1 change: 1 addition & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"pg-hstore": "^2.3.4",
"pino": "^9.2.0",
"pino-pretty": "^11.2.1",
"postgres": "^3.4.4",
"protobufjs": "^6.11.2",
"semver": "^7.3.8",
"sequelize": "^6.21.3",
Expand Down
4 changes: 0 additions & 4 deletions apps/api/src/billing/config/env.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import dotenv from "dotenv";
import { z } from "zod";

dotenv.config({ path: ".env.local" });
dotenv.config();

const envSchema = z.object({
MASTER_WALLET_MNEMONIC: z.string(),
NETWORK: z.enum(["mainnet", "testnet", "sandbox"]),
Expand Down
8 changes: 6 additions & 2 deletions apps/api/src/console.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import "reflect-metadata";

import { Command } from "commander";
import dotenv from "dotenv";
import { container } from "tsyringe";

import { WalletController } from "@src/billing/controllers/wallet/wallet.controller";
import { PostgresMigratorService } from "@src/core";

dotenv.config({ path: ".env.local" });
dotenv.config();

const program = new Command();

Expand All @@ -14,7 +17,8 @@ program
.command("refill-wallets")
.description("Refill draining wallets")
.action(async () => {
await container.resolve(PostgresMigratorService).migrate();
// eslint-disable-next-line @typescript-eslint/no-var-requires
await require("./core/providers/postgres.provider").migratePG();
await container.resolve(WalletController).refillAll();
});

Expand Down
4 changes: 0 additions & 4 deletions apps/api/src/core/config/env.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import dotenv from "dotenv";
import { z } from "zod";

dotenv.config({ path: ".env.local" });
dotenv.config();

const envSchema = z.object({
LOG_LEVEL: z.enum(["fatal", "error", "warn", "info", "debug", "trace"]).optional().default("info"),
LOG_FORMAT: z.enum(["json", "pretty"]).optional().default("json"),
Expand Down
21 changes: 14 additions & 7 deletions apps/api/src/core/providers/postgres.provider.ts
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;
1 change: 0 additions & 1 deletion apps/api/src/core/services/index.ts
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";

This file was deleted.

7 changes: 5 additions & 2 deletions apps/api/src/core/services/tx/tx.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ExtractTablesWithRelations } from "drizzle-orm";
import type { NodePgQueryResultHKT } from "drizzle-orm/node-postgres";
import type { PgTransaction } from "drizzle-orm/pg-core";
import type {} from "drizzle-orm/postgres-js";
import { PostgresJsQueryResultHKT } from "drizzle-orm/postgres-js/session";
import { AsyncLocalStorage } from "node:async_hooks";
import { container, singleton } from "tsyringe";

Expand All @@ -10,7 +11,9 @@ type TxType = "PG_TX";

@singleton()
export class TxService {
private readonly storage = new AsyncLocalStorage<Map<TxType, PgTransaction<NodePgQueryResultHKT, ApiPgSchema, ExtractTablesWithRelations<ApiPgSchema>>>>();
private readonly storage = new AsyncLocalStorage<
Map<TxType, PgTransaction<PostgresJsQueryResultHKT, ApiPgSchema, ExtractTablesWithRelations<ApiPgSchema>>>
>();

constructor(@InjectPg() private readonly pg: ApiPgDatabase) {}

Expand Down
21 changes: 13 additions & 8 deletions apps/api/src/index.ts
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();
4 changes: 0 additions & 4 deletions apps/api/src/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import dotenv from "dotenv";
import { z } from "zod";

dotenv.config({ path: ".env.local" });
dotenv.config();

export const env = z
.object({
SentryDSN: z.string().optional(),
Expand Down
2 changes: 1 addition & 1 deletion apps/api/test/functional/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { closeConnections } from "@src/db/dbConnection";

describe("app", () => {
beforeAll(async () => {
await initDb({ log: false });
await initDb();
});

afterAll(async () => {
Expand Down
6 changes: 3 additions & 3 deletions apps/api/test/setup-functional-tests.ts
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();
});
4 changes: 4 additions & 0 deletions apps/api/test/setup.ts
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();
4 changes: 2 additions & 2 deletions docker/Dockerfile.nextjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ RUN apk add --no-cache libc6-compat

COPY $WORKSPACE ./$WORKSPACE
COPY /packages /app/packages
COPY package.json /app
COPY package-lock.json /app
COPY /$WORKSPACE/package*.json /app/$WORKSPACE
COPY package*.json /app

RUN npm install

Expand Down
4 changes: 2 additions & 2 deletions docker/Dockerfile.node
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ FROM base AS development

COPY /$WORKSPACE /app/$WORKSPACE
COPY /packages /app/packages
COPY package.json /app
COPY package-lock.json /app
COPY /$WORKSPACE/package*.json /app/$WORKSPACE
COPY package*.json /app

RUN npm install

Expand Down
Loading

0 comments on commit f6a1973

Please sign in to comment.