@hatk/hatk 0.0.1-alpha.6 → 0.0.1-alpha.60

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 (163) hide show
  1. package/dist/adapter.d.ts +19 -0
  2. package/dist/adapter.d.ts.map +1 -0
  3. package/dist/adapter.js +108 -0
  4. package/dist/backfill.d.ts +2 -2
  5. package/dist/backfill.d.ts.map +1 -1
  6. package/dist/backfill.js +78 -31
  7. package/dist/car.d.ts +42 -10
  8. package/dist/car.d.ts.map +1 -1
  9. package/dist/car.js +154 -14
  10. package/dist/cli.js +243 -1043
  11. package/dist/config.d.ts +31 -1
  12. package/dist/config.d.ts.map +1 -1
  13. package/dist/config.js +40 -9
  14. package/dist/database/adapter-factory.d.ts +6 -0
  15. package/dist/database/adapter-factory.d.ts.map +1 -0
  16. package/dist/database/adapter-factory.js +20 -0
  17. package/dist/database/adapters/duckdb-search.d.ts +12 -0
  18. package/dist/database/adapters/duckdb-search.d.ts.map +1 -0
  19. package/dist/database/adapters/duckdb-search.js +27 -0
  20. package/dist/database/adapters/duckdb.d.ts +25 -0
  21. package/dist/database/adapters/duckdb.d.ts.map +1 -0
  22. package/dist/database/adapters/duckdb.js +161 -0
  23. package/dist/database/adapters/sqlite-search.d.ts +23 -0
  24. package/dist/database/adapters/sqlite-search.d.ts.map +1 -0
  25. package/dist/database/adapters/sqlite-search.js +74 -0
  26. package/dist/database/adapters/sqlite.d.ts +18 -0
  27. package/dist/database/adapters/sqlite.d.ts.map +1 -0
  28. package/dist/database/adapters/sqlite.js +88 -0
  29. package/dist/{db.d.ts → database/db.d.ts} +57 -6
  30. package/dist/database/db.d.ts.map +1 -0
  31. package/dist/{db.js → database/db.js} +730 -549
  32. package/dist/database/dialect.d.ts +45 -0
  33. package/dist/database/dialect.d.ts.map +1 -0
  34. package/dist/database/dialect.js +72 -0
  35. package/dist/{fts.d.ts → database/fts.d.ts} +7 -0
  36. package/dist/database/fts.d.ts.map +1 -0
  37. package/dist/{fts.js → database/fts.js} +116 -32
  38. package/dist/database/index.d.ts +7 -0
  39. package/dist/database/index.d.ts.map +1 -0
  40. package/dist/database/index.js +6 -0
  41. package/dist/database/ports.d.ts +50 -0
  42. package/dist/database/ports.d.ts.map +1 -0
  43. package/dist/database/ports.js +1 -0
  44. package/dist/{schema.d.ts → database/schema.d.ts} +14 -3
  45. package/dist/database/schema.d.ts.map +1 -0
  46. package/dist/{schema.js → database/schema.js} +81 -41
  47. package/dist/dev-entry.d.ts +8 -0
  48. package/dist/dev-entry.d.ts.map +1 -0
  49. package/dist/dev-entry.js +112 -0
  50. package/dist/feeds.d.ts +12 -8
  51. package/dist/feeds.d.ts.map +1 -1
  52. package/dist/feeds.js +51 -6
  53. package/dist/hooks.d.ts +85 -0
  54. package/dist/hooks.d.ts.map +1 -0
  55. package/dist/hooks.js +161 -0
  56. package/dist/hydrate.d.ts +7 -6
  57. package/dist/hydrate.d.ts.map +1 -1
  58. package/dist/hydrate.js +4 -16
  59. package/dist/indexer.d.ts +22 -0
  60. package/dist/indexer.d.ts.map +1 -1
  61. package/dist/indexer.js +123 -32
  62. package/dist/labels.d.ts +36 -0
  63. package/dist/labels.d.ts.map +1 -1
  64. package/dist/labels.js +71 -6
  65. package/dist/lexicon-resolve.d.ts.map +1 -1
  66. package/dist/lexicon-resolve.js +27 -112
  67. package/dist/lexicons/com/atproto/label/defs.json +75 -0
  68. package/dist/lexicons/com/atproto/moderation/defs.json +30 -0
  69. package/dist/lexicons/com/atproto/repo/strongRef.json +24 -0
  70. package/dist/lexicons/dev/hatk/applyWrites.json +87 -0
  71. package/dist/lexicons/dev/hatk/createRecord.json +40 -0
  72. package/dist/lexicons/dev/hatk/createReport.json +48 -0
  73. package/dist/lexicons/dev/hatk/deleteRecord.json +25 -0
  74. package/dist/lexicons/dev/hatk/describeCollections.json +41 -0
  75. package/dist/lexicons/dev/hatk/describeFeeds.json +29 -0
  76. package/dist/lexicons/dev/hatk/describeLabels.json +45 -0
  77. package/dist/lexicons/dev/hatk/getFeed.json +30 -0
  78. package/dist/lexicons/dev/hatk/getPreferences.json +19 -0
  79. package/dist/lexicons/dev/hatk/getRecord.json +26 -0
  80. package/dist/lexicons/dev/hatk/getRecords.json +32 -0
  81. package/dist/lexicons/dev/hatk/putPreference.json +28 -0
  82. package/dist/lexicons/dev/hatk/putRecord.json +41 -0
  83. package/dist/lexicons/dev/hatk/searchRecords.json +32 -0
  84. package/dist/lexicons/dev/hatk/uploadBlob.json +23 -0
  85. package/dist/logger.d.ts +29 -0
  86. package/dist/logger.d.ts.map +1 -1
  87. package/dist/logger.js +29 -0
  88. package/dist/main.js +137 -67
  89. package/dist/mst.d.ts +18 -1
  90. package/dist/mst.d.ts.map +1 -1
  91. package/dist/mst.js +19 -8
  92. package/dist/oauth/db.d.ts +3 -1
  93. package/dist/oauth/db.d.ts.map +1 -1
  94. package/dist/oauth/db.js +48 -19
  95. package/dist/oauth/server.d.ts +24 -0
  96. package/dist/oauth/server.d.ts.map +1 -1
  97. package/dist/oauth/server.js +198 -22
  98. package/dist/oauth/session.d.ts +11 -0
  99. package/dist/oauth/session.d.ts.map +1 -0
  100. package/dist/oauth/session.js +65 -0
  101. package/dist/opengraph.d.ts +10 -0
  102. package/dist/opengraph.d.ts.map +1 -1
  103. package/dist/opengraph.js +80 -40
  104. package/dist/pds-proxy.d.ts +60 -0
  105. package/dist/pds-proxy.d.ts.map +1 -0
  106. package/dist/pds-proxy.js +277 -0
  107. package/dist/push.d.ts +34 -0
  108. package/dist/push.d.ts.map +1 -0
  109. package/dist/push.js +184 -0
  110. package/dist/renderer.d.ts +27 -0
  111. package/dist/renderer.d.ts.map +1 -0
  112. package/dist/renderer.js +46 -0
  113. package/dist/resolve-hatk.d.ts +6 -0
  114. package/dist/resolve-hatk.d.ts.map +1 -0
  115. package/dist/resolve-hatk.js +20 -0
  116. package/dist/response.d.ts +16 -0
  117. package/dist/response.d.ts.map +1 -0
  118. package/dist/response.js +69 -0
  119. package/dist/scanner.d.ts +21 -0
  120. package/dist/scanner.d.ts.map +1 -0
  121. package/dist/scanner.js +88 -0
  122. package/dist/seed.d.ts +19 -0
  123. package/dist/seed.d.ts.map +1 -1
  124. package/dist/seed.js +43 -4
  125. package/dist/server-init.d.ts +8 -0
  126. package/dist/server-init.d.ts.map +1 -0
  127. package/dist/server-init.js +62 -0
  128. package/dist/server.d.ts +26 -3
  129. package/dist/server.d.ts.map +1 -1
  130. package/dist/server.js +629 -635
  131. package/dist/setup.d.ts +28 -1
  132. package/dist/setup.d.ts.map +1 -1
  133. package/dist/setup.js +50 -3
  134. package/dist/templates/feed.tpl +14 -0
  135. package/dist/templates/hook.tpl +5 -0
  136. package/dist/templates/label.tpl +15 -0
  137. package/dist/templates/og.tpl +17 -0
  138. package/dist/templates/seed.tpl +11 -0
  139. package/dist/templates/setup.tpl +5 -0
  140. package/dist/templates/test-feed.tpl +19 -0
  141. package/dist/templates/test-xrpc.tpl +19 -0
  142. package/dist/templates/xrpc.tpl +41 -0
  143. package/dist/test.d.ts +1 -1
  144. package/dist/test.d.ts.map +1 -1
  145. package/dist/test.js +39 -32
  146. package/dist/views.js +1 -1
  147. package/dist/vite-plugin.d.ts +1 -1
  148. package/dist/vite-plugin.d.ts.map +1 -1
  149. package/dist/vite-plugin.js +254 -66
  150. package/dist/xrpc.d.ts +75 -11
  151. package/dist/xrpc.d.ts.map +1 -1
  152. package/dist/xrpc.js +189 -39
  153. package/package.json +14 -7
  154. package/public/admin.html +133 -54
  155. package/dist/db.d.ts.map +0 -1
  156. package/dist/fts.d.ts.map +0 -1
  157. package/dist/oauth/hooks.d.ts +0 -10
  158. package/dist/oauth/hooks.d.ts.map +0 -1
  159. package/dist/oauth/hooks.js +0 -40
  160. package/dist/schema.d.ts.map +0 -1
  161. package/dist/test-browser.d.ts +0 -14
  162. package/dist/test-browser.d.ts.map +0 -1
  163. package/dist/test-browser.js +0 -26
@@ -1,136 +1,59 @@
1
- import { DuckDBInstance } from '@duckdb/node-api';
2
- import { toSnakeCase } from "./schema.js";
3
- import { getSearchColumns, stripStopWords } from "./fts.js";
4
- import { emit, timer } from "./logger.js";
5
- import { OAUTH_DDL } from "./oauth/db.js";
6
- let instance;
7
- let con;
8
- let readCon;
1
+ import { toSnakeCase, q } from "./schema.js";
2
+ import { getSearchColumns, stripStopWords, getSearchPort, updateFtsRecord, deleteFtsRecord } from "./fts.js";
3
+ import { emit, timer } from "../logger.js";
4
+ import { OAUTH_DDL } from "../oauth/db.js";
5
+ import { getDialect } from "./dialect.js";
6
+ let port;
7
+ let dialect;
9
8
  const schemas = new Map();
10
- export function closeDatabase() {
11
- try {
12
- readCon?.closeSync();
13
- }
14
- catch { }
15
- try {
16
- con?.closeSync();
17
- }
18
- catch { }
19
- try {
20
- instance?.closeSync();
21
- }
22
- catch { }
23
- }
24
- let writeQueue = Promise.resolve();
25
- let readQueue = Promise.resolve();
26
- function enqueue(queue, fn) {
27
- if (queue === 'write') {
28
- const p = writeQueue.then(fn);
29
- writeQueue = p.then(() => { }, () => { });
30
- return p;
31
- }
32
- else {
33
- const p = readQueue.then(fn);
34
- readQueue = p.then(() => { }, () => { });
35
- return p;
36
- }
9
+ export function getDatabasePort() {
10
+ return port;
37
11
  }
38
- function bindParams(prepared, params) {
39
- for (let i = 0; i < params.length; i++) {
40
- const idx = i + 1;
41
- const value = params[i];
42
- if (value === null || value === undefined) {
43
- prepared.bindNull(idx);
44
- }
45
- else if (typeof value === 'string') {
46
- prepared.bindVarchar(idx, value);
47
- }
48
- else if (typeof value === 'number') {
49
- if (Number.isInteger(value)) {
50
- prepared.bindInteger(idx, value);
51
- }
52
- else {
53
- prepared.bindDouble(idx, value);
54
- }
55
- }
56
- else if (typeof value === 'boolean') {
57
- prepared.bindBoolean(idx, value);
58
- }
59
- else if (typeof value === 'bigint') {
60
- prepared.bindBigInt(idx, value);
61
- }
62
- else if (value instanceof Uint8Array) {
63
- prepared.bindBlob(idx, value);
64
- }
65
- else {
66
- prepared.bindVarchar(idx, String(value));
67
- }
68
- }
12
+ export function getSqlDialect() {
13
+ return dialect;
69
14
  }
70
- async function runDirect(sql, ...params) {
71
- if (params.length === 0) {
72
- await con.run(sql);
73
- return;
74
- }
75
- const prepared = await con.prepare(sql);
76
- bindParams(prepared, params);
77
- await prepared.run();
15
+ export function closeDatabase() {
16
+ port?.close();
78
17
  }
79
- async function run(sql, ...params) {
80
- return enqueue('write', () => runDirect(sql, ...params));
18
+ async function run(sql, params = []) {
19
+ return port.execute(sql, params);
81
20
  }
82
21
  export async function runBatch(operations) {
83
- return enqueue('write', async () => {
84
- await con.run('BEGIN TRANSACTION');
22
+ await port.beginTransaction();
23
+ try {
85
24
  for (const op of operations) {
86
25
  try {
87
- if (op.params.length === 0) {
88
- await con.run(op.sql);
89
- }
90
- else {
91
- const prepared = await con.prepare(op.sql);
92
- bindParams(prepared, op.params);
93
- await prepared.run();
94
- }
26
+ await port.execute(op.sql, op.params);
95
27
  }
96
28
  catch {
97
29
  // Skip bad records, continue with rest of batch
98
30
  }
99
31
  }
100
- await con.run('COMMIT');
101
- });
102
- }
103
- async function allDirect(sql, ...params) {
104
- if (params.length === 0) {
105
- const reader = await readCon.runAndReadAll(sql);
106
- return reader.getRowObjects();
32
+ await port.commit();
33
+ }
34
+ catch {
35
+ await port.rollback();
107
36
  }
108
- const prepared = await readCon.prepare(sql);
109
- bindParams(prepared, params);
110
- const reader = await prepared.runAndReadAll();
111
- return reader.getRowObjects();
112
37
  }
113
- async function all(sql, ...params) {
114
- return enqueue('read', () => allDirect(sql, ...params));
38
+ async function all(sql, params = []) {
39
+ return port.query(sql, params);
115
40
  }
116
- export async function initDatabase(dbPath, tableSchemas, ddlStatements) {
117
- instance = await DuckDBInstance.create(dbPath === ':memory:' ? undefined : dbPath);
118
- con = await instance.connect();
119
- readCon = await instance.connect();
41
+ export async function initDatabase(adapter, dbPath, tableSchemas, ddlStatements) {
42
+ port = adapter;
43
+ dialect = getDialect(adapter.dialect);
44
+ await port.open(dbPath);
120
45
  for (const schema of tableSchemas) {
121
46
  schemas.set(schema.collection, schema);
122
47
  }
123
48
  for (const ddl of ddlStatements) {
124
- for (const statement of ddl.split(';').filter((s) => s.trim())) {
125
- await run(statement);
126
- }
49
+ await port.executeMultiple(ddl);
127
50
  }
128
51
  // Internal tables for backfill state
129
52
  await run(`CREATE TABLE IF NOT EXISTS _repos (
130
53
  did TEXT PRIMARY KEY,
131
54
  status TEXT NOT NULL DEFAULT 'pending',
132
55
  handle TEXT,
133
- backfilled_at TIMESTAMP,
56
+ backfilled_at ${dialect.timestampType},
134
57
  rev TEXT,
135
58
  retry_count INTEGER NOT NULL DEFAULT 0,
136
59
  retry_after INTEGER NOT NULL DEFAULT 0
@@ -147,74 +70,337 @@ export async function initDatabase(dbPath, tableSchemas, ddlStatements) {
147
70
  value TEXT NOT NULL
148
71
  )`);
149
72
  // Labels table (atproto-compatible)
150
- await run(`CREATE SEQUENCE IF NOT EXISTS _labels_seq START 1`);
151
- await run(`CREATE TABLE IF NOT EXISTS _labels (
152
- id INTEGER PRIMARY KEY DEFAULT nextval('_labels_seq'),
153
- src TEXT NOT NULL,
154
- uri TEXT NOT NULL,
155
- val TEXT NOT NULL,
156
- neg BOOLEAN DEFAULT FALSE,
157
- cts TIMESTAMP NOT NULL,
158
- exp TIMESTAMP
159
- )`);
73
+ if (dialect.supportsSequences) {
74
+ await run(`CREATE SEQUENCE IF NOT EXISTS _labels_seq START 1`);
75
+ await run(`CREATE TABLE IF NOT EXISTS _labels (
76
+ id INTEGER PRIMARY KEY DEFAULT nextval('_labels_seq'),
77
+ src TEXT NOT NULL,
78
+ uri TEXT NOT NULL,
79
+ val TEXT NOT NULL,
80
+ neg ${dialect.typeMap.boolean} DEFAULT FALSE,
81
+ cts ${dialect.timestampType} NOT NULL,
82
+ exp ${dialect.timestampType}
83
+ )`);
84
+ }
85
+ else {
86
+ await run(`CREATE TABLE IF NOT EXISTS _labels (
87
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
88
+ src TEXT NOT NULL,
89
+ uri TEXT NOT NULL,
90
+ val TEXT NOT NULL,
91
+ neg INTEGER DEFAULT 0,
92
+ cts TEXT NOT NULL,
93
+ exp TEXT
94
+ )`);
95
+ }
160
96
  await run(`CREATE INDEX IF NOT EXISTS idx_labels_uri ON _labels(uri)`);
161
97
  await run(`CREATE INDEX IF NOT EXISTS idx_labels_src ON _labels(src)`);
162
98
  // Preferences table (generic key-value per user)
163
99
  await run(`CREATE TABLE IF NOT EXISTS _preferences (
164
100
  did TEXT NOT NULL,
165
101
  key TEXT NOT NULL,
166
- value JSON NOT NULL,
167
- updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
102
+ value ${dialect.jsonType} NOT NULL,
103
+ updated_at ${dialect.timestampType} DEFAULT ${dialect.currentTimestamp},
168
104
  PRIMARY KEY (did, key)
105
+ )`);
106
+ // Reports table (user-submitted moderation reports)
107
+ if (dialect.supportsSequences) {
108
+ await run(`CREATE SEQUENCE IF NOT EXISTS _reports_seq START 1`);
109
+ await run(`CREATE TABLE IF NOT EXISTS _reports (
110
+ id INTEGER PRIMARY KEY DEFAULT nextval('_reports_seq'),
111
+ subject_uri TEXT NOT NULL,
112
+ subject_did TEXT NOT NULL,
113
+ label TEXT NOT NULL,
114
+ reason TEXT,
115
+ reported_by TEXT NOT NULL,
116
+ status TEXT NOT NULL DEFAULT 'open',
117
+ resolved_by TEXT,
118
+ resolved_at ${dialect.timestampType},
119
+ created_at ${dialect.timestampType} NOT NULL
120
+ )`);
121
+ }
122
+ else {
123
+ await run(`CREATE TABLE IF NOT EXISTS _reports (
124
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
125
+ subject_uri TEXT NOT NULL,
126
+ subject_did TEXT NOT NULL,
127
+ label TEXT NOT NULL,
128
+ reason TEXT,
129
+ reported_by TEXT NOT NULL,
130
+ status TEXT NOT NULL DEFAULT 'open',
131
+ resolved_by TEXT,
132
+ resolved_at TEXT,
133
+ created_at TEXT NOT NULL
134
+ )`);
135
+ }
136
+ await run(`CREATE INDEX IF NOT EXISTS idx_reports_status ON _reports(status)`);
137
+ await run(`CREATE INDEX IF NOT EXISTS idx_reports_subject_uri ON _reports(subject_uri)`);
138
+ // Push notification tokens
139
+ await run(`CREATE TABLE IF NOT EXISTS _push_tokens (
140
+ did TEXT NOT NULL,
141
+ token TEXT NOT NULL,
142
+ platform TEXT NOT NULL,
143
+ created_at TEXT NOT NULL,
144
+ PRIMARY KEY (did, token)
169
145
  )`);
170
146
  // OAuth tables
171
- for (const statement of OAUTH_DDL.split(';').filter((s) => s.trim())) {
172
- await run(statement);
147
+ await port.executeMultiple(OAUTH_DDL);
148
+ // Migrations: add pds_auth_server to existing sessions tables
149
+ try {
150
+ await run(`ALTER TABLE _oauth_sessions ADD COLUMN pds_auth_server TEXT`);
151
+ }
152
+ catch { }
153
+ }
154
+ /** Normalize SQL type names to handle dialect differences (e.g. VARCHAR → TEXT) */
155
+ function normalizeType(type) {
156
+ const upper = type.toUpperCase();
157
+ if (upper === 'VARCHAR' || upper === 'CHARACTER VARYING')
158
+ return 'TEXT';
159
+ if (upper === 'TIMESTAMP WITH TIME ZONE')
160
+ return 'TIMESTAMPTZ';
161
+ if (upper === 'BOOLEAN' || upper === 'BOOL')
162
+ return 'BOOLEAN';
163
+ if (upper === 'INT' || upper === 'INT4' || upper === 'INT8' || upper === 'BIGINT' || upper === 'SMALLINT')
164
+ return 'INTEGER';
165
+ return upper;
166
+ }
167
+ async function getExistingColumns(tableName) {
168
+ if (!/^[a-zA-Z0-9._]+$/.test(tableName)) {
169
+ throw new Error(`Invalid table name for introspection: ${tableName}`);
170
+ }
171
+ const cols = new Map();
172
+ try {
173
+ const query = dialect.introspectColumnsQuery(tableName);
174
+ const rows = await all(query);
175
+ for (const row of rows) {
176
+ // SQLite PRAGMA returns { name, type }, DuckDB returns { column_name, data_type }
177
+ const name = (row.column_name || row.name);
178
+ const type = normalizeType((row.data_type || row.type || 'TEXT'));
179
+ cols.set(name, type);
180
+ }
181
+ }
182
+ catch {
183
+ // Table doesn't exist yet
184
+ }
185
+ return cols;
186
+ }
187
+ function diffColumns(tableName, existingCols, expectedCols, changes) {
188
+ for (const [colName, colType] of expectedCols) {
189
+ if (!existingCols.has(colName)) {
190
+ changes.push({ table: tableName, action: 'add', column: colName, type: colType });
191
+ }
192
+ }
193
+ for (const [colName] of existingCols) {
194
+ if (!expectedCols.has(colName)) {
195
+ changes.push({ table: tableName, action: 'drop', column: colName });
196
+ }
197
+ }
198
+ for (const [colName, colType] of expectedCols) {
199
+ const existingType = existingCols.get(colName);
200
+ if (existingType && normalizeType(existingType) !== normalizeType(colType)) {
201
+ changes.push({ table: tableName, action: 'retype', column: colName, type: colType });
202
+ }
203
+ }
204
+ }
205
+ /** Build expected columns map for a child/union table */
206
+ function buildChildExpectedCols(columns) {
207
+ const expected = new Map();
208
+ expected.set('parent_uri', 'TEXT');
209
+ expected.set('parent_did', 'TEXT');
210
+ for (const col of columns) {
211
+ expected.set(col.name, normalizeType(col.sqlType));
212
+ }
213
+ return expected;
214
+ }
215
+ export async function migrateSchema(tableSchemas) {
216
+ const changes = [];
217
+ const newCollections = new Set();
218
+ for (const schema of tableSchemas) {
219
+ if (schema.columns.length === 0)
220
+ continue; // generic JSON storage, skip
221
+ const tableName = schema.collection;
222
+ const existingCols = await getExistingColumns(tableName);
223
+ if (existingCols.size === 0) {
224
+ newCollections.add(schema.collection);
225
+ continue; // table just created, nothing to migrate
226
+ }
227
+ // Expected columns: base columns (uri, cid, did, indexed_at) + schema columns
228
+ const expectedCols = new Map();
229
+ expectedCols.set('uri', 'TEXT');
230
+ expectedCols.set('cid', 'TEXT');
231
+ expectedCols.set('did', 'TEXT');
232
+ expectedCols.set('indexed_at', normalizeType(dialect.timestampType));
233
+ for (const col of schema.columns) {
234
+ expectedCols.set(col.name, normalizeType(col.sqlType));
235
+ }
236
+ diffColumns(tableName, existingCols, expectedCols, changes);
237
+ // Diff child tables
238
+ for (const child of schema.children) {
239
+ const childTable = child.tableName.replace(/"/g, '');
240
+ const existingChildCols = await getExistingColumns(childTable);
241
+ if (existingChildCols.size === 0)
242
+ continue;
243
+ diffColumns(childTable, existingChildCols, buildChildExpectedCols(child.columns), changes);
244
+ }
245
+ // Diff union branch tables
246
+ for (const union of schema.unions) {
247
+ for (const branch of union.branches) {
248
+ const branchTable = branch.tableName.replace(/"/g, '');
249
+ const existingBranchCols = await getExistingColumns(branchTable);
250
+ if (existingBranchCols.size === 0)
251
+ continue;
252
+ diffColumns(branchTable, existingBranchCols, buildChildExpectedCols(branch.columns), changes);
253
+ }
254
+ }
255
+ }
256
+ // Detect and drop orphaned child/union tables (query table list once)
257
+ let allTableNames = null;
258
+ try {
259
+ const rows = await all(dialect.listTablesQuery);
260
+ allTableNames = rows.map((r) => r.table_name);
261
+ }
262
+ catch { }
263
+ if (allTableNames) {
264
+ for (const schema of tableSchemas) {
265
+ if (schema.columns.length === 0)
266
+ continue;
267
+ const expectedTables = new Set();
268
+ for (const child of schema.children) {
269
+ expectedTables.add(child.tableName.replace(/"/g, ''));
270
+ }
271
+ for (const union of schema.unions) {
272
+ for (const branch of union.branches) {
273
+ expectedTables.add(branch.tableName.replace(/"/g, ''));
274
+ }
275
+ }
276
+ for (const name of allTableNames) {
277
+ if (name.startsWith(schema.collection + '__') && !expectedTables.has(name)) {
278
+ await run(`DROP TABLE IF EXISTS "${name}"`);
279
+ emit('migration', 'drop_table', { table: name });
280
+ }
281
+ }
282
+ }
283
+ }
284
+ if (changes.length > 0) {
285
+ await applyMigrationChanges(changes);
286
+ }
287
+ // Trigger backfill only for genuinely new collections (tables created this startup)
288
+ // Previously this checked ALL empty tables, which caused infinite resync loops
289
+ // for collections that are legitimately empty (e.g. blocks when nobody has blocked)
290
+ if (newCollections.size > 0) {
291
+ const [hasRepos] = await all(`SELECT 1 FROM _repos LIMIT 1`);
292
+ if (hasRepos) {
293
+ await run(`UPDATE _repos SET status = 'pending' WHERE status = 'active'`);
294
+ for (const collection of newCollections) {
295
+ emit('migration', 'new_collection', { collection });
296
+ }
297
+ }
298
+ }
299
+ return changes;
300
+ }
301
+ async function applyMigrationChanges(changes) {
302
+ for (const change of changes) {
303
+ const quotedTable = `"${change.table}"`;
304
+ const quotedColumn = `"${change.column}"`;
305
+ try {
306
+ switch (change.action) {
307
+ case 'add': {
308
+ await run(`ALTER TABLE ${quotedTable} ADD COLUMN ${quotedColumn} ${change.type}`);
309
+ emit('migration', 'add_column', { table: change.table, column: change.column, type: change.type });
310
+ const schema = schemas.get(change.table);
311
+ if (schema?.refColumns.includes(change.column)) {
312
+ const prefix = change.table.replace(/\./g, '_');
313
+ await run(`CREATE INDEX IF NOT EXISTS idx_${prefix}_${change.column} ON ${quotedTable}(${quotedColumn})`);
314
+ }
315
+ break;
316
+ }
317
+ case 'drop':
318
+ await run(`ALTER TABLE ${quotedTable} DROP COLUMN ${quotedColumn}`);
319
+ emit('migration', 'drop_column', { table: change.table, column: change.column });
320
+ break;
321
+ case 'retype':
322
+ await run(`ALTER TABLE ${quotedTable} DROP COLUMN ${quotedColumn}`);
323
+ await run(`ALTER TABLE ${quotedTable} ADD COLUMN ${quotedColumn} ${change.type}`);
324
+ emit('migration', 'retype_column', { table: change.table, column: change.column, type: change.type });
325
+ break;
326
+ }
327
+ }
328
+ catch (err) {
329
+ console.warn(`[migration] failed to ${change.action} column "${change.column}" on "${change.table}": ${err.message}`);
330
+ emit('migration', 'error', {
331
+ action: change.action,
332
+ table: change.table,
333
+ column: change.column,
334
+ error: err.message,
335
+ });
336
+ }
173
337
  }
174
338
  }
175
339
  export async function getCursor(key) {
176
- const rows = await all(`SELECT value FROM _cursor WHERE key = $1`, key);
340
+ const rows = await all(`SELECT value FROM _cursor WHERE key = $1`, [key]);
177
341
  return rows[0]?.value || null;
178
342
  }
179
343
  export async function setCursor(key, value) {
180
- await run(`INSERT OR REPLACE INTO _cursor (key, value) VALUES ($1, $2)`, key, value);
344
+ await run(`INSERT OR REPLACE INTO _cursor (key, value) VALUES ($1, $2)`, [key, value]);
181
345
  }
182
346
  export async function getRepoStatus(did) {
183
- const rows = await all(`SELECT status FROM _repos WHERE did = $1`, did);
347
+ const rows = await all(`SELECT status FROM _repos WHERE did = $1`, [did]);
184
348
  return rows[0]?.status || null;
185
349
  }
186
350
  export async function setRepoStatus(did, status, rev, opts) {
187
351
  if (status === 'active') {
188
352
  // Update existing row preserving handle if not provided
189
- await run(`UPDATE _repos SET status = $1, handle = COALESCE($2, handle), backfilled_at = $3, rev = COALESCE($4, rev), retry_count = 0, retry_after = 0 WHERE did = $5`, status, opts?.handle || null, new Date().toISOString(), rev || null, did);
353
+ await run(`UPDATE _repos SET status = $1, handle = COALESCE($2, handle), backfilled_at = $3, rev = COALESCE($4, rev), retry_count = 0, retry_after = 0 WHERE did = $5`, [status, opts?.handle || null, new Date().toISOString(), rev || null, did]);
190
354
  // Insert if row didn't exist yet
191
- await run(`INSERT OR IGNORE INTO _repos (did, status, handle, backfilled_at, rev, retry_count, retry_after) VALUES ($1, $2, $3, $4, $5, 0, 0)`, did, status, opts?.handle || null, new Date().toISOString(), rev || null);
355
+ await run(`INSERT OR IGNORE INTO _repos (did, status, handle, backfilled_at, rev, retry_count, retry_after) VALUES ($1, $2, $3, $4, $5, 0, 0)`, [did, status, opts?.handle || null, new Date().toISOString(), rev || null]);
192
356
  }
193
357
  else if (status === 'failed' && opts) {
194
- await run(`UPDATE _repos SET status = $1, retry_count = $2, retry_after = $3, handle = COALESCE($4, handle) WHERE did = $5`, status, opts.retryCount ?? 0, opts.retryAfter ?? 0, opts.handle || null, did);
358
+ await run(`UPDATE _repos SET status = $1, retry_count = $2, retry_after = $3, handle = COALESCE($4, handle) WHERE did = $5`, [status, opts.retryCount ?? 0, opts.retryAfter ?? 0, opts.handle || null, did]);
195
359
  // If row didn't exist yet, insert it
196
- await run(`INSERT OR IGNORE INTO _repos (did, status, handle, retry_count, retry_after) VALUES ($1, $2, $3, $4, $5)`, did, status, opts.handle || null, opts.retryCount ?? 0, opts.retryAfter ?? 0);
360
+ await run(`INSERT OR IGNORE INTO _repos (did, status, handle, retry_count, retry_after) VALUES ($1, $2, $3, $4, $5)`, [did, status, opts.handle || null, opts.retryCount ?? 0, opts.retryAfter ?? 0]);
197
361
  }
198
362
  else {
199
- await run(`UPDATE _repos SET status = $1 WHERE did = $2`, status, did);
200
- await run(`INSERT OR IGNORE INTO _repos (did, status) VALUES ($1, $2)`, did, status);
363
+ await run(`UPDATE _repos SET status = $1 WHERE did = $2`, [status, did]);
364
+ await run(`INSERT OR IGNORE INTO _repos (did, status) VALUES ($1, $2)`, [did, status]);
201
365
  }
202
366
  }
367
+ /** Update the handle for a DID if it exists in _repos. */
368
+ export async function updateRepoHandle(did, handle) {
369
+ await run(`UPDATE _repos SET handle = $1 WHERE did = $2`, [handle, did]);
370
+ }
371
+ export async function getRepoRev(did) {
372
+ const rows = await all(`SELECT rev FROM _repos WHERE did = $1`, [did]);
373
+ return rows[0]?.rev ?? null;
374
+ }
203
375
  export async function getRepoRetryInfo(did) {
204
- const rows = await all(`SELECT retry_count, retry_after FROM _repos WHERE did = $1`, did);
376
+ const rows = await all(`SELECT retry_count, retry_after FROM _repos WHERE did = $1`, [did]);
205
377
  if (rows.length === 0)
206
378
  return null;
207
379
  return { retryCount: Number(rows[0].retry_count), retryAfter: Number(rows[0].retry_after) };
208
380
  }
209
381
  export async function listRetryEligibleRepos(maxRetries) {
210
382
  const now = Math.floor(Date.now() / 1000);
211
- const rows = await all(`SELECT did FROM _repos WHERE status = 'failed' AND retry_after <= $1 AND retry_count < $2`, now, maxRetries);
383
+ const rows = await all(`SELECT did FROM _repos WHERE status = 'failed' AND retry_after <= $1 AND retry_count < $2`, [
384
+ now,
385
+ maxRetries,
386
+ ]);
212
387
  return rows.map((r) => r.did);
213
388
  }
214
389
  export async function listPendingRepos() {
215
390
  const rows = await all(`SELECT did FROM _repos WHERE status = 'pending'`);
216
391
  return rows.map((r) => r.did);
217
392
  }
393
+ export async function listActiveRepoDids() {
394
+ const rows = await all(`SELECT did FROM _repos WHERE status = 'active'`);
395
+ return rows.map((r) => r.did);
396
+ }
397
+ export async function removeRepo(did) {
398
+ await run(`DELETE FROM _repos WHERE did = $1`, [did]);
399
+ }
400
+ export async function getRepoHandle(did) {
401
+ const rows = await all(`SELECT handle FROM _repos WHERE did = $1`, [did]);
402
+ return rows[0]?.handle ?? null;
403
+ }
218
404
  export async function listAllRepoStatuses() {
219
405
  return (await all(`SELECT did, status FROM _repos`));
220
406
  }
@@ -228,27 +414,96 @@ export async function listReposPaginated(opts = {}) {
228
414
  params.push(status);
229
415
  }
230
416
  if (q) {
231
- conditions.push(`(did ILIKE $${paramIdx} OR handle ILIKE $${paramIdx})`);
417
+ conditions.push(`(did ${dialect.ilike} $${paramIdx} OR handle ${dialect.ilike} $${paramIdx})`);
232
418
  params.push(`%${q}%`);
233
419
  paramIdx++;
234
420
  }
235
421
  const where = conditions.length ? ' WHERE ' + conditions.join(' AND ') : '';
236
- const countRows = await all(`SELECT COUNT(*)::INTEGER as total FROM _repos${where}`, ...params);
422
+ const countRows = await all(`SELECT ${dialect.countAsInteger} as total FROM _repos${where}`, params);
237
423
  const total = Number(countRows[0]?.total || 0);
238
- const rows = await all(`SELECT did, handle, status, backfilled_at, rev FROM _repos${where} ORDER BY backfilled_at DESC NULLS LAST, did LIMIT $${paramIdx++} OFFSET $${paramIdx++}`, ...params, limit, offset);
424
+ const rows = await all(`SELECT did, handle, status, backfilled_at, rev FROM _repos${where} ORDER BY CASE WHEN backfilled_at IS NULL THEN 1 ELSE 0 END, backfilled_at DESC, did LIMIT $${paramIdx++} OFFSET $${paramIdx++}`, [...params, limit, offset]);
239
425
  return { repos: rows, total };
240
426
  }
241
427
  export async function getCollectionCounts() {
242
428
  const counts = {};
243
429
  for (const [collection, schema] of schemas) {
244
- const rows = await all(`SELECT COUNT(*)::INTEGER as count FROM ${schema.tableName}`);
430
+ const rows = await all(`SELECT ${dialect.countAsInteger} as count FROM ${schema.tableName}`);
245
431
  counts[collection] = Number(rows[0]?.count || 0);
246
432
  }
247
433
  return counts;
248
434
  }
435
+ export async function getRepoStatusCounts() {
436
+ const rows = await all(`SELECT status, ${dialect.countAsInteger} as count FROM _repos GROUP BY status`);
437
+ const counts = {};
438
+ for (const row of rows)
439
+ counts[row.status] = Number(row.count);
440
+ return counts;
441
+ }
442
+ export async function getDatabaseSize() {
443
+ if (dialect.supportsSequences) {
444
+ // DuckDB: pragma_database_size returns pre-formatted strings
445
+ const rows = await all('SELECT database_size, memory_usage, memory_limit FROM pragma_database_size()');
446
+ return rows[0] ?? {};
447
+ }
448
+ // SQLite: compute from page_count * page_size
449
+ const pages = await all('SELECT page_count FROM pragma_page_count()');
450
+ const sizes = await all('SELECT page_size FROM pragma_page_size()');
451
+ const pageCount = Number(pages[0]?.page_count ?? 0);
452
+ const pageSize = Number(sizes[0]?.page_size ?? 0);
453
+ const bytes = pageCount * pageSize;
454
+ const mib = (bytes / 1024 / 1024).toFixed(1);
455
+ return { database_size: `${mib} MiB`, memory_usage: 'N/A', memory_limit: 'N/A' };
456
+ }
457
+ export async function getLabelCount(val) {
458
+ const rows = await all(`SELECT ${dialect.countAsInteger} as count FROM _labels WHERE val = $1`, [val]);
459
+ return Number(rows[0]?.count || 0);
460
+ }
461
+ export async function deleteLabels(val) {
462
+ const count = await getLabelCount(val);
463
+ await run(`DELETE FROM _labels WHERE val = $1`, [val]);
464
+ return count;
465
+ }
466
+ export async function getRecentRecords(collection, limit) {
467
+ const schema = schemas.get(collection);
468
+ if (!schema)
469
+ return [];
470
+ const rows = await all(`SELECT t.* FROM ${schema.tableName} t JOIN _repos r ON t.did = r.did WHERE t.indexed_at > r.backfilled_at ORDER BY t.indexed_at DESC LIMIT $1`, [limit]);
471
+ return rows;
472
+ }
249
473
  export async function getSchemaDump() {
250
- const rows = await all(`SELECT sql FROM duckdb_tables() ORDER BY table_name`);
251
- return rows.map((r) => r.sql + ';').join('\n\n');
474
+ let rows;
475
+ if (dialect.supportsSequences) {
476
+ // DuckDB: use duckdb_tables() for full DDL
477
+ rows = await all(`SELECT sql FROM duckdb_tables() ORDER BY table_name`);
478
+ }
479
+ else {
480
+ // SQLite: use sqlite_master, skip FTS shadow/internal tables
481
+ rows = await all(`SELECT sql FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name NOT LIKE '_fts_%' AND sql IS NOT NULL ORDER BY name`);
482
+ }
483
+ // Normalize indentation and formatting
484
+ return rows
485
+ .map((r) => {
486
+ let sql = r.sql.trim();
487
+ // Remove quotes around column names (SQLite adds them for some columns)
488
+ sql = sql.replace(/\n\s*"(\w+)"/g, '\n$1');
489
+ // Ensure closing paren is on its own line
490
+ sql = sql.replace(/([^(\s])\)$/, '$1\n)');
491
+ // Normalize leading-comma columns added by ALTER TABLE into trailing commas
492
+ sql = sql.replace(/\n\s*,\s*/g, ',\n');
493
+ // Split into lines and re-indent consistently
494
+ const lines = sql.split('\n').map((l) => l.trim());
495
+ sql = lines
496
+ .map((line, i) => {
497
+ if (i === 0)
498
+ return line; // CREATE TABLE line
499
+ if (line.startsWith(')'))
500
+ return ')'; // closing paren at top level
501
+ return ' ' + line; // indent columns
502
+ })
503
+ .join('\n');
504
+ return sql + ';';
505
+ })
506
+ .join('\n\n');
252
507
  }
253
508
  export function buildInsertOp(collection, uri, cid, authorDid, record) {
254
509
  const schema = schemas.get(collection);
@@ -267,12 +522,12 @@ export function buildInsertOp(collection, uri, cid, authorDid, record) {
267
522
  else if (col.originalName.endsWith('__cid') && record[col.originalName.replace('__cid', '')]) {
268
523
  rawValue = record[col.originalName.replace('__cid', '')].cid;
269
524
  }
270
- colNames.push(col.name);
525
+ colNames.push(q(col.name));
271
526
  placeholders.push(`$${paramIdx++}`);
272
527
  if (rawValue === undefined || rawValue === null) {
273
528
  values.push(null);
274
529
  }
275
- else if (col.duckdbType === 'JSON') {
530
+ else if (col.isJson) {
276
531
  values.push(JSON.stringify(rawValue));
277
532
  }
278
533
  else {
@@ -287,34 +542,34 @@ export async function insertRecord(collection, uri, cid, authorDid, record) {
287
542
  if (!schema)
288
543
  throw new Error(`Unknown collection: ${collection}`);
289
544
  const { sql, params } = buildInsertOp(collection, uri, cid, authorDid, record);
290
- await run(sql, ...params);
545
+ await run(sql, params);
291
546
  // Insert child table rows
292
547
  for (const child of schema.children) {
293
548
  const items = record[child.fieldName];
294
549
  if (!Array.isArray(items))
295
550
  continue;
296
551
  // Delete existing child rows (handles INSERT OR REPLACE on main table)
297
- await run(`DELETE FROM ${child.tableName} WHERE parent_uri = $1`, uri);
552
+ await run(`DELETE FROM ${child.tableName} WHERE parent_uri = $1`, [uri]);
298
553
  for (const item of items) {
299
554
  const colNames = ['parent_uri', 'parent_did'];
300
555
  const placeholders = ['$1', '$2'];
301
556
  const values = [uri, authorDid];
302
557
  let idx = 3;
303
558
  for (const col of child.columns) {
304
- colNames.push(col.name);
559
+ colNames.push(q(col.name));
305
560
  placeholders.push(`$${idx++}`);
306
561
  const raw = item[col.originalName];
307
562
  if (raw === undefined || raw === null) {
308
563
  values.push(null);
309
564
  }
310
- else if (col.duckdbType === 'JSON') {
565
+ else if (col.isJson) {
311
566
  values.push(JSON.stringify(raw));
312
567
  }
313
568
  else {
314
569
  values.push(raw);
315
570
  }
316
571
  }
317
- await run(`INSERT INTO ${child.tableName} (${colNames.join(', ')}) VALUES (${placeholders.join(', ')})`, ...values);
572
+ await run(`INSERT INTO ${child.tableName} (${colNames.join(', ')}) VALUES (${placeholders.join(', ')})`, values);
318
573
  }
319
574
  }
320
575
  // Insert union branch rows
@@ -327,7 +582,7 @@ export async function insertRecord(collection, uri, cid, authorDid, record) {
327
582
  continue;
328
583
  // Delete existing branch rows (handles INSERT OR REPLACE)
329
584
  for (const b of union.branches) {
330
- await run(`DELETE FROM ${b.tableName} WHERE parent_uri = $1`, uri);
585
+ await run(`DELETE FROM ${b.tableName} WHERE parent_uri = $1`, [uri]);
331
586
  }
332
587
  if (branch.isArray && branch.arrayField) {
333
588
  // Array branch (e.g., embed.images) — insert one row per array item
@@ -340,20 +595,20 @@ export async function insertRecord(collection, uri, cid, authorDid, record) {
340
595
  const values = [uri, authorDid];
341
596
  let idx = 3;
342
597
  for (const col of branch.columns) {
343
- colNames.push(col.name);
598
+ colNames.push(q(col.name));
344
599
  placeholders.push(`$${idx++}`);
345
600
  const raw = item[col.originalName];
346
601
  if (raw === undefined || raw === null) {
347
602
  values.push(null);
348
603
  }
349
- else if (col.duckdbType === 'JSON') {
604
+ else if (col.isJson) {
350
605
  values.push(JSON.stringify(raw));
351
606
  }
352
607
  else {
353
608
  values.push(raw);
354
609
  }
355
610
  }
356
- await run(`INSERT INTO ${branch.tableName} (${colNames.join(', ')}) VALUES (${placeholders.join(', ')})`, ...values);
611
+ await run(`INSERT INTO ${branch.tableName} (${colNames.join(', ')}) VALUES (${placeholders.join(', ')})`, values);
357
612
  }
358
613
  }
359
614
  else {
@@ -364,22 +619,24 @@ export async function insertRecord(collection, uri, cid, authorDid, record) {
364
619
  const values = [uri, authorDid];
365
620
  let idx = 3;
366
621
  for (const col of branch.columns) {
367
- colNames.push(col.name);
622
+ colNames.push(q(col.name));
368
623
  placeholders.push(`$${idx++}`);
369
624
  const raw = branchData[col.originalName];
370
625
  if (raw === undefined || raw === null) {
371
626
  values.push(null);
372
627
  }
373
- else if (col.duckdbType === 'JSON') {
628
+ else if (col.isJson) {
374
629
  values.push(JSON.stringify(raw));
375
630
  }
376
631
  else {
377
632
  values.push(raw);
378
633
  }
379
634
  }
380
- await run(`INSERT INTO ${branch.tableName} (${colNames.join(', ')}) VALUES (${placeholders.join(', ')})`, ...values);
635
+ await run(`INSERT INTO ${branch.tableName} (${colNames.join(', ')}) VALUES (${placeholders.join(', ')})`, values);
381
636
  }
382
637
  }
638
+ // Incrementally update FTS index for this record
639
+ await updateFtsRecord(collection, uri);
383
640
  }
384
641
  /** Extract branch data from a union value, handling wrapper properties */
385
642
  function resolveBranchData(unionValue, branch) {
@@ -394,32 +651,41 @@ export async function deleteRecord(collection, uri) {
394
651
  const schema = schemas.get(collection);
395
652
  if (!schema)
396
653
  return;
654
+ // Remove from FTS index before deleting the record data
655
+ await deleteFtsRecord(collection, uri);
397
656
  for (const child of schema.children) {
398
- await run(`DELETE FROM ${child.tableName} WHERE parent_uri = $1`, uri);
657
+ await run(`DELETE FROM ${child.tableName} WHERE parent_uri = $1`, [uri]);
399
658
  }
400
659
  for (const union of schema.unions) {
401
660
  for (const branch of union.branches) {
402
- await run(`DELETE FROM ${branch.tableName} WHERE parent_uri = $1`, uri);
661
+ await run(`DELETE FROM ${branch.tableName} WHERE parent_uri = $1`, [uri]);
403
662
  }
404
663
  }
405
- await run(`DELETE FROM ${schema.tableName} WHERE uri = $1`, uri);
664
+ await run(`DELETE FROM ${schema.tableName} WHERE uri = $1`, [uri]);
406
665
  }
407
666
  export async function insertLabels(labels) {
408
667
  if (labels.length === 0)
409
668
  return;
410
669
  for (const label of labels) {
411
- // Skip if an active (non-negated, non-expired, not-superseded-by-negation) label already exists
412
- const existing = await all(`SELECT 1 FROM _labels l1 WHERE l1.src = $1 AND l1.uri = $2 AND l1.val = $3 AND l1.neg = false AND (l1.exp IS NULL OR l1.exp > CURRENT_TIMESTAMP) AND NOT EXISTS (SELECT 1 FROM _labels l2 WHERE l2.uri = l1.uri AND l2.val = l1.val AND l2.neg = true AND l2.id > l1.id) LIMIT 1`, label.src, label.uri, label.val);
670
+ // Skip if an active (non-negated, non-expired, not-superseded-by-negation) label already exists for this src+uri+val
671
+ const existing = await all(`SELECT 1 FROM _labels l1 WHERE l1.src = $1 AND l1.uri = $2 AND l1.val = $3 AND l1.neg = false AND (l1.exp IS NULL OR l1.exp > CURRENT_TIMESTAMP) AND NOT EXISTS (SELECT 1 FROM _labels l2 WHERE l2.uri = l1.uri AND l2.val = l1.val AND l2.neg = true AND l2.id > l1.id) LIMIT 1`, [label.src, label.uri, label.val]);
413
672
  if (!label.neg && existing.length > 0)
414
673
  continue;
415
- await run(`INSERT INTO _labels (src, uri, val, neg, cts, exp) VALUES ($1, $2, $3, $4, $5, $6)`, label.src, label.uri, label.val, label.neg || false, label.cts || new Date().toISOString(), label.exp || null);
674
+ await run(`INSERT INTO _labels (src, uri, val, neg, cts, exp) VALUES ($1, $2, $3, $4, $5, $6)`, [
675
+ label.src,
676
+ label.uri,
677
+ label.val,
678
+ label.neg || false,
679
+ label.cts || new Date().toISOString(),
680
+ label.exp || null,
681
+ ]);
416
682
  }
417
683
  }
418
684
  export async function queryLabelsForUris(uris) {
419
685
  if (uris.length === 0)
420
686
  return new Map();
421
687
  const placeholders = uris.map((_, i) => `$${i + 1}`).join(',');
422
- const rows = await all(`SELECT src, uri, val, neg, cts, exp FROM _labels l1 WHERE uri IN (${placeholders}) AND (exp IS NULL OR exp > CURRENT_TIMESTAMP) AND neg = false AND NOT EXISTS (SELECT 1 FROM _labels l2 WHERE l2.uri = l1.uri AND l2.val = l1.val AND l2.neg = true AND l2.id > l1.id)`, ...uris);
688
+ const rows = await all(`SELECT src, uri, val, neg, cts, exp FROM _labels l1 WHERE uri IN (${placeholders}) AND (exp IS NULL OR exp > CURRENT_TIMESTAMP) AND neg = false AND NOT EXISTS (SELECT 1 FROM _labels l2 WHERE l2.uri = l1.uri AND l2.val = l1.val AND l2.neg = true AND l2.id > l1.id) GROUP BY uri, val`, uris);
423
689
  const result = new Map();
424
690
  for (const row of rows) {
425
691
  const key = row.uri;
@@ -429,9 +695,9 @@ export async function queryLabelsForUris(uris) {
429
695
  src: row.src,
430
696
  uri: row.uri,
431
697
  val: row.val,
432
- neg: row.neg,
698
+ neg: !!row.neg,
433
699
  cts: normalizeValue(row.cts),
434
- exp: row.exp ? String(row.exp) : null,
700
+ ...(row.exp ? { exp: String(row.exp) } : {}),
435
701
  });
436
702
  }
437
703
  return result;
@@ -452,246 +718,209 @@ export async function bulkInsertRecords(records) {
452
718
  if (!schema)
453
719
  continue;
454
720
  const stagingTable = `_staging_${collection.replace(/\./g, '_')}`;
455
- const allCols = ['uri', 'cid', 'did', 'indexed_at', ...schema.columns.map((c) => c.name)];
721
+ const allCols = ['uri', 'cid', 'did', 'indexed_at', ...schema.columns.map((c) => q(c.name))];
456
722
  const colDefs = [
457
723
  'uri TEXT',
458
724
  'cid TEXT',
459
725
  'did TEXT',
460
726
  'indexed_at TEXT',
461
- ...schema.columns.map((c) => `${c.name} ${c.duckdbType === 'TIMESTAMP' ? 'TEXT' : c.duckdbType}`),
727
+ ...schema.columns.map((c) => {
728
+ const t = c.sqlType;
729
+ // Use TEXT for timestamp columns in staging (will cast on merge)
730
+ return `${q(c.name)} ${t === 'TIMESTAMP' || t === 'TIMESTAMPTZ' ? 'TEXT' : t}`;
731
+ }),
462
732
  ];
463
- // Create staging table + appender + merge all in one write queue slot
464
- await enqueue('write', async () => {
465
- await con.run(`DROP TABLE IF EXISTS ${stagingTable}`);
466
- await con.run(`CREATE TABLE ${stagingTable} (${colDefs.join(', ')})`);
467
- const appender = await con.createAppender(stagingTable);
468
- const now = new Date().toISOString();
733
+ await port.execute(`DROP TABLE IF EXISTS ${stagingTable}`, []);
734
+ await port.execute(`CREATE TABLE ${stagingTable} (${colDefs.join(', ')})`, []);
735
+ const inserter = await port.createBulkInserter(stagingTable, allCols);
736
+ const now = new Date().toISOString();
737
+ for (const rec of recs) {
738
+ try {
739
+ const values = [rec.uri, rec.cid, rec.did, now];
740
+ for (const col of schema.columns) {
741
+ values.push(resolveColumnValue(col, rec.record));
742
+ }
743
+ inserter.append(values);
744
+ inserted++;
745
+ }
746
+ catch {
747
+ // Skip bad records
748
+ }
749
+ }
750
+ await inserter.close();
751
+ // Merge into target, filtering rows that would violate NOT NULL
752
+ const selectCols = allCols.map((name) => {
753
+ const col = schema.columns.find((c) => q(c.name) === name);
754
+ if (name === 'indexed_at' || (col && (col.sqlType === 'TIMESTAMP' || col.sqlType === 'TIMESTAMPTZ'))) {
755
+ return `${dialect.tryCastTimestamp(name)} AS ${name}`;
756
+ }
757
+ return name;
758
+ });
759
+ const notNullChecks = ['uri IS NOT NULL', 'did IS NOT NULL'];
760
+ for (const col of schema.columns) {
761
+ if (col.notNull) {
762
+ if (col.sqlType === 'TIMESTAMP' || col.sqlType === 'TIMESTAMPTZ') {
763
+ notNullChecks.push(`${dialect.tryCastTimestamp(q(col.name))} IS NOT NULL`);
764
+ }
765
+ else {
766
+ notNullChecks.push(`${q(col.name)} IS NOT NULL`);
767
+ }
768
+ }
769
+ }
770
+ const whereClause = notNullChecks.length ? ` WHERE ${notNullChecks.join(' AND ')}` : '';
771
+ await port.execute(`INSERT OR REPLACE INTO ${schema.tableName} (${allCols.join(', ')}) SELECT ${selectCols.join(', ')} FROM ${stagingTable}${whereClause}`, []);
772
+ await port.execute(`DROP TABLE ${stagingTable}`, []);
773
+ // Populate child tables
774
+ for (const child of schema.children) {
775
+ const childStagingTable = `_staging_${collection.replace(/\./g, '_')}__${child.fieldName}`;
776
+ const childColDefs = [
777
+ 'parent_uri TEXT',
778
+ 'parent_did TEXT',
779
+ ...child.columns.map((c) => {
780
+ const t = c.sqlType;
781
+ return `${q(c.name)} ${t === 'TIMESTAMP' || t === 'TIMESTAMPTZ' ? 'TEXT' : t}`;
782
+ }),
783
+ ];
784
+ const childAllCols = ['parent_uri', 'parent_did', ...child.columns.map((c) => q(c.name))];
785
+ await port.execute(`DROP TABLE IF EXISTS ${childStagingTable}`, []);
786
+ await port.execute(`CREATE TABLE ${childStagingTable} (${childColDefs.join(', ')})`, []);
787
+ const childInserter = await port.createBulkInserter(childStagingTable, childAllCols);
469
788
  for (const rec of recs) {
470
- try {
471
- appender.appendVarchar(rec.uri);
472
- appender.appendVarchar(rec.cid);
473
- appender.appendVarchar(rec.did);
474
- appender.appendVarchar(now);
475
- for (const col of schema.columns) {
476
- let rawValue = rec.record[col.originalName];
477
- if (rawValue && typeof rawValue === 'object' && col.name.endsWith('_uri') && col.isRef) {
478
- rawValue = rawValue.uri;
479
- }
480
- else if (col.originalName.endsWith('__cid') && rec.record[col.originalName.replace('__cid', '')]) {
481
- rawValue = rec.record[col.originalName.replace('__cid', '')].cid;
482
- }
483
- if (rawValue === undefined || rawValue === null) {
484
- appender.appendNull();
485
- }
486
- else if (col.duckdbType === 'JSON') {
487
- appender.appendVarchar(JSON.stringify(rawValue));
488
- }
489
- else if (col.duckdbType === 'INTEGER') {
490
- appender.appendInteger(typeof rawValue === 'number' ? rawValue : parseInt(rawValue));
491
- }
492
- else if (col.duckdbType === 'BOOLEAN') {
493
- appender.appendBoolean(!!rawValue);
494
- }
495
- else {
496
- appender.appendVarchar(String(rawValue));
789
+ const items = rec.record[child.fieldName];
790
+ if (!Array.isArray(items))
791
+ continue;
792
+ for (const item of items) {
793
+ try {
794
+ const values = [rec.uri, rec.did];
795
+ for (const col of child.columns) {
796
+ values.push(resolveRawColumnValue(col, item));
497
797
  }
798
+ childInserter.append(values);
799
+ }
800
+ catch {
801
+ // Skip bad items
498
802
  }
499
- appender.endRow();
500
- inserted++;
501
- }
502
- catch {
503
- // Skip bad records
504
803
  }
505
804
  }
506
- appender.flushSync();
507
- appender.closeSync();
508
- // Merge into target with TRY_CAST for TIMESTAMP columns, filtering rows that would violate NOT NULL
509
- const selectCols = allCols.map((name) => {
510
- const col = schema.columns.find((c) => c.name === name);
511
- if (name === 'indexed_at' || (col && col.duckdbType === 'TIMESTAMP')) {
512
- return `TRY_CAST(${name} AS TIMESTAMP) AS ${name}`;
805
+ await childInserter.close();
806
+ // Delete existing child rows for these URIs, then merge staging
807
+ const uriPlaceholders = recs.map((_, i) => `$${i + 1}`).join(',');
808
+ await port.execute(`DELETE FROM ${child.tableName} WHERE parent_uri IN (${uriPlaceholders})`, recs.map((r) => r.uri));
809
+ const childSelectCols = childAllCols.map((name) => {
810
+ const col = child.columns.find((c) => q(c.name) === name);
811
+ if (col && (col.sqlType === 'TIMESTAMP' || col.sqlType === 'TIMESTAMPTZ')) {
812
+ return `${dialect.tryCastTimestamp(name)} AS ${name}`;
513
813
  }
514
814
  return name;
515
815
  });
516
- // Build WHERE clause to exclude rows missing NOT NULL fields
517
- const notNullChecks = ['uri IS NOT NULL', 'did IS NOT NULL'];
518
- for (const col of schema.columns) {
519
- if (col.notNull) {
520
- if (col.duckdbType === 'TIMESTAMP') {
521
- notNullChecks.push(`TRY_CAST(${col.name} AS TIMESTAMP) IS NOT NULL`);
522
- }
523
- else {
524
- notNullChecks.push(`${col.name} IS NOT NULL`);
525
- }
526
- }
527
- }
528
- const whereClause = notNullChecks.length ? ` WHERE ${notNullChecks.join(' AND ')}` : '';
529
- await con.run(`INSERT OR REPLACE INTO ${schema.tableName} (${allCols.join(', ')}) SELECT ${selectCols.join(', ')} FROM ${stagingTable}${whereClause}`);
530
- await con.run(`DROP TABLE ${stagingTable}`);
531
- // Populate child tables
532
- for (const child of schema.children) {
533
- const childStagingTable = `_staging_${collection.replace(/\./g, '_')}__${child.fieldName}`;
534
- const childColDefs = [
816
+ await port.execute(`INSERT INTO ${child.tableName} (${childAllCols.join(', ')}) SELECT ${childSelectCols.join(', ')} FROM ${childStagingTable} WHERE parent_uri IS NOT NULL`, []);
817
+ await port.execute(`DROP TABLE ${childStagingTable}`, []);
818
+ }
819
+ // Populate union branch tables
820
+ for (const union of schema.unions) {
821
+ for (const branch of union.branches) {
822
+ const branchStagingTable = `_staging_${collection.replace(/\./g, '_')}__${toSnakeCase(union.fieldName)}_${branch.branchName}`;
823
+ const branchColDefs = [
535
824
  'parent_uri TEXT',
536
825
  'parent_did TEXT',
537
- ...child.columns.map((c) => `${c.name} ${c.duckdbType === 'TIMESTAMP' ? 'TEXT' : c.duckdbType}`),
826
+ ...branch.columns.map((c) => {
827
+ const t = c.sqlType;
828
+ return `${q(c.name)} ${t === 'TIMESTAMP' || t === 'TIMESTAMPTZ' ? 'TEXT' : t}`;
829
+ }),
538
830
  ];
539
- const childAllCols = ['parent_uri', 'parent_did', ...child.columns.map((c) => c.name)];
540
- await con.run(`DROP TABLE IF EXISTS ${childStagingTable}`);
541
- await con.run(`CREATE TABLE ${childStagingTable} (${childColDefs.join(', ')})`);
542
- const childAppender = await con.createAppender(childStagingTable);
831
+ const branchAllCols = ['parent_uri', 'parent_did', ...branch.columns.map((c) => q(c.name))];
832
+ await port.execute(`DROP TABLE IF EXISTS ${branchStagingTable}`, []);
833
+ await port.execute(`CREATE TABLE ${branchStagingTable} (${branchColDefs.join(', ')})`, []);
834
+ const branchInserter = await port.createBulkInserter(branchStagingTable, branchAllCols);
543
835
  for (const rec of recs) {
544
- const items = rec.record[child.fieldName];
545
- if (!Array.isArray(items))
836
+ const unionValue = rec.record[union.fieldName];
837
+ if (!unionValue || typeof unionValue !== 'object')
546
838
  continue;
547
- for (const item of items) {
548
- try {
549
- childAppender.appendVarchar(rec.uri);
550
- childAppender.appendVarchar(rec.did);
551
- for (const col of child.columns) {
552
- const rawValue = item[col.originalName];
553
- if (rawValue === undefined || rawValue === null) {
554
- childAppender.appendNull();
555
- }
556
- else if (col.duckdbType === 'JSON') {
557
- childAppender.appendVarchar(JSON.stringify(rawValue));
558
- }
559
- else if (col.duckdbType === 'INTEGER') {
560
- childAppender.appendInteger(typeof rawValue === 'number' ? rawValue : parseInt(rawValue));
561
- }
562
- else if (col.duckdbType === 'BOOLEAN') {
563
- childAppender.appendBoolean(!!rawValue);
564
- }
565
- else {
566
- childAppender.appendVarchar(String(rawValue));
567
- }
568
- }
569
- childAppender.endRow();
570
- }
571
- catch {
572
- // Skip bad items
573
- }
574
- }
575
- }
576
- childAppender.flushSync();
577
- childAppender.closeSync();
578
- // Delete existing child rows for these URIs, then merge staging
579
- const uriPlaceholders = recs.map((_, i) => `$${i + 1}`).join(',');
580
- const delStmt = await con.prepare(`DELETE FROM ${child.tableName} WHERE parent_uri IN (${uriPlaceholders})`);
581
- bindParams(delStmt, recs.map((r) => r.uri));
582
- await delStmt.run();
583
- const childSelectCols = childAllCols.map((name) => {
584
- const col = child.columns.find((c) => c.name === name);
585
- if (col && col.duckdbType === 'TIMESTAMP')
586
- return `TRY_CAST(${name} AS TIMESTAMP) AS ${name}`;
587
- return name;
588
- });
589
- await con.run(`INSERT INTO ${child.tableName} (${childAllCols.join(', ')}) SELECT ${childSelectCols.join(', ')} FROM ${childStagingTable} WHERE parent_uri IS NOT NULL`);
590
- await con.run(`DROP TABLE ${childStagingTable}`);
591
- }
592
- // Populate union branch tables
593
- for (const union of schema.unions) {
594
- for (const branch of union.branches) {
595
- const branchStagingTable = `_staging_${collection.replace(/\./g, '_')}__${toSnakeCase(union.fieldName)}_${branch.branchName}`;
596
- const branchColDefs = [
597
- 'parent_uri TEXT',
598
- 'parent_did TEXT',
599
- ...branch.columns.map((c) => `${c.name} ${c.duckdbType === 'TIMESTAMP' ? 'TEXT' : c.duckdbType}`),
600
- ];
601
- const branchAllCols = ['parent_uri', 'parent_did', ...branch.columns.map((c) => c.name)];
602
- await con.run(`DROP TABLE IF EXISTS ${branchStagingTable}`);
603
- await con.run(`CREATE TABLE ${branchStagingTable} (${branchColDefs.join(', ')})`);
604
- const branchAppender = await con.createAppender(branchStagingTable);
605
- for (const rec of recs) {
606
- const unionValue = rec.record[union.fieldName];
607
- if (!unionValue || typeof unionValue !== 'object')
608
- continue;
609
- if (unionValue.$type !== branch.type)
839
+ if (unionValue.$type !== branch.type)
840
+ continue;
841
+ if (branch.isArray && branch.arrayField) {
842
+ const items = resolveBranchData(unionValue, branch)[branch.arrayField];
843
+ if (!Array.isArray(items))
610
844
  continue;
611
- if (branch.isArray && branch.arrayField) {
612
- const items = resolveBranchData(unionValue, branch)[branch.arrayField];
613
- if (!Array.isArray(items))
614
- continue;
615
- for (const item of items) {
616
- try {
617
- branchAppender.appendVarchar(rec.uri);
618
- branchAppender.appendVarchar(rec.did);
619
- for (const col of branch.columns) {
620
- const rawValue = item[col.originalName];
621
- if (rawValue === undefined || rawValue === null) {
622
- branchAppender.appendNull();
623
- }
624
- else if (col.duckdbType === 'JSON') {
625
- branchAppender.appendVarchar(JSON.stringify(rawValue));
626
- }
627
- else if (col.duckdbType === 'INTEGER') {
628
- branchAppender.appendInteger(typeof rawValue === 'number' ? rawValue : parseInt(rawValue));
629
- }
630
- else if (col.duckdbType === 'BOOLEAN') {
631
- branchAppender.appendBoolean(!!rawValue);
632
- }
633
- else {
634
- branchAppender.appendVarchar(String(rawValue));
635
- }
636
- }
637
- branchAppender.endRow();
638
- }
639
- catch {
640
- // Skip bad items
641
- }
642
- }
643
- }
644
- else {
845
+ for (const item of items) {
645
846
  try {
646
- const branchData = resolveBranchData(unionValue, branch);
647
- branchAppender.appendVarchar(rec.uri);
648
- branchAppender.appendVarchar(rec.did);
847
+ const values = [rec.uri, rec.did];
649
848
  for (const col of branch.columns) {
650
- const rawValue = branchData[col.originalName];
651
- if (rawValue === undefined || rawValue === null) {
652
- branchAppender.appendNull();
653
- }
654
- else if (col.duckdbType === 'JSON') {
655
- branchAppender.appendVarchar(JSON.stringify(rawValue));
656
- }
657
- else if (col.duckdbType === 'INTEGER') {
658
- branchAppender.appendInteger(typeof rawValue === 'number' ? rawValue : parseInt(rawValue));
659
- }
660
- else if (col.duckdbType === 'BOOLEAN') {
661
- branchAppender.appendBoolean(!!rawValue);
662
- }
663
- else {
664
- branchAppender.appendVarchar(String(rawValue));
665
- }
849
+ values.push(resolveRawColumnValue(col, item));
666
850
  }
667
- branchAppender.endRow();
851
+ branchInserter.append(values);
668
852
  }
669
853
  catch {
670
- // Skip bad records
854
+ // Skip bad items
671
855
  }
672
856
  }
673
857
  }
674
- branchAppender.flushSync();
675
- branchAppender.closeSync();
676
- // Delete existing branch rows for these URIs, then merge staging
677
- const uriPlaceholders = recs.map((_, i) => `$${i + 1}`).join(',');
678
- const delStmt = await con.prepare(`DELETE FROM ${branch.tableName} WHERE parent_uri IN (${uriPlaceholders})`);
679
- bindParams(delStmt, recs.map((r) => r.uri));
680
- await delStmt.run();
681
- const branchSelectCols = branchAllCols.map((name) => {
682
- const col = branch.columns.find((c) => c.name === name);
683
- if (col && col.duckdbType === 'TIMESTAMP')
684
- return `TRY_CAST(${name} AS TIMESTAMP) AS ${name}`;
685
- return name;
686
- });
687
- await con.run(`INSERT INTO ${branch.tableName} (${branchAllCols.join(', ')}) SELECT ${branchSelectCols.join(', ')} FROM ${branchStagingTable} WHERE parent_uri IS NOT NULL`);
688
- await con.run(`DROP TABLE ${branchStagingTable}`);
858
+ else {
859
+ try {
860
+ const branchData = resolveBranchData(unionValue, branch);
861
+ const values = [rec.uri, rec.did];
862
+ for (const col of branch.columns) {
863
+ values.push(resolveRawColumnValue(col, branchData));
864
+ }
865
+ branchInserter.append(values);
866
+ }
867
+ catch {
868
+ // Skip bad records
869
+ }
870
+ }
689
871
  }
872
+ await branchInserter.close();
873
+ // Delete existing branch rows for these URIs, then merge staging
874
+ const uriPlaceholders = recs.map((_, i) => `$${i + 1}`).join(',');
875
+ await port.execute(`DELETE FROM ${branch.tableName} WHERE parent_uri IN (${uriPlaceholders})`, recs.map((r) => r.uri));
876
+ const branchSelectCols = branchAllCols.map((name) => {
877
+ const col = branch.columns.find((c) => q(c.name) === name);
878
+ if (col && (col.sqlType === 'TIMESTAMP' || col.sqlType === 'TIMESTAMPTZ')) {
879
+ return `${dialect.tryCastTimestamp(name)} AS ${name}`;
880
+ }
881
+ return name;
882
+ });
883
+ await port.execute(`INSERT INTO ${branch.tableName} (${branchAllCols.join(', ')}) SELECT ${branchSelectCols.join(', ')} FROM ${branchStagingTable} WHERE parent_uri IS NOT NULL`, []);
884
+ await port.execute(`DROP TABLE ${branchStagingTable}`, []);
690
885
  }
691
- });
886
+ }
692
887
  }
693
888
  return inserted;
694
889
  }
890
+ /** Extract a column value from a record, handling strongRef expansion and type coercion for bulk insert */
891
+ function resolveColumnValue(col, record) {
892
+ let rawValue = record[col.originalName];
893
+ if (rawValue && typeof rawValue === 'object' && col.name.endsWith('_uri') && col.isRef) {
894
+ rawValue = rawValue.uri;
895
+ }
896
+ else if (col.originalName.endsWith('__cid') && record[col.originalName.replace('__cid', '')]) {
897
+ rawValue = record[col.originalName.replace('__cid', '')].cid;
898
+ }
899
+ return coerceValue(col.sqlType, rawValue);
900
+ }
901
+ /** Extract a raw column value from a data object and coerce for bulk insert */
902
+ function resolveRawColumnValue(col, data) {
903
+ return coerceValue(col.sqlType, data[col.originalName]);
904
+ }
905
+ /** Coerce a value to the appropriate type for insertion */
906
+ function coerceValue(sqlType, rawValue) {
907
+ if (rawValue === undefined || rawValue === null)
908
+ return null;
909
+ // Objects and arrays always need JSON stringification regardless of sqlType
910
+ // (on SQLite, JSON columns map to TEXT but still need stringification)
911
+ if (typeof rawValue === 'object' && !(rawValue instanceof Uint8Array)) {
912
+ return JSON.stringify(rawValue);
913
+ }
914
+ if (sqlType === 'JSON' || sqlType === 'TEXT') {
915
+ return String(rawValue);
916
+ }
917
+ if (sqlType === 'INTEGER' || sqlType === 'BIGINT') {
918
+ return typeof rawValue === 'number' ? rawValue : parseInt(rawValue);
919
+ }
920
+ if (sqlType === 'BOOLEAN')
921
+ return !!rawValue;
922
+ return String(rawValue);
923
+ }
695
924
  export async function queryRecords(collection, opts = {}) {
696
925
  const schema = schemas.get(collection);
697
926
  if (!schema)
@@ -734,7 +963,7 @@ export async function queryRecords(collection, opts = {}) {
734
963
  sql += ' AND ' + conditions.join(' AND ');
735
964
  sql += ` ORDER BY t.${sortName} ${order.toUpperCase()}, t.cid ${order.toUpperCase()} LIMIT $${paramIdx++}`;
736
965
  params.push(limit + 1); // fetch one extra for cursor
737
- const rows = await all(sql, ...params);
966
+ const rows = await all(sql, params);
738
967
  const hasMore = rows.length > limit;
739
968
  if (hasMore)
740
969
  rows.pop();
@@ -774,7 +1003,7 @@ export async function queryRecords(collection, opts = {}) {
774
1003
  }
775
1004
  export async function getRecordByUri(uri) {
776
1005
  for (const [_collection, schema] of schemas) {
777
- const rows = await all(`SELECT t.*, r.handle FROM ${schema.tableName} t LEFT JOIN _repos r ON t.did = r.did WHERE t.uri = $1 AND (r.status IS NULL OR r.status != 'takendown')`, uri);
1006
+ const rows = await all(`SELECT t.*, r.handle FROM ${schema.tableName} t LEFT JOIN _repos r ON t.did = r.did WHERE t.uri = $1 AND (r.status IS NULL OR r.status != 'takendown')`, [uri]);
778
1007
  if (rows.length > 0) {
779
1008
  const row = rows[0];
780
1009
  if (schema.children.length > 0) {
@@ -811,7 +1040,7 @@ export async function getRecordsByUris(collection, uris) {
811
1040
  if (!schema)
812
1041
  return [];
813
1042
  const placeholders = uris.map((_, i) => `$${i + 1}`).join(',');
814
- const rows = await all(`SELECT t.*, r.handle FROM ${schema.tableName} t LEFT JOIN _repos r ON t.did = r.did WHERE t.uri IN (${placeholders}) AND (r.status IS NULL OR r.status != 'takendown')`, ...uris);
1043
+ const rows = await all(`SELECT t.*, r.handle FROM ${schema.tableName} t LEFT JOIN _repos r ON t.did = r.did WHERE t.uri IN (${placeholders}) AND (r.status IS NULL OR r.status != 'takendown')`, uris);
815
1044
  // Batch-fetch child rows for all URIs
816
1045
  const childData = new Map();
817
1046
  for (const child of schema.children) {
@@ -839,6 +1068,19 @@ export async function getRecordsByUris(collection, uris) {
839
1068
  const byUri = new Map(rows.map((r) => [r.uri, r]));
840
1069
  return uris.map((u) => byUri.get(u)).filter(Boolean);
841
1070
  }
1071
+ /** Fetch records by URIs and return as a shaped Map keyed by URI. */
1072
+ export async function getRecordsMap(collection, uris) {
1073
+ if (uris.length === 0)
1074
+ return new Map();
1075
+ const records = await getRecordsByUris(collection, uris);
1076
+ const map = new Map();
1077
+ for (const r of records) {
1078
+ const shaped = reshapeRow(r, r?.__childData, r?.__unionData);
1079
+ if (shaped)
1080
+ map.set(shaped.uri, shaped);
1081
+ }
1082
+ return map;
1083
+ }
842
1084
  /**
843
1085
  * Multi-phase search across any collection's records.
844
1086
  *
@@ -859,8 +1101,8 @@ export async function searchRecords(collection, query, opts = {}) {
859
1101
  if (!schema)
860
1102
  throw new Error(`Unknown collection: ${collection}`);
861
1103
  const elapsed = timer();
862
- const { limit = 20, cursor, fuzzy = true } = opts;
863
- const textCols = schema.columns.filter((c) => c.duckdbType === 'TEXT');
1104
+ const { limit = 20, fuzzy = true } = opts;
1105
+ const textCols = schema.columns.filter((c) => c.sqlType === 'TEXT');
864
1106
  // Also check if FTS has indexed any columns (including derived JSON columns)
865
1107
  const ftsSearchCols = getSearchColumns(collection);
866
1108
  if (textCols.length === 0 && ftsSearchCols.length === 0) {
@@ -868,149 +1110,67 @@ export async function searchRecords(collection, query, opts = {}) {
868
1110
  }
869
1111
  // FTS shadow table name (dots replaced with underscores)
870
1112
  const safeName = '_fts_' + collection.replace(/\./g, '_');
871
- const ftsSchema = `fts_main_${safeName}`;
872
1113
  const phaseErrors = [];
873
1114
  const phasesUsed = [];
874
- // Phase 1: BM25 ranked search on FTS shadow table
1115
+ // Phase 1: BM25 ranked search via SearchPort
875
1116
  let bm25Results = [];
876
- try {
877
- let paramIdx = 1;
878
- const ftsQuery = stripStopWords(query);
879
- const isMultiWord = ftsQuery.split(/\s+/).length > 1;
880
- const conjunctiveFlag = isMultiWord ? ', conjunctive := 1' : '';
881
- let sql = `SELECT m.*, ${ftsSchema}.match_bm25(s.uri, $${paramIdx++}${conjunctiveFlag}) AS score
882
- FROM ${safeName} s
883
- JOIN ${schema.tableName} m ON m.uri = s.uri
884
- LEFT JOIN _repos r ON m.did = r.did
885
- WHERE score IS NOT NULL
886
- AND (r.status IS NULL OR r.status != 'takendown')`;
887
- const params = [ftsQuery];
888
- if (cursor) {
889
- const parsed = unpackCursor(cursor);
890
- if (parsed) {
891
- const pScore1 = `$${paramIdx++}`;
892
- const pScore2 = `$${paramIdx++}`;
893
- const pCid = `$${paramIdx++}`;
894
- sql += ` AND (score > ${pScore1} OR (score = ${pScore2} AND m.cid < ${pCid}))`;
895
- params.push(parsed.primary, parsed.primary, parsed.cid);
1117
+ const sp = getSearchPort();
1118
+ if (sp)
1119
+ try {
1120
+ const ftsQuery = stripStopWords(query);
1121
+ const ftsSearchColNames = getSearchColumns(collection);
1122
+ // Get ranked URIs from the search port
1123
+ const hits = await sp.search(safeName, ftsQuery, ftsSearchColNames, limit + 1, 0);
1124
+ if (hits.length > 0) {
1125
+ const uriList = hits.map((h) => h.uri);
1126
+ const scoreMap = new Map(hits.map((h) => [h.uri, h.score]));
1127
+ // Fetch full records for matched URIs
1128
+ const placeholders = uriList.map((_, i) => `$${i + 1}`).join(', ');
1129
+ const rows = await all(`SELECT m.* FROM ${schema.tableName} m
1130
+ LEFT JOIN _repos r ON m.did = r.did
1131
+ WHERE m.uri IN (${placeholders})
1132
+ AND (r.status IS NULL OR r.status != 'takendown')`, uriList);
1133
+ // Re-attach scores and sort
1134
+ bm25Results = rows
1135
+ .map((r) => ({ ...r, score: scoreMap.get(r.uri) ?? 0 }))
1136
+ .sort((a, b) => b.score - a.score);
896
1137
  }
1138
+ phasesUsed.push('bm25');
1139
+ }
1140
+ catch (err) {
1141
+ phaseErrors.push(`bm25: ${err.message}`);
897
1142
  }
898
- sql += ` ORDER BY score, m.cid DESC LIMIT $${paramIdx++}`;
899
- params.push(limit + 1);
900
- bm25Results = await all(sql, ...params);
901
- phasesUsed.push('bm25');
902
- }
903
- catch (err) {
904
- phaseErrors.push(`bm25: ${err.message}`);
905
- }
906
1143
  const bm25Count = bm25Results.length;
907
1144
  const hasMore = bm25Results.length > limit;
908
1145
  if (hasMore)
909
1146
  bm25Results.pop();
910
- // Phase 2: Exact substring match — boosts phrase matches above BM25 results
911
- const exactMatchResults = [];
912
- const bm25Uris = new Set(bm25Results.map((r) => r.uri));
913
- try {
914
- const searchParam = `%${query}%`;
915
- let paramIdx = 1;
916
- const ilikeConds = [];
917
- const params = [];
918
- // TEXT columns — direct ILIKE
919
- for (const c of textCols) {
920
- ilikeConds.push(`t.${c.name} ILIKE $${paramIdx++}`);
921
- params.push(searchParam);
922
- }
923
- // JSON columns — cast to text then ILIKE
924
- const jsonCols = schema.columns.filter((c) => c.duckdbType === 'JSON');
925
- for (const c of jsonCols) {
926
- ilikeConds.push(`CAST(t.${c.name} AS TEXT) ILIKE $${paramIdx++}`);
927
- params.push(searchParam);
928
- }
929
- // Handle from _repos table
930
- ilikeConds.push(`r.handle ILIKE $${paramIdx++}`);
931
- params.push(searchParam);
932
- if (ilikeConds.length > 0) {
933
- const exactSQL = `SELECT t.* FROM ${schema.tableName} t LEFT JOIN _repos r ON t.did = r.did
934
- WHERE (${ilikeConds.join(' OR ')})
935
- ORDER BY t.indexed_at DESC
936
- LIMIT $${paramIdx++}`;
937
- params.push(limit);
938
- const rows = await all(exactSQL, ...params);
939
- phasesUsed.push('exact');
940
- for (const row of rows) {
941
- if (!bm25Uris.has(row.uri)) {
942
- exactMatchResults.push(row);
943
- bm25Uris.add(row.uri);
944
- }
945
- }
946
- }
947
- }
948
- catch (err) {
949
- phaseErrors.push(`exact: ${err.message}`);
950
- }
951
- // Merge: exact matches first, then BM25 results, capped at limit
952
- const mergedResults = [...exactMatchResults, ...bm25Results].slice(0, limit + (hasMore ? 1 : 0));
953
- // Replace bm25Results with merged for downstream phases
954
- bm25Results = mergedResults;
955
- // Phase 3: ILIKE scan of rows written since last FTS rebuild (immediate searchability)
956
1147
  const existingUris = new Set(bm25Results.map((r) => r.uri));
957
- const { getLastRebuiltAt } = await import("./fts.js");
958
- const rebuiltAt = getLastRebuiltAt(collection);
959
- let recentCount = 0;
960
- if (rebuiltAt && bm25Results.length < limit) {
961
- const remaining = limit - bm25Results.length;
962
- const searchParam = `%${query}%`;
963
- let paramIdx = 1;
964
- const ilikeParts = textCols.map((c) => `t.${c.name} ILIKE $${paramIdx++}`);
965
- ilikeParts.push(`r.handle ILIKE $${paramIdx++}`);
966
- const ilikeConds = ilikeParts.join(' OR ');
967
- const params = [...textCols.map(() => searchParam), searchParam];
968
- const recentSQL = `SELECT t.* FROM ${schema.tableName} t LEFT JOIN _repos r ON t.did = r.did
969
- WHERE t.indexed_at >= $${paramIdx++} AND t.uri NOT IN (SELECT uri FROM ${safeName}) AND (${ilikeConds})
970
- ORDER BY t.indexed_at DESC
971
- LIMIT $${paramIdx++}`;
972
- params.push(rebuiltAt, remaining + existingUris.size);
973
- try {
974
- const recentRows = await all(recentSQL, ...params);
975
- phasesUsed.push('recent');
976
- for (const row of recentRows) {
977
- if (bm25Results.length >= limit)
978
- break;
979
- if (!existingUris.has(row.uri)) {
980
- existingUris.add(row.uri);
981
- bm25Results.push(row);
982
- recentCount++;
983
- }
984
- }
985
- }
986
- catch (err) {
987
- phaseErrors.push(`recent: ${err.message}`);
988
- }
989
- }
990
- // Phase 4: Fuzzy fallback for typo tolerance (if still under limit)
1148
+ // Phase 2: Fuzzy fallback for typo tolerance (if still under limit)
1149
+ // Only available on dialects with jaro_winkler_similarity (DuckDB)
991
1150
  let fuzzyCount = 0;
992
- if (fuzzy && bm25Results.length < limit) {
1151
+ if (fuzzy && dialect.jaroWinklerSimilarity && bm25Results.length < limit) {
993
1152
  const remaining = limit - bm25Results.length;
1153
+ const jwFn = dialect.jaroWinklerSimilarity;
994
1154
  const simExprs = [
995
- ...textCols.map((c) => `jaro_winkler_similarity(lower(t.${c.name}), lower($1))`),
996
- `jaro_winkler_similarity(lower(r.handle), lower($1))`,
1155
+ ...textCols.map((c) => `${jwFn}(lower(t.${q(c.name)}), lower($1))`),
1156
+ `${jwFn}(lower(r.handle), lower($1))`,
997
1157
  ];
998
1158
  // Include child table TEXT columns via correlated subquery
999
1159
  for (const child of schema.children) {
1000
1160
  for (const col of child.columns) {
1001
- if (col.duckdbType === 'TEXT') {
1002
- simExprs.push(`COALESCE((SELECT MAX(jaro_winkler_similarity(lower(c.${col.name}), lower($1))) FROM ${child.tableName} c WHERE c.parent_uri = t.uri), 0)`);
1161
+ if (col.sqlType === 'TEXT') {
1162
+ simExprs.push(`COALESCE((SELECT MAX(${jwFn}(lower(c.${q(col.name)}), lower($1))) FROM ${child.tableName} c WHERE c.parent_uri = t.uri), 0)`);
1003
1163
  }
1004
1164
  }
1005
1165
  }
1006
- const greatestExpr = `GREATEST(${simExprs.join(', ')})`;
1166
+ const greatestExpr = dialect.greatest(simExprs);
1007
1167
  const fuzzySQL = `SELECT t.*, ${greatestExpr} AS fuzzy_score
1008
1168
  FROM ${schema.tableName} t LEFT JOIN _repos r ON t.did = r.did
1009
1169
  WHERE ${greatestExpr} >= 0.8
1010
1170
  ORDER BY fuzzy_score DESC
1011
1171
  LIMIT $2`;
1012
1172
  try {
1013
- const fuzzyRows = await all(fuzzySQL, query, remaining + existingUris.size);
1173
+ const fuzzyRows = await all(fuzzySQL, [query, remaining + existingUris.size]);
1014
1174
  phasesUsed.push('fuzzy');
1015
1175
  for (const row of fuzzyRows) {
1016
1176
  if (bm25Results.length >= limit)
@@ -1025,16 +1185,17 @@ export async function searchRecords(collection, query, opts = {}) {
1025
1185
  phaseErrors.push(`fuzzy: ${err.message}`);
1026
1186
  }
1027
1187
  }
1028
- // Remove score columns from results
1029
- const records = bm25Results.map(({ score: _score, fuzzy_score: _fuzzy_score, ...rest }) => rest);
1188
+ // Remove score columns and reshape into Row<T> with value
1189
+ const rawRecords = bm25Results.map(({ score: _score, fuzzy_score: _fuzzy_score, ...rest }) => rest);
1190
+ const records = rawRecords
1191
+ .map((r) => reshapeRow(r, r?.__childData, r?.__unionData))
1192
+ .filter((r) => r != null);
1030
1193
  const lastRow = bm25Results[bm25Results.length - 1];
1031
1194
  const nextCursor = hasMore && lastRow?.score != null ? packCursor(lastRow.score, lastRow.cid) : undefined;
1032
1195
  emit('search', 'query', {
1033
1196
  collection,
1034
1197
  query,
1035
1198
  bm25_count: bm25Count > limit ? bm25Count - 1 : bm25Count,
1036
- exact_count: exactMatchResults.length,
1037
- recent_count: recentCount,
1038
1199
  fuzzy_count: fuzzyCount,
1039
1200
  total_results: records.length,
1040
1201
  duration_ms: elapsed(),
@@ -1045,10 +1206,13 @@ export async function searchRecords(collection, query, opts = {}) {
1045
1206
  }
1046
1207
  // Raw SQL for script feeds
1047
1208
  export async function querySQL(sql, params = []) {
1048
- return all(sql, ...params);
1209
+ return all(sql, params);
1049
1210
  }
1050
- export async function runSQL(sql, ...params) {
1051
- return run(sql, ...params);
1211
+ export async function runSQL(sql, params = []) {
1212
+ return run(sql, params);
1213
+ }
1214
+ export async function createBulkInserterSQL(table, columns, options) {
1215
+ return port.createBulkInserter(table, columns, options);
1052
1216
  }
1053
1217
  export function getSchema(collection) {
1054
1218
  return schemas.get(collection);
@@ -1057,7 +1221,7 @@ export async function countByField(collection, field, value) {
1057
1221
  const schema = schemas.get(collection);
1058
1222
  if (!schema)
1059
1223
  return 0;
1060
- const rows = await all(`SELECT COUNT(*) as count FROM ${schema.tableName} WHERE ${field} = $1`, value);
1224
+ const rows = await all(`SELECT COUNT(*) as count FROM ${schema.tableName} WHERE ${field} = $1`, [value]);
1061
1225
  return Number(rows[0]?.count || 0);
1062
1226
  }
1063
1227
  export async function countByFieldBatch(collection, field, values) {
@@ -1067,7 +1231,7 @@ export async function countByFieldBatch(collection, field, values) {
1067
1231
  if (!schema)
1068
1232
  return new Map();
1069
1233
  const placeholders = values.map((_, i) => `$${i + 1}`).join(',');
1070
- const rows = await all(`SELECT ${field}, COUNT(*) as count FROM ${schema.tableName} WHERE ${field} IN (${placeholders}) GROUP BY ${field}`, ...values);
1234
+ const rows = await all(`SELECT ${field}, COUNT(*) as count FROM ${schema.tableName} WHERE ${field} IN (${placeholders}) GROUP BY ${field}`, values);
1071
1235
  const result = new Map();
1072
1236
  for (const row of rows) {
1073
1237
  result.set(row[field], Number(row.count));
@@ -1078,7 +1242,7 @@ export async function findByField(collection, field, value) {
1078
1242
  const schema = schemas.get(collection);
1079
1243
  if (!schema)
1080
1244
  return null;
1081
- const rows = await all(`SELECT * FROM ${schema.tableName} WHERE ${field} = $1 LIMIT 1`, value);
1245
+ const rows = await all(`SELECT * FROM ${schema.tableName} WHERE ${field} = $1 LIMIT 1`, [value]);
1082
1246
  return rows[0] || null;
1083
1247
  }
1084
1248
  export async function findByFieldBatch(collection, field, values) {
@@ -1088,7 +1252,7 @@ export async function findByFieldBatch(collection, field, values) {
1088
1252
  if (!schema)
1089
1253
  return new Map();
1090
1254
  const placeholders = values.map((_, i) => `$${i + 1}`).join(',');
1091
- const rows = await all(`SELECT t.*, r.handle FROM ${schema.tableName} t LEFT JOIN _repos r ON t.did = r.did WHERE t.${field} IN (${placeholders})`, ...values);
1255
+ const rows = await all(`SELECT t.*, r.handle FROM ${schema.tableName} t LEFT JOIN _repos r ON t.did = r.did WHERE t.${field} IN (${placeholders})`, values);
1092
1256
  // Attach child data if this collection has decomposed arrays
1093
1257
  if (schema.children.length > 0 && rows.length > 0) {
1094
1258
  const uris = rows.map((r) => r.uri);
@@ -1098,7 +1262,6 @@ export async function findByFieldBatch(collection, field, values) {
1098
1262
  childData.set(child.fieldName, childRows);
1099
1263
  }
1100
1264
  for (const row of rows) {
1101
- ;
1102
1265
  row.__childData = childData;
1103
1266
  }
1104
1267
  }
@@ -1129,7 +1292,7 @@ export async function findUriByFields(collection, conditions) {
1129
1292
  return null;
1130
1293
  const where = conditions.map((c, i) => `${c.field} = $${i + 1}`).join(' AND ');
1131
1294
  const params = conditions.map((c) => c.value);
1132
- const rows = await all(`SELECT uri FROM ${schema.tableName} WHERE ${where} LIMIT 1`, ...params);
1295
+ const rows = await all(`SELECT uri FROM ${schema.tableName} WHERE ${where} LIMIT 1`, params);
1133
1296
  return rows[0]?.uri || null;
1134
1297
  }
1135
1298
  const ENVELOPE_KEYS = new Set(['uri', 'cid', 'did', 'handle', 'indexed_at']);
@@ -1145,7 +1308,7 @@ export async function getChildRows(childTableName, parentUris) {
1145
1308
  if (parentUris.length === 0)
1146
1309
  return new Map();
1147
1310
  const placeholders = parentUris.map((_, i) => `$${i + 1}`).join(',');
1148
- const rows = await all(`SELECT * FROM ${childTableName} WHERE parent_uri IN (${placeholders})`, ...parentUris);
1311
+ const rows = await all(`SELECT * FROM ${childTableName} WHERE parent_uri IN (${placeholders})`, parentUris);
1149
1312
  const result = new Map();
1150
1313
  for (const row of rows) {
1151
1314
  const key = row.parent_uri;
@@ -1167,7 +1330,7 @@ export function reshapeRow(row, childData, unionData) {
1167
1330
  if (schema) {
1168
1331
  for (const col of schema.columns) {
1169
1332
  nameMap.set(col.name, col.originalName);
1170
- if (col.duckdbType === 'JSON')
1333
+ if (col.isJson)
1171
1334
  jsonCols.add(col.name);
1172
1335
  }
1173
1336
  }
@@ -1273,15 +1436,17 @@ export function unpackCursor(cursor) {
1273
1436
  }
1274
1437
  }
1275
1438
  export async function queryLabelsByDid(did) {
1276
- return all(`SELECT * FROM _labels WHERE uri LIKE $1 AND neg = false AND (exp IS NULL OR exp > CURRENT_TIMESTAMP)`, `at://${did}/%`);
1439
+ return all(`SELECT * FROM _labels WHERE uri LIKE $1 AND neg = false AND (exp IS NULL OR exp > CURRENT_TIMESTAMP)`, [
1440
+ `at://${did}/%`,
1441
+ ]);
1277
1442
  }
1278
1443
  export async function searchAccounts(query, limit = 20) {
1279
- return all(`SELECT did, handle, status FROM _repos WHERE did ILIKE $1 OR handle ILIKE $1 ORDER BY handle LIMIT $2`, `%${query}%`, limit);
1444
+ return all(`SELECT did, handle, status FROM _repos WHERE did ${dialect.ilike} $1 OR handle ${dialect.ilike} $1 ORDER BY handle LIMIT $2`, [`%${query}%`, limit]);
1280
1445
  }
1281
1446
  export async function getAccountRecordCount(did) {
1282
1447
  let total = 0;
1283
1448
  for (const [, schema] of schemas) {
1284
- const rows = await all(`SELECT COUNT(*) as count FROM ${schema.tableName} WHERE did = $1`, did);
1449
+ const rows = await all(`SELECT COUNT(*) as count FROM ${schema.tableName} WHERE did = $1`, [did]);
1285
1450
  total += Number(rows[0]?.count || 0);
1286
1451
  }
1287
1452
  return total;
@@ -1289,17 +1454,17 @@ export async function getAccountRecordCount(did) {
1289
1454
  export async function getAllRecordUrisForDid(did) {
1290
1455
  const uris = [];
1291
1456
  for (const [, schema] of schemas) {
1292
- const rows = await all(`SELECT uri FROM ${schema.tableName} WHERE did = $1`, did);
1457
+ const rows = await all(`SELECT uri FROM ${schema.tableName} WHERE did = $1`, [did]);
1293
1458
  uris.push(...rows.map((r) => r.uri));
1294
1459
  }
1295
1460
  return uris;
1296
1461
  }
1297
1462
  export async function isTakendownDid(did) {
1298
- const rows = await all(`SELECT 1 FROM _repos WHERE did = $1 AND status = 'takendown' LIMIT 1`, did);
1463
+ const rows = await all(`SELECT 1 FROM _repos WHERE did = $1 AND status = 'takendown' LIMIT 1`, [did]);
1299
1464
  return rows.length > 0;
1300
1465
  }
1301
1466
  export async function getPreferences(did) {
1302
- const rows = await all(`SELECT key, value FROM _preferences WHERE did = $1`, did);
1467
+ const rows = await all(`SELECT key, value FROM _preferences WHERE did = $1`, [did]);
1303
1468
  const prefs = {};
1304
1469
  for (const row of rows) {
1305
1470
  try {
@@ -1312,50 +1477,66 @@ export async function getPreferences(did) {
1312
1477
  return prefs;
1313
1478
  }
1314
1479
  export async function putPreference(did, key, value) {
1315
- await run(`INSERT OR REPLACE INTO _preferences (did, key, value, updated_at) VALUES ($1, $2, $3, $4)`, did, key, JSON.stringify(value), new Date().toISOString());
1480
+ await run(`INSERT OR REPLACE INTO _preferences (did, key, value, updated_at) VALUES ($1, $2, $3, $4)`, [
1481
+ did,
1482
+ key,
1483
+ JSON.stringify(value),
1484
+ new Date().toISOString(),
1485
+ ]);
1486
+ }
1487
+ export async function resolveHandleToDid(handle) {
1488
+ if (handle.startsWith('did:'))
1489
+ return handle;
1490
+ const rows = await all(`SELECT did FROM _repos WHERE handle = $1 LIMIT 1`, [handle]);
1491
+ return rows[0]?.did ?? null;
1316
1492
  }
1317
1493
  export async function filterTakendownDids(dids) {
1318
1494
  if (dids.length === 0)
1319
1495
  return new Set();
1320
1496
  const placeholders = dids.map((_, i) => `$${i + 1}`).join(',');
1321
- const rows = await all(`SELECT did FROM _repos WHERE did IN (${placeholders}) AND status = 'takendown'`, ...dids);
1497
+ const rows = await all(`SELECT did FROM _repos WHERE did IN (${placeholders}) AND status = 'takendown'`, dids);
1322
1498
  return new Set(rows.map((r) => r.did));
1323
1499
  }
1324
- export async function backfillChildTables() {
1325
- for (const [, schema] of schemas) {
1326
- for (const child of schema.children) {
1327
- // Check if child table needs backfill (significantly fewer rows than parent)
1328
- const mainCount = (await all(`SELECT COUNT(*)::INTEGER as n FROM ${schema.tableName}`))[0]?.n || 0;
1329
- if (mainCount === 0)
1330
- continue;
1331
- const childCount = (await all(`SELECT COUNT(DISTINCT parent_uri)::INTEGER as n FROM ${child.tableName}`))[0]?.n || 0;
1332
- if (childCount >= mainCount * 0.9)
1333
- continue;
1334
- console.log(`[db] Backfilling ${child.tableName} from ${schema.tableName}...`);
1335
- const snakeField = toSnakeCase(child.fieldName);
1336
- const childColSelects = child.columns
1337
- .map((c) => `json_extract_string(item.val, '$.${c.originalName}')`)
1338
- .join(', ');
1339
- const childColNames = ['parent_uri', 'parent_did', ...child.columns.map((c) => c.name)];
1340
- const notNullFilters = child.columns
1341
- .filter((c) => c.notNull)
1342
- .map((c) => `json_extract_string(item.val, '$.${c.originalName}') IS NOT NULL`);
1343
- const whereClause = [`p.${snakeField} IS NOT NULL`, ...notNullFilters].join(' AND ');
1344
- try {
1345
- await run(`DELETE FROM ${child.tableName}`);
1346
- await run(`
1347
- INSERT INTO ${child.tableName} (${childColNames.join(', ')})
1348
- SELECT p.uri, p.did, ${childColSelects}
1349
- FROM ${schema.tableName} p,
1350
- unnest(from_json(p.${snakeField}::JSON, '["json"]')) AS item(val)
1351
- WHERE ${whereClause}
1352
- `);
1353
- const result = await all(`SELECT COUNT(*)::INTEGER as n FROM ${child.tableName}`);
1354
- console.log(`[db] Backfilled ${child.tableName}: ${result[0]?.n || 0} rows`);
1355
- }
1356
- catch (err) {
1357
- console.warn(`[db] Backfill skipped for ${child.tableName}: ${err.message}`);
1358
- }
1359
- }
1500
+ export async function insertReport(report) {
1501
+ const createdAt = new Date().toISOString();
1502
+ const rows = await all(`INSERT INTO _reports (subject_uri, subject_did, label, reason, reported_by, created_at) VALUES ($1, $2, $3, $4, $5, $6) RETURNING id`, [report.subjectUri, report.subjectDid, report.label, report.reason || null, report.reportedBy, createdAt]);
1503
+ return { id: rows[0].id };
1504
+ }
1505
+ export async function queryReports(opts) {
1506
+ const conditions = [];
1507
+ const params = [];
1508
+ let idx = 1;
1509
+ if (opts.status) {
1510
+ conditions.push(`r.status = $${idx++}`);
1511
+ params.push(opts.status);
1360
1512
  }
1513
+ if (opts.label) {
1514
+ conditions.push(`r.label = $${idx++}`);
1515
+ params.push(opts.label);
1516
+ }
1517
+ const where = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
1518
+ const limit = opts.limit || 50;
1519
+ const offset = opts.offset || 0;
1520
+ const countRows = await all(`SELECT ${dialect.countAsInteger} as count FROM _reports r ${where}`, params);
1521
+ const total = Number(countRows[0]?.count || 0);
1522
+ const rows = await all(`SELECT r.*, rp.handle as reported_by_handle FROM _reports r LEFT JOIN _repos rp ON r.reported_by = rp.did ${where} ORDER BY r.created_at DESC LIMIT $${idx++} OFFSET $${idx++}`, [...params, limit, offset]);
1523
+ return { reports: rows, total };
1524
+ }
1525
+ export async function resolveReport(id, action, resolvedBy) {
1526
+ const rows = await all(`SELECT subject_uri, label, status FROM _reports WHERE id = $1`, [id]);
1527
+ if (!rows.length)
1528
+ return null;
1529
+ if (rows[0].status !== 'open')
1530
+ return null;
1531
+ await run(`UPDATE _reports SET status = $1, resolved_by = $2, resolved_at = $3 WHERE id = $4`, [
1532
+ action,
1533
+ resolvedBy,
1534
+ new Date().toISOString(),
1535
+ id,
1536
+ ]);
1537
+ return { subjectUri: rows[0].subject_uri, label: rows[0].label };
1538
+ }
1539
+ export async function getOpenReportCount() {
1540
+ const rows = await all(`SELECT ${dialect.countAsInteger} as count FROM _reports WHERE status = 'open'`);
1541
+ return Number(rows[0]?.count || 0);
1361
1542
  }