@secondlayer/shared 0.10.2 → 0.12.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.
Files changed (46) hide show
  1. package/dist/src/db/index.d.ts +180 -2
  2. package/dist/src/db/queries/accounts.d.ts +157 -2
  3. package/dist/src/db/queries/accounts.js +17 -1
  4. package/dist/src/db/queries/accounts.js.map +3 -3
  5. package/dist/src/db/queries/integrity.d.ts +150 -1
  6. package/dist/src/db/queries/marketplace.d.ts +464 -0
  7. package/dist/src/db/queries/marketplace.js +142 -0
  8. package/dist/src/db/queries/marketplace.js.map +10 -0
  9. package/dist/src/db/queries/metrics.d.ts +150 -1
  10. package/dist/src/db/queries/projects.d.ts +424 -0
  11. package/dist/src/db/queries/projects.js +47 -0
  12. package/dist/src/db/queries/projects.js.map +10 -0
  13. package/dist/src/db/queries/subgraph-gaps.d.ts +150 -1
  14. package/dist/src/db/queries/subgraphs.d.ts +158 -6
  15. package/dist/src/db/queries/subgraphs.js +18 -13
  16. package/dist/src/db/queries/subgraphs.js.map +3 -3
  17. package/dist/src/db/queries/usage.d.ts +150 -1
  18. package/dist/src/db/queries/workflows.d.ts +440 -0
  19. package/dist/src/db/queries/workflows.js +115 -0
  20. package/dist/src/db/queries/workflows.js.map +11 -0
  21. package/dist/src/db/schema.d.ts +180 -2
  22. package/dist/src/index.d.ts +258 -10
  23. package/dist/src/index.js +91 -72
  24. package/dist/src/index.js.map +5 -4
  25. package/dist/src/node/local-client.d.ts +150 -1
  26. package/dist/src/schemas/index.d.ts +79 -9
  27. package/dist/src/schemas/index.js +93 -74
  28. package/dist/src/schemas/index.js.map +5 -4
  29. package/dist/src/schemas/marketplace.d.ts +63 -0
  30. package/dist/src/schemas/marketplace.js +39 -0
  31. package/dist/src/schemas/marketplace.js.map +10 -0
  32. package/dist/src/schemas/subgraphs.d.ts +8 -0
  33. package/dist/src/schemas/subgraphs.js.map +2 -2
  34. package/dist/src/schemas/workflows.d.ts +66 -0
  35. package/dist/src/schemas/workflows.js +39 -0
  36. package/dist/src/schemas/workflows.js.map +10 -0
  37. package/dist/src/types.d.ts +1 -0
  38. package/migrations/0022_marketplace.ts +88 -0
  39. package/migrations/0023_projects.ts +149 -0
  40. package/migrations/0024_chat_sessions.ts +51 -0
  41. package/migrations/0025_chat_session_summary.ts +15 -0
  42. package/migrations/0026_workflows.ts +204 -0
  43. package/migrations/0027_workflow_cursors.ts +16 -0
  44. package/migrations/0028_subgraph_account_scoping.ts +116 -0
  45. package/migrations/0029_subgraph_handler_code.ts +23 -0
  46. package/package.json +22 -2
@@ -0,0 +1,116 @@
1
+ import { sql, type Kysely } from "kysely";
2
+
3
+ /**
4
+ * Account-wide subgraph scoping.
5
+ *
6
+ * Changes:
7
+ * - Add `account_id` column to `subgraphs`, backfilled from `api_keys.account_id`
8
+ * - Swap unique index from (name, api_key_id) → (name, account_id)
9
+ * - Make api_key_id FK nullable (ON DELETE SET NULL) for audit trail
10
+ * - Rename existing PG schemas from key-prefix to account-prefix
11
+ * - Update schema_name column to match
12
+ *
13
+ * After this migration, any API key on the same account can deploy/update
14
+ * the same named subgraph without creating duplicates.
15
+ */
16
+ export async function up(db: Kysely<any>): Promise<void> {
17
+ // 1. Add account_id column (nullable first so we can backfill)
18
+ await db.schema
19
+ .alterTable("subgraphs")
20
+ .addColumn("account_id", "text")
21
+ .execute();
22
+
23
+ // 2. Backfill account_id from api_keys
24
+ await sql`
25
+ UPDATE subgraphs s
26
+ SET account_id = k.account_id
27
+ FROM api_keys k
28
+ WHERE k.id = s.api_key_id
29
+ `.execute(db);
30
+
31
+ // 3. Set NOT NULL after backfill (default '' for any orphaned rows)
32
+ await sql`
33
+ UPDATE subgraphs SET account_id = '' WHERE account_id IS NULL
34
+ `.execute(db);
35
+
36
+ await db.schema
37
+ .alterTable("subgraphs")
38
+ .alterColumn("account_id", (c) => c.setNotNull())
39
+ .execute();
40
+
41
+ // 4. Drop old unique index on (name, api_key_id)
42
+ await db.schema
43
+ .dropIndex("subgraphs_name_api_key_id_unique")
44
+ .ifExists()
45
+ .execute();
46
+
47
+ // 5. Create new unique index on (name, account_id)
48
+ await db.schema
49
+ .createIndex("subgraphs_name_account_id_unique")
50
+ .unique()
51
+ .on("subgraphs")
52
+ .columns(["name", "account_id"])
53
+ .execute();
54
+
55
+ // 6. Add index on account_id for fast lookups
56
+ await db.schema
57
+ .createIndex("subgraphs_account_id_idx")
58
+ .on("subgraphs")
59
+ .column("account_id")
60
+ .execute();
61
+
62
+ // 7. Make api_key_id nullable (keep for audit, but allow key deletion)
63
+ await db.schema
64
+ .alterTable("subgraphs")
65
+ .alterColumn("api_key_id", (c) => c.dropNotNull())
66
+ .execute();
67
+
68
+ // 8. Rename existing PG schemas from key-prefix to account-prefix
69
+ // and update schema_name column accordingly.
70
+ // New format: subgraph_{first8charsOfAccountId}_{name}
71
+ const rows = await sql<{
72
+ id: string;
73
+ name: string;
74
+ schema_name: string | null;
75
+ account_id: string;
76
+ }>`
77
+ SELECT id, name, schema_name, account_id FROM subgraphs
78
+ WHERE schema_name IS NOT NULL
79
+ `.execute(db);
80
+
81
+ for (const row of rows.rows) {
82
+ const oldSchema = row.schema_name!;
83
+ const accountPrefix = row.account_id.slice(0, 8).replace(/-/g, "_");
84
+ const safeName = row.name.replace(/-/g, "_");
85
+ const newSchema = `subgraph_${accountPrefix}_${safeName}`;
86
+
87
+ if (oldSchema === newSchema) continue;
88
+
89
+ // Check if old schema exists before renaming
90
+ const exists = await sql<{ exists: boolean }>`
91
+ SELECT EXISTS(
92
+ SELECT 1 FROM information_schema.schemata
93
+ WHERE schema_name = ${oldSchema}
94
+ ) AS exists
95
+ `.execute(db);
96
+
97
+ if (exists.rows[0]?.exists) {
98
+ await sql`ALTER SCHEMA ${sql.raw(`"${oldSchema}"`)} RENAME TO ${sql.raw(`"${newSchema}"`)}`.execute(db);
99
+ }
100
+
101
+ // Update schema_name column regardless of whether schema existed
102
+ await sql`
103
+ UPDATE subgraphs SET schema_name = ${newSchema} WHERE id = ${row.id}
104
+ `.execute(db);
105
+ }
106
+ }
107
+
108
+ export async function down(db: Kysely<any>): Promise<void> {
109
+ await db.schema.dropIndex("subgraphs_account_id_idx").ifExists().execute();
110
+ await db.schema
111
+ .dropIndex("subgraphs_name_account_id_unique")
112
+ .ifExists()
113
+ .execute();
114
+ await db.schema.alterTable("subgraphs").dropColumn("account_id").execute();
115
+ // Note: unique index on (name, api_key_id) is not restored — use a subsequent migration if needed
116
+ }
@@ -0,0 +1,23 @@
1
+ import type { Kysely } from "kysely";
2
+
3
+ /**
4
+ * Store bundled handler code in the subgraphs table so it can be
5
+ * regenerated on container restart without requiring a redeploy.
6
+ *
7
+ * Fixes: handler files in /data/subgraphs/ live in the container's
8
+ * writable layer and are lost on container recreation (e.g. CI deploys).
9
+ * With handler_code stored in DB, the API can restore the file on startup.
10
+ */
11
+ export async function up(db: Kysely<any>): Promise<void> {
12
+ await db.schema
13
+ .alterTable("subgraphs")
14
+ .addColumn("handler_code", "text")
15
+ .execute();
16
+ }
17
+
18
+ export async function down(db: Kysely<any>): Promise<void> {
19
+ await db.schema
20
+ .alterTable("subgraphs")
21
+ .dropColumn("handler_code")
22
+ .execute();
23
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@secondlayer/shared",
3
- "version": "0.10.2",
3
+ "version": "0.12.0",
4
4
  "type": "module",
5
5
  "main": "./dist/src/index.js",
6
6
  "types": "./dist/src/index.d.ts",
@@ -73,6 +73,26 @@
73
73
  "types": "./dist/src/schemas/subgraphs.d.ts",
74
74
  "import": "./dist/src/schemas/subgraphs.js"
75
75
  },
76
+ "./schemas/marketplace": {
77
+ "types": "./dist/src/schemas/marketplace.d.ts",
78
+ "import": "./dist/src/schemas/marketplace.js"
79
+ },
80
+ "./schemas/workflows": {
81
+ "types": "./dist/src/schemas/workflows.d.ts",
82
+ "import": "./dist/src/schemas/workflows.js"
83
+ },
84
+ "./db/queries/workflows": {
85
+ "types": "./dist/src/db/queries/workflows.d.ts",
86
+ "import": "./dist/src/db/queries/workflows.js"
87
+ },
88
+ "./db/queries/marketplace": {
89
+ "types": "./dist/src/db/queries/marketplace.d.ts",
90
+ "import": "./dist/src/db/queries/marketplace.js"
91
+ },
92
+ "./db/queries/projects": {
93
+ "types": "./dist/src/db/queries/projects.d.ts",
94
+ "import": "./dist/src/db/queries/projects.js"
95
+ },
76
96
  "./types": {
77
97
  "types": "./dist/src/types.d.ts",
78
98
  "import": "./dist/src/types.js"
@@ -137,7 +157,7 @@
137
157
  },
138
158
  "dependencies": {
139
159
  "@secondlayer/stacks": "^0.2.2",
140
- "kysely": "0.28.10",
160
+ "kysely": "0.28.15",
141
161
  "kysely-postgres-js": "3.0.0",
142
162
  "postgres": "^3.4.6",
143
163
  "zod": "^4.3.6"