@agentbadge/database 0.2.0 → 0.4.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,32 @@
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_SpendEvent;
5
+ export interface SpendEventInsert {
6
+ id: string;
7
+ wallet: string;
8
+ venueId?: string | null;
9
+ kind: string;
10
+ amountUsd: number;
11
+ state: string;
12
+ refId: string;
13
+ txHash?: string | null;
14
+ sourceChain?: string | null;
15
+ denialReason?: string | null;
16
+ at: number;
17
+ payload: JsonValue;
18
+ }
19
+ export declare class SpendLedgerRepository {
20
+ private readonly db;
21
+ constructor(db: PrismaDb);
22
+ append(e: SpendEventInsert): Promise<Row | null>;
23
+ transition(id: string, to: "settled" | "released" | "failed", txHash?: string, sourceChain?: string): Promise<boolean>;
24
+ listByWallet(wallet: string): Promise<Row[]>;
25
+ sumByWalletInWindow(wallet: string, sinceMs: number): Promise<number>;
26
+ getByTxHash(txHash: string): Promise<Row | null>;
27
+ listDenials(opts?: {
28
+ since?: number;
29
+ limit?: number;
30
+ }): Promise<Row[]>;
31
+ }
32
+ export {};
@@ -0,0 +1,69 @@
1
+ export class SpendLedgerRepository {
2
+ db;
3
+ constructor(db) {
4
+ this.db = db;
5
+ }
6
+ async append(e) {
7
+ const existing = await this.db.orm.public.SpendEvent.where({
8
+ id: e.id,
9
+ }).first();
10
+ if (existing)
11
+ return null;
12
+ return this.db.orm.public.SpendEvent.create({
13
+ id: e.id,
14
+ wallet: e.wallet.toLowerCase(),
15
+ venueId: e.venueId ?? null,
16
+ kind: e.kind,
17
+ amountUsd: String(e.amountUsd),
18
+ state: e.state,
19
+ refId: e.refId,
20
+ txHash: e.txHash ?? null,
21
+ sourceChain: e.sourceChain ?? null,
22
+ denialReason: e.denialReason ?? null,
23
+ at: BigInt(e.at),
24
+ payload: e.payload,
25
+ });
26
+ }
27
+ async transition(id, to, txHash, sourceChain) {
28
+ const row = await this.db.orm.public.SpendEvent.where({
29
+ id,
30
+ state: "reserved",
31
+ }).update({
32
+ state: to,
33
+ ...(txHash !== undefined ? { txHash } : {}),
34
+ ...(sourceChain !== undefined ? { sourceChain } : {}),
35
+ });
36
+ return row !== null;
37
+ }
38
+ async listByWallet(wallet) {
39
+ return this.db.orm.public.SpendEvent.orderBy((e) => e.at.desc())
40
+ .where({ wallet: wallet.toLowerCase() })
41
+ .all();
42
+ }
43
+ async sumByWalletInWindow(wallet, sinceMs) {
44
+ const rows = await this.db.orm.public.SpendEvent.where({
45
+ wallet: wallet.toLowerCase(),
46
+ }).all();
47
+ let sum = 0;
48
+ for (const r of rows) {
49
+ if (r.state !== "reserved" && r.state !== "settled")
50
+ continue;
51
+ if (Number(r.at) < sinceMs)
52
+ continue;
53
+ sum += Number(r.amountUsd);
54
+ }
55
+ return sum;
56
+ }
57
+ async getByTxHash(txHash) {
58
+ return this.db.orm.public.SpendEvent.where({ txHash }).first();
59
+ }
60
+ async listDenials(opts) {
61
+ let q = this.db.orm.public.SpendEvent.orderBy((e) => e.at.desc()).where({ state: "denied" });
62
+ if (opts?.limit !== undefined)
63
+ q = q.limit(opts.limit);
64
+ const rows = await q.all();
65
+ return opts?.since !== undefined
66
+ ? rows.filter((r) => Number(r.at) >= opts.since)
67
+ : rows;
68
+ }
69
+ }
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agentbadge/database",
3
- "version": "0.2.0",
3
+ "version": "0.4.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",