-
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.
Merge pull request #6 from theapexlab/db-setup
APEX-1587 - db setup
- Loading branch information
Showing
26 changed files
with
1,575 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,6 @@ node_modules | |
# local env files | ||
.env*.local | ||
|
||
sib-report.txt | ||
sib-report.txt | ||
|
||
cdk.context.json |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
services: | ||
postgres: | ||
image: "postgres:13.9" | ||
ports: | ||
- "5432:5432" | ||
env_file: | ||
- .env.local |
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { RDSDataClient } from "@aws-sdk/client-rds-data"; | ||
import { drizzle as drizzleRds } from "drizzle-orm/aws-data-api/pg"; | ||
import { migrate as migrateRds } from "drizzle-orm/aws-data-api/pg/migrator"; | ||
import { drizzle as drizzleNode } from "drizzle-orm/node-postgres"; | ||
import { migrate as migrateNode } from "drizzle-orm/node-postgres/migrator"; | ||
import pg from "pg"; | ||
|
||
const migrationsFolder = "./packages/core/db/migrations"; | ||
|
||
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, | ||
}); | ||
|
||
const db = drizzleNode(pool); | ||
|
||
return [ | ||
db, | ||
async () => { | ||
await migrateNode(db, { | ||
migrationsFolder, | ||
}); | ||
}, | ||
] as const; | ||
} | ||
|
||
const db = drizzleRds(new RDSDataClient({}), { | ||
...payload, | ||
}); | ||
|
||
return [ | ||
db, | ||
async () => { | ||
await migrateRds(db, { | ||
migrationsFolder, | ||
}); | ||
}, | ||
] as const; | ||
}; |
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,21 @@ | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
import { RDS } from "sst/node/rds"; | ||
|
||
import { dbFactory } from "./dbFactory"; | ||
|
||
export const [db, migrate] = dbFactory( | ||
process.env.IS_LOCAL || process.env.NODE_ENV === "development" | ||
? { | ||
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, | ||
}, | ||
); |
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,11 @@ | ||
CREATE TABLE IF NOT EXISTS "testItems" ( | ||
"id" varchar PRIMARY KEY NOT NULL, | ||
"payload" varchar | ||
); | ||
--> statement-breakpoint | ||
CREATE TABLE IF NOT EXISTS "users" ( | ||
"id" varchar, | ||
"team_id" varchar, | ||
"birthday" date, | ||
CONSTRAINT users_id_team_id PRIMARY KEY("id","team_id") | ||
); |
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,73 @@ | ||
{ | ||
"version": "5", | ||
"dialect": "pg", | ||
"id": "43c150de-7698-45b1-9f52-8feb96883247", | ||
"prevId": "00000000-0000-0000-0000-000000000000", | ||
"tables": { | ||
"testItems": { | ||
"name": "testItems", | ||
"schema": "", | ||
"columns": { | ||
"id": { | ||
"name": "id", | ||
"type": "varchar", | ||
"primaryKey": true, | ||
"notNull": true | ||
}, | ||
"payload": { | ||
"name": "payload", | ||
"type": "varchar", | ||
"primaryKey": false, | ||
"notNull": false | ||
} | ||
}, | ||
"indexes": {}, | ||
"foreignKeys": {}, | ||
"compositePrimaryKeys": {}, | ||
"uniqueConstraints": {} | ||
}, | ||
"users": { | ||
"name": "users", | ||
"schema": "", | ||
"columns": { | ||
"id": { | ||
"name": "id", | ||
"type": "varchar", | ||
"primaryKey": false, | ||
"notNull": false | ||
}, | ||
"team_id": { | ||
"name": "team_id", | ||
"type": "varchar", | ||
"primaryKey": false, | ||
"notNull": false | ||
}, | ||
"birthday": { | ||
"name": "birthday", | ||
"type": "date", | ||
"primaryKey": false, | ||
"notNull": false | ||
} | ||
}, | ||
"indexes": {}, | ||
"foreignKeys": {}, | ||
"compositePrimaryKeys": { | ||
"users_id_team_id": { | ||
"name": "users_id_team_id", | ||
"columns": [ | ||
"id", | ||
"team_id" | ||
] | ||
} | ||
}, | ||
"uniqueConstraints": {} | ||
} | ||
}, | ||
"enums": {}, | ||
"schemas": {}, | ||
"_meta": { | ||
"schemas": {}, | ||
"tables": {}, | ||
"columns": {} | ||
} | ||
} |
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,13 @@ | ||
{ | ||
"version": "5", | ||
"dialect": "pg", | ||
"entries": [ | ||
{ | ||
"idx": 0, | ||
"version": "5", | ||
"when": 1698670168682, | ||
"tag": "0000_clammy_vargas", | ||
"breakpoints": true | ||
} | ||
] | ||
} |
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,18 @@ | ||
import { date, pgTable, primaryKey, varchar } from "drizzle-orm/pg-core"; | ||
|
||
export const users = pgTable( | ||
"users", | ||
{ | ||
id: varchar("id"), | ||
teamId: varchar("team_id"), | ||
birthday: date("birthday", { mode: "date" }), | ||
}, | ||
(t) => ({ | ||
pk: primaryKey(t.id, t.teamId), | ||
}), | ||
); | ||
|
||
export const testItems = pgTable("testItems", { | ||
id: varchar("id").primaryKey(), | ||
payload: varchar("payload"), | ||
}); |
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.