Skip to content

Commit

Permalink
chore: refactor db factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Balint Dolla committed Oct 31, 2023
1 parent 675976c commit 7c1911d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 51 deletions.
33 changes: 16 additions & 17 deletions packages/core/db/dbFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -39,7 +38,7 @@ export const dbFactory = (type: DbType, payload: FactoryPayload) => {
}

const db = drizzleRds(new RDSDataClient({}), {
...payload.aws,
...payload,
});

return [
Expand Down
28 changes: 13 additions & 15 deletions packages/core/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
);
12 changes: 3 additions & 9 deletions scripts/migrateLocal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
23 changes: 13 additions & 10 deletions tests/utils/testDb.ts
Original file line number Diff line number Diff line change
@@ -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!,
},
);

0 comments on commit 7c1911d

Please sign in to comment.