Skip to content

Commit 22e977f

Browse files
authored
feat: scaffold packages/db (#168)
1 parent 638f6c9 commit 22e977f

12 files changed

Lines changed: 568 additions & 4 deletions

File tree

.claude/settings.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"permissions": {
3+
"allow": [
4+
"Bash(npm view *)",
5+
"Bash(yarn install *)",
6+
"Bash(yarn workspace *)",
7+
"Bash(yarn turbo *)",
8+
"Bash(yarn prettier *)"
9+
]
10+
}
11+
}

.env.public

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
ROOT_DOMAIN=automoderator.app
22

33
PRISMA_DATABASE_URL=postgres://chatsift:admin@127.0.0.1:${LOCAL_DATABASE_PORT}/chatsift
4+
DATABASE_URL=postgres://chatsift:admin@127.0.0.1:${LOCAL_DATABASE_PORT}/chatsift
45
REDIS_URL=redis://redis:6379
56

67
API_PORT=7004

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"type": "module",
88
"workspaces": [
99
"apps/*",
10+
"packages/db",
1011
"packages/public/*",
1112
"packages/private/*",
1213
"services/*"

packages/db/atlas.hcl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
variable "database_url" {
2+
type = string
3+
default = getenv("DATABASE_URL")
4+
}
5+
6+
env "local" {
7+
src = "file://schema/schema.sql"
8+
url = var.database_url
9+
10+
# Used by `atlas migrate diff` / `atlas migrate lint` to compute a clean diff against an
11+
# ephemeral database rather than the real dev DB.
12+
dev = "docker://postgres/17/dev?search_path=public"
13+
14+
migration {
15+
dir = "file://migrations"
16+
}
17+
18+
format {
19+
migrate {
20+
diff = "{{ sql . \" \" }}"
21+
}
22+
}
23+
}

packages/db/kanel.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/** @type {import('kanel').Config} */
2+
export default {
3+
connection: {
4+
connectionString: process.env.DATABASE_URL,
5+
},
6+
7+
schemas: ['public'],
8+
outputPath: './src/generated',
9+
preDeleteOutputFolder: true,
10+
};

packages/db/migrations/.gitkeep

Whitespace-only changes.

packages/db/package.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"$schema": "https://json.schemastore.org/package.json",
3+
"name": "@chatsift/db",
4+
"version": "0.1.0",
5+
"description": "Database schema, migrations, and query client (postgres.js + Atlas + kanel)",
6+
"private": true,
7+
"type": "module",
8+
"exports": {
9+
".": "./dist/index.js"
10+
},
11+
"types": "./dist/index.d.ts",
12+
"scripts": {
13+
"build": "tsc",
14+
"test": "vitest run",
15+
"test:watch": "vitest",
16+
"lint": "eslint src",
17+
"lint:fix": "eslint src --fix",
18+
"clean": "rimraf dist"
19+
},
20+
"devDependencies": {
21+
"@types/node": "^24.9.1",
22+
"kanel": "^4.0.2",
23+
"rimraf": "^6.0.1",
24+
"typescript": "~5.9.3"
25+
},
26+
"dependencies": {
27+
"postgres": "^3.4.9"
28+
}
29+
}

packages/db/schema/schema.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Declarative schema (source of truth for `atlas migrate diff`).
2+
-- Authored in a follow-up (see docs/roadmap/02-foundation.md Part A, checklist item 2):
3+
-- reproduces the 6 current Prisma models (Experiment, ExperimentOverride, DashboardGrant,
4+
-- AMASession, AMAPromptData, AMAQuestion + AMAQuestionState enum).

packages/db/src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import postgres from 'postgres';
2+
3+
export type Database = postgres.Sql;
4+
5+
export interface CreateDbOptions {
6+
/**
7+
* Passed through to `postgres()` as-is. Row/column naming convention (e.g. a `postgres.camel`
8+
* transform) is decided alongside the schema — see docs/roadmap/02-foundation.md Part A.
9+
*/
10+
options?: postgres.Options<Record<string, postgres.PostgresType>>;
11+
url: string;
12+
}
13+
14+
export function createDb({ url, options }: CreateDbOptions): Database {
15+
return postgres(url, options);
16+
}

packages/db/tsconfig.eslint.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "../../tsconfig.eslint.json",
3+
"include": ["src/**/*"]
4+
}

0 commit comments

Comments
 (0)