@agentbadge/database 0.6.7 → 0.6.9

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.
@@ -1,3 +1,4 @@
1
+ import type { JsonValue } from "@prisma/orm-postgres/target/codec-types";
1
2
  import type { PrismaDb } from "./event-repository.js";
2
3
  import type { ActorRow, ActorStore, ActorUpsert, EntitlementInsert, EntitlementRow, EntitlementStore, IdentityStores, ReputationFactInsert, ReputationFactRow, ReputationFactStore } from "./identity-store.js";
3
4
  export declare class ActorRepository implements ActorStore {
@@ -25,8 +26,8 @@ export declare class EntitlementRepository implements EntitlementStore {
25
26
  limit?: number;
26
27
  }): Promise<EntitlementRow[]>;
27
28
  listExpiring(nowMs: number, limit?: number): Promise<EntitlementRow[]>;
28
- listActiveWithSource(limit?: number): Promise<EntitlementRow[]>;
29
- applyStatus(id: string, expectedFrom: string, to: string): Promise<EntitlementRow | null>;
29
+ listActiveWithSource(limit?: number, afterKey?: string): Promise<EntitlementRow[]>;
30
+ applyStatus(id: string, expectedFrom: string, to: string, payloadPatch?: Record<string, JsonValue | undefined>): Promise<EntitlementRow | null>;
30
31
  }
31
32
  export declare class ReputationFactRepository implements ReputationFactStore {
32
33
  private readonly db;
@@ -163,26 +163,42 @@ export class EntitlementRepository {
163
163
  .filter((r) => r.validUntilMs != null && r.validUntilMs <= nowMs);
164
164
  return rows.slice(0, need);
165
165
  }
166
- async listActiveWithSource(limit) {
166
+ async listActiveWithSource(limit, afterKey) {
167
167
  const rows = await this.orm
168
168
  .where({ status: "ACTIVE" })
169
169
  .orderBy((t) => t.createdAt.asc())
170
- .limit((limit ?? 200) * 4)
171
170
  .all();
172
- return rows
171
+ let out = rows
173
172
  .map(toEntitlementRow)
174
- .filter((r) => r.sourcePaymentId != null)
175
- .slice(0, limit ?? 200);
173
+ .filter((r) => r.sourcePaymentId != null);
174
+ out = out.sort((a, b) => a.createdAt.localeCompare(b.createdAt) ||
175
+ a.id.localeCompare(b.id));
176
+ if (afterKey !== undefined) {
177
+ const sep = afterKey.indexOf("|");
178
+ const cAt = afterKey.slice(0, sep);
179
+ const cId = afterKey.slice(sep + 1);
180
+ out = out.filter((r) => r.createdAt > cAt || (r.createdAt === cAt && r.id > cId));
181
+ }
182
+ if (limit !== undefined)
183
+ out = out.slice(0, limit);
184
+ return out;
176
185
  }
177
- async applyStatus(id, expectedFrom, to) {
186
+ async applyStatus(id, expectedFrom, to, payloadPatch) {
178
187
  const existing = await this.getById(id);
179
188
  if (!existing || existing.status !== expectedFrom)
180
189
  return null;
181
190
  const slot = entActiveSlot({ ...existing, status: to });
182
- const row = await catchUnique(this.orm.where({ id, status: expectedFrom }).update({
191
+ const update = {
183
192
  status: to,
184
193
  activeSlot: slot,
185
- }));
194
+ };
195
+ if (payloadPatch) {
196
+ const base = existing.payload && typeof existing.payload === "object"
197
+ ? existing.payload
198
+ : {};
199
+ update.payload = { ...base, ...payloadPatch };
200
+ }
201
+ const row = await catchUnique(this.orm.where({ id, status: expectedFrom }).update(update));
186
202
  return row ? toEntitlementRow(row) : null;
187
203
  }
188
204
  }
@@ -48,8 +48,8 @@ export interface EntitlementStore {
48
48
  limit?: number;
49
49
  }): Promise<EntitlementRow[]>;
50
50
  listExpiring(nowMs: number, limit?: number): Promise<EntitlementRow[]>;
51
- listActiveWithSource(limit?: number): Promise<EntitlementRow[]>;
52
- applyStatus(id: string, expectedFrom: string, to: string): Promise<EntitlementRow | null>;
51
+ listActiveWithSource(limit?: number, afterKey?: string): Promise<EntitlementRow[]>;
52
+ applyStatus(id: string, expectedFrom: string, to: string, payloadPatch?: Record<string, JsonValue | undefined>): Promise<EntitlementRow | null>;
53
53
  }
54
54
  export interface ReputationFactInsert {
55
55
  id: string;
@@ -100,8 +100,8 @@ export declare class InMemoryEntitlementStore implements EntitlementStore {
100
100
  limit?: number;
101
101
  }): Promise<EntitlementRow[]>;
102
102
  listExpiring(nowMs: number, limit?: number): Promise<EntitlementRow[]>;
103
- listActiveWithSource(limit?: number): Promise<EntitlementRow[]>;
104
- applyStatus(id: string, expectedFrom: string, to: string): Promise<EntitlementRow | null>;
103
+ listActiveWithSource(limit?: number, afterKey?: string): Promise<EntitlementRow[]>;
104
+ applyStatus(id: string, expectedFrom: string, to: string, payloadPatch?: Record<string, JsonValue | undefined>): Promise<EntitlementRow | null>;
105
105
  }
106
106
  export declare class InMemoryReputationFactStore implements ReputationFactStore {
107
107
  private readonly rows;
@@ -85,14 +85,21 @@ export class InMemoryEntitlementStore {
85
85
  out = out.slice(0, limit);
86
86
  return out;
87
87
  }
88
- async listActiveWithSource(limit) {
88
+ async listActiveWithSource(limit, afterKey) {
89
89
  let out = [...this.rows.values()].filter((r) => r.status === "ACTIVE" && r.sourcePaymentId != null);
90
- out = out.sort((a, b) => a.createdAt.localeCompare(b.createdAt));
90
+ out = out.sort((a, b) => a.createdAt.localeCompare(b.createdAt) ||
91
+ a.id.localeCompare(b.id));
92
+ if (afterKey !== undefined) {
93
+ const sep = afterKey.indexOf("|");
94
+ const cAt = afterKey.slice(0, sep);
95
+ const cId = afterKey.slice(sep + 1);
96
+ out = out.filter((r) => r.createdAt > cAt || (r.createdAt === cAt && r.id > cId));
97
+ }
91
98
  if (limit !== undefined)
92
99
  out = out.slice(0, limit);
93
100
  return out;
94
101
  }
95
- async applyStatus(id, expectedFrom, to) {
102
+ async applyStatus(id, expectedFrom, to, payloadPatch) {
96
103
  const row = this.rows.get(id);
97
104
  if (!row || row.status !== expectedFrom)
98
105
  return null;
@@ -106,6 +113,15 @@ export class InMemoryEntitlementStore {
106
113
  activeSlot: slot,
107
114
  updatedAt: nowIso(),
108
115
  };
116
+ if (payloadPatch) {
117
+ const base = next.payload && typeof next.payload === "object"
118
+ ? next.payload
119
+ : {};
120
+ next.payload = {
121
+ ...base,
122
+ ...payloadPatch,
123
+ };
124
+ }
109
125
  this.rows.set(id, next);
110
126
  return next;
111
127
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentbadge/database",
3
- "version": "0.6.7",
3
+ "version": "0.6.9",
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",