@agentbadge/database 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +170 -0
- package/dist/database.d.ts +13 -0
- package/dist/database.js +32 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +6 -0
- package/dist/prisma/contract.json +218 -0
- package/dist/prisma/db.d.ts +3 -0
- package/dist/prisma/db.js +7 -0
- package/dist/repositories/event-repository.d.ts +17 -0
- package/dist/repositories/event-repository.js +28 -0
- package/dist/repository.d.ts +10 -0
- package/dist/repository.js +2 -0
- package/dist/store.d.ts +30 -0
- package/dist/store.js +43 -0
- package/package.json +56 -0
package/README.md
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
# @agentbadge/database
|
|
2
|
+
|
|
3
|
+
Shared database layer for AgentBadge — Prisma ORM 8 contract-based client,
|
|
4
|
+
repository abstraction, and in-memory fallback. Consumers call
|
|
5
|
+
`createDatabase()` — never touch Prisma directly.
|
|
6
|
+
|
|
7
|
+
## Quickstart (clone → seeded DB in <5 min)
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
cd packages/database
|
|
11
|
+
bun install
|
|
12
|
+
bun run db:up # Postgres 17 in Docker, host port 5335
|
|
13
|
+
cp .env.example .env # DATABASE_URL + DIRECT_URL → localhost:5335
|
|
14
|
+
bun run contract:emit # regenerate contract.json/.d.ts (committed)
|
|
15
|
+
bun run db:init # bootstrap schema + sign marker
|
|
16
|
+
bun run db:seed # 3 sample events (idempotent)
|
|
17
|
+
bunx vitest run # unit tests (PG-backed when DATABASE_URL set)
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Stop / inspect / wipe:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
bun run db:down # stop container (named volume keeps data)
|
|
24
|
+
bun run db:logs # follow Postgres logs
|
|
25
|
+
docker compose down -v # wipe data completely
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Consumer guide
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { createDatabase } from "@agentbadge/database";
|
|
32
|
+
|
|
33
|
+
const database = createDatabase({
|
|
34
|
+
enabled: config.database?.enabled ?? false, // DATABASE_ENABLED gate
|
|
35
|
+
url: config.database?.url, // DATABASE_URL (pooled)
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// database.db — Prisma client facade, null when disabled
|
|
39
|
+
// database.events — Store (EventRepository on PG, InMemoryStore otherwise)
|
|
40
|
+
// database.health()— SELECT 1 probe → boolean
|
|
41
|
+
// database.close() — call ONCE at shutdown, never per-request
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
`enabled:false` or missing `url` → in-memory store, `db=null`,
|
|
45
|
+
`health()→false`, never throws. Server wiring lives in
|
|
46
|
+
`hackathon/server/src/server/lib/database.ts` (lazy `getDatabase()`
|
|
47
|
+
singleton); env section in `src/config/env/database.ts`.
|
|
48
|
+
|
|
49
|
+
**Env topology:** the package never reads env at runtime — the caller passes
|
|
50
|
+
`url`. Server runtime uses `hackathon/server/.env` locally and Fly secrets
|
|
51
|
+
in prod (`DATABASE_ENABLED` + `DATABASE_URL` only). This package's `.env` is
|
|
52
|
+
for CLI/migrations/tests inside the package dir only and is never published
|
|
53
|
+
(`files: ["dist"]`). `DIRECT_URL` (unpooled, migrations) stays local in
|
|
54
|
+
`.env.deployer` — see `RUNBOOK.md`.
|
|
55
|
+
|
|
56
|
+
## Adding a new entity
|
|
57
|
+
|
|
58
|
+
1. Add the model to `src/prisma/contract.prisma`
|
|
59
|
+
2. `bun run contract:emit` — regenerate committed artifacts
|
|
60
|
+
3. `bun run db:plan --name add_<entity>` — writes `migrations/app/<ts>_add_<entity>/`
|
|
61
|
+
4. `bun run db:migrate` — apply (single tx + advisory lock on PG)
|
|
62
|
+
5. `bun run db:verify` — marker + schema match contract
|
|
63
|
+
6. Create `src/repositories/<entity>-repository.ts` copying
|
|
64
|
+
`EventRepository` (extends `Repository<T,TCreate,TUpdate>`, implements
|
|
65
|
+
the entity's `Store` interface)
|
|
66
|
+
7. Extend the `Store` contract + `InMemoryStore` if the entity needs an
|
|
67
|
+
in-memory fallback; wire into `createDatabase()`
|
|
68
|
+
8. Tests: add the backend to the parity suite in `tests/store.test.ts`
|
|
69
|
+
|
|
70
|
+
## Scripts
|
|
71
|
+
|
|
72
|
+
| Script | Purpose |
|
|
73
|
+
|--------|---------|
|
|
74
|
+
| `bun run build` | `tsc` → `dist/` |
|
|
75
|
+
| `bun run typecheck` | `tsc --noEmit` over src + scripts + tests |
|
|
76
|
+
| `bun test` / `bunx vitest run` | unit tests (PG suites skip without `DATABASE_URL`) |
|
|
77
|
+
| `bun run db:up` / `db:down` / `db:logs` | dev Postgres lifecycle |
|
|
78
|
+
| `bun run contract:emit` | regenerate `contract.json` + `contract.d.ts` |
|
|
79
|
+
| `bun run db:init` | bootstrap DB schema + sign marker (fresh DB) |
|
|
80
|
+
| `bun run db:update` | dev-loop schema sync (destructive needs `--confirm <db>`) |
|
|
81
|
+
| `bun run db:plan --name <slug>` | write a reviewable migration package |
|
|
82
|
+
| `bun run db:migrate` | replay committed migrations (`--advance-ref db`) |
|
|
83
|
+
| `bun run db:verify` | check DB marker + live schema match contract |
|
|
84
|
+
| `bun run db:seed` | upsert 3 sample events (idempotent) |
|
|
85
|
+
|
|
86
|
+
## Contract workflow (Prisma ORM 8)
|
|
87
|
+
|
|
88
|
+
No `prisma generate`, no `generated/` client folder. The contract is the
|
|
89
|
+
source of truth; `contract.json` + `contract.d.ts` are **committed to git**
|
|
90
|
+
(D16). Migrations are on-disk packages under `migrations/app/` — replayed by
|
|
91
|
+
`db migrate`, never invented at apply time. See `RUNBOOK.md` for prod.
|
|
92
|
+
|
|
93
|
+
Pinned RC versions (API breaks between RCs — do not upgrade casually):
|
|
94
|
+
`prisma@8.0.0-rc.15`, `@prisma/orm-postgres@8.0.0-rc.11`,
|
|
95
|
+
`@prisma/cli-engine@0.4.0`.
|
|
96
|
+
|
|
97
|
+
## Gotchas
|
|
98
|
+
|
|
99
|
+
- **No `db.close()` in handlers** — the pool is shared; close once at
|
|
100
|
+
shutdown (server does this in the SIGTERM/SIGINT handler).
|
|
101
|
+
- **`db` is not exported from the package index** — `src/prisma/db.ts` reads
|
|
102
|
+
`process.env.DATABASE_URL` at module load; only `scripts/seed.ts` imports
|
|
103
|
+
it directly. Consumers: `createDatabase()`.
|
|
104
|
+
- **`CONTRACT.MARKER_READ_FAILED` / `CONFIG.DB_CONNECTION_REQUIRED`** —
|
|
105
|
+
`DATABASE_URL`/`DIRECT_URL` not set in the package's `.env` (CLI context).
|
|
106
|
+
- **Destructive ops** — `db update` refuses without `--confirm <database>`
|
|
107
|
+
(`--yes` does NOT grant consent). `db migrate` has no flag — review
|
|
108
|
+
destructive ops in the plan output before applying.
|
|
109
|
+
- **Renames** — no contract-level rename hint; planner sees drop+add.
|
|
110
|
+
Hand-edit `migration.ts` after `db:plan`, or keep-then-drop in two
|
|
111
|
+
migrations.
|
|
112
|
+
- **`upsert` conflictOn** — pass the unique field as an object:
|
|
113
|
+
`conflictOn: { id }`, not `"id"`.
|
|
114
|
+
- **Health probe needs a column alias** — `SELECT 1 AS health` (bare
|
|
115
|
+
`SELECT 1` yields `?column?`, which fails the row spec).
|
|
116
|
+
|
|
117
|
+
## Consuming the package
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
# local dev (monorepo) — already wired in hackathon/server:
|
|
121
|
+
# "@agentbadge/database": "file:../../packages/database"
|
|
122
|
+
# after npm publish:
|
|
123
|
+
bun add @agentbadge/database
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
import { createDatabase } from "@agentbadge/database";
|
|
128
|
+
|
|
129
|
+
const database = createDatabase({
|
|
130
|
+
enabled: process.env.DATABASE_ENABLED === "true",
|
|
131
|
+
url: process.env.DATABASE_URL, // pooled postgres:// URL
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
await database.events.create({ type: "scan.completed", payload: { s: 92 } });
|
|
135
|
+
const up = await database.health(); // SELECT 1 probe
|
|
136
|
+
await database.close(); // once, at shutdown — never per-request
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
The package ships `dist/` only (`files: ["dist"]`) — contract artifacts are
|
|
140
|
+
compiled in, no `prisma generate` needed at install. It never reads env at
|
|
141
|
+
runtime; the caller passes `url` explicitly.
|
|
142
|
+
|
|
143
|
+
## Publishing to npm
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
# from repo root — "database" is in the PACKAGES list:
|
|
147
|
+
./scripts/publish-packages.sh patch --dry-run # verify tarball contents
|
|
148
|
+
./scripts/publish-packages.sh patch # real publish (needs npm login + OTP)
|
|
149
|
+
|
|
150
|
+
# then switch the consumer from file: to the published version:
|
|
151
|
+
# hackathon/server/package.json:
|
|
152
|
+
# "@agentbadge/database": "file:../../packages/database" → "^0.1.0"
|
|
153
|
+
cd hackathon/server && bun install
|
|
154
|
+
bunx vitest run tests/e2e/database.test.ts --config vitest.e2e.config.ts
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Version bumps follow semver: patch = fixes, minor = new entity/store,
|
|
158
|
+
major = `CacheProvider`/`Store`/`Database` interface breaks.
|
|
159
|
+
|
|
160
|
+
## Layout
|
|
161
|
+
|
|
162
|
+
- `src/` — package source (published via `dist/`)
|
|
163
|
+
- `src/prisma/` — contract artifacts (`contract.json`, `contract.d.ts`,
|
|
164
|
+
`db.ts` dev client) — committed (D16)
|
|
165
|
+
- `src/repositories/` — `EventRepository` (canonical template)
|
|
166
|
+
- `migrations/` — committed migration packages + snapshot store + `db` ref
|
|
167
|
+
- `tests/` — vitest suites (PG legs skip without `DATABASE_URL`)
|
|
168
|
+
- `scripts/seed.ts` — idempotent seed
|
|
169
|
+
- `docker-compose.yml` — local dev Postgres only; prod = managed Prisma
|
|
170
|
+
Postgres (see `RUNBOOK.md`)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type PrismaDb } from "./repositories/event-repository.js";
|
|
2
|
+
import { type Store } from "./store.js";
|
|
3
|
+
export interface DatabaseConfig {
|
|
4
|
+
url?: string;
|
|
5
|
+
enabled?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface Database {
|
|
8
|
+
db: PrismaDb | null;
|
|
9
|
+
events: Store;
|
|
10
|
+
health(): Promise<boolean>;
|
|
11
|
+
close(): Promise<void>;
|
|
12
|
+
}
|
|
13
|
+
export declare function createDatabase(config: DatabaseConfig): Database;
|
package/dist/database.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import postgres from "@prisma/orm-postgres/runtime";
|
|
2
|
+
import contractJson from "./prisma/contract.json" with { type: "json" };
|
|
3
|
+
import { EventRepository } from "./repositories/event-repository.js";
|
|
4
|
+
import { InMemoryStore } from "./store.js";
|
|
5
|
+
export function createDatabase(config) {
|
|
6
|
+
const enabled = config.enabled !== false && Boolean(config.url);
|
|
7
|
+
if (!enabled) {
|
|
8
|
+
const events = new InMemoryStore();
|
|
9
|
+
return {
|
|
10
|
+
db: null,
|
|
11
|
+
events,
|
|
12
|
+
health: () => Promise.resolve(false),
|
|
13
|
+
close: () => Promise.resolve(),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
const db = postgres({ contractJson, url: config.url });
|
|
17
|
+
return {
|
|
18
|
+
db,
|
|
19
|
+
events: new EventRepository(db),
|
|
20
|
+
health: async () => {
|
|
21
|
+
try {
|
|
22
|
+
const plan = db.raw.sql `SELECT 1 AS health`.returnsRow({ health: "pg/int4@1" }).build();
|
|
23
|
+
await db.runtime().query(plan);
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
close: () => db.close(),
|
|
31
|
+
};
|
|
32
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const PACKAGE_NAME: "@agentbadge/database";
|
|
2
|
+
export declare const PACKAGE_VERSION: "0.1.0";
|
|
3
|
+
export type { Contract } from "./prisma/contract.d.js";
|
|
4
|
+
export { createDatabase } from "./database.js";
|
|
5
|
+
export type { Database, DatabaseConfig } from "./database.js";
|
|
6
|
+
export { Repository } from "./repository.js";
|
|
7
|
+
export { InMemoryStore } from "./store.js";
|
|
8
|
+
export type { Event, EventCreate, EventUpdate, Store } from "./store.js";
|
|
9
|
+
export { EventRepository } from "./repositories/event-repository.js";
|
|
10
|
+
export type { PrismaDb } from "./repositories/event-repository.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export const PACKAGE_NAME = "@agentbadge/database";
|
|
2
|
+
export const PACKAGE_VERSION = "0.1.0";
|
|
3
|
+
export { createDatabase } from "./database.js";
|
|
4
|
+
export { Repository } from "./repository.js";
|
|
5
|
+
export { InMemoryStore } from "./store.js";
|
|
6
|
+
export { EventRepository } from "./repositories/event-repository.js";
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": "1",
|
|
3
|
+
"targetFamily": "sql",
|
|
4
|
+
"target": "postgres",
|
|
5
|
+
"profileHash": "3916f444a8a17ad749191acf9e08dad97d1a327b88c2f1d45d12f240296aa8b2",
|
|
6
|
+
"roots": {
|
|
7
|
+
"event": {
|
|
8
|
+
"model": "Event",
|
|
9
|
+
"namespace": "public"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"domain": {
|
|
13
|
+
"namespaces": {
|
|
14
|
+
"public": {
|
|
15
|
+
"models": {
|
|
16
|
+
"Event": {
|
|
17
|
+
"fields": {
|
|
18
|
+
"createdAt": {
|
|
19
|
+
"nullable": false,
|
|
20
|
+
"type": {
|
|
21
|
+
"codecId": "pg/timestamptz-string@1",
|
|
22
|
+
"kind": "scalar"
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"id": {
|
|
26
|
+
"nullable": false,
|
|
27
|
+
"type": {
|
|
28
|
+
"codecId": "pg/text@1",
|
|
29
|
+
"kind": "scalar"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"payload": {
|
|
33
|
+
"nullable": false,
|
|
34
|
+
"type": {
|
|
35
|
+
"codecId": "pg/json@1",
|
|
36
|
+
"kind": "scalar"
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"source": {
|
|
40
|
+
"nullable": true,
|
|
41
|
+
"type": {
|
|
42
|
+
"codecId": "pg/text@1",
|
|
43
|
+
"kind": "scalar"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"type": {
|
|
47
|
+
"nullable": false,
|
|
48
|
+
"type": {
|
|
49
|
+
"codecId": "pg/text@1",
|
|
50
|
+
"kind": "scalar"
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
"updatedAt": {
|
|
54
|
+
"nullable": false,
|
|
55
|
+
"type": {
|
|
56
|
+
"codecId": "pg/timestamptz-string@1",
|
|
57
|
+
"kind": "scalar"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"relations": {},
|
|
62
|
+
"storage": {
|
|
63
|
+
"fields": {
|
|
64
|
+
"createdAt": {
|
|
65
|
+
"column": "createdAt"
|
|
66
|
+
},
|
|
67
|
+
"id": {
|
|
68
|
+
"column": "id"
|
|
69
|
+
},
|
|
70
|
+
"payload": {
|
|
71
|
+
"column": "payload"
|
|
72
|
+
},
|
|
73
|
+
"source": {
|
|
74
|
+
"column": "source"
|
|
75
|
+
},
|
|
76
|
+
"type": {
|
|
77
|
+
"column": "type"
|
|
78
|
+
},
|
|
79
|
+
"updatedAt": {
|
|
80
|
+
"column": "updatedAt"
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
"namespaceId": "public",
|
|
84
|
+
"table": "event"
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
"storage": {
|
|
92
|
+
"namespaces": {
|
|
93
|
+
"public": {
|
|
94
|
+
"entries": {
|
|
95
|
+
"table": {
|
|
96
|
+
"event": {
|
|
97
|
+
"columns": {
|
|
98
|
+
"createdAt": {
|
|
99
|
+
"codecId": "pg/timestamptz-string@1",
|
|
100
|
+
"default": {
|
|
101
|
+
"expression": "now()",
|
|
102
|
+
"kind": "function"
|
|
103
|
+
},
|
|
104
|
+
"nativeType": "timestamptz",
|
|
105
|
+
"nullable": false
|
|
106
|
+
},
|
|
107
|
+
"id": {
|
|
108
|
+
"codecId": "pg/text@1",
|
|
109
|
+
"nativeType": "text",
|
|
110
|
+
"nullable": false
|
|
111
|
+
},
|
|
112
|
+
"payload": {
|
|
113
|
+
"codecId": "pg/json@1",
|
|
114
|
+
"nativeType": "json",
|
|
115
|
+
"nullable": false
|
|
116
|
+
},
|
|
117
|
+
"source": {
|
|
118
|
+
"codecId": "pg/text@1",
|
|
119
|
+
"nativeType": "text",
|
|
120
|
+
"nullable": true
|
|
121
|
+
},
|
|
122
|
+
"type": {
|
|
123
|
+
"codecId": "pg/text@1",
|
|
124
|
+
"nativeType": "text",
|
|
125
|
+
"nullable": false
|
|
126
|
+
},
|
|
127
|
+
"updatedAt": {
|
|
128
|
+
"codecId": "pg/timestamptz-string@1",
|
|
129
|
+
"nativeType": "timestamptz",
|
|
130
|
+
"nullable": false
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
"foreignKeys": [],
|
|
134
|
+
"indexes": [
|
|
135
|
+
{
|
|
136
|
+
"columns": [
|
|
137
|
+
"type",
|
|
138
|
+
"createdAt"
|
|
139
|
+
],
|
|
140
|
+
"name": "event_type_createdAt_idx_00d94001",
|
|
141
|
+
"prefix": "event_type_createdAt_idx",
|
|
142
|
+
"unique": false
|
|
143
|
+
}
|
|
144
|
+
],
|
|
145
|
+
"primaryKey": {
|
|
146
|
+
"columns": [
|
|
147
|
+
"id"
|
|
148
|
+
]
|
|
149
|
+
},
|
|
150
|
+
"uniques": []
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
"id": "public",
|
|
155
|
+
"kind": "postgres-schema"
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
"storageHash": "a5ff6d007b451cfd1899c6d722772616290d119afe49050eff5db9569aa23021"
|
|
159
|
+
},
|
|
160
|
+
"execution": {
|
|
161
|
+
"executionHash": "d9353986428bd238a3eb9d95dc87811ed78064fca7d68d9e9fb44b10b26bc55d",
|
|
162
|
+
"mutations": {
|
|
163
|
+
"defaults": [
|
|
164
|
+
{
|
|
165
|
+
"onCreate": {
|
|
166
|
+
"id": "uuidv4",
|
|
167
|
+
"kind": "generator"
|
|
168
|
+
},
|
|
169
|
+
"ref": {
|
|
170
|
+
"column": "id",
|
|
171
|
+
"namespace": "public",
|
|
172
|
+
"table": "event"
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
"onCreate": {
|
|
177
|
+
"id": "timestampNow",
|
|
178
|
+
"kind": "generator"
|
|
179
|
+
},
|
|
180
|
+
"onUpdate": {
|
|
181
|
+
"id": "timestampNow",
|
|
182
|
+
"kind": "generator"
|
|
183
|
+
},
|
|
184
|
+
"ref": {
|
|
185
|
+
"column": "updatedAt",
|
|
186
|
+
"namespace": "public",
|
|
187
|
+
"table": "event"
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
]
|
|
191
|
+
}
|
|
192
|
+
},
|
|
193
|
+
"capabilities": {
|
|
194
|
+
"postgres": {
|
|
195
|
+
"distinctOn": true,
|
|
196
|
+
"jsonAgg": true,
|
|
197
|
+
"lateral": true,
|
|
198
|
+
"limit": true,
|
|
199
|
+
"orderBy": true,
|
|
200
|
+
"returning": true
|
|
201
|
+
},
|
|
202
|
+
"sql": {
|
|
203
|
+
"checkConstraint": true,
|
|
204
|
+
"defaultInInsert": true,
|
|
205
|
+
"enums": true,
|
|
206
|
+
"lateral": true,
|
|
207
|
+
"returning": true,
|
|
208
|
+
"scalarList": true
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
"extensions": {},
|
|
212
|
+
"meta": {},
|
|
213
|
+
"_generated": {
|
|
214
|
+
"warning": "⚠️ GENERATED FILE - DO NOT EDIT",
|
|
215
|
+
"message": "This file is automatically generated by \"prisma contract emit\".",
|
|
216
|
+
"regenerate": "To regenerate, run: prisma contract emit"
|
|
217
|
+
}
|
|
218
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { PostgresClient } from "@prisma/orm-postgres/runtime";
|
|
2
|
+
import type { Contract } from "../prisma/contract.d.js";
|
|
3
|
+
import { Repository } from "../repository.js";
|
|
4
|
+
import type { Event, EventCreate, EventUpdate, Store } from "../store.js";
|
|
5
|
+
export type PrismaDb = PostgresClient<Contract>;
|
|
6
|
+
export declare class EventRepository extends Repository<Event, EventCreate, EventUpdate> implements Store {
|
|
7
|
+
private readonly db;
|
|
8
|
+
constructor(db: PrismaDb);
|
|
9
|
+
findById(id: string): Promise<Event | null>;
|
|
10
|
+
list(opts?: {
|
|
11
|
+
limit?: number;
|
|
12
|
+
offset?: number;
|
|
13
|
+
}): Promise<Event[]>;
|
|
14
|
+
create(data: EventCreate): Promise<Event>;
|
|
15
|
+
update(id: string, data: EventUpdate): Promise<Event | null>;
|
|
16
|
+
delete(id: string): Promise<boolean>;
|
|
17
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Repository } from "../repository.js";
|
|
2
|
+
export class EventRepository extends Repository {
|
|
3
|
+
db;
|
|
4
|
+
constructor(db) {
|
|
5
|
+
super();
|
|
6
|
+
this.db = db;
|
|
7
|
+
}
|
|
8
|
+
async findById(id) {
|
|
9
|
+
return this.db.orm.public.Event.where({ id }).first();
|
|
10
|
+
}
|
|
11
|
+
async list(opts) {
|
|
12
|
+
let q = this.db.orm.public.Event.orderBy((e) => e.createdAt.desc());
|
|
13
|
+
if (opts?.offset !== undefined)
|
|
14
|
+
q = q.offset(opts.offset);
|
|
15
|
+
if (opts?.limit !== undefined)
|
|
16
|
+
q = q.limit(opts.limit);
|
|
17
|
+
return q.all();
|
|
18
|
+
}
|
|
19
|
+
async create(data) {
|
|
20
|
+
return this.db.orm.public.Event.create(data);
|
|
21
|
+
}
|
|
22
|
+
async update(id, data) {
|
|
23
|
+
return this.db.orm.public.Event.where({ id }).update(data);
|
|
24
|
+
}
|
|
25
|
+
async delete(id) {
|
|
26
|
+
return (await this.db.orm.public.Event.where({ id }).delete()) !== null;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare abstract class Repository<T, TCreate = Omit<T, "id">, TUpdate = Partial<TCreate>> {
|
|
2
|
+
abstract findById(id: string): Promise<T | null>;
|
|
3
|
+
abstract list(opts?: {
|
|
4
|
+
limit?: number;
|
|
5
|
+
offset?: number;
|
|
6
|
+
}): Promise<T[]>;
|
|
7
|
+
abstract create(data: TCreate): Promise<T>;
|
|
8
|
+
abstract update(id: string, data: TUpdate): Promise<T | null>;
|
|
9
|
+
abstract delete(id: string): Promise<boolean>;
|
|
10
|
+
}
|
package/dist/store.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Models } from "./prisma/contract.d.js";
|
|
2
|
+
export type Event = Models.public_Event;
|
|
3
|
+
export type EventCreate = {
|
|
4
|
+
type: string;
|
|
5
|
+
payload: Event["payload"];
|
|
6
|
+
source?: string | null;
|
|
7
|
+
id?: string;
|
|
8
|
+
};
|
|
9
|
+
export type EventUpdate = Partial<Omit<EventCreate, "id">>;
|
|
10
|
+
export interface Store {
|
|
11
|
+
findById(id: string): Promise<Event | null>;
|
|
12
|
+
list(opts?: {
|
|
13
|
+
limit?: number;
|
|
14
|
+
offset?: number;
|
|
15
|
+
}): Promise<Event[]>;
|
|
16
|
+
create(data: EventCreate): Promise<Event>;
|
|
17
|
+
update(id: string, data: EventUpdate): Promise<Event | null>;
|
|
18
|
+
delete(id: string): Promise<boolean>;
|
|
19
|
+
}
|
|
20
|
+
export declare class InMemoryStore implements Store {
|
|
21
|
+
private readonly events;
|
|
22
|
+
findById(id: string): Promise<Event | null>;
|
|
23
|
+
list(opts?: {
|
|
24
|
+
limit?: number;
|
|
25
|
+
offset?: number;
|
|
26
|
+
}): Promise<Event[]>;
|
|
27
|
+
create(data: EventCreate): Promise<Event>;
|
|
28
|
+
update(id: string, data: EventUpdate): Promise<Event | null>;
|
|
29
|
+
delete(id: string): Promise<boolean>;
|
|
30
|
+
}
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
export class InMemoryStore {
|
|
3
|
+
events = new Map();
|
|
4
|
+
async findById(id) {
|
|
5
|
+
return this.events.get(id) ?? null;
|
|
6
|
+
}
|
|
7
|
+
async list(opts) {
|
|
8
|
+
const all = [...this.events.values()].reverse();
|
|
9
|
+
const offset = opts?.offset ?? 0;
|
|
10
|
+
const limit = opts?.limit ?? all.length;
|
|
11
|
+
return all.slice(offset, offset + limit);
|
|
12
|
+
}
|
|
13
|
+
async create(data) {
|
|
14
|
+
const now = new Date().toISOString();
|
|
15
|
+
const event = {
|
|
16
|
+
id: data.id ?? randomUUID(),
|
|
17
|
+
type: data.type,
|
|
18
|
+
payload: data.payload,
|
|
19
|
+
source: data.source ?? null,
|
|
20
|
+
createdAt: now,
|
|
21
|
+
updatedAt: now,
|
|
22
|
+
};
|
|
23
|
+
this.events.set(event.id, event);
|
|
24
|
+
return event;
|
|
25
|
+
}
|
|
26
|
+
async update(id, data) {
|
|
27
|
+
const existing = this.events.get(id);
|
|
28
|
+
if (!existing)
|
|
29
|
+
return null;
|
|
30
|
+
const updated = {
|
|
31
|
+
...existing,
|
|
32
|
+
...(data.type !== undefined ? { type: data.type } : {}),
|
|
33
|
+
...(data.payload !== undefined ? { payload: data.payload } : {}),
|
|
34
|
+
...(data.source !== undefined ? { source: data.source } : {}),
|
|
35
|
+
updatedAt: new Date().toISOString(),
|
|
36
|
+
};
|
|
37
|
+
this.events.set(id, updated);
|
|
38
|
+
return updated;
|
|
39
|
+
}
|
|
40
|
+
async delete(id) {
|
|
41
|
+
return this.events.delete(id);
|
|
42
|
+
}
|
|
43
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agentbadge/database",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Shared database layer for AgentBadge — Prisma ORM 8 client, repositories, store abstraction",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"author": "AgentBadge",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"keywords": [
|
|
14
|
+
"database",
|
|
15
|
+
"postgres",
|
|
16
|
+
"prisma",
|
|
17
|
+
"agentbadge"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"engines": {
|
|
23
|
+
"node": ">=18"
|
|
24
|
+
},
|
|
25
|
+
"scripts": {
|
|
26
|
+
"build": "tsc",
|
|
27
|
+
"typecheck": "tsc -p tsconfig.typecheck.json",
|
|
28
|
+
"prepublishOnly": "npm run build",
|
|
29
|
+
"test": "vitest run --run",
|
|
30
|
+
"db:up": "docker compose up -d postgres valkey",
|
|
31
|
+
"db:down": "docker compose down",
|
|
32
|
+
"db:logs": "docker compose logs -f postgres",
|
|
33
|
+
"contract:emit": "prisma contract emit",
|
|
34
|
+
"db:init": "prisma db init",
|
|
35
|
+
"db:update": "prisma db update",
|
|
36
|
+
"db:plan": "prisma migration plan",
|
|
37
|
+
"db:migrate": "prisma db migrate --advance-ref db",
|
|
38
|
+
"db:migrate:prod": "bash scripts/migrate-prod.sh",
|
|
39
|
+
"db:verify": "prisma db verify",
|
|
40
|
+
"db:seed": "bun run scripts/seed.ts",
|
|
41
|
+
"postinstall": "prisma skills sync || exit 0"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@prisma/cli-engine": "0.4.0",
|
|
45
|
+
"@types/node": "^26.6.2",
|
|
46
|
+
"@vitest/coverage-v8": "^2.0.0",
|
|
47
|
+
"bun-types": "^1.3.14",
|
|
48
|
+
"prisma": "8.0.0-rc.15",
|
|
49
|
+
"typescript": "^5.9.0",
|
|
50
|
+
"vitest": "^2.0.0"
|
|
51
|
+
},
|
|
52
|
+
"dependencies": {
|
|
53
|
+
"@prisma/orm-postgres": "8.0.0-rc.11",
|
|
54
|
+
"dotenv": "^18.0.3"
|
|
55
|
+
}
|
|
56
|
+
}
|