@agentbadge/database 0.6.7 → 0.6.8

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, afterId?: 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,40 @@ 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, afterId) {
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 (afterId !== undefined) {
177
+ const idx = out.findIndex((r) => r.id === afterId);
178
+ out = idx >= 0 ? out.slice(idx + 1) : out;
179
+ }
180
+ if (limit !== undefined)
181
+ out = out.slice(0, limit);
182
+ return out;
176
183
  }
177
- async applyStatus(id, expectedFrom, to) {
184
+ async applyStatus(id, expectedFrom, to, payloadPatch) {
178
185
  const existing = await this.getById(id);
179
186
  if (!existing || existing.status !== expectedFrom)
180
187
  return null;
181
188
  const slot = entActiveSlot({ ...existing, status: to });
182
- const row = await catchUnique(this.orm.where({ id, status: expectedFrom }).update({
189
+ const update = {
183
190
  status: to,
184
191
  activeSlot: slot,
185
- }));
192
+ };
193
+ if (payloadPatch) {
194
+ const base = existing.payload && typeof existing.payload === "object"
195
+ ? existing.payload
196
+ : {};
197
+ update.payload = { ...base, ...payloadPatch };
198
+ }
199
+ const row = await catchUnique(this.orm.where({ id, status: expectedFrom }).update(update));
186
200
  return row ? toEntitlementRow(row) : null;
187
201
  }
188
202
  }
@@ -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, afterId?: 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, afterId?: 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,19 @@ export class InMemoryEntitlementStore {
85
85
  out = out.slice(0, limit);
86
86
  return out;
87
87
  }
88
- async listActiveWithSource(limit) {
88
+ async listActiveWithSource(limit, afterId) {
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 (afterId !== undefined) {
93
+ const idx = out.findIndex((r) => r.id === afterId);
94
+ out = idx >= 0 ? out.slice(idx + 1) : out;
95
+ }
91
96
  if (limit !== undefined)
92
97
  out = out.slice(0, limit);
93
98
  return out;
94
99
  }
95
- async applyStatus(id, expectedFrom, to) {
100
+ async applyStatus(id, expectedFrom, to, payloadPatch) {
96
101
  const row = this.rows.get(id);
97
102
  if (!row || row.status !== expectedFrom)
98
103
  return null;
@@ -106,6 +111,15 @@ export class InMemoryEntitlementStore {
106
111
  activeSlot: slot,
107
112
  updatedAt: nowIso(),
108
113
  };
114
+ if (payloadPatch) {
115
+ const base = next.payload && typeof next.payload === "object"
116
+ ? next.payload
117
+ : {};
118
+ next.payload = {
119
+ ...base,
120
+ ...payloadPatch,
121
+ };
122
+ }
109
123
  this.rows.set(id, next);
110
124
  return next;
111
125
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentbadge/database",
3
- "version": "0.6.7",
3
+ "version": "0.6.8",
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",