diff --git a/packages/core/db/dbFactory.ts b/packages/core/db/dbFactory.ts index 531f6b3..2971f04 100644 --- a/packages/core/db/dbFactory.ts +++ b/packages/core/db/dbFactory.ts @@ -7,23 +7,22 @@ import pg from "pg"; const migrationsFolder = "./packages/core/db/migrations"; -type DbType = "node" | "aws"; - -type FactoryPayload = { - node: { - connectionString: string; - }; - aws: { - database: string; - secretArn: string; - resourceArn: string; - }; -}; - -export const dbFactory = (type: DbType, payload: FactoryPayload) => { - if (type === "node") { +type FactoryPayload = + | { + type: "node"; + connectionString: string; + } + | { + type: "aws"; + database: string; + secretArn: string; + resourceArn: string; + }; + +export const dbFactory = (payload: FactoryPayload) => { + if (payload.type === "node") { const pool = new pg.Pool({ - ...payload.node, + ...payload, }); const db = drizzleNode(pool); @@ -39,7 +38,7 @@ export const dbFactory = (type: DbType, payload: FactoryPayload) => { } const db = drizzleRds(new RDSDataClient({}), { - ...payload.aws, + ...payload, }); return [ diff --git a/packages/core/db/index.ts b/packages/core/db/index.ts index 7be71f1..0fbcec6 100644 --- a/packages/core/db/index.ts +++ b/packages/core/db/index.ts @@ -5,19 +5,17 @@ import { dbFactory } from "./dbFactory"; export const [db, migrate] = dbFactory( process.env.IS_LOCAL || process.env.NODE_ENV === "development" - ? "node" - : "aws", - { - node: { - connectionString: process.env.DB_URL ?? "", - }, - aws: { - //@ts-ignore - database: RDS.Database.defaultDatabaseName, - //@ts-ignore - secretArn: RDS.Database.secretArn, - //@ts-ignore - resourceArn: RDS.Database.clusterArn, - }, - }, + ? { + type: "node", + connectionString: process.env.DB_URL ?? "", + } + : { + type: "aws", + //@ts-ignore + database: RDS.Database.defaultDatabaseName, + //@ts-ignore + secretArn: RDS.Database.secretArn, + //@ts-ignore + resourceArn: RDS.Database.clusterArn, + }, ); diff --git a/scripts/migrateLocal.ts b/scripts/migrateLocal.ts index 9a020f1..17dd934 100644 --- a/scripts/migrateLocal.ts +++ b/scripts/migrateLocal.ts @@ -6,15 +6,9 @@ dotenv.config({ path: ".env.local", }); -const [, migrate] = dbFactory("node", { - node: { - connectionString: process.env.DB_URL ?? "", - }, - aws: { - database: "", - secretArn: "", - resourceArn: "", - }, +const [, migrate] = dbFactory({ + type: "node", + connectionString: process.env.DB_URL ?? "", }); await migrate(); diff --git a/tests/utils/testDb.ts b/tests/utils/testDb.ts index 7df9867..9e94e01 100644 --- a/tests/utils/testDb.ts +++ b/tests/utils/testDb.ts @@ -1,12 +1,15 @@ import { dbFactory } from "@/db/dbFactory"; -export const [testDb] = dbFactory(import.meta.env.VITE_CI ? "aws" : "node", { - node: { - connectionString: import.meta.env.VITE_DB_URL!, - }, - aws: { - database: import.meta.env.VITE_DB_NAME!, - secretArn: import.meta.env.VITE_DB_SECRET_ARN!, - resourceArn: import.meta.env.VITE_DB_CLUSTER_ARN!, - }, -}); +export const [testDb] = dbFactory( + import.meta.env.VITE_CI + ? { + type: "aws", + database: import.meta.env.VITE_DB_NAME!, + secretArn: import.meta.env.VITE_DB_SECRET_ARN!, + resourceArn: import.meta.env.VITE_DB_CLUSTER_ARN!, + } + : { + type: "node", + connectionString: import.meta.env.VITE_DB_URL!, + }, +);