@agentbadge/database 0.1.1 → 0.3.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.
@@ -0,0 +1,29 @@
1
+ import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
2
+ import type { Models } from "../prisma/contract.d.js";
3
+ import type { PrismaDb } from "./event-repository.js";
4
+ type Row = Models.public_AgentWallet;
5
+ export interface AgentWalletUpsert {
6
+ address: string;
7
+ agentId?: string | null;
8
+ venueId?: string | null;
9
+ label: string;
10
+ kind: string;
11
+ registeredBy: string;
12
+ active?: boolean;
13
+ payload: JsonValue;
14
+ }
15
+ export declare class AgentWalletRepository {
16
+ private readonly db;
17
+ constructor(db: PrismaDb);
18
+ upsert(w: AgentWalletUpsert): Promise<Row>;
19
+ getByAddress(address: string): Promise<Row | null>;
20
+ list(opts?: {
21
+ venueId?: string;
22
+ agentId?: string;
23
+ active?: boolean;
24
+ limit?: number;
25
+ }): Promise<Row[]>;
26
+ deactivate(address: string): Promise<boolean>;
27
+ setEnvelope(address: string, payload: JsonValue): Promise<boolean>;
28
+ }
29
+ export {};
@@ -0,0 +1,59 @@
1
+ export class AgentWalletRepository {
2
+ db;
3
+ constructor(db) {
4
+ this.db = db;
5
+ }
6
+ async upsert(w) {
7
+ return this.db.orm.public.AgentWallet.upsert({
8
+ create: {
9
+ address: w.address.toLowerCase(),
10
+ agentId: w.agentId ?? null,
11
+ venueId: w.venueId ?? null,
12
+ label: w.label,
13
+ kind: w.kind,
14
+ registeredBy: w.registeredBy,
15
+ active: w.active ?? true,
16
+ payload: w.payload,
17
+ },
18
+ update: {
19
+ agentId: w.agentId ?? null,
20
+ venueId: w.venueId ?? null,
21
+ label: w.label,
22
+ kind: w.kind,
23
+ registeredBy: w.registeredBy,
24
+ ...(w.active !== undefined ? { active: w.active } : {}),
25
+ payload: w.payload,
26
+ },
27
+ conflictOn: { address: w.address.toLowerCase() },
28
+ });
29
+ }
30
+ async getByAddress(address) {
31
+ return this.db.orm.public.AgentWallet.where({
32
+ address: address.toLowerCase(),
33
+ }).first();
34
+ }
35
+ async list(opts) {
36
+ let q = this.db.orm.public.AgentWallet.orderBy((w) => w.createdAt.desc());
37
+ if (opts?.venueId !== undefined)
38
+ q = q.where({ venueId: opts.venueId });
39
+ if (opts?.agentId !== undefined)
40
+ q = q.where({ agentId: opts.agentId });
41
+ if (opts?.active !== undefined)
42
+ q = q.where({ active: opts.active });
43
+ if (opts?.limit !== undefined)
44
+ q = q.limit(opts.limit);
45
+ return q.all();
46
+ }
47
+ async deactivate(address) {
48
+ const row = await this.db.orm.public.AgentWallet.where({
49
+ address: address.toLowerCase(),
50
+ }).update({ active: false });
51
+ return row !== null;
52
+ }
53
+ async setEnvelope(address, payload) {
54
+ const row = await this.db.orm.public.AgentWallet.where({
55
+ address: address.toLowerCase(),
56
+ }).update({ payload });
57
+ return row !== null;
58
+ }
59
+ }
@@ -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
+ }
@@ -0,0 +1,36 @@
1
+ import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
2
+ import type { Models } from "../prisma/contract.d.js";
3
+ import type { PrismaDb } from "./event-repository.js";
4
+ type VenueJobRow = Models.public_VenueJob;
5
+ type VenueOfferRow = Models.public_VenueOffer;
6
+ type VenueMetaRow = Models.public_VenueMeta;
7
+ export interface VenueJobUpsert {
8
+ jobId: string;
9
+ status: string;
10
+ client: string;
11
+ provider?: string | null;
12
+ payload: JsonValue;
13
+ }
14
+ export interface VenueOfferUpsert {
15
+ id: string;
16
+ providerAddress: string;
17
+ active: boolean;
18
+ payload: JsonValue;
19
+ }
20
+ export declare class VenueRepository {
21
+ private readonly db;
22
+ constructor(db: PrismaDb);
23
+ upsertJob(j: VenueJobUpsert): Promise<VenueJobRow>;
24
+ getJob(jobId: string): Promise<VenueJobRow | null>;
25
+ listJobs(opts?: {
26
+ status?: string;
27
+ limit?: number;
28
+ }): Promise<VenueJobRow[]>;
29
+ upsertOffer(o: VenueOfferUpsert): Promise<VenueOfferRow>;
30
+ listOffers(opts?: {
31
+ active?: boolean;
32
+ }): Promise<VenueOfferRow[]>;
33
+ getMeta(key: string): Promise<JsonValue | undefined>;
34
+ setMeta(key: string, value: JsonValue): Promise<VenueMetaRow>;
35
+ }
36
+ export {};
@@ -0,0 +1,68 @@
1
+ export class VenueRepository {
2
+ db;
3
+ constructor(db) {
4
+ this.db = db;
5
+ }
6
+ async upsertJob(j) {
7
+ return this.db.orm.public.VenueJob.upsert({
8
+ create: {
9
+ jobId: j.jobId,
10
+ status: j.status,
11
+ client: j.client,
12
+ provider: j.provider ?? null,
13
+ payload: j.payload,
14
+ },
15
+ update: {
16
+ status: j.status,
17
+ client: j.client,
18
+ provider: j.provider ?? null,
19
+ payload: j.payload,
20
+ },
21
+ conflictOn: { jobId: j.jobId },
22
+ });
23
+ }
24
+ async getJob(jobId) {
25
+ return this.db.orm.public.VenueJob.where({ jobId }).first();
26
+ }
27
+ async listJobs(opts) {
28
+ let q = this.db.orm.public.VenueJob.orderBy((j) => j.createdAt.desc());
29
+ if (opts?.status !== undefined)
30
+ q = q.where({ status: opts.status });
31
+ if (opts?.limit !== undefined)
32
+ q = q.limit(opts.limit);
33
+ return q.all();
34
+ }
35
+ async upsertOffer(o) {
36
+ return this.db.orm.public.VenueOffer.upsert({
37
+ create: {
38
+ id: o.id,
39
+ providerAddress: o.providerAddress,
40
+ active: o.active,
41
+ payload: o.payload,
42
+ },
43
+ update: {
44
+ providerAddress: o.providerAddress,
45
+ active: o.active,
46
+ payload: o.payload,
47
+ },
48
+ conflictOn: { id: o.id },
49
+ });
50
+ }
51
+ async listOffers(opts) {
52
+ let q = this.db.orm.public.VenueOffer.orderBy((o) => o.createdAt.desc());
53
+ if (opts?.active !== undefined)
54
+ q = q.where({ active: opts.active });
55
+ return q.all();
56
+ }
57
+ async getMeta(key) {
58
+ const row = await this.db.orm.public.VenueMeta.where({ key }).first();
59
+ return row?.value;
60
+ }
61
+ async setMeta(key, value) {
62
+ return this.db.orm.public.VenueMeta.upsert({
63
+ create: { key, value },
64
+ update: { value },
65
+ conflictOn: { key },
66
+ });
67
+ }
68
+ }
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.3.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",