@agentbadge/database 0.6.4 → 0.6.6
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/dist/database.d.ts +4 -0
- package/dist/database.js +8 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +4 -0
- package/dist/prisma/contract.json +2296 -817
- package/dist/repositories/commerce-repository.d.ts +2 -0
- package/dist/repositories/commerce-repository.js +69 -33
- package/dist/repositories/commerce-store.d.ts +4 -0
- package/dist/repositories/commerce-store.js +35 -9
- package/dist/repositories/event-repository.d.ts +1 -0
- package/dist/repositories/event-repository.js +30 -2
- package/dist/repositories/finops-repository.d.ts +46 -0
- package/dist/repositories/finops-repository.js +208 -0
- package/dist/repositories/finops-store.d.ts +130 -0
- package/dist/repositories/finops-store.js +151 -0
- package/dist/repositories/identity-repository.d.ts +42 -0
- package/dist/repositories/identity-repository.js +246 -0
- package/dist/repositories/identity-store.d.ts +114 -0
- package/dist/repositories/identity-store.js +170 -0
- package/dist/store.d.ts +2 -0
- package/dist/store.js +3 -0
- package/package.json +1 -1
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
const nowIso = () => new Date().toISOString();
|
|
2
|
+
const entActiveSlot = (e) => e.status === "ACTIVE" ? `${e.ownerId}|${e.productCode}` : null;
|
|
3
|
+
export class InMemoryActorStore {
|
|
4
|
+
rows = new Map();
|
|
5
|
+
async upsert(a) {
|
|
6
|
+
const ts = nowIso();
|
|
7
|
+
const existing = this.rows.get(a.wallet);
|
|
8
|
+
const merge = (key) => (a[key] === undefined ? existing?.[key] : a[key]) ?? null;
|
|
9
|
+
const row = {
|
|
10
|
+
wallet: a.wallet,
|
|
11
|
+
type: a.type,
|
|
12
|
+
agentId: merge("agentId"),
|
|
13
|
+
passportTokenId: merge("passportTokenId"),
|
|
14
|
+
did: merge("did"),
|
|
15
|
+
orgId: merge("orgId"),
|
|
16
|
+
tier: merge("tier"),
|
|
17
|
+
payload: a.payload,
|
|
18
|
+
createdAt: existing?.createdAt ?? ts,
|
|
19
|
+
updatedAt: ts,
|
|
20
|
+
};
|
|
21
|
+
this.rows.set(row.wallet, row);
|
|
22
|
+
return row;
|
|
23
|
+
}
|
|
24
|
+
async getById(wallet) {
|
|
25
|
+
return this.rows.get(wallet) ?? null;
|
|
26
|
+
}
|
|
27
|
+
async list(opts) {
|
|
28
|
+
let out = [...this.rows.values()].filter((r) => opts?.type === undefined || r.type === opts.type);
|
|
29
|
+
out = out.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
|
30
|
+
if (opts?.limit !== undefined)
|
|
31
|
+
out = out.slice(0, opts.limit);
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export class InMemoryEntitlementStore {
|
|
36
|
+
rows = new Map();
|
|
37
|
+
async grant(e) {
|
|
38
|
+
if (this.rows.has(e.id))
|
|
39
|
+
return null;
|
|
40
|
+
if ([...this.rows.values()].some((r) => r.idempotencyKey === e.idempotencyKey))
|
|
41
|
+
return null;
|
|
42
|
+
const slot = entActiveSlot(e);
|
|
43
|
+
if (slot !== null &&
|
|
44
|
+
[...this.rows.values()].some((r) => r.activeSlot === slot))
|
|
45
|
+
return null;
|
|
46
|
+
const ts = nowIso();
|
|
47
|
+
const row = {
|
|
48
|
+
...e,
|
|
49
|
+
sourcePaymentId: e.sourcePaymentId ?? null,
|
|
50
|
+
validUntilMs: e.validUntilMs ?? null,
|
|
51
|
+
activeSlot: slot,
|
|
52
|
+
createdAt: ts,
|
|
53
|
+
updatedAt: ts,
|
|
54
|
+
};
|
|
55
|
+
this.rows.set(row.id, row);
|
|
56
|
+
return row;
|
|
57
|
+
}
|
|
58
|
+
async getById(id) {
|
|
59
|
+
return this.rows.get(id) ?? null;
|
|
60
|
+
}
|
|
61
|
+
async getByIdempotencyKey(key) {
|
|
62
|
+
return ([...this.rows.values()].find((r) => r.idempotencyKey === key) ??
|
|
63
|
+
null);
|
|
64
|
+
}
|
|
65
|
+
async getActive(ownerId, productCode) {
|
|
66
|
+
const slot = `${ownerId}|${productCode}`;
|
|
67
|
+
return ([...this.rows.values()].find((r) => r.activeSlot === slot) ?? null);
|
|
68
|
+
}
|
|
69
|
+
async listByOwner(ownerId, opts) {
|
|
70
|
+
let out = [...this.rows.values()].filter((r) => r.ownerId === ownerId &&
|
|
71
|
+
(opts?.status === undefined || r.status === opts.status) &&
|
|
72
|
+
(opts?.productCode === undefined ||
|
|
73
|
+
r.productCode === opts.productCode));
|
|
74
|
+
out = out.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
|
75
|
+
if (opts?.limit !== undefined)
|
|
76
|
+
out = out.slice(0, opts.limit);
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
async listExpiring(nowMs, limit) {
|
|
80
|
+
let out = [...this.rows.values()].filter((r) => r.status === "ACTIVE" &&
|
|
81
|
+
r.validUntilMs != null &&
|
|
82
|
+
r.validUntilMs <= nowMs);
|
|
83
|
+
out = out.sort((a, b) => a.validUntilMs - b.validUntilMs);
|
|
84
|
+
if (limit !== undefined)
|
|
85
|
+
out = out.slice(0, limit);
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
88
|
+
async applyStatus(id, expectedFrom, to) {
|
|
89
|
+
const row = this.rows.get(id);
|
|
90
|
+
if (!row || row.status !== expectedFrom)
|
|
91
|
+
return null;
|
|
92
|
+
const slot = entActiveSlot({ ...row, status: to });
|
|
93
|
+
if (slot !== null &&
|
|
94
|
+
[...this.rows.values()].some((r) => r.id !== id && r.activeSlot === slot))
|
|
95
|
+
return null;
|
|
96
|
+
const next = {
|
|
97
|
+
...row,
|
|
98
|
+
status: to,
|
|
99
|
+
activeSlot: slot,
|
|
100
|
+
updatedAt: nowIso(),
|
|
101
|
+
};
|
|
102
|
+
this.rows.set(id, next);
|
|
103
|
+
return next;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
export class InMemoryReputationFactStore {
|
|
107
|
+
rows = new Map();
|
|
108
|
+
async append(f) {
|
|
109
|
+
if (this.rows.has(f.id))
|
|
110
|
+
return null;
|
|
111
|
+
if ([...this.rows.values()].some((r) => r.dedupeKey === f.dedupeKey))
|
|
112
|
+
return null;
|
|
113
|
+
const row = {
|
|
114
|
+
...f,
|
|
115
|
+
subjectId: f.subjectId ?? null,
|
|
116
|
+
createdAt: nowIso(),
|
|
117
|
+
};
|
|
118
|
+
this.rows.set(row.id, row);
|
|
119
|
+
return row;
|
|
120
|
+
}
|
|
121
|
+
async getById(id) {
|
|
122
|
+
return this.rows.get(id) ?? null;
|
|
123
|
+
}
|
|
124
|
+
async listByActor(actorWallet, opts) {
|
|
125
|
+
let out = [...this.rows.values()].filter((r) => r.actorWallet === actorWallet &&
|
|
126
|
+
(opts?.factType === undefined || r.factType === opts.factType));
|
|
127
|
+
out = out.sort((a, b) => b.createdAt.localeCompare(a.createdAt));
|
|
128
|
+
if (opts?.limit !== undefined)
|
|
129
|
+
out = out.slice(0, opts.limit);
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
async countsByActor(actorWallet) {
|
|
133
|
+
const out = {};
|
|
134
|
+
for (const r of this.rows.values()) {
|
|
135
|
+
if (r.actorWallet !== actorWallet)
|
|
136
|
+
continue;
|
|
137
|
+
out[r.factType] = (out[r.factType] ?? 0) + 1;
|
|
138
|
+
}
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
function cloneRows(value) {
|
|
143
|
+
if (value === null || typeof value !== "object")
|
|
144
|
+
return value;
|
|
145
|
+
return structuredClone(value);
|
|
146
|
+
}
|
|
147
|
+
function cloneStore(store) {
|
|
148
|
+
return new Proxy(store, {
|
|
149
|
+
get(target, prop, receiver) {
|
|
150
|
+
const value = Reflect.get(target, prop, receiver);
|
|
151
|
+
if (typeof value !== "function")
|
|
152
|
+
return value;
|
|
153
|
+
return async (...args) => cloneRows(await value.apply(target, args));
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
export function createInMemoryIdentityStores() {
|
|
158
|
+
let tail = Promise.resolve();
|
|
159
|
+
const stores = {
|
|
160
|
+
actors: cloneStore(new InMemoryActorStore()),
|
|
161
|
+
entitlements: cloneStore(new InMemoryEntitlementStore()),
|
|
162
|
+
facts: cloneStore(new InMemoryReputationFactStore()),
|
|
163
|
+
atomic: async (fn) => {
|
|
164
|
+
const run = tail.then(() => fn(stores));
|
|
165
|
+
tail = run.catch(() => { });
|
|
166
|
+
return run;
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
return stores;
|
|
170
|
+
}
|
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);
|