@agentbadge/database 0.6.3 → 0.6.5

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,253 @@
1
+ import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
2
+ export interface CommerceTaskInsert {
3
+ id: string;
4
+ buyerWallet: string;
5
+ buyerAgentId?: string | null;
6
+ title: string;
7
+ description: string;
8
+ requirements?: JsonValue | null;
9
+ budget?: string | null;
10
+ currency?: string;
11
+ expiresAtMs?: number | null;
12
+ status: string;
13
+ payload: JsonValue;
14
+ }
15
+ export interface CommerceTaskRow extends CommerceTaskInsert {
16
+ createdAt: string;
17
+ updatedAt: string;
18
+ }
19
+ export interface CommerceTaskStore {
20
+ create(t: CommerceTaskInsert): Promise<CommerceTaskRow | null>;
21
+ getById(id: string): Promise<CommerceTaskRow | null>;
22
+ list(opts?: {
23
+ status?: string;
24
+ buyerWallet?: string;
25
+ visibleTo?: string;
26
+ limit?: number;
27
+ }): Promise<CommerceTaskRow[]>;
28
+ applyStatus(id: string, expectedFrom: string, to: string): Promise<CommerceTaskRow | null>;
29
+ }
30
+ export interface CommerceOfferInsert {
31
+ id: string;
32
+ taskId: string;
33
+ providerWallet: string;
34
+ providerAgentId?: string | null;
35
+ price: string;
36
+ currency?: string;
37
+ eta?: string | null;
38
+ proposal: string;
39
+ status: string;
40
+ payload: JsonValue;
41
+ }
42
+ export interface CommerceOfferRow extends CommerceOfferInsert {
43
+ createdAt: string;
44
+ updatedAt: string;
45
+ }
46
+ export interface CommerceOfferStore {
47
+ create(o: CommerceOfferInsert): Promise<CommerceOfferRow | null>;
48
+ getById(id: string): Promise<CommerceOfferRow | null>;
49
+ listByTask(taskId: string): Promise<CommerceOfferRow[]>;
50
+ applyStatus(id: string, expectedFrom: string, to: string): Promise<CommerceOfferRow | null>;
51
+ }
52
+ export interface CommerceAgreementInsert {
53
+ id: string;
54
+ taskId: string;
55
+ offerId: string;
56
+ buyerWallet: string;
57
+ providerWallet: string;
58
+ agreedAmount: string;
59
+ currency?: string;
60
+ acceptanceCriteria?: JsonValue | null;
61
+ status: string;
62
+ completedAtMs?: number | null;
63
+ payload: JsonValue;
64
+ }
65
+ export interface CommerceAgreementRow extends CommerceAgreementInsert {
66
+ createdAt: string;
67
+ updatedAt: string;
68
+ }
69
+ export interface AgreementStatusPatch {
70
+ completedAtMs?: number;
71
+ }
72
+ export interface CommerceAgreementStore {
73
+ create(a: CommerceAgreementInsert): Promise<CommerceAgreementRow | null>;
74
+ getById(id: string): Promise<CommerceAgreementRow | null>;
75
+ getByTaskId(taskId: string): Promise<CommerceAgreementRow | null>;
76
+ getByOfferId(offerId: string): Promise<CommerceAgreementRow | null>;
77
+ applyStatus(id: string, expectedFrom: string, to: string, patch?: AgreementStatusPatch): Promise<CommerceAgreementRow | null>;
78
+ }
79
+ export interface CommerceSubmissionInsert {
80
+ id: string;
81
+ agreementId: string;
82
+ providerWallet: string;
83
+ summary: string;
84
+ resultUrl?: string | null;
85
+ status: string;
86
+ reviewNote?: string | null;
87
+ submittedAtMs: number;
88
+ reviewedAtMs?: number | null;
89
+ payload: JsonValue;
90
+ }
91
+ export interface CommerceSubmissionRow extends CommerceSubmissionInsert {
92
+ createdAt: string;
93
+ updatedAt: string;
94
+ }
95
+ export interface SubmissionStatusPatch {
96
+ reviewNote?: string;
97
+ reviewedAtMs?: number;
98
+ }
99
+ export interface CommerceSubmissionStore {
100
+ create(s: CommerceSubmissionInsert): Promise<CommerceSubmissionRow | null>;
101
+ getById(id: string): Promise<CommerceSubmissionRow | null>;
102
+ listByAgreement(agreementId: string): Promise<CommerceSubmissionRow[]>;
103
+ applyStatus(id: string, expectedFrom: string, to: string, patch?: SubmissionStatusPatch): Promise<CommerceSubmissionRow | null>;
104
+ }
105
+ export interface CommerceEvidenceInsert {
106
+ id: string;
107
+ submissionId?: string | null;
108
+ agreementId?: string | null;
109
+ type: string;
110
+ url?: string | null;
111
+ hash?: string | null;
112
+ content?: string | null;
113
+ metadata?: JsonValue | null;
114
+ payload: JsonValue;
115
+ }
116
+ export interface CommerceEvidenceRow extends CommerceEvidenceInsert {
117
+ createdAt: string;
118
+ }
119
+ export interface CommerceEvidenceStore {
120
+ append(e: CommerceEvidenceInsert): Promise<CommerceEvidenceRow | null>;
121
+ getById(id: string): Promise<CommerceEvidenceRow | null>;
122
+ listBySubmission(submissionId: string): Promise<CommerceEvidenceRow[]>;
123
+ listByAgreement(agreementId: string): Promise<CommerceEvidenceRow[]>;
124
+ }
125
+ export interface CommerceEventInsert {
126
+ id: string;
127
+ entityType: string;
128
+ entityId: string;
129
+ eventType: string;
130
+ actorWallet?: string | null;
131
+ prevHash: string;
132
+ hash: string;
133
+ payload: JsonValue;
134
+ }
135
+ export interface CommerceEventRow extends CommerceEventInsert {
136
+ eventSeq: string;
137
+ createdAt: string;
138
+ }
139
+ export interface CommerceEventStore {
140
+ append(e: CommerceEventInsert): Promise<CommerceEventRow | null>;
141
+ getById(id: string): Promise<CommerceEventRow | null>;
142
+ latest(): Promise<CommerceEventRow | null>;
143
+ listByEntity(entityType: string, entityId: string): Promise<CommerceEventRow[]>;
144
+ list(opts?: {
145
+ entityType?: string;
146
+ limit?: number;
147
+ }): Promise<CommerceEventRow[]>;
148
+ }
149
+ export interface CommercePaymentInsert {
150
+ id: string;
151
+ agreementId: string;
152
+ mode?: string;
153
+ status: string;
154
+ amount?: string | null;
155
+ currency?: string;
156
+ paymentId?: string | null;
157
+ recordedBy?: string | null;
158
+ recordedAtMs?: number | null;
159
+ verifiedBy?: string | null;
160
+ verifiedAtMs?: number | null;
161
+ note?: string | null;
162
+ payload: JsonValue;
163
+ }
164
+ export interface CommercePaymentRow extends CommercePaymentInsert {
165
+ createdAt: string;
166
+ updatedAt: string;
167
+ }
168
+ export interface PaymentStatusPatch {
169
+ amount?: string;
170
+ recordedBy?: string;
171
+ recordedAtMs?: number;
172
+ verifiedBy?: string;
173
+ verifiedAtMs?: number;
174
+ paymentId?: string | null;
175
+ note?: string;
176
+ }
177
+ export interface CommercePaymentStore {
178
+ create(p: CommercePaymentInsert): Promise<CommercePaymentRow | null>;
179
+ getById(id: string): Promise<CommercePaymentRow | null>;
180
+ getByAgreement(agreementId: string): Promise<CommercePaymentRow | null>;
181
+ applyStatus(id: string, expectedFrom: string, to: string, patch?: PaymentStatusPatch): Promise<CommercePaymentRow | null>;
182
+ }
183
+ export interface CommerceStores {
184
+ tasks: CommerceTaskStore;
185
+ offers: CommerceOfferStore;
186
+ agreements: CommerceAgreementStore;
187
+ submissions: CommerceSubmissionStore;
188
+ evidence: CommerceEvidenceStore;
189
+ events: CommerceEventStore;
190
+ payments: CommercePaymentStore;
191
+ atomic<R>(fn: (stores: CommerceStores) => Promise<R>): Promise<R>;
192
+ }
193
+ export declare class InMemoryCommerceTaskStore implements CommerceTaskStore {
194
+ private readonly rows;
195
+ create(t: CommerceTaskInsert): Promise<CommerceTaskRow | null>;
196
+ getById(id: string): Promise<CommerceTaskRow | null>;
197
+ list(opts?: {
198
+ status?: string;
199
+ buyerWallet?: string;
200
+ visibleTo?: string;
201
+ limit?: number;
202
+ }): Promise<CommerceTaskRow[]>;
203
+ applyStatus(id: string, expectedFrom: string, to: string): Promise<CommerceTaskRow | null>;
204
+ }
205
+ export declare class InMemoryCommerceOfferStore implements CommerceOfferStore {
206
+ private readonly rows;
207
+ create(o: CommerceOfferInsert): Promise<CommerceOfferRow | null>;
208
+ getById(id: string): Promise<CommerceOfferRow | null>;
209
+ listByTask(taskId: string): Promise<CommerceOfferRow[]>;
210
+ applyStatus(id: string, expectedFrom: string, to: string): Promise<CommerceOfferRow | null>;
211
+ }
212
+ export declare class InMemoryCommerceAgreementStore implements CommerceAgreementStore {
213
+ private readonly rows;
214
+ create(a: CommerceAgreementInsert): Promise<CommerceAgreementRow | null>;
215
+ getById(id: string): Promise<CommerceAgreementRow | null>;
216
+ getByTaskId(taskId: string): Promise<CommerceAgreementRow | null>;
217
+ getByOfferId(offerId: string): Promise<CommerceAgreementRow | null>;
218
+ applyStatus(id: string, expectedFrom: string, to: string, patch?: AgreementStatusPatch): Promise<CommerceAgreementRow | null>;
219
+ }
220
+ export declare class InMemoryCommerceSubmissionStore implements CommerceSubmissionStore {
221
+ private readonly rows;
222
+ create(s: CommerceSubmissionInsert): Promise<CommerceSubmissionRow | null>;
223
+ getById(id: string): Promise<CommerceSubmissionRow | null>;
224
+ listByAgreement(agreementId: string): Promise<CommerceSubmissionRow[]>;
225
+ applyStatus(id: string, expectedFrom: string, to: string, patch?: SubmissionStatusPatch): Promise<CommerceSubmissionRow | null>;
226
+ }
227
+ export declare class InMemoryCommerceEvidenceStore implements CommerceEvidenceStore {
228
+ private readonly rows;
229
+ append(e: CommerceEvidenceInsert): Promise<CommerceEvidenceRow | null>;
230
+ getById(id: string): Promise<CommerceEvidenceRow | null>;
231
+ listBySubmission(submissionId: string): Promise<CommerceEvidenceRow[]>;
232
+ listByAgreement(agreementId: string): Promise<CommerceEvidenceRow[]>;
233
+ }
234
+ export declare class InMemoryCommerceEventStore implements CommerceEventStore {
235
+ private readonly rows;
236
+ private seq;
237
+ append(e: CommerceEventInsert): Promise<CommerceEventRow | null>;
238
+ getById(id: string): Promise<CommerceEventRow | null>;
239
+ latest(): Promise<CommerceEventRow | null>;
240
+ listByEntity(entityType: string, entityId: string): Promise<CommerceEventRow[]>;
241
+ list(opts?: {
242
+ entityType?: string;
243
+ limit?: number;
244
+ }): Promise<CommerceEventRow[]>;
245
+ }
246
+ export declare class InMemoryCommercePaymentStore implements CommercePaymentStore {
247
+ private readonly rows;
248
+ create(p: CommercePaymentInsert): Promise<CommercePaymentRow | null>;
249
+ getById(id: string): Promise<CommercePaymentRow | null>;
250
+ getByAgreement(agreementId: string): Promise<CommercePaymentRow | null>;
251
+ applyStatus(id: string, expectedFrom: string, to: string, patch?: PaymentStatusPatch): Promise<CommercePaymentRow | null>;
252
+ }
253
+ export declare function createInMemoryCommerceStores(): CommerceStores;
@@ -0,0 +1,337 @@
1
+ const nowIso = () => new Date().toISOString();
2
+ export class InMemoryCommerceTaskStore {
3
+ rows = new Map();
4
+ async create(t) {
5
+ if (this.rows.has(t.id))
6
+ return null;
7
+ const ts = nowIso();
8
+ const row = {
9
+ ...t,
10
+ buyerAgentId: t.buyerAgentId ?? null,
11
+ requirements: t.requirements ?? null,
12
+ budget: t.budget ?? null,
13
+ currency: t.currency ?? "USD",
14
+ expiresAtMs: t.expiresAtMs ?? null,
15
+ createdAt: ts,
16
+ updatedAt: ts,
17
+ };
18
+ this.rows.set(row.id, row);
19
+ return row;
20
+ }
21
+ async getById(id) {
22
+ return this.rows.get(id) ?? null;
23
+ }
24
+ async list(opts) {
25
+ let out = [...this.rows.values()].filter((r) => (opts?.status === undefined || r.status === opts.status) &&
26
+ (opts?.buyerWallet === undefined || r.buyerWallet === opts.buyerWallet) &&
27
+ (opts?.visibleTo === undefined ||
28
+ r.status !== "DRAFT" ||
29
+ r.buyerWallet === opts.visibleTo));
30
+ out = out.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
31
+ if (opts?.limit !== undefined)
32
+ out = out.slice(0, opts.limit);
33
+ return out;
34
+ }
35
+ async applyStatus(id, expectedFrom, to) {
36
+ const row = this.rows.get(id);
37
+ if (!row || row.status !== expectedFrom)
38
+ return null;
39
+ const next = { ...row, status: to, updatedAt: nowIso() };
40
+ this.rows.set(id, next);
41
+ return next;
42
+ }
43
+ }
44
+ export class InMemoryCommerceOfferStore {
45
+ rows = new Map();
46
+ async create(o) {
47
+ if (this.rows.has(o.id))
48
+ return null;
49
+ const ts = nowIso();
50
+ const row = {
51
+ ...o,
52
+ providerAgentId: o.providerAgentId ?? null,
53
+ currency: o.currency ?? "USD",
54
+ eta: o.eta ?? null,
55
+ createdAt: ts,
56
+ updatedAt: ts,
57
+ };
58
+ this.rows.set(row.id, row);
59
+ return row;
60
+ }
61
+ async getById(id) {
62
+ return this.rows.get(id) ?? null;
63
+ }
64
+ async listByTask(taskId) {
65
+ return [...this.rows.values()]
66
+ .filter((r) => r.taskId === taskId)
67
+ .sort((a, b) => a.createdAt.localeCompare(b.createdAt));
68
+ }
69
+ async applyStatus(id, expectedFrom, to) {
70
+ const row = this.rows.get(id);
71
+ if (!row || row.status !== expectedFrom)
72
+ return null;
73
+ const next = { ...row, status: to, updatedAt: nowIso() };
74
+ this.rows.set(id, next);
75
+ return next;
76
+ }
77
+ }
78
+ export class InMemoryCommerceAgreementStore {
79
+ rows = new Map();
80
+ async create(a) {
81
+ if (this.rows.has(a.id))
82
+ return null;
83
+ for (const x of this.rows.values()) {
84
+ if (x.taskId === a.taskId || x.offerId === a.offerId)
85
+ return null;
86
+ }
87
+ const ts = nowIso();
88
+ const row = {
89
+ ...a,
90
+ currency: a.currency ?? "USD",
91
+ acceptanceCriteria: a.acceptanceCriteria ?? null,
92
+ completedAtMs: a.completedAtMs ?? null,
93
+ createdAt: ts,
94
+ updatedAt: ts,
95
+ };
96
+ this.rows.set(row.id, row);
97
+ return row;
98
+ }
99
+ async getById(id) {
100
+ return this.rows.get(id) ?? null;
101
+ }
102
+ async getByTaskId(taskId) {
103
+ return [...this.rows.values()].find((r) => r.taskId === taskId) ?? null;
104
+ }
105
+ async getByOfferId(offerId) {
106
+ return [...this.rows.values()].find((r) => r.offerId === offerId) ?? null;
107
+ }
108
+ async applyStatus(id, expectedFrom, to, patch) {
109
+ const row = this.rows.get(id);
110
+ if (!row || row.status !== expectedFrom)
111
+ return null;
112
+ const next = {
113
+ ...row,
114
+ status: to,
115
+ ...(patch?.completedAtMs !== undefined
116
+ ? { completedAtMs: patch.completedAtMs }
117
+ : {}),
118
+ updatedAt: nowIso(),
119
+ };
120
+ this.rows.set(id, next);
121
+ return next;
122
+ }
123
+ }
124
+ export class InMemoryCommerceSubmissionStore {
125
+ rows = new Map();
126
+ async create(s) {
127
+ if (this.rows.has(s.id))
128
+ return null;
129
+ const ts = nowIso();
130
+ const row = {
131
+ ...s,
132
+ resultUrl: s.resultUrl ?? null,
133
+ reviewNote: s.reviewNote ?? null,
134
+ reviewedAtMs: s.reviewedAtMs ?? null,
135
+ createdAt: ts,
136
+ updatedAt: ts,
137
+ };
138
+ this.rows.set(row.id, row);
139
+ return row;
140
+ }
141
+ async getById(id) {
142
+ return this.rows.get(id) ?? null;
143
+ }
144
+ async listByAgreement(agreementId) {
145
+ return [...this.rows.values()]
146
+ .filter((r) => r.agreementId === agreementId)
147
+ .sort((a, b) => a.createdAt.localeCompare(b.createdAt));
148
+ }
149
+ async applyStatus(id, expectedFrom, to, patch) {
150
+ const row = this.rows.get(id);
151
+ if (!row || row.status !== expectedFrom)
152
+ return null;
153
+ const next = {
154
+ ...row,
155
+ status: to,
156
+ ...(patch?.reviewNote !== undefined ? { reviewNote: patch.reviewNote } : {}),
157
+ ...(patch?.reviewedAtMs !== undefined
158
+ ? { reviewedAtMs: patch.reviewedAtMs }
159
+ : {}),
160
+ updatedAt: nowIso(),
161
+ };
162
+ this.rows.set(id, next);
163
+ return next;
164
+ }
165
+ }
166
+ export class InMemoryCommerceEvidenceStore {
167
+ rows = new Map();
168
+ async append(e) {
169
+ if (this.rows.has(e.id))
170
+ return null;
171
+ const row = {
172
+ ...e,
173
+ submissionId: e.submissionId ?? null,
174
+ agreementId: e.agreementId ?? null,
175
+ url: e.url ?? null,
176
+ hash: e.hash ?? null,
177
+ content: e.content ?? null,
178
+ metadata: e.metadata ?? null,
179
+ createdAt: nowIso(),
180
+ };
181
+ this.rows.set(row.id, row);
182
+ return row;
183
+ }
184
+ async getById(id) {
185
+ return this.rows.get(id) ?? null;
186
+ }
187
+ async listBySubmission(submissionId) {
188
+ return [...this.rows.values()]
189
+ .filter((r) => r.submissionId === submissionId)
190
+ .sort((a, b) => a.createdAt.localeCompare(b.createdAt));
191
+ }
192
+ async listByAgreement(agreementId) {
193
+ return [...this.rows.values()]
194
+ .filter((r) => r.agreementId === agreementId)
195
+ .sort((a, b) => a.createdAt.localeCompare(b.createdAt));
196
+ }
197
+ }
198
+ export class InMemoryCommerceEventStore {
199
+ rows = new Map();
200
+ seq = 0n;
201
+ async append(e) {
202
+ if (this.rows.has(e.id))
203
+ return null;
204
+ const row = {
205
+ ...e,
206
+ actorWallet: e.actorWallet ?? null,
207
+ eventSeq: String(++this.seq),
208
+ createdAt: nowIso(),
209
+ };
210
+ this.rows.set(row.id, row);
211
+ return row;
212
+ }
213
+ async getById(id) {
214
+ return this.rows.get(id) ?? null;
215
+ }
216
+ async latest() {
217
+ let head = null;
218
+ for (const r of this.rows.values()) {
219
+ if (head === null || BigInt(r.eventSeq) > BigInt(head.eventSeq)) {
220
+ head = r;
221
+ }
222
+ }
223
+ return head;
224
+ }
225
+ async listByEntity(entityType, entityId) {
226
+ return [...this.rows.values()]
227
+ .filter((r) => r.entityType === entityType && r.entityId === entityId)
228
+ .sort((a, b) => BigInt(a.eventSeq) < BigInt(b.eventSeq)
229
+ ? -1
230
+ : BigInt(a.eventSeq) > BigInt(b.eventSeq)
231
+ ? 1
232
+ : 0);
233
+ }
234
+ async list(opts) {
235
+ let out = [...this.rows.values()]
236
+ .filter((r) => opts?.entityType === undefined || r.entityType === opts.entityType)
237
+ .sort((a, b) => BigInt(a.eventSeq) > BigInt(b.eventSeq)
238
+ ? -1
239
+ : BigInt(a.eventSeq) < BigInt(b.eventSeq)
240
+ ? 1
241
+ : 0);
242
+ if (opts?.limit !== undefined)
243
+ out = out.slice(0, opts.limit);
244
+ return out;
245
+ }
246
+ }
247
+ export class InMemoryCommercePaymentStore {
248
+ rows = new Map();
249
+ async create(p) {
250
+ if (this.rows.has(p.id))
251
+ return null;
252
+ for (const x of this.rows.values()) {
253
+ if (x.agreementId === p.agreementId)
254
+ return null;
255
+ }
256
+ const ts = nowIso();
257
+ const row = {
258
+ ...p,
259
+ mode: p.mode ?? "RECORD_ONLY",
260
+ amount: p.amount ?? null,
261
+ currency: p.currency ?? "USD",
262
+ paymentId: p.paymentId ?? null,
263
+ recordedBy: p.recordedBy ?? null,
264
+ recordedAtMs: p.recordedAtMs ?? null,
265
+ verifiedBy: p.verifiedBy ?? null,
266
+ verifiedAtMs: p.verifiedAtMs ?? null,
267
+ note: p.note ?? null,
268
+ createdAt: ts,
269
+ updatedAt: ts,
270
+ };
271
+ this.rows.set(row.id, row);
272
+ return row;
273
+ }
274
+ async getById(id) {
275
+ return this.rows.get(id) ?? null;
276
+ }
277
+ async getByAgreement(agreementId) {
278
+ return ([...this.rows.values()].find((r) => r.agreementId === agreementId) ??
279
+ null);
280
+ }
281
+ async applyStatus(id, expectedFrom, to, patch) {
282
+ const row = this.rows.get(id);
283
+ if (!row || row.status !== expectedFrom)
284
+ return null;
285
+ const next = {
286
+ ...row,
287
+ status: to,
288
+ ...(patch?.recordedBy !== undefined ? { recordedBy: patch.recordedBy } : {}),
289
+ ...(patch?.amount !== undefined ? { amount: patch.amount } : {}),
290
+ ...(patch?.recordedAtMs !== undefined
291
+ ? { recordedAtMs: patch.recordedAtMs }
292
+ : {}),
293
+ ...(patch?.verifiedBy !== undefined ? { verifiedBy: patch.verifiedBy } : {}),
294
+ ...(patch?.verifiedAtMs !== undefined
295
+ ? { verifiedAtMs: patch.verifiedAtMs }
296
+ : {}),
297
+ ...(patch?.paymentId !== undefined ? { paymentId: patch.paymentId } : {}),
298
+ ...(patch?.note !== undefined ? { note: patch.note } : {}),
299
+ updatedAt: nowIso(),
300
+ };
301
+ this.rows.set(id, next);
302
+ return next;
303
+ }
304
+ }
305
+ function cloneRows(value) {
306
+ if (value === null || typeof value !== "object")
307
+ return value;
308
+ return structuredClone(value);
309
+ }
310
+ function cloneStore(store) {
311
+ return new Proxy(store, {
312
+ get(target, prop, receiver) {
313
+ const value = Reflect.get(target, prop, receiver);
314
+ if (typeof value !== "function")
315
+ return value;
316
+ return async (...args) => cloneRows(await value.apply(target, args));
317
+ },
318
+ });
319
+ }
320
+ export function createInMemoryCommerceStores() {
321
+ let tail = Promise.resolve();
322
+ const stores = {
323
+ tasks: cloneStore(new InMemoryCommerceTaskStore()),
324
+ offers: cloneStore(new InMemoryCommerceOfferStore()),
325
+ agreements: cloneStore(new InMemoryCommerceAgreementStore()),
326
+ submissions: cloneStore(new InMemoryCommerceSubmissionStore()),
327
+ evidence: cloneStore(new InMemoryCommerceEvidenceStore()),
328
+ events: cloneStore(new InMemoryCommerceEventStore()),
329
+ payments: cloneStore(new InMemoryCommercePaymentStore()),
330
+ atomic: async (fn) => {
331
+ const run = tail.then(() => fn(stores));
332
+ tail = run.catch(() => { });
333
+ return run;
334
+ },
335
+ };
336
+ return stores;
337
+ }
@@ -11,6 +11,7 @@ export declare class EventRepository extends Repository<Event, EventCreate, Even
11
11
  limit?: number;
12
12
  offset?: number;
13
13
  type?: string;
14
+ since?: string;
14
15
  }): Promise<Event[]>;
15
16
  create(data: EventCreate): Promise<Event>;
16
17
  update(id: string, data: EventUpdate): Promise<Event | null>;
@@ -1,4 +1,16 @@
1
+ import { param } from "@prisma/orm-postgres/relational-core/expression";
1
2
  import { Repository } from "../repository.js";
3
+ const EVENT_ROW_SPEC = {
4
+ id: "pg/text@1",
5
+ type: "pg/text@1",
6
+ payload: "pg/json@1",
7
+ source: { codecId: "pg/text@1", nullable: true },
8
+ wallet: { codecId: "pg/text@1", nullable: true },
9
+ venueId: { codecId: "pg/text@1", nullable: true },
10
+ at: { codecId: "pg/int8@1", nullable: true },
11
+ createdAt: "pg/timestamptz-string@1",
12
+ updatedAt: "pg/timestamptz-string@1",
13
+ };
2
14
  export class EventRepository extends Repository {
3
15
  db;
4
16
  constructor(db) {
@@ -9,9 +21,25 @@ export class EventRepository extends Repository {
9
21
  return this.db.orm.public.Event.where({ id }).first();
10
22
  }
11
23
  async list(opts) {
24
+ const type = opts?.type;
25
+ const since = opts?.since;
26
+ if (since !== undefined) {
27
+ const limit = param(opts?.limit ?? 100_000, { codecId: "pg/int4@1" });
28
+ const offset = param(opts?.offset ?? 0, { codecId: "pg/int4@1" });
29
+ const sinceParam = param(since, { codecId: "pg/timestamptz-string@1" });
30
+ const plan = type !== undefined
31
+ ? this.db.raw.sql `SELECT "id","type","payload","source","wallet","venueId","at","createdAt","updatedAt" FROM "event" WHERE "type" = ${param(type, { codecId: "pg/text@1" })} AND "createdAt" >= ${sinceParam} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`
32
+ .returnsRow(EVENT_ROW_SPEC)
33
+ .build()
34
+ : this.db.raw.sql `SELECT "id","type","payload","source","wallet","venueId","at","createdAt","updatedAt" FROM "event" WHERE "createdAt" >= ${sinceParam} ORDER BY "createdAt" DESC LIMIT ${limit} OFFSET ${offset}`
35
+ .returnsRow(EVENT_ROW_SPEC)
36
+ .build();
37
+ const rows = await this.db.runtime().query(plan);
38
+ return rows;
39
+ }
12
40
  let q = this.db.orm.public.Event.orderBy((e) => e.createdAt.desc());
13
- if (opts?.type !== undefined) {
14
- q = q.where({ type: opts.type });
41
+ if (type !== undefined) {
42
+ q = q.where({ type });
15
43
  }
16
44
  if (opts?.offset !== undefined)
17
45
  q = q.offset(opts.offset);
package/dist/store.d.ts CHANGED
@@ -16,6 +16,7 @@ export interface Store {
16
16
  limit?: number;
17
17
  offset?: number;
18
18
  type?: string;
19
+ since?: string;
19
20
  }): Promise<Event[]>;
20
21
  create(data: EventCreate): Promise<Event>;
21
22
  update(id: string, data: EventUpdate): Promise<Event | null>;
@@ -28,6 +29,7 @@ export declare class InMemoryStore implements Store {
28
29
  limit?: number;
29
30
  offset?: number;
30
31
  type?: string;
32
+ since?: string;
31
33
  }): Promise<Event[]>;
32
34
  create(data: EventCreate): Promise<Event>;
33
35
  update(id: string, data: EventUpdate): Promise<Event | null>;
package/dist/store.js CHANGED
@@ -9,6 +9,9 @@ export class InMemoryStore {
9
9
  if (opts?.type !== undefined) {
10
10
  all = all.filter((e) => e.type === opts.type);
11
11
  }
12
+ if (opts?.since !== undefined) {
13
+ all = all.filter((e) => e.createdAt >= opts.since);
14
+ }
12
15
  const offset = opts?.offset ?? 0;
13
16
  const limit = opts?.limit ?? all.length;
14
17
  return all.slice(offset, offset + limit);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentbadge/database",
3
- "version": "0.6.3",
3
+ "version": "0.6.5",
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",