@intx/db 0.1.2

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.
Files changed (91) hide show
  1. package/README.md +37 -0
  2. package/drizzle.config.ts +23 -0
  3. package/migrations/.gitkeep +0 -0
  4. package/migrations/0000_brown_wither.sql +50 -0
  5. package/migrations/0001_white_aqueduct.sql +105 -0
  6. package/migrations/0002_clever_falcon.sql +56 -0
  7. package/migrations/0003_stiff_tyrannus.sql +44 -0
  8. package/migrations/0004_rename_capability_to_offering.sql +1 -0
  9. package/migrations/0005_gigantic_cardiac.sql +1 -0
  10. package/migrations/0006_sidecar.sql +8 -0
  11. package/migrations/0007_agent_running_session.sql +1 -0
  12. package/migrations/0008_session.sql +10 -0
  13. package/migrations/0009_session_messages.sql +18 -0
  14. package/migrations/0010_agent_sidecar_pubkey.sql +2 -0
  15. package/migrations/0011_session_message_from.sql +2 -0
  16. package/migrations/0012_agent_instance.sql +22 -0
  17. package/migrations/0013_instance_session_id.sql +2 -0
  18. package/migrations/0014_drop_agent_runtime_columns.sql +12 -0
  19. package/migrations/0015_add_instance_id_to_session_message.sql +2 -0
  20. package/migrations/0016_jazzy_gamma_corps.sql +3 -0
  21. package/migrations/0017_hesitant_marvex.sql +1 -0
  22. package/migrations/0018_natural_sinister_six.sql +5 -0
  23. package/migrations/0019_rename_grant_source_to_origin.sql +1 -0
  24. package/migrations/0020_add_agent_role.sql +9 -0
  25. package/migrations/0021_acoustic_ozymandias.sql +15 -0
  26. package/migrations/0022_material_sleepwalker.sql +27 -0
  27. package/migrations/0023_flawless_scarlet_witch.sql +2 -0
  28. package/migrations/0024_bumpy_sharon_ventura.sql +1 -0
  29. package/migrations/0025_curvy_firestar.sql +26 -0
  30. package/migrations/0026_keen_ultimo.sql +13 -0
  31. package/migrations/0027_git_tokens.sql +21 -0
  32. package/migrations/0028_wet_sugar_man.sql +1 -0
  33. package/migrations/meta/0000_snapshot.json +316 -0
  34. package/migrations/meta/0001_snapshot.json +968 -0
  35. package/migrations/meta/0002_snapshot.json +1315 -0
  36. package/migrations/meta/0003_snapshot.json +1594 -0
  37. package/migrations/meta/0004_snapshot.json +1594 -0
  38. package/migrations/meta/0005_snapshot.json +1600 -0
  39. package/migrations/meta/0011_snapshot.json +1921 -0
  40. package/migrations/meta/0012_snapshot.json +2067 -0
  41. package/migrations/meta/0013_snapshot.json +2082 -0
  42. package/migrations/meta/0014_snapshot.json +2049 -0
  43. package/migrations/meta/0015_snapshot.json +2064 -0
  44. package/migrations/meta/0016_snapshot.json +2085 -0
  45. package/migrations/meta/0017_snapshot.json +2085 -0
  46. package/migrations/meta/0018_snapshot.json +2070 -0
  47. package/migrations/meta/0019_snapshot.json +2070 -0
  48. package/migrations/meta/0020_snapshot.json +2126 -0
  49. package/migrations/meta/0021_snapshot.json +2239 -0
  50. package/migrations/meta/0022_snapshot.json +2425 -0
  51. package/migrations/meta/0023_snapshot.json +2260 -0
  52. package/migrations/meta/0024_snapshot.json +2254 -0
  53. package/migrations/meta/0025_snapshot.json +2418 -0
  54. package/migrations/meta/0026_snapshot.json +2508 -0
  55. package/migrations/meta/0027_snapshot.json +2657 -0
  56. package/migrations/meta/0028_snapshot.json +2657 -0
  57. package/migrations/meta/_journal.json +209 -0
  58. package/package.json +27 -0
  59. package/src/client.ts +24 -0
  60. package/src/config.ts +19 -0
  61. package/src/connection.ts +27 -0
  62. package/src/credential-resolution.ts +378 -0
  63. package/src/grant-store.ts +51 -0
  64. package/src/index.ts +32 -0
  65. package/src/migrate.test.ts +35 -0
  66. package/src/migrate.ts +168 -0
  67. package/src/parse-row.test.ts +113 -0
  68. package/src/parse-row.ts +185 -0
  69. package/src/schema/agent-assets.ts +21 -0
  70. package/src/schema/agents.ts +49 -0
  71. package/src/schema/assets.ts +24 -0
  72. package/src/schema/auth.ts +51 -0
  73. package/src/schema/credentials.ts +43 -0
  74. package/src/schema/git-tokens.ts +83 -0
  75. package/src/schema/grants.ts +26 -0
  76. package/src/schema/index.ts +19 -0
  77. package/src/schema/instances.ts +36 -0
  78. package/src/schema/messages.ts +108 -0
  79. package/src/schema/oauth-clients.ts +26 -0
  80. package/src/schema/offerings.ts +20 -0
  81. package/src/schema/principals.ts +28 -0
  82. package/src/schema/providers.ts +23 -0
  83. package/src/schema/roles.ts +51 -0
  84. package/src/schema/session-assets.ts +49 -0
  85. package/src/schema/sessions.ts +26 -0
  86. package/src/schema/sidecar.ts +14 -0
  87. package/src/schema/tenants.ts +41 -0
  88. package/src/schema/wallets.ts +44 -0
  89. package/src/tenant-hierarchy.ts +34 -0
  90. package/tsconfig.json +4 -0
  91. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,44 @@
1
+ import { jsonb, pgTable, text, timestamp } from "drizzle-orm/pg-core";
2
+
3
+ import { agent } from "./agents";
4
+ import { tenant } from "./tenants";
5
+
6
+ export const wallet = pgTable("wallet", {
7
+ id: text("id").primaryKey(),
8
+ tenantId: text("tenant_id")
9
+ .notNull()
10
+ .references(() => tenant.id, { onDelete: "cascade" }),
11
+ name: text("name").notNull(),
12
+ backendType: text("backend_type", {
13
+ enum: ["crypto", "fiat", "credits"],
14
+ }).notNull(),
15
+ currency: text("currency").notNull(),
16
+ balance: text("balance").notNull().default("0"),
17
+ config: jsonb("config"),
18
+ createdAt: timestamp("created_at").notNull().defaultNow(),
19
+ updatedAt: timestamp("updated_at").notNull().defaultNow(),
20
+ });
21
+
22
+ export const transaction = pgTable("transaction", {
23
+ id: text("id").primaryKey(),
24
+ walletId: text("wallet_id")
25
+ .notNull()
26
+ .references(() => wallet.id, { onDelete: "cascade" }),
27
+ agentId: text("agent_id").references(() => agent.id, {
28
+ onDelete: "set null",
29
+ }),
30
+ direction: text("direction", {
31
+ enum: ["inbound", "outbound"],
32
+ }).notNull(),
33
+ amount: text("amount").notNull(),
34
+ currency: text("currency").notNull(),
35
+ recipientId: text("recipient_id"),
36
+ senderId: text("sender_id"),
37
+ requestId: text("request_id"),
38
+ status: text("status", {
39
+ enum: ["pending", "completed", "failed"],
40
+ })
41
+ .notNull()
42
+ .default("pending"),
43
+ createdAt: timestamp("created_at").notNull().defaultNow(),
44
+ });
@@ -0,0 +1,34 @@
1
+ import { eq } from "drizzle-orm";
2
+
3
+ import type { DB } from "./client";
4
+ import { tenant } from "./schema/tenants";
5
+
6
+ const MAX_DEPTH = 20;
7
+
8
+ /**
9
+ * Walks the tenant parentId chain from the given tenant up to the root.
10
+ * Returns an ordered array of tenant IDs: [tenantId, parentId, grandparentId, ...rootId].
11
+ *
12
+ * Protects against cycles with a depth limit.
13
+ */
14
+ export async function getAncestorChain(
15
+ db: DB["db"],
16
+ tenantId: string,
17
+ ): Promise<string[]> {
18
+ const chain: string[] = [tenantId];
19
+ let currentId = tenantId;
20
+
21
+ for (let depth = 0; depth < MAX_DEPTH; depth++) {
22
+ const row = await db.query.tenant.findFirst({
23
+ where: eq(tenant.id, currentId),
24
+ columns: { parentId: true },
25
+ });
26
+
27
+ if (!row?.parentId) break;
28
+
29
+ chain.push(row.parentId);
30
+ currentId = row.parentId;
31
+ }
32
+
33
+ return chain;
34
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "include": ["src/**/*.ts"]
4
+ }