@agentbadge/database 0.1.1 → 0.2.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 CHANGED
@@ -37,6 +37,8 @@ const database = createDatabase({
37
37
 
38
38
  // database.db — Prisma client facade, null when disabled
39
39
  // database.events — Store (EventRepository on PG, InMemoryStore otherwise)
40
+ // database.scanResults — ScanResultStore (ScanResultRepository │ InMemoryScanResultStore)
41
+ // database.chatSubscriptions — ChatSubscriptionStore (ChatSubscriptionRepository │ InMemoryChatSubscriptionStore)
40
42
  // database.health()— SELECT 1 probe → boolean
41
43
  // database.close() — call ONCE at shutdown, never per-request
42
44
  ```
@@ -53,6 +55,20 @@ for CLI/migrations/tests inside the package dir only and is never published
53
55
  (`files: ["dist"]`). `DIRECT_URL` (unpooled, migrations) stays local in
54
56
  `.env.deployer` — see `RUNBOOK.md`.
55
57
 
58
+ ## `Event` as a generic persisted-event table
59
+
60
+ `Event` is intentionally generic — new persisted event streams do NOT need
61
+ their own model. Convention (EPIC-145):
62
+
63
+ - `type` — the stream kind, e.g. `"audit"` (keeperhub audit events)
64
+ - `source` — the producing subsystem, e.g. `"keeperhub"`
65
+ - `payload` — the event JSON
66
+ - `Store.list({ type })` filters by stream kind (added in SLICE-145-4)
67
+
68
+ Dedicated models exist only where the shape is truly relational:
69
+ `ScanResult` (latest-scan-per-domain lookups) and `ChatSubscription`
70
+ (telegram chat ↔ username bindings, unique constraints + upsert).
71
+
56
72
  ## Adding a new entity
57
73
 
58
74
  1. Add the model to `src/prisma/contract.prisma`
@@ -163,6 +179,8 @@ major = `CacheProvider`/`Store`/`Database` interface breaks.
163
179
  hackathon/server (Hono) Fly.io machine
164
180
  └─ createDatabase({enabled, url}) ──► Prisma Postgres
165
181
  ├─ events: Store (EventRepository │ InMemoryStore)
182
+ ├─ scanResults: ScanResultStore (ScanResultRepository │ InMemoryScanResultStore)
183
+ ├─ chatSubscriptions: ChatSubscriptionStore (ChatSubscriptionRepository │ InMemoryChatSubscriptionStore)
166
184
  ├─ health() → SELECT 1 (pooled DATABASE_URL)
167
185
  └─ close() on SIGTERM/SIGINT
168
186
 
@@ -177,7 +195,7 @@ packages/database (this repo) local dev
177
195
  - `src/` — package source (published via `dist/`)
178
196
  - `src/prisma/` — contract artifacts (`contract.json`, `contract.d.ts`,
179
197
  `db.ts` dev client) — committed (D16)
180
- - `src/repositories/` — `EventRepository` (canonical template)
198
+ - `src/repositories/` — `EventRepository` (canonical template), `ScanResultRepository`, `ChatSubscriptionRepository`
181
199
  - `migrations/` — committed migration packages + snapshot store + `db` ref
182
200
  - `tests/` — vitest suites (PG legs skip without `DATABASE_URL`)
183
201
  - `scripts/seed.ts` — idempotent seed
@@ -1,5 +1,5 @@
1
1
  import { type PrismaDb } from "./repositories/event-repository.js";
2
- import { type Store } from "./store.js";
2
+ import { type ChatSubscriptionStore, type ScanResultStore, type Store } from "./store.js";
3
3
  export interface DatabaseConfig {
4
4
  url?: string;
5
5
  enabled?: boolean;
@@ -7,6 +7,8 @@ export interface DatabaseConfig {
7
7
  export interface Database {
8
8
  db: PrismaDb | null;
9
9
  events: Store;
10
+ scanResults: ScanResultStore;
11
+ chatSubscriptions: ChatSubscriptionStore;
10
12
  health(): Promise<boolean>;
11
13
  close(): Promise<void>;
12
14
  }
package/dist/database.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import postgres from "@prisma/orm-postgres/runtime";
2
2
  import contractJson from "./prisma/contract.json" with { type: "json" };
3
3
  import { EventRepository } from "./repositories/event-repository.js";
4
- import { InMemoryStore } from "./store.js";
4
+ import { ScanResultRepository } from "./repositories/scan-result-repository.js";
5
+ import { ChatSubscriptionRepository } from "./repositories/chat-subscription-repository.js";
6
+ import { InMemoryChatSubscriptionStore, InMemoryScanResultStore, InMemoryStore, } from "./store.js";
5
7
  export function createDatabase(config) {
6
8
  const enabled = config.enabled !== false && Boolean(config.url);
7
9
  if (!enabled) {
@@ -9,6 +11,8 @@ export function createDatabase(config) {
9
11
  return {
10
12
  db: null,
11
13
  events,
14
+ scanResults: new InMemoryScanResultStore(),
15
+ chatSubscriptions: new InMemoryChatSubscriptionStore(),
12
16
  health: () => Promise.resolve(false),
13
17
  close: () => Promise.resolve(),
14
18
  };
@@ -17,6 +21,8 @@ export function createDatabase(config) {
17
21
  return {
18
22
  db,
19
23
  events: new EventRepository(db),
24
+ scanResults: new ScanResultRepository(db),
25
+ chatSubscriptions: new ChatSubscriptionRepository(db),
20
26
  health: async () => {
21
27
  try {
22
28
  const plan = db.raw.sql `SELECT 1 AS health`.returnsRow({ health: "pg/int4@1" }).build();
package/dist/index.d.ts CHANGED
@@ -4,7 +4,9 @@ export type { Contract } from "./prisma/contract.d.js";
4
4
  export { createDatabase } from "./database.js";
5
5
  export type { Database, DatabaseConfig } from "./database.js";
6
6
  export { Repository } from "./repository.js";
7
- export { InMemoryStore } from "./store.js";
8
- export type { Event, EventCreate, EventUpdate, Store } from "./store.js";
7
+ export { InMemoryChatSubscriptionStore, InMemoryScanResultStore, InMemoryStore, } from "./store.js";
8
+ export type { ChatSubscription, ChatSubscriptionCreate, ChatSubscriptionStore, ChatSubscriptionUpdate, Event, EventCreate, EventUpdate, ScanResult, ScanResultCreate, ScanResultStore, ScanResultUpdate, Store, } from "./store.js";
9
9
  export { EventRepository } from "./repositories/event-repository.js";
10
+ export { ScanResultRepository } from "./repositories/scan-result-repository.js";
11
+ export { ChatSubscriptionRepository } from "./repositories/chat-subscription-repository.js";
10
12
  export type { PrismaDb } from "./repositories/event-repository.js";
package/dist/index.js CHANGED
@@ -2,5 +2,7 @@ export const PACKAGE_NAME = "@agentbadge/database";
2
2
  export const PACKAGE_VERSION = "0.1.0";
3
3
  export { createDatabase } from "./database.js";
4
4
  export { Repository } from "./repository.js";
5
- export { InMemoryStore } from "./store.js";
5
+ export { InMemoryChatSubscriptionStore, InMemoryScanResultStore, InMemoryStore, } from "./store.js";
6
6
  export { EventRepository } from "./repositories/event-repository.js";
7
+ export { ScanResultRepository } from "./repositories/scan-result-repository.js";
8
+ export { ChatSubscriptionRepository } from "./repositories/chat-subscription-repository.js";
@@ -4,15 +4,84 @@
4
4
  "target": "postgres",
5
5
  "profileHash": "3916f444a8a17ad749191acf9e08dad97d1a327b88c2f1d45d12f240296aa8b2",
6
6
  "roots": {
7
+ "chatSubscription": {
8
+ "model": "ChatSubscription",
9
+ "namespace": "public"
10
+ },
7
11
  "event": {
8
12
  "model": "Event",
9
13
  "namespace": "public"
14
+ },
15
+ "scanResult": {
16
+ "model": "ScanResult",
17
+ "namespace": "public"
10
18
  }
11
19
  },
12
20
  "domain": {
13
21
  "namespaces": {
14
22
  "public": {
15
23
  "models": {
24
+ "ChatSubscription": {
25
+ "fields": {
26
+ "chatId": {
27
+ "nullable": false,
28
+ "type": {
29
+ "codecId": "pg/int8@1",
30
+ "kind": "scalar"
31
+ }
32
+ },
33
+ "createdAt": {
34
+ "nullable": false,
35
+ "type": {
36
+ "codecId": "pg/timestamptz-string@1",
37
+ "kind": "scalar"
38
+ }
39
+ },
40
+ "id": {
41
+ "nullable": false,
42
+ "type": {
43
+ "codecId": "pg/text@1",
44
+ "kind": "scalar"
45
+ }
46
+ },
47
+ "updatedAt": {
48
+ "nullable": false,
49
+ "type": {
50
+ "codecId": "pg/timestamptz-string@1",
51
+ "kind": "scalar"
52
+ }
53
+ },
54
+ "username": {
55
+ "nullable": false,
56
+ "type": {
57
+ "codecId": "pg/text@1",
58
+ "kind": "scalar"
59
+ }
60
+ }
61
+ },
62
+ "relations": {},
63
+ "storage": {
64
+ "fields": {
65
+ "chatId": {
66
+ "column": "chatId"
67
+ },
68
+ "createdAt": {
69
+ "column": "createdAt"
70
+ },
71
+ "id": {
72
+ "column": "id"
73
+ },
74
+ "updatedAt": {
75
+ "column": "updatedAt"
76
+ },
77
+ "username": {
78
+ "column": "username"
79
+ }
80
+ },
81
+ "namespaceId": "public",
82
+ "table": "chatSubscription"
83
+ }
84
+ },
16
85
  "Event": {
17
86
  "fields": {
18
87
  "createdAt": {
@@ -83,6 +152,97 @@
83
152
  "namespaceId": "public",
84
153
  "table": "event"
85
154
  }
155
+ },
156
+ "ScanResult": {
157
+ "fields": {
158
+ "createdAt": {
159
+ "nullable": false,
160
+ "type": {
161
+ "codecId": "pg/timestamptz-string@1",
162
+ "kind": "scalar"
163
+ }
164
+ },
165
+ "domain": {
166
+ "nullable": false,
167
+ "type": {
168
+ "codecId": "pg/text@1",
169
+ "kind": "scalar"
170
+ }
171
+ },
172
+ "id": {
173
+ "nullable": false,
174
+ "type": {
175
+ "codecId": "pg/text@1",
176
+ "kind": "scalar"
177
+ }
178
+ },
179
+ "report": {
180
+ "nullable": false,
181
+ "type": {
182
+ "codecId": "pg/json@1",
183
+ "kind": "scalar"
184
+ }
185
+ },
186
+ "rulesetVersion": {
187
+ "nullable": true,
188
+ "type": {
189
+ "codecId": "pg/text@1",
190
+ "kind": "scalar"
191
+ }
192
+ },
193
+ "score": {
194
+ "nullable": true,
195
+ "type": {
196
+ "codecId": "pg/int4@1",
197
+ "kind": "scalar"
198
+ }
199
+ },
200
+ "updatedAt": {
201
+ "nullable": false,
202
+ "type": {
203
+ "codecId": "pg/timestamptz-string@1",
204
+ "kind": "scalar"
205
+ }
206
+ },
207
+ "url": {
208
+ "nullable": false,
209
+ "type": {
210
+ "codecId": "pg/text@1",
211
+ "kind": "scalar"
212
+ }
213
+ }
214
+ },
215
+ "relations": {},
216
+ "storage": {
217
+ "fields": {
218
+ "createdAt": {
219
+ "column": "createdAt"
220
+ },
221
+ "domain": {
222
+ "column": "domain"
223
+ },
224
+ "id": {
225
+ "column": "id"
226
+ },
227
+ "report": {
228
+ "column": "report"
229
+ },
230
+ "rulesetVersion": {
231
+ "column": "rulesetVersion"
232
+ },
233
+ "score": {
234
+ "column": "score"
235
+ },
236
+ "updatedAt": {
237
+ "column": "updatedAt"
238
+ },
239
+ "url": {
240
+ "column": "url"
241
+ }
242
+ },
243
+ "namespaceId": "public",
244
+ "table": "scanResult"
245
+ }
86
246
  }
87
247
  }
88
248
  }
@@ -93,6 +253,58 @@
93
253
  "public": {
94
254
  "entries": {
95
255
  "table": {
256
+ "chatSubscription": {
257
+ "columns": {
258
+ "chatId": {
259
+ "codecId": "pg/int8@1",
260
+ "nativeType": "int8",
261
+ "nullable": false
262
+ },
263
+ "createdAt": {
264
+ "codecId": "pg/timestamptz-string@1",
265
+ "default": {
266
+ "expression": "now()",
267
+ "kind": "function"
268
+ },
269
+ "nativeType": "timestamptz",
270
+ "nullable": false
271
+ },
272
+ "id": {
273
+ "codecId": "pg/text@1",
274
+ "nativeType": "text",
275
+ "nullable": false
276
+ },
277
+ "updatedAt": {
278
+ "codecId": "pg/timestamptz-string@1",
279
+ "nativeType": "timestamptz",
280
+ "nullable": false
281
+ },
282
+ "username": {
283
+ "codecId": "pg/text@1",
284
+ "nativeType": "text",
285
+ "nullable": false
286
+ }
287
+ },
288
+ "foreignKeys": [],
289
+ "indexes": [],
290
+ "primaryKey": {
291
+ "columns": [
292
+ "id"
293
+ ]
294
+ },
295
+ "uniques": [
296
+ {
297
+ "columns": [
298
+ "chatId"
299
+ ]
300
+ },
301
+ {
302
+ "columns": [
303
+ "username"
304
+ ]
305
+ }
306
+ ]
307
+ },
96
308
  "event": {
97
309
  "columns": {
98
310
  "createdAt": {
@@ -148,6 +360,72 @@
148
360
  ]
149
361
  },
150
362
  "uniques": []
363
+ },
364
+ "scanResult": {
365
+ "columns": {
366
+ "createdAt": {
367
+ "codecId": "pg/timestamptz-string@1",
368
+ "default": {
369
+ "expression": "now()",
370
+ "kind": "function"
371
+ },
372
+ "nativeType": "timestamptz",
373
+ "nullable": false
374
+ },
375
+ "domain": {
376
+ "codecId": "pg/text@1",
377
+ "nativeType": "text",
378
+ "nullable": false
379
+ },
380
+ "id": {
381
+ "codecId": "pg/text@1",
382
+ "nativeType": "text",
383
+ "nullable": false
384
+ },
385
+ "report": {
386
+ "codecId": "pg/json@1",
387
+ "nativeType": "json",
388
+ "nullable": false
389
+ },
390
+ "rulesetVersion": {
391
+ "codecId": "pg/text@1",
392
+ "nativeType": "text",
393
+ "nullable": true
394
+ },
395
+ "score": {
396
+ "codecId": "pg/int4@1",
397
+ "nativeType": "int4",
398
+ "nullable": true
399
+ },
400
+ "updatedAt": {
401
+ "codecId": "pg/timestamptz-string@1",
402
+ "nativeType": "timestamptz",
403
+ "nullable": false
404
+ },
405
+ "url": {
406
+ "codecId": "pg/text@1",
407
+ "nativeType": "text",
408
+ "nullable": false
409
+ }
410
+ },
411
+ "foreignKeys": [],
412
+ "indexes": [
413
+ {
414
+ "columns": [
415
+ "domain",
416
+ "createdAt"
417
+ ],
418
+ "name": "scanResult_domain_createdAt_idx_c5bd4696",
419
+ "prefix": "scanResult_domain_createdAt_idx",
420
+ "unique": false
421
+ }
422
+ ],
423
+ "primaryKey": {
424
+ "columns": [
425
+ "id"
426
+ ]
427
+ },
428
+ "uniques": []
151
429
  }
152
430
  }
153
431
  },
@@ -155,12 +433,38 @@
155
433
  "kind": "postgres-schema"
156
434
  }
157
435
  },
158
- "storageHash": "a5ff6d007b451cfd1899c6d722772616290d119afe49050eff5db9569aa23021"
436
+ "storageHash": "34b23f5aeb2d36a58036b2eadcc94edf8dbcdf7644aa9eae5517ba51d66451df"
159
437
  },
160
438
  "execution": {
161
- "executionHash": "d9353986428bd238a3eb9d95dc87811ed78064fca7d68d9e9fb44b10b26bc55d",
439
+ "executionHash": "9a73be325ad1106310581a4337ffd4cb14486296a033307f46ad89116d36d6ce",
162
440
  "mutations": {
163
441
  "defaults": [
442
+ {
443
+ "onCreate": {
444
+ "id": "uuidv4",
445
+ "kind": "generator"
446
+ },
447
+ "ref": {
448
+ "column": "id",
449
+ "namespace": "public",
450
+ "table": "chatSubscription"
451
+ }
452
+ },
453
+ {
454
+ "onCreate": {
455
+ "id": "timestampNow",
456
+ "kind": "generator"
457
+ },
458
+ "onUpdate": {
459
+ "id": "timestampNow",
460
+ "kind": "generator"
461
+ },
462
+ "ref": {
463
+ "column": "updatedAt",
464
+ "namespace": "public",
465
+ "table": "chatSubscription"
466
+ }
467
+ },
164
468
  {
165
469
  "onCreate": {
166
470
  "id": "uuidv4",
@@ -186,6 +490,32 @@
186
490
  "namespace": "public",
187
491
  "table": "event"
188
492
  }
493
+ },
494
+ {
495
+ "onCreate": {
496
+ "id": "uuidv4",
497
+ "kind": "generator"
498
+ },
499
+ "ref": {
500
+ "column": "id",
501
+ "namespace": "public",
502
+ "table": "scanResult"
503
+ }
504
+ },
505
+ {
506
+ "onCreate": {
507
+ "id": "timestampNow",
508
+ "kind": "generator"
509
+ },
510
+ "onUpdate": {
511
+ "id": "timestampNow",
512
+ "kind": "generator"
513
+ },
514
+ "ref": {
515
+ "column": "updatedAt",
516
+ "namespace": "public",
517
+ "table": "scanResult"
518
+ }
189
519
  }
190
520
  ]
191
521
  }
@@ -0,0 +1,17 @@
1
+ import { Repository } from "../repository.js";
2
+ import type { PrismaDb } from "./event-repository.js";
3
+ import type { ChatSubscription, ChatSubscriptionCreate, ChatSubscriptionStore, ChatSubscriptionUpdate } from "../store.js";
4
+ export declare class ChatSubscriptionRepository extends Repository<ChatSubscription, ChatSubscriptionCreate, ChatSubscriptionUpdate> implements ChatSubscriptionStore {
5
+ private readonly db;
6
+ constructor(db: PrismaDb);
7
+ findById(id: string): Promise<ChatSubscription | null>;
8
+ list(opts?: {
9
+ limit?: number;
10
+ offset?: number;
11
+ }): Promise<ChatSubscription[]>;
12
+ create(data: ChatSubscriptionCreate): Promise<ChatSubscription>;
13
+ update(id: string, data: ChatSubscriptionUpdate): Promise<ChatSubscription | null>;
14
+ delete(id: string): Promise<boolean>;
15
+ upsert(chatId: ChatSubscription["chatId"], username: string): Promise<ChatSubscription>;
16
+ findByUsername(username: string): Promise<ChatSubscription | null>;
17
+ }
@@ -0,0 +1,39 @@
1
+ import { Repository } from "../repository.js";
2
+ export class ChatSubscriptionRepository extends Repository {
3
+ db;
4
+ constructor(db) {
5
+ super();
6
+ this.db = db;
7
+ }
8
+ async findById(id) {
9
+ return this.db.orm.public.ChatSubscription.where({ id }).first();
10
+ }
11
+ async list(opts) {
12
+ let q = this.db.orm.public.ChatSubscription.orderBy((s) => s.createdAt.asc());
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.ChatSubscription.create(data);
21
+ }
22
+ async update(id, data) {
23
+ return this.db.orm.public.ChatSubscription.where({ id }).update(data);
24
+ }
25
+ async delete(id) {
26
+ return ((await this.db.orm.public.ChatSubscription.where({ id }).delete()) !==
27
+ null);
28
+ }
29
+ async upsert(chatId, username) {
30
+ return this.db.orm.public.ChatSubscription.upsert({
31
+ create: { chatId, username },
32
+ update: { chatId, username },
33
+ conflictOn: { chatId },
34
+ });
35
+ }
36
+ async findByUsername(username) {
37
+ return this.db.orm.public.ChatSubscription.where({ username }).first();
38
+ }
39
+ }
@@ -10,6 +10,7 @@ export declare class EventRepository extends Repository<Event, EventCreate, Even
10
10
  list(opts?: {
11
11
  limit?: number;
12
12
  offset?: number;
13
+ type?: string;
13
14
  }): Promise<Event[]>;
14
15
  create(data: EventCreate): Promise<Event>;
15
16
  update(id: string, data: EventUpdate): Promise<Event | null>;
@@ -10,6 +10,9 @@ export class EventRepository extends Repository {
10
10
  }
11
11
  async list(opts) {
12
12
  let q = this.db.orm.public.Event.orderBy((e) => e.createdAt.desc());
13
+ if (opts?.type !== undefined) {
14
+ q = q.where({ type: opts.type });
15
+ }
13
16
  if (opts?.offset !== undefined)
14
17
  q = q.offset(opts.offset);
15
18
  if (opts?.limit !== undefined)
@@ -0,0 +1,20 @@
1
+ import { Repository } from "../repository.js";
2
+ import type { ScanResult, ScanResultCreate, ScanResultStore, ScanResultUpdate } from "../store.js";
3
+ import type { PrismaDb } from "./event-repository.js";
4
+ export declare class ScanResultRepository extends Repository<ScanResult, ScanResultCreate, ScanResultUpdate> implements ScanResultStore {
5
+ private readonly db;
6
+ constructor(db: PrismaDb);
7
+ findById(id: string): Promise<ScanResult | null>;
8
+ list(opts?: {
9
+ limit?: number;
10
+ offset?: number;
11
+ }): Promise<ScanResult[]>;
12
+ create(data: ScanResultCreate): Promise<ScanResult>;
13
+ update(id: string, data: ScanResultUpdate): Promise<ScanResult | null>;
14
+ delete(id: string): Promise<boolean>;
15
+ latestByDomain(domain: string): Promise<ScanResult | null>;
16
+ listByDomain(domain: string, opts?: {
17
+ limit?: number;
18
+ offset?: number;
19
+ }): Promise<ScanResult[]>;
20
+ }
@@ -0,0 +1,41 @@
1
+ import { Repository } from "../repository.js";
2
+ export class ScanResultRepository extends Repository {
3
+ db;
4
+ constructor(db) {
5
+ super();
6
+ this.db = db;
7
+ }
8
+ async findById(id) {
9
+ return this.db.orm.public.ScanResult.where({ id }).first();
10
+ }
11
+ async list(opts) {
12
+ let q = this.db.orm.public.ScanResult.orderBy((s) => s.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.ScanResult.create(data);
21
+ }
22
+ async update(id, data) {
23
+ return this.db.orm.public.ScanResult.where({ id }).update(data);
24
+ }
25
+ async delete(id) {
26
+ return (await this.db.orm.public.ScanResult.where({ id }).delete()) !== null;
27
+ }
28
+ async latestByDomain(domain) {
29
+ return this.db.orm.public.ScanResult.where({ domain })
30
+ .orderBy((s) => s.createdAt.desc())
31
+ .first();
32
+ }
33
+ async listByDomain(domain, opts) {
34
+ let q = this.db.orm.public.ScanResult.where({ domain }).orderBy((s) => s.createdAt.desc());
35
+ if (opts?.offset !== undefined)
36
+ q = q.offset(opts.offset);
37
+ if (opts?.limit !== undefined)
38
+ q = q.limit(opts.limit);
39
+ return q.all();
40
+ }
41
+ }
package/dist/store.d.ts CHANGED
@@ -12,6 +12,7 @@ export interface Store {
12
12
  list(opts?: {
13
13
  limit?: number;
14
14
  offset?: number;
15
+ type?: string;
15
16
  }): Promise<Event[]>;
16
17
  create(data: EventCreate): Promise<Event>;
17
18
  update(id: string, data: EventUpdate): Promise<Event | null>;
@@ -23,8 +24,82 @@ export declare class InMemoryStore implements Store {
23
24
  list(opts?: {
24
25
  limit?: number;
25
26
  offset?: number;
27
+ type?: string;
26
28
  }): Promise<Event[]>;
27
29
  create(data: EventCreate): Promise<Event>;
28
30
  update(id: string, data: EventUpdate): Promise<Event | null>;
29
31
  delete(id: string): Promise<boolean>;
30
32
  }
33
+ export type ScanResult = Models.public_ScanResult;
34
+ export type ScanResultCreate = {
35
+ domain: string;
36
+ url: string;
37
+ report: ScanResult["report"];
38
+ score?: number | null;
39
+ rulesetVersion?: string | null;
40
+ id?: string;
41
+ };
42
+ export type ScanResultUpdate = Partial<Omit<ScanResultCreate, "id">>;
43
+ export interface ScanResultStore {
44
+ findById(id: string): Promise<ScanResult | null>;
45
+ list(opts?: {
46
+ limit?: number;
47
+ offset?: number;
48
+ }): Promise<ScanResult[]>;
49
+ create(data: ScanResultCreate): Promise<ScanResult>;
50
+ update(id: string, data: ScanResultUpdate): Promise<ScanResult | null>;
51
+ delete(id: string): Promise<boolean>;
52
+ latestByDomain(domain: string): Promise<ScanResult | null>;
53
+ listByDomain(domain: string, opts?: {
54
+ limit?: number;
55
+ offset?: number;
56
+ }): Promise<ScanResult[]>;
57
+ }
58
+ export declare class InMemoryScanResultStore implements ScanResultStore {
59
+ private readonly rows;
60
+ findById(id: string): Promise<ScanResult | null>;
61
+ list(opts?: {
62
+ limit?: number;
63
+ offset?: number;
64
+ }): Promise<ScanResult[]>;
65
+ create(data: ScanResultCreate): Promise<ScanResult>;
66
+ update(id: string, data: ScanResultUpdate): Promise<ScanResult | null>;
67
+ delete(id: string): Promise<boolean>;
68
+ latestByDomain(domain: string): Promise<ScanResult | null>;
69
+ listByDomain(domain: string, opts?: {
70
+ limit?: number;
71
+ offset?: number;
72
+ }): Promise<ScanResult[]>;
73
+ }
74
+ export type ChatSubscription = Models.public_ChatSubscription;
75
+ export type ChatSubscriptionCreate = {
76
+ chatId: ChatSubscription["chatId"];
77
+ username: string;
78
+ id?: string;
79
+ };
80
+ export type ChatSubscriptionUpdate = Partial<Omit<ChatSubscriptionCreate, "id">>;
81
+ export interface ChatSubscriptionStore {
82
+ findById(id: string): Promise<ChatSubscription | null>;
83
+ list(opts?: {
84
+ limit?: number;
85
+ offset?: number;
86
+ }): Promise<ChatSubscription[]>;
87
+ create(data: ChatSubscriptionCreate): Promise<ChatSubscription>;
88
+ update(id: string, data: ChatSubscriptionUpdate): Promise<ChatSubscription | null>;
89
+ delete(id: string): Promise<boolean>;
90
+ upsert(chatId: ChatSubscription["chatId"], username: string): Promise<ChatSubscription>;
91
+ findByUsername(username: string): Promise<ChatSubscription | null>;
92
+ }
93
+ export declare class InMemoryChatSubscriptionStore implements ChatSubscriptionStore {
94
+ private readonly rows;
95
+ findById(id: string): Promise<ChatSubscription | null>;
96
+ list(opts?: {
97
+ limit?: number;
98
+ offset?: number;
99
+ }): Promise<ChatSubscription[]>;
100
+ create(data: ChatSubscriptionCreate): Promise<ChatSubscription>;
101
+ update(id: string, data: ChatSubscriptionUpdate): Promise<ChatSubscription | null>;
102
+ delete(id: string): Promise<boolean>;
103
+ upsert(chatId: ChatSubscription["chatId"], username: string): Promise<ChatSubscription>;
104
+ findByUsername(username: string): Promise<ChatSubscription | null>;
105
+ }
package/dist/store.js CHANGED
@@ -5,7 +5,10 @@ export class InMemoryStore {
5
5
  return this.events.get(id) ?? null;
6
6
  }
7
7
  async list(opts) {
8
- const all = [...this.events.values()].reverse();
8
+ let all = [...this.events.values()].reverse();
9
+ if (opts?.type !== undefined) {
10
+ all = all.filter((e) => e.type === opts.type);
11
+ }
9
12
  const offset = opts?.offset ?? 0;
10
13
  const limit = opts?.limit ?? all.length;
11
14
  return all.slice(offset, offset + limit);
@@ -41,3 +44,121 @@ export class InMemoryStore {
41
44
  return this.events.delete(id);
42
45
  }
43
46
  }
47
+ export class InMemoryScanResultStore {
48
+ rows = new Map();
49
+ async findById(id) {
50
+ return this.rows.get(id) ?? null;
51
+ }
52
+ async list(opts) {
53
+ const all = [...this.rows.values()].reverse();
54
+ const offset = opts?.offset ?? 0;
55
+ const limit = opts?.limit ?? all.length;
56
+ return all.slice(offset, offset + limit);
57
+ }
58
+ async create(data) {
59
+ const now = new Date().toISOString();
60
+ const row = {
61
+ id: data.id ?? randomUUID(),
62
+ domain: data.domain,
63
+ url: data.url,
64
+ score: data.score ?? null,
65
+ report: data.report,
66
+ rulesetVersion: data.rulesetVersion ?? null,
67
+ createdAt: now,
68
+ updatedAt: now,
69
+ };
70
+ this.rows.set(row.id, row);
71
+ return row;
72
+ }
73
+ async update(id, data) {
74
+ const existing = this.rows.get(id);
75
+ if (!existing)
76
+ return null;
77
+ const updated = {
78
+ ...existing,
79
+ ...(data.domain !== undefined ? { domain: data.domain } : {}),
80
+ ...(data.url !== undefined ? { url: data.url } : {}),
81
+ ...(data.score !== undefined ? { score: data.score } : {}),
82
+ ...(data.report !== undefined ? { report: data.report } : {}),
83
+ ...(data.rulesetVersion !== undefined
84
+ ? { rulesetVersion: data.rulesetVersion }
85
+ : {}),
86
+ updatedAt: new Date().toISOString(),
87
+ };
88
+ this.rows.set(id, updated);
89
+ return updated;
90
+ }
91
+ async delete(id) {
92
+ return this.rows.delete(id);
93
+ }
94
+ async latestByDomain(domain) {
95
+ const [latest] = await this.listByDomain(domain, { limit: 1 });
96
+ return latest ?? null;
97
+ }
98
+ async listByDomain(domain, opts) {
99
+ const matching = [...this.rows.values()]
100
+ .filter((r) => r.domain === domain)
101
+ .reverse()
102
+ .sort((a, b) => b.createdAt.localeCompare(a.createdAt));
103
+ const offset = opts?.offset ?? 0;
104
+ const limit = opts?.limit ?? matching.length;
105
+ return matching.slice(offset, offset + limit);
106
+ }
107
+ }
108
+ export class InMemoryChatSubscriptionStore {
109
+ rows = new Map();
110
+ async findById(id) {
111
+ return this.rows.get(id) ?? null;
112
+ }
113
+ async list(opts) {
114
+ const all = [...this.rows.values()];
115
+ const offset = opts?.offset ?? 0;
116
+ const limit = opts?.limit ?? all.length;
117
+ return all.slice(offset, offset + limit);
118
+ }
119
+ async create(data) {
120
+ const now = new Date().toISOString();
121
+ const row = {
122
+ id: data.id ?? randomUUID(),
123
+ chatId: data.chatId,
124
+ username: data.username,
125
+ createdAt: now,
126
+ updatedAt: now,
127
+ };
128
+ this.rows.set(row.id, row);
129
+ return row;
130
+ }
131
+ async update(id, data) {
132
+ const existing = this.rows.get(id);
133
+ if (!existing)
134
+ return null;
135
+ const updated = {
136
+ ...existing,
137
+ ...(data.chatId !== undefined ? { chatId: data.chatId } : {}),
138
+ ...(data.username !== undefined ? { username: data.username } : {}),
139
+ updatedAt: new Date().toISOString(),
140
+ };
141
+ this.rows.set(id, updated);
142
+ return updated;
143
+ }
144
+ async delete(id) {
145
+ return this.rows.delete(id);
146
+ }
147
+ async upsert(chatId, username) {
148
+ const existing = [...this.rows.values()].find((r) => r.chatId === chatId || r.username === username);
149
+ if (existing) {
150
+ const updated = {
151
+ ...existing,
152
+ chatId,
153
+ username,
154
+ updatedAt: new Date().toISOString(),
155
+ };
156
+ this.rows.set(existing.id, updated);
157
+ return updated;
158
+ }
159
+ return this.create({ chatId, username });
160
+ }
161
+ async findByUsername(username) {
162
+ return ([...this.rows.values()].find((r) => r.username === username) ?? null);
163
+ }
164
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentbadge/database",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Shared database layer for AgentBadge — Prisma ORM 8 client, repositories, store abstraction",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",