@hatk/hatk 0.0.1-alpha.5 → 0.0.1-alpha.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapter.d.ts +19 -0
- package/dist/adapter.d.ts.map +1 -0
- package/dist/adapter.js +107 -0
- package/dist/backfill.d.ts +60 -1
- package/dist/backfill.d.ts.map +1 -1
- package/dist/backfill.js +167 -33
- package/dist/car.d.ts +59 -1
- package/dist/car.d.ts.map +1 -1
- package/dist/car.js +179 -7
- package/dist/cbor.d.ts +37 -0
- package/dist/cbor.d.ts.map +1 -1
- package/dist/cbor.js +36 -3
- package/dist/cid.d.ts +37 -0
- package/dist/cid.d.ts.map +1 -1
- package/dist/cid.js +38 -3
- package/dist/cli.js +243 -996
- package/dist/config.d.ts +24 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +37 -9
- package/dist/database/adapter-factory.d.ts +6 -0
- package/dist/database/adapter-factory.d.ts.map +1 -0
- package/dist/database/adapter-factory.js +20 -0
- package/dist/database/adapters/duckdb-search.d.ts +12 -0
- package/dist/database/adapters/duckdb-search.d.ts.map +1 -0
- package/dist/database/adapters/duckdb-search.js +27 -0
- package/dist/database/adapters/duckdb.d.ts +25 -0
- package/dist/database/adapters/duckdb.d.ts.map +1 -0
- package/dist/database/adapters/duckdb.js +161 -0
- package/dist/database/adapters/sqlite-search.d.ts +23 -0
- package/dist/database/adapters/sqlite-search.d.ts.map +1 -0
- package/dist/database/adapters/sqlite-search.js +74 -0
- package/dist/database/adapters/sqlite.d.ts +18 -0
- package/dist/database/adapters/sqlite.d.ts.map +1 -0
- package/dist/database/adapters/sqlite.js +88 -0
- package/dist/{db.d.ts → database/db.d.ts} +56 -6
- package/dist/database/db.d.ts.map +1 -0
- package/dist/{db.js → database/db.js} +727 -549
- package/dist/database/dialect.d.ts +45 -0
- package/dist/database/dialect.d.ts.map +1 -0
- package/dist/database/dialect.js +72 -0
- package/dist/{fts.d.ts → database/fts.d.ts} +7 -0
- package/dist/database/fts.d.ts.map +1 -0
- package/dist/{fts.js → database/fts.js} +116 -32
- package/dist/database/index.d.ts +7 -0
- package/dist/database/index.d.ts.map +1 -0
- package/dist/database/index.js +6 -0
- package/dist/database/ports.d.ts +50 -0
- package/dist/database/ports.d.ts.map +1 -0
- package/dist/database/ports.js +1 -0
- package/dist/{schema.d.ts → database/schema.d.ts} +14 -3
- package/dist/database/schema.d.ts.map +1 -0
- package/dist/{schema.js → database/schema.js} +81 -41
- package/dist/dev-entry.d.ts +8 -0
- package/dist/dev-entry.d.ts.map +1 -0
- package/dist/dev-entry.js +111 -0
- package/dist/feeds.d.ts +12 -8
- package/dist/feeds.d.ts.map +1 -1
- package/dist/feeds.js +45 -6
- package/dist/hooks.d.ts +85 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +161 -0
- package/dist/hydrate.d.ts +6 -5
- package/dist/hydrate.d.ts.map +1 -1
- package/dist/hydrate.js +4 -16
- package/dist/indexer.d.ts +22 -0
- package/dist/indexer.d.ts.map +1 -1
- package/dist/indexer.js +96 -8
- package/dist/labels.d.ts +36 -0
- package/dist/labels.d.ts.map +1 -1
- package/dist/labels.js +71 -6
- package/dist/lexicon-resolve.d.ts.map +1 -1
- package/dist/lexicon-resolve.js +27 -112
- package/dist/lexicons/com/atproto/label/defs.json +75 -0
- package/dist/lexicons/com/atproto/moderation/defs.json +30 -0
- package/dist/lexicons/com/atproto/repo/strongRef.json +24 -0
- package/dist/lexicons/dev/hatk/createRecord.json +40 -0
- package/dist/lexicons/dev/hatk/createReport.json +48 -0
- package/dist/lexicons/dev/hatk/deleteRecord.json +25 -0
- package/dist/lexicons/dev/hatk/describeCollections.json +41 -0
- package/dist/lexicons/dev/hatk/describeFeeds.json +29 -0
- package/dist/lexicons/dev/hatk/describeLabels.json +45 -0
- package/dist/lexicons/dev/hatk/getFeed.json +30 -0
- package/dist/lexicons/dev/hatk/getPreferences.json +19 -0
- package/dist/lexicons/dev/hatk/getRecord.json +26 -0
- package/dist/lexicons/dev/hatk/getRecords.json +32 -0
- package/dist/lexicons/dev/hatk/putPreference.json +28 -0
- package/dist/lexicons/dev/hatk/putRecord.json +41 -0
- package/dist/lexicons/dev/hatk/searchRecords.json +32 -0
- package/dist/lexicons/dev/hatk/uploadBlob.json +23 -0
- package/dist/logger.d.ts +29 -0
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +29 -0
- package/dist/main.js +136 -67
- package/dist/mst.d.ts +18 -1
- package/dist/mst.d.ts.map +1 -1
- package/dist/mst.js +19 -8
- package/dist/oauth/db.d.ts +3 -1
- package/dist/oauth/db.d.ts.map +1 -1
- package/dist/oauth/db.js +48 -19
- package/dist/oauth/server.d.ts +24 -0
- package/dist/oauth/server.d.ts.map +1 -1
- package/dist/oauth/server.js +198 -22
- package/dist/oauth/session.d.ts +11 -0
- package/dist/oauth/session.d.ts.map +1 -0
- package/dist/oauth/session.js +65 -0
- package/dist/opengraph.d.ts +10 -0
- package/dist/opengraph.d.ts.map +1 -1
- package/dist/opengraph.js +73 -39
- package/dist/pds-proxy.d.ts +42 -0
- package/dist/pds-proxy.d.ts.map +1 -0
- package/dist/pds-proxy.js +207 -0
- package/dist/push.d.ts +33 -0
- package/dist/push.d.ts.map +1 -0
- package/dist/push.js +166 -0
- package/dist/renderer.d.ts +27 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +46 -0
- package/dist/resolve-hatk.d.ts +6 -0
- package/dist/resolve-hatk.d.ts.map +1 -0
- package/dist/resolve-hatk.js +20 -0
- package/dist/response.d.ts +16 -0
- package/dist/response.d.ts.map +1 -0
- package/dist/response.js +69 -0
- package/dist/scanner.d.ts +21 -0
- package/dist/scanner.d.ts.map +1 -0
- package/dist/scanner.js +88 -0
- package/dist/seed.d.ts +19 -0
- package/dist/seed.d.ts.map +1 -1
- package/dist/seed.js +43 -4
- package/dist/server-init.d.ts +8 -0
- package/dist/server-init.d.ts.map +1 -0
- package/dist/server-init.js +62 -0
- package/dist/server.d.ts +26 -3
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +624 -635
- package/dist/setup.d.ts +28 -1
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +50 -3
- package/dist/templates/feed.tpl +14 -0
- package/dist/templates/hook.tpl +5 -0
- package/dist/templates/label.tpl +15 -0
- package/dist/templates/og.tpl +17 -0
- package/dist/templates/seed.tpl +11 -0
- package/dist/templates/setup.tpl +5 -0
- package/dist/templates/test-feed.tpl +19 -0
- package/dist/templates/test-xrpc.tpl +19 -0
- package/dist/templates/xrpc.tpl +41 -0
- package/dist/test.d.ts +1 -1
- package/dist/test.d.ts.map +1 -1
- package/dist/test.js +38 -32
- package/dist/views.js +1 -1
- package/dist/vite-plugin.d.ts +1 -1
- package/dist/vite-plugin.d.ts.map +1 -1
- package/dist/vite-plugin.js +254 -66
- package/dist/xrpc.d.ts +60 -10
- package/dist/xrpc.d.ts.map +1 -1
- package/dist/xrpc.js +155 -39
- package/package.json +15 -7
- package/public/admin.html +133 -54
- package/dist/db.d.ts.map +0 -1
- package/dist/fts.d.ts.map +0 -1
- package/dist/oauth/hooks.d.ts +0 -10
- package/dist/oauth/hooks.d.ts.map +0 -1
- package/dist/oauth/hooks.js +0 -40
- package/dist/schema.d.ts.map +0 -1
- package/dist/test-browser.d.ts +0 -14
- package/dist/test-browser.d.ts.map +0 -1
- package/dist/test-browser.js +0 -26
|
@@ -1,136 +1,59 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
let
|
|
7
|
-
let
|
|
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
|
|
11
|
-
|
|
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
|
|
39
|
-
|
|
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
|
-
|
|
71
|
-
|
|
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,
|
|
80
|
-
return
|
|
18
|
+
async function run(sql, params = []) {
|
|
19
|
+
return port.execute(sql, params);
|
|
81
20
|
}
|
|
82
21
|
export async function runBatch(operations) {
|
|
83
|
-
|
|
84
|
-
|
|
22
|
+
await port.beginTransaction();
|
|
23
|
+
try {
|
|
85
24
|
for (const op of operations) {
|
|
86
25
|
try {
|
|
87
|
-
|
|
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
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
|
|
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,
|
|
114
|
-
return
|
|
38
|
+
async function all(sql, params = []) {
|
|
39
|
+
return port.query(sql, params);
|
|
115
40
|
}
|
|
116
|
-
export async function initDatabase(dbPath, tableSchemas, ddlStatements) {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
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
|
|
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,340 @@ export async function initDatabase(dbPath, tableSchemas, ddlStatements) {
|
|
|
147
70
|
value TEXT NOT NULL
|
|
148
71
|
)`);
|
|
149
72
|
// Labels table (atproto-compatible)
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
|
167
|
-
updated_at
|
|
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
|
-
|
|
172
|
-
|
|
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
|
+
for (const schema of tableSchemas) {
|
|
218
|
+
if (schema.columns.length === 0)
|
|
219
|
+
continue; // generic JSON storage, skip
|
|
220
|
+
const tableName = schema.collection;
|
|
221
|
+
const existingCols = await getExistingColumns(tableName);
|
|
222
|
+
if (existingCols.size === 0)
|
|
223
|
+
continue; // table just created, nothing to migrate
|
|
224
|
+
// Expected columns: base columns (uri, cid, did, indexed_at) + schema columns
|
|
225
|
+
const expectedCols = new Map();
|
|
226
|
+
expectedCols.set('uri', 'TEXT');
|
|
227
|
+
expectedCols.set('cid', 'TEXT');
|
|
228
|
+
expectedCols.set('did', 'TEXT');
|
|
229
|
+
expectedCols.set('indexed_at', normalizeType(dialect.timestampType));
|
|
230
|
+
for (const col of schema.columns) {
|
|
231
|
+
expectedCols.set(col.name, normalizeType(col.sqlType));
|
|
232
|
+
}
|
|
233
|
+
diffColumns(tableName, existingCols, expectedCols, changes);
|
|
234
|
+
// Diff child tables
|
|
235
|
+
for (const child of schema.children) {
|
|
236
|
+
const childTable = child.tableName.replace(/"/g, '');
|
|
237
|
+
const existingChildCols = await getExistingColumns(childTable);
|
|
238
|
+
if (existingChildCols.size === 0)
|
|
239
|
+
continue;
|
|
240
|
+
diffColumns(childTable, existingChildCols, buildChildExpectedCols(child.columns), changes);
|
|
241
|
+
}
|
|
242
|
+
// Diff union branch tables
|
|
243
|
+
for (const union of schema.unions) {
|
|
244
|
+
for (const branch of union.branches) {
|
|
245
|
+
const branchTable = branch.tableName.replace(/"/g, '');
|
|
246
|
+
const existingBranchCols = await getExistingColumns(branchTable);
|
|
247
|
+
if (existingBranchCols.size === 0)
|
|
248
|
+
continue;
|
|
249
|
+
diffColumns(branchTable, existingBranchCols, buildChildExpectedCols(branch.columns), changes);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
// Detect and drop orphaned child/union tables (query table list once)
|
|
254
|
+
let allTableNames = null;
|
|
255
|
+
try {
|
|
256
|
+
const rows = await all(dialect.listTablesQuery);
|
|
257
|
+
allTableNames = rows.map((r) => r.table_name);
|
|
258
|
+
}
|
|
259
|
+
catch { }
|
|
260
|
+
if (allTableNames) {
|
|
261
|
+
for (const schema of tableSchemas) {
|
|
262
|
+
if (schema.columns.length === 0)
|
|
263
|
+
continue;
|
|
264
|
+
const expectedTables = new Set();
|
|
265
|
+
for (const child of schema.children) {
|
|
266
|
+
expectedTables.add(child.tableName.replace(/"/g, ''));
|
|
267
|
+
}
|
|
268
|
+
for (const union of schema.unions) {
|
|
269
|
+
for (const branch of union.branches) {
|
|
270
|
+
expectedTables.add(branch.tableName.replace(/"/g, ''));
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
for (const name of allTableNames) {
|
|
274
|
+
if (name.startsWith(schema.collection + '__') && !expectedTables.has(name)) {
|
|
275
|
+
await run(`DROP TABLE IF EXISTS "${name}"`);
|
|
276
|
+
emit('migration', 'drop_table', { table: name });
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (changes.length > 0) {
|
|
282
|
+
await applyMigrationChanges(changes);
|
|
283
|
+
}
|
|
284
|
+
// Check for empty collection tables — these are newly added and need backfill
|
|
285
|
+
// Skip on fresh DB (no repos yet) since backfill runs naturally
|
|
286
|
+
const [hasRepos] = await all(`SELECT 1 FROM _repos LIMIT 1`);
|
|
287
|
+
if (hasRepos) {
|
|
288
|
+
for (const schema of tableSchemas) {
|
|
289
|
+
if (schema.columns.length === 0)
|
|
290
|
+
continue;
|
|
291
|
+
try {
|
|
292
|
+
const [row] = await all(`SELECT 1 FROM ${schema.tableName} LIMIT 1`);
|
|
293
|
+
if (!row) {
|
|
294
|
+
await run(`UPDATE _repos SET status = 'pending' WHERE status = 'active'`);
|
|
295
|
+
emit('migration', 'new_collection', { collection: schema.collection });
|
|
296
|
+
break; // only need to mark once
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
catch { }
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
return changes;
|
|
303
|
+
}
|
|
304
|
+
async function applyMigrationChanges(changes) {
|
|
305
|
+
for (const change of changes) {
|
|
306
|
+
const quotedTable = `"${change.table}"`;
|
|
307
|
+
const quotedColumn = `"${change.column}"`;
|
|
308
|
+
try {
|
|
309
|
+
switch (change.action) {
|
|
310
|
+
case 'add': {
|
|
311
|
+
await run(`ALTER TABLE ${quotedTable} ADD COLUMN ${quotedColumn} ${change.type}`);
|
|
312
|
+
emit('migration', 'add_column', { table: change.table, column: change.column, type: change.type });
|
|
313
|
+
const schema = schemas.get(change.table);
|
|
314
|
+
if (schema?.refColumns.includes(change.column)) {
|
|
315
|
+
const prefix = change.table.replace(/\./g, '_');
|
|
316
|
+
await run(`CREATE INDEX IF NOT EXISTS idx_${prefix}_${change.column} ON ${quotedTable}(${quotedColumn})`);
|
|
317
|
+
}
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
case 'drop':
|
|
321
|
+
await run(`ALTER TABLE ${quotedTable} DROP COLUMN ${quotedColumn}`);
|
|
322
|
+
emit('migration', 'drop_column', { table: change.table, column: change.column });
|
|
323
|
+
break;
|
|
324
|
+
case 'retype':
|
|
325
|
+
await run(`ALTER TABLE ${quotedTable} DROP COLUMN ${quotedColumn}`);
|
|
326
|
+
await run(`ALTER TABLE ${quotedTable} ADD COLUMN ${quotedColumn} ${change.type}`);
|
|
327
|
+
emit('migration', 'retype_column', { table: change.table, column: change.column, type: change.type });
|
|
328
|
+
break;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
catch (err) {
|
|
332
|
+
console.warn(`[migration] failed to ${change.action} column "${change.column}" on "${change.table}": ${err.message}`);
|
|
333
|
+
emit('migration', 'error', {
|
|
334
|
+
action: change.action,
|
|
335
|
+
table: change.table,
|
|
336
|
+
column: change.column,
|
|
337
|
+
error: err.message,
|
|
338
|
+
});
|
|
339
|
+
}
|
|
173
340
|
}
|
|
174
341
|
}
|
|
175
342
|
export async function getCursor(key) {
|
|
176
|
-
const rows = await all(`SELECT value FROM _cursor WHERE key = $1`, key);
|
|
343
|
+
const rows = await all(`SELECT value FROM _cursor WHERE key = $1`, [key]);
|
|
177
344
|
return rows[0]?.value || null;
|
|
178
345
|
}
|
|
179
346
|
export async function setCursor(key, value) {
|
|
180
|
-
await run(`INSERT OR REPLACE INTO _cursor (key, value) VALUES ($1, $2)`, key, value);
|
|
347
|
+
await run(`INSERT OR REPLACE INTO _cursor (key, value) VALUES ($1, $2)`, [key, value]);
|
|
181
348
|
}
|
|
182
349
|
export async function getRepoStatus(did) {
|
|
183
|
-
const rows = await all(`SELECT status FROM _repos WHERE did = $1`, did);
|
|
350
|
+
const rows = await all(`SELECT status FROM _repos WHERE did = $1`, [did]);
|
|
184
351
|
return rows[0]?.status || null;
|
|
185
352
|
}
|
|
186
353
|
export async function setRepoStatus(did, status, rev, opts) {
|
|
187
354
|
if (status === 'active') {
|
|
188
355
|
// 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);
|
|
356
|
+
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
357
|
// 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);
|
|
358
|
+
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
359
|
}
|
|
193
360
|
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);
|
|
361
|
+
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
362
|
// 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);
|
|
363
|
+
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
364
|
}
|
|
198
365
|
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);
|
|
366
|
+
await run(`UPDATE _repos SET status = $1 WHERE did = $2`, [status, did]);
|
|
367
|
+
await run(`INSERT OR IGNORE INTO _repos (did, status) VALUES ($1, $2)`, [did, status]);
|
|
201
368
|
}
|
|
202
369
|
}
|
|
370
|
+
/** Update the handle for a DID if it exists in _repos. */
|
|
371
|
+
export async function updateRepoHandle(did, handle) {
|
|
372
|
+
await run(`UPDATE _repos SET handle = $1 WHERE did = $2`, [handle, did]);
|
|
373
|
+
}
|
|
374
|
+
export async function getRepoRev(did) {
|
|
375
|
+
const rows = await all(`SELECT rev FROM _repos WHERE did = $1`, [did]);
|
|
376
|
+
return rows[0]?.rev ?? null;
|
|
377
|
+
}
|
|
203
378
|
export async function getRepoRetryInfo(did) {
|
|
204
|
-
const rows = await all(`SELECT retry_count, retry_after FROM _repos WHERE did = $1`, did);
|
|
379
|
+
const rows = await all(`SELECT retry_count, retry_after FROM _repos WHERE did = $1`, [did]);
|
|
205
380
|
if (rows.length === 0)
|
|
206
381
|
return null;
|
|
207
382
|
return { retryCount: Number(rows[0].retry_count), retryAfter: Number(rows[0].retry_after) };
|
|
208
383
|
}
|
|
209
384
|
export async function listRetryEligibleRepos(maxRetries) {
|
|
210
385
|
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`,
|
|
386
|
+
const rows = await all(`SELECT did FROM _repos WHERE status = 'failed' AND retry_after <= $1 AND retry_count < $2`, [
|
|
387
|
+
now,
|
|
388
|
+
maxRetries,
|
|
389
|
+
]);
|
|
212
390
|
return rows.map((r) => r.did);
|
|
213
391
|
}
|
|
214
392
|
export async function listPendingRepos() {
|
|
215
393
|
const rows = await all(`SELECT did FROM _repos WHERE status = 'pending'`);
|
|
216
394
|
return rows.map((r) => r.did);
|
|
217
395
|
}
|
|
396
|
+
export async function listActiveRepoDids() {
|
|
397
|
+
const rows = await all(`SELECT did FROM _repos WHERE status = 'active'`);
|
|
398
|
+
return rows.map((r) => r.did);
|
|
399
|
+
}
|
|
400
|
+
export async function removeRepo(did) {
|
|
401
|
+
await run(`DELETE FROM _repos WHERE did = $1`, [did]);
|
|
402
|
+
}
|
|
403
|
+
export async function getRepoHandle(did) {
|
|
404
|
+
const rows = await all(`SELECT handle FROM _repos WHERE did = $1`, [did]);
|
|
405
|
+
return rows[0]?.handle ?? null;
|
|
406
|
+
}
|
|
218
407
|
export async function listAllRepoStatuses() {
|
|
219
408
|
return (await all(`SELECT did, status FROM _repos`));
|
|
220
409
|
}
|
|
@@ -228,27 +417,96 @@ export async function listReposPaginated(opts = {}) {
|
|
|
228
417
|
params.push(status);
|
|
229
418
|
}
|
|
230
419
|
if (q) {
|
|
231
|
-
conditions.push(`(did
|
|
420
|
+
conditions.push(`(did ${dialect.ilike} $${paramIdx} OR handle ${dialect.ilike} $${paramIdx})`);
|
|
232
421
|
params.push(`%${q}%`);
|
|
233
422
|
paramIdx++;
|
|
234
423
|
}
|
|
235
424
|
const where = conditions.length ? ' WHERE ' + conditions.join(' AND ') : '';
|
|
236
|
-
const countRows = await all(`SELECT
|
|
425
|
+
const countRows = await all(`SELECT ${dialect.countAsInteger} as total FROM _repos${where}`, params);
|
|
237
426
|
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
|
|
427
|
+
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
428
|
return { repos: rows, total };
|
|
240
429
|
}
|
|
241
430
|
export async function getCollectionCounts() {
|
|
242
431
|
const counts = {};
|
|
243
432
|
for (const [collection, schema] of schemas) {
|
|
244
|
-
const rows = await all(`SELECT
|
|
433
|
+
const rows = await all(`SELECT ${dialect.countAsInteger} as count FROM ${schema.tableName}`);
|
|
245
434
|
counts[collection] = Number(rows[0]?.count || 0);
|
|
246
435
|
}
|
|
247
436
|
return counts;
|
|
248
437
|
}
|
|
438
|
+
export async function getRepoStatusCounts() {
|
|
439
|
+
const rows = await all(`SELECT status, ${dialect.countAsInteger} as count FROM _repos GROUP BY status`);
|
|
440
|
+
const counts = {};
|
|
441
|
+
for (const row of rows)
|
|
442
|
+
counts[row.status] = Number(row.count);
|
|
443
|
+
return counts;
|
|
444
|
+
}
|
|
445
|
+
export async function getDatabaseSize() {
|
|
446
|
+
if (dialect.supportsSequences) {
|
|
447
|
+
// DuckDB: pragma_database_size returns pre-formatted strings
|
|
448
|
+
const rows = await all('SELECT database_size, memory_usage, memory_limit FROM pragma_database_size()');
|
|
449
|
+
return rows[0] ?? {};
|
|
450
|
+
}
|
|
451
|
+
// SQLite: compute from page_count * page_size
|
|
452
|
+
const pages = await all('SELECT page_count FROM pragma_page_count()');
|
|
453
|
+
const sizes = await all('SELECT page_size FROM pragma_page_size()');
|
|
454
|
+
const pageCount = Number(pages[0]?.page_count ?? 0);
|
|
455
|
+
const pageSize = Number(sizes[0]?.page_size ?? 0);
|
|
456
|
+
const bytes = pageCount * pageSize;
|
|
457
|
+
const mib = (bytes / 1024 / 1024).toFixed(1);
|
|
458
|
+
return { database_size: `${mib} MiB`, memory_usage: 'N/A', memory_limit: 'N/A' };
|
|
459
|
+
}
|
|
460
|
+
export async function getLabelCount(val) {
|
|
461
|
+
const rows = await all(`SELECT ${dialect.countAsInteger} as count FROM _labels WHERE val = $1`, [val]);
|
|
462
|
+
return Number(rows[0]?.count || 0);
|
|
463
|
+
}
|
|
464
|
+
export async function deleteLabels(val) {
|
|
465
|
+
const count = await getLabelCount(val);
|
|
466
|
+
await run(`DELETE FROM _labels WHERE val = $1`, [val]);
|
|
467
|
+
return count;
|
|
468
|
+
}
|
|
469
|
+
export async function getRecentRecords(collection, limit) {
|
|
470
|
+
const schema = schemas.get(collection);
|
|
471
|
+
if (!schema)
|
|
472
|
+
return [];
|
|
473
|
+
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]);
|
|
474
|
+
return rows;
|
|
475
|
+
}
|
|
249
476
|
export async function getSchemaDump() {
|
|
250
|
-
|
|
251
|
-
|
|
477
|
+
let rows;
|
|
478
|
+
if (dialect.supportsSequences) {
|
|
479
|
+
// DuckDB: use duckdb_tables() for full DDL
|
|
480
|
+
rows = await all(`SELECT sql FROM duckdb_tables() ORDER BY table_name`);
|
|
481
|
+
}
|
|
482
|
+
else {
|
|
483
|
+
// SQLite: use sqlite_master, skip FTS shadow/internal tables
|
|
484
|
+
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`);
|
|
485
|
+
}
|
|
486
|
+
// Normalize indentation and formatting
|
|
487
|
+
return rows
|
|
488
|
+
.map((r) => {
|
|
489
|
+
let sql = r.sql.trim();
|
|
490
|
+
// Remove quotes around column names (SQLite adds them for some columns)
|
|
491
|
+
sql = sql.replace(/\n\s*"(\w+)"/g, '\n$1');
|
|
492
|
+
// Ensure closing paren is on its own line
|
|
493
|
+
sql = sql.replace(/([^(\s])\)$/, '$1\n)');
|
|
494
|
+
// Normalize leading-comma columns added by ALTER TABLE into trailing commas
|
|
495
|
+
sql = sql.replace(/\n\s*,\s*/g, ',\n');
|
|
496
|
+
// Split into lines and re-indent consistently
|
|
497
|
+
const lines = sql.split('\n').map((l) => l.trim());
|
|
498
|
+
sql = lines
|
|
499
|
+
.map((line, i) => {
|
|
500
|
+
if (i === 0)
|
|
501
|
+
return line; // CREATE TABLE line
|
|
502
|
+
if (line.startsWith(')'))
|
|
503
|
+
return ')'; // closing paren at top level
|
|
504
|
+
return ' ' + line; // indent columns
|
|
505
|
+
})
|
|
506
|
+
.join('\n');
|
|
507
|
+
return sql + ';';
|
|
508
|
+
})
|
|
509
|
+
.join('\n\n');
|
|
252
510
|
}
|
|
253
511
|
export function buildInsertOp(collection, uri, cid, authorDid, record) {
|
|
254
512
|
const schema = schemas.get(collection);
|
|
@@ -267,12 +525,12 @@ export function buildInsertOp(collection, uri, cid, authorDid, record) {
|
|
|
267
525
|
else if (col.originalName.endsWith('__cid') && record[col.originalName.replace('__cid', '')]) {
|
|
268
526
|
rawValue = record[col.originalName.replace('__cid', '')].cid;
|
|
269
527
|
}
|
|
270
|
-
colNames.push(col.name);
|
|
528
|
+
colNames.push(q(col.name));
|
|
271
529
|
placeholders.push(`$${paramIdx++}`);
|
|
272
530
|
if (rawValue === undefined || rawValue === null) {
|
|
273
531
|
values.push(null);
|
|
274
532
|
}
|
|
275
|
-
else if (col.
|
|
533
|
+
else if (col.isJson) {
|
|
276
534
|
values.push(JSON.stringify(rawValue));
|
|
277
535
|
}
|
|
278
536
|
else {
|
|
@@ -287,34 +545,34 @@ export async function insertRecord(collection, uri, cid, authorDid, record) {
|
|
|
287
545
|
if (!schema)
|
|
288
546
|
throw new Error(`Unknown collection: ${collection}`);
|
|
289
547
|
const { sql, params } = buildInsertOp(collection, uri, cid, authorDid, record);
|
|
290
|
-
await run(sql,
|
|
548
|
+
await run(sql, params);
|
|
291
549
|
// Insert child table rows
|
|
292
550
|
for (const child of schema.children) {
|
|
293
551
|
const items = record[child.fieldName];
|
|
294
552
|
if (!Array.isArray(items))
|
|
295
553
|
continue;
|
|
296
554
|
// Delete existing child rows (handles INSERT OR REPLACE on main table)
|
|
297
|
-
await run(`DELETE FROM ${child.tableName} WHERE parent_uri = $1`, uri);
|
|
555
|
+
await run(`DELETE FROM ${child.tableName} WHERE parent_uri = $1`, [uri]);
|
|
298
556
|
for (const item of items) {
|
|
299
557
|
const colNames = ['parent_uri', 'parent_did'];
|
|
300
558
|
const placeholders = ['$1', '$2'];
|
|
301
559
|
const values = [uri, authorDid];
|
|
302
560
|
let idx = 3;
|
|
303
561
|
for (const col of child.columns) {
|
|
304
|
-
colNames.push(col.name);
|
|
562
|
+
colNames.push(q(col.name));
|
|
305
563
|
placeholders.push(`$${idx++}`);
|
|
306
564
|
const raw = item[col.originalName];
|
|
307
565
|
if (raw === undefined || raw === null) {
|
|
308
566
|
values.push(null);
|
|
309
567
|
}
|
|
310
|
-
else if (col.
|
|
568
|
+
else if (col.isJson) {
|
|
311
569
|
values.push(JSON.stringify(raw));
|
|
312
570
|
}
|
|
313
571
|
else {
|
|
314
572
|
values.push(raw);
|
|
315
573
|
}
|
|
316
574
|
}
|
|
317
|
-
await run(`INSERT INTO ${child.tableName} (${colNames.join(', ')}) VALUES (${placeholders.join(', ')})`,
|
|
575
|
+
await run(`INSERT INTO ${child.tableName} (${colNames.join(', ')}) VALUES (${placeholders.join(', ')})`, values);
|
|
318
576
|
}
|
|
319
577
|
}
|
|
320
578
|
// Insert union branch rows
|
|
@@ -327,7 +585,7 @@ export async function insertRecord(collection, uri, cid, authorDid, record) {
|
|
|
327
585
|
continue;
|
|
328
586
|
// Delete existing branch rows (handles INSERT OR REPLACE)
|
|
329
587
|
for (const b of union.branches) {
|
|
330
|
-
await run(`DELETE FROM ${b.tableName} WHERE parent_uri = $1`, uri);
|
|
588
|
+
await run(`DELETE FROM ${b.tableName} WHERE parent_uri = $1`, [uri]);
|
|
331
589
|
}
|
|
332
590
|
if (branch.isArray && branch.arrayField) {
|
|
333
591
|
// Array branch (e.g., embed.images) — insert one row per array item
|
|
@@ -340,20 +598,20 @@ export async function insertRecord(collection, uri, cid, authorDid, record) {
|
|
|
340
598
|
const values = [uri, authorDid];
|
|
341
599
|
let idx = 3;
|
|
342
600
|
for (const col of branch.columns) {
|
|
343
|
-
colNames.push(col.name);
|
|
601
|
+
colNames.push(q(col.name));
|
|
344
602
|
placeholders.push(`$${idx++}`);
|
|
345
603
|
const raw = item[col.originalName];
|
|
346
604
|
if (raw === undefined || raw === null) {
|
|
347
605
|
values.push(null);
|
|
348
606
|
}
|
|
349
|
-
else if (col.
|
|
607
|
+
else if (col.isJson) {
|
|
350
608
|
values.push(JSON.stringify(raw));
|
|
351
609
|
}
|
|
352
610
|
else {
|
|
353
611
|
values.push(raw);
|
|
354
612
|
}
|
|
355
613
|
}
|
|
356
|
-
await run(`INSERT INTO ${branch.tableName} (${colNames.join(', ')}) VALUES (${placeholders.join(', ')})`,
|
|
614
|
+
await run(`INSERT INTO ${branch.tableName} (${colNames.join(', ')}) VALUES (${placeholders.join(', ')})`, values);
|
|
357
615
|
}
|
|
358
616
|
}
|
|
359
617
|
else {
|
|
@@ -364,22 +622,24 @@ export async function insertRecord(collection, uri, cid, authorDid, record) {
|
|
|
364
622
|
const values = [uri, authorDid];
|
|
365
623
|
let idx = 3;
|
|
366
624
|
for (const col of branch.columns) {
|
|
367
|
-
colNames.push(col.name);
|
|
625
|
+
colNames.push(q(col.name));
|
|
368
626
|
placeholders.push(`$${idx++}`);
|
|
369
627
|
const raw = branchData[col.originalName];
|
|
370
628
|
if (raw === undefined || raw === null) {
|
|
371
629
|
values.push(null);
|
|
372
630
|
}
|
|
373
|
-
else if (col.
|
|
631
|
+
else if (col.isJson) {
|
|
374
632
|
values.push(JSON.stringify(raw));
|
|
375
633
|
}
|
|
376
634
|
else {
|
|
377
635
|
values.push(raw);
|
|
378
636
|
}
|
|
379
637
|
}
|
|
380
|
-
await run(`INSERT INTO ${branch.tableName} (${colNames.join(', ')}) VALUES (${placeholders.join(', ')})`,
|
|
638
|
+
await run(`INSERT INTO ${branch.tableName} (${colNames.join(', ')}) VALUES (${placeholders.join(', ')})`, values);
|
|
381
639
|
}
|
|
382
640
|
}
|
|
641
|
+
// Incrementally update FTS index for this record
|
|
642
|
+
await updateFtsRecord(collection, uri);
|
|
383
643
|
}
|
|
384
644
|
/** Extract branch data from a union value, handling wrapper properties */
|
|
385
645
|
function resolveBranchData(unionValue, branch) {
|
|
@@ -394,32 +654,41 @@ export async function deleteRecord(collection, uri) {
|
|
|
394
654
|
const schema = schemas.get(collection);
|
|
395
655
|
if (!schema)
|
|
396
656
|
return;
|
|
657
|
+
// Remove from FTS index before deleting the record data
|
|
658
|
+
await deleteFtsRecord(collection, uri);
|
|
397
659
|
for (const child of schema.children) {
|
|
398
|
-
await run(`DELETE FROM ${child.tableName} WHERE parent_uri = $1`, uri);
|
|
660
|
+
await run(`DELETE FROM ${child.tableName} WHERE parent_uri = $1`, [uri]);
|
|
399
661
|
}
|
|
400
662
|
for (const union of schema.unions) {
|
|
401
663
|
for (const branch of union.branches) {
|
|
402
|
-
await run(`DELETE FROM ${branch.tableName} WHERE parent_uri = $1`, uri);
|
|
664
|
+
await run(`DELETE FROM ${branch.tableName} WHERE parent_uri = $1`, [uri]);
|
|
403
665
|
}
|
|
404
666
|
}
|
|
405
|
-
await run(`DELETE FROM ${schema.tableName} WHERE uri = $1`, uri);
|
|
667
|
+
await run(`DELETE FROM ${schema.tableName} WHERE uri = $1`, [uri]);
|
|
406
668
|
}
|
|
407
669
|
export async function insertLabels(labels) {
|
|
408
670
|
if (labels.length === 0)
|
|
409
671
|
return;
|
|
410
672
|
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);
|
|
673
|
+
// Skip if an active (non-negated, non-expired, not-superseded-by-negation) label already exists for this src+uri+val
|
|
674
|
+
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
675
|
if (!label.neg && existing.length > 0)
|
|
414
676
|
continue;
|
|
415
|
-
await run(`INSERT INTO _labels (src, uri, val, neg, cts, exp) VALUES ($1, $2, $3, $4, $5, $6)`,
|
|
677
|
+
await run(`INSERT INTO _labels (src, uri, val, neg, cts, exp) VALUES ($1, $2, $3, $4, $5, $6)`, [
|
|
678
|
+
label.src,
|
|
679
|
+
label.uri,
|
|
680
|
+
label.val,
|
|
681
|
+
label.neg || false,
|
|
682
|
+
label.cts || new Date().toISOString(),
|
|
683
|
+
label.exp || null,
|
|
684
|
+
]);
|
|
416
685
|
}
|
|
417
686
|
}
|
|
418
687
|
export async function queryLabelsForUris(uris) {
|
|
419
688
|
if (uris.length === 0)
|
|
420
689
|
return new Map();
|
|
421
690
|
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)`,
|
|
691
|
+
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
692
|
const result = new Map();
|
|
424
693
|
for (const row of rows) {
|
|
425
694
|
const key = row.uri;
|
|
@@ -429,9 +698,9 @@ export async function queryLabelsForUris(uris) {
|
|
|
429
698
|
src: row.src,
|
|
430
699
|
uri: row.uri,
|
|
431
700
|
val: row.val,
|
|
432
|
-
neg: row.neg,
|
|
701
|
+
neg: !!row.neg,
|
|
433
702
|
cts: normalizeValue(row.cts),
|
|
434
|
-
|
|
703
|
+
...(row.exp ? { exp: String(row.exp) } : {}),
|
|
435
704
|
});
|
|
436
705
|
}
|
|
437
706
|
return result;
|
|
@@ -452,246 +721,209 @@ export async function bulkInsertRecords(records) {
|
|
|
452
721
|
if (!schema)
|
|
453
722
|
continue;
|
|
454
723
|
const stagingTable = `_staging_${collection.replace(/\./g, '_')}`;
|
|
455
|
-
const allCols = ['uri', 'cid', 'did', 'indexed_at', ...schema.columns.map((c) => c.name)];
|
|
724
|
+
const allCols = ['uri', 'cid', 'did', 'indexed_at', ...schema.columns.map((c) => q(c.name))];
|
|
456
725
|
const colDefs = [
|
|
457
726
|
'uri TEXT',
|
|
458
727
|
'cid TEXT',
|
|
459
728
|
'did TEXT',
|
|
460
729
|
'indexed_at TEXT',
|
|
461
|
-
...schema.columns.map((c) =>
|
|
730
|
+
...schema.columns.map((c) => {
|
|
731
|
+
const t = c.sqlType;
|
|
732
|
+
// Use TEXT for timestamp columns in staging (will cast on merge)
|
|
733
|
+
return `${q(c.name)} ${t === 'TIMESTAMP' || t === 'TIMESTAMPTZ' ? 'TEXT' : t}`;
|
|
734
|
+
}),
|
|
462
735
|
];
|
|
463
|
-
|
|
464
|
-
await
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
736
|
+
await port.execute(`DROP TABLE IF EXISTS ${stagingTable}`, []);
|
|
737
|
+
await port.execute(`CREATE TABLE ${stagingTable} (${colDefs.join(', ')})`, []);
|
|
738
|
+
const inserter = await port.createBulkInserter(stagingTable, allCols);
|
|
739
|
+
const now = new Date().toISOString();
|
|
740
|
+
for (const rec of recs) {
|
|
741
|
+
try {
|
|
742
|
+
const values = [rec.uri, rec.cid, rec.did, now];
|
|
743
|
+
for (const col of schema.columns) {
|
|
744
|
+
values.push(resolveColumnValue(col, rec.record));
|
|
745
|
+
}
|
|
746
|
+
inserter.append(values);
|
|
747
|
+
inserted++;
|
|
748
|
+
}
|
|
749
|
+
catch {
|
|
750
|
+
// Skip bad records
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
await inserter.close();
|
|
754
|
+
// Merge into target, filtering rows that would violate NOT NULL
|
|
755
|
+
const selectCols = allCols.map((name) => {
|
|
756
|
+
const col = schema.columns.find((c) => q(c.name) === name);
|
|
757
|
+
if (name === 'indexed_at' || (col && (col.sqlType === 'TIMESTAMP' || col.sqlType === 'TIMESTAMPTZ'))) {
|
|
758
|
+
return `${dialect.tryCastTimestamp(name)} AS ${name}`;
|
|
759
|
+
}
|
|
760
|
+
return name;
|
|
761
|
+
});
|
|
762
|
+
const notNullChecks = ['uri IS NOT NULL', 'did IS NOT NULL'];
|
|
763
|
+
for (const col of schema.columns) {
|
|
764
|
+
if (col.notNull) {
|
|
765
|
+
if (col.sqlType === 'TIMESTAMP' || col.sqlType === 'TIMESTAMPTZ') {
|
|
766
|
+
notNullChecks.push(`${dialect.tryCastTimestamp(q(col.name))} IS NOT NULL`);
|
|
767
|
+
}
|
|
768
|
+
else {
|
|
769
|
+
notNullChecks.push(`${q(col.name)} IS NOT NULL`);
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
const whereClause = notNullChecks.length ? ` WHERE ${notNullChecks.join(' AND ')}` : '';
|
|
774
|
+
await port.execute(`INSERT OR REPLACE INTO ${schema.tableName} (${allCols.join(', ')}) SELECT ${selectCols.join(', ')} FROM ${stagingTable}${whereClause}`, []);
|
|
775
|
+
await port.execute(`DROP TABLE ${stagingTable}`, []);
|
|
776
|
+
// Populate child tables
|
|
777
|
+
for (const child of schema.children) {
|
|
778
|
+
const childStagingTable = `_staging_${collection.replace(/\./g, '_')}__${child.fieldName}`;
|
|
779
|
+
const childColDefs = [
|
|
780
|
+
'parent_uri TEXT',
|
|
781
|
+
'parent_did TEXT',
|
|
782
|
+
...child.columns.map((c) => {
|
|
783
|
+
const t = c.sqlType;
|
|
784
|
+
return `${q(c.name)} ${t === 'TIMESTAMP' || t === 'TIMESTAMPTZ' ? 'TEXT' : t}`;
|
|
785
|
+
}),
|
|
786
|
+
];
|
|
787
|
+
const childAllCols = ['parent_uri', 'parent_did', ...child.columns.map((c) => q(c.name))];
|
|
788
|
+
await port.execute(`DROP TABLE IF EXISTS ${childStagingTable}`, []);
|
|
789
|
+
await port.execute(`CREATE TABLE ${childStagingTable} (${childColDefs.join(', ')})`, []);
|
|
790
|
+
const childInserter = await port.createBulkInserter(childStagingTable, childAllCols);
|
|
469
791
|
for (const rec of recs) {
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
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));
|
|
792
|
+
const items = rec.record[child.fieldName];
|
|
793
|
+
if (!Array.isArray(items))
|
|
794
|
+
continue;
|
|
795
|
+
for (const item of items) {
|
|
796
|
+
try {
|
|
797
|
+
const values = [rec.uri, rec.did];
|
|
798
|
+
for (const col of child.columns) {
|
|
799
|
+
values.push(resolveRawColumnValue(col, item));
|
|
497
800
|
}
|
|
801
|
+
childInserter.append(values);
|
|
802
|
+
}
|
|
803
|
+
catch {
|
|
804
|
+
// Skip bad items
|
|
498
805
|
}
|
|
499
|
-
appender.endRow();
|
|
500
|
-
inserted++;
|
|
501
|
-
}
|
|
502
|
-
catch {
|
|
503
|
-
// Skip bad records
|
|
504
806
|
}
|
|
505
807
|
}
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
808
|
+
await childInserter.close();
|
|
809
|
+
// Delete existing child rows for these URIs, then merge staging
|
|
810
|
+
const uriPlaceholders = recs.map((_, i) => `$${i + 1}`).join(',');
|
|
811
|
+
await port.execute(`DELETE FROM ${child.tableName} WHERE parent_uri IN (${uriPlaceholders})`, recs.map((r) => r.uri));
|
|
812
|
+
const childSelectCols = childAllCols.map((name) => {
|
|
813
|
+
const col = child.columns.find((c) => q(c.name) === name);
|
|
814
|
+
if (col && (col.sqlType === 'TIMESTAMP' || col.sqlType === 'TIMESTAMPTZ')) {
|
|
815
|
+
return `${dialect.tryCastTimestamp(name)} AS ${name}`;
|
|
513
816
|
}
|
|
514
817
|
return name;
|
|
515
818
|
});
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
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 = [
|
|
819
|
+
await port.execute(`INSERT INTO ${child.tableName} (${childAllCols.join(', ')}) SELECT ${childSelectCols.join(', ')} FROM ${childStagingTable} WHERE parent_uri IS NOT NULL`, []);
|
|
820
|
+
await port.execute(`DROP TABLE ${childStagingTable}`, []);
|
|
821
|
+
}
|
|
822
|
+
// Populate union branch tables
|
|
823
|
+
for (const union of schema.unions) {
|
|
824
|
+
for (const branch of union.branches) {
|
|
825
|
+
const branchStagingTable = `_staging_${collection.replace(/\./g, '_')}__${toSnakeCase(union.fieldName)}_${branch.branchName}`;
|
|
826
|
+
const branchColDefs = [
|
|
535
827
|
'parent_uri TEXT',
|
|
536
828
|
'parent_did TEXT',
|
|
537
|
-
...
|
|
829
|
+
...branch.columns.map((c) => {
|
|
830
|
+
const t = c.sqlType;
|
|
831
|
+
return `${q(c.name)} ${t === 'TIMESTAMP' || t === 'TIMESTAMPTZ' ? 'TEXT' : t}`;
|
|
832
|
+
}),
|
|
538
833
|
];
|
|
539
|
-
const
|
|
540
|
-
await
|
|
541
|
-
await
|
|
542
|
-
const
|
|
834
|
+
const branchAllCols = ['parent_uri', 'parent_did', ...branch.columns.map((c) => q(c.name))];
|
|
835
|
+
await port.execute(`DROP TABLE IF EXISTS ${branchStagingTable}`, []);
|
|
836
|
+
await port.execute(`CREATE TABLE ${branchStagingTable} (${branchColDefs.join(', ')})`, []);
|
|
837
|
+
const branchInserter = await port.createBulkInserter(branchStagingTable, branchAllCols);
|
|
543
838
|
for (const rec of recs) {
|
|
544
|
-
const
|
|
545
|
-
if (!
|
|
839
|
+
const unionValue = rec.record[union.fieldName];
|
|
840
|
+
if (!unionValue || typeof unionValue !== 'object')
|
|
546
841
|
continue;
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
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)
|
|
842
|
+
if (unionValue.$type !== branch.type)
|
|
843
|
+
continue;
|
|
844
|
+
if (branch.isArray && branch.arrayField) {
|
|
845
|
+
const items = resolveBranchData(unionValue, branch)[branch.arrayField];
|
|
846
|
+
if (!Array.isArray(items))
|
|
610
847
|
continue;
|
|
611
|
-
|
|
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 {
|
|
848
|
+
for (const item of items) {
|
|
645
849
|
try {
|
|
646
|
-
const
|
|
647
|
-
branchAppender.appendVarchar(rec.uri);
|
|
648
|
-
branchAppender.appendVarchar(rec.did);
|
|
850
|
+
const values = [rec.uri, rec.did];
|
|
649
851
|
for (const col of branch.columns) {
|
|
650
|
-
|
|
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
|
-
}
|
|
852
|
+
values.push(resolveRawColumnValue(col, item));
|
|
666
853
|
}
|
|
667
|
-
|
|
854
|
+
branchInserter.append(values);
|
|
668
855
|
}
|
|
669
856
|
catch {
|
|
670
|
-
// Skip bad
|
|
857
|
+
// Skip bad items
|
|
671
858
|
}
|
|
672
859
|
}
|
|
673
860
|
}
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
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}`);
|
|
861
|
+
else {
|
|
862
|
+
try {
|
|
863
|
+
const branchData = resolveBranchData(unionValue, branch);
|
|
864
|
+
const values = [rec.uri, rec.did];
|
|
865
|
+
for (const col of branch.columns) {
|
|
866
|
+
values.push(resolveRawColumnValue(col, branchData));
|
|
867
|
+
}
|
|
868
|
+
branchInserter.append(values);
|
|
869
|
+
}
|
|
870
|
+
catch {
|
|
871
|
+
// Skip bad records
|
|
872
|
+
}
|
|
873
|
+
}
|
|
689
874
|
}
|
|
875
|
+
await branchInserter.close();
|
|
876
|
+
// Delete existing branch rows for these URIs, then merge staging
|
|
877
|
+
const uriPlaceholders = recs.map((_, i) => `$${i + 1}`).join(',');
|
|
878
|
+
await port.execute(`DELETE FROM ${branch.tableName} WHERE parent_uri IN (${uriPlaceholders})`, recs.map((r) => r.uri));
|
|
879
|
+
const branchSelectCols = branchAllCols.map((name) => {
|
|
880
|
+
const col = branch.columns.find((c) => q(c.name) === name);
|
|
881
|
+
if (col && (col.sqlType === 'TIMESTAMP' || col.sqlType === 'TIMESTAMPTZ')) {
|
|
882
|
+
return `${dialect.tryCastTimestamp(name)} AS ${name}`;
|
|
883
|
+
}
|
|
884
|
+
return name;
|
|
885
|
+
});
|
|
886
|
+
await port.execute(`INSERT INTO ${branch.tableName} (${branchAllCols.join(', ')}) SELECT ${branchSelectCols.join(', ')} FROM ${branchStagingTable} WHERE parent_uri IS NOT NULL`, []);
|
|
887
|
+
await port.execute(`DROP TABLE ${branchStagingTable}`, []);
|
|
690
888
|
}
|
|
691
|
-
}
|
|
889
|
+
}
|
|
692
890
|
}
|
|
693
891
|
return inserted;
|
|
694
892
|
}
|
|
893
|
+
/** Extract a column value from a record, handling strongRef expansion and type coercion for bulk insert */
|
|
894
|
+
function resolveColumnValue(col, record) {
|
|
895
|
+
let rawValue = record[col.originalName];
|
|
896
|
+
if (rawValue && typeof rawValue === 'object' && col.name.endsWith('_uri') && col.isRef) {
|
|
897
|
+
rawValue = rawValue.uri;
|
|
898
|
+
}
|
|
899
|
+
else if (col.originalName.endsWith('__cid') && record[col.originalName.replace('__cid', '')]) {
|
|
900
|
+
rawValue = record[col.originalName.replace('__cid', '')].cid;
|
|
901
|
+
}
|
|
902
|
+
return coerceValue(col.sqlType, rawValue);
|
|
903
|
+
}
|
|
904
|
+
/** Extract a raw column value from a data object and coerce for bulk insert */
|
|
905
|
+
function resolveRawColumnValue(col, data) {
|
|
906
|
+
return coerceValue(col.sqlType, data[col.originalName]);
|
|
907
|
+
}
|
|
908
|
+
/** Coerce a value to the appropriate type for insertion */
|
|
909
|
+
function coerceValue(sqlType, rawValue) {
|
|
910
|
+
if (rawValue === undefined || rawValue === null)
|
|
911
|
+
return null;
|
|
912
|
+
// Objects and arrays always need JSON stringification regardless of sqlType
|
|
913
|
+
// (on SQLite, JSON columns map to TEXT but still need stringification)
|
|
914
|
+
if (typeof rawValue === 'object' && !(rawValue instanceof Uint8Array)) {
|
|
915
|
+
return JSON.stringify(rawValue);
|
|
916
|
+
}
|
|
917
|
+
if (sqlType === 'JSON' || sqlType === 'TEXT') {
|
|
918
|
+
return String(rawValue);
|
|
919
|
+
}
|
|
920
|
+
if (sqlType === 'INTEGER' || sqlType === 'BIGINT') {
|
|
921
|
+
return typeof rawValue === 'number' ? rawValue : parseInt(rawValue);
|
|
922
|
+
}
|
|
923
|
+
if (sqlType === 'BOOLEAN')
|
|
924
|
+
return !!rawValue;
|
|
925
|
+
return String(rawValue);
|
|
926
|
+
}
|
|
695
927
|
export async function queryRecords(collection, opts = {}) {
|
|
696
928
|
const schema = schemas.get(collection);
|
|
697
929
|
if (!schema)
|
|
@@ -734,7 +966,7 @@ export async function queryRecords(collection, opts = {}) {
|
|
|
734
966
|
sql += ' AND ' + conditions.join(' AND ');
|
|
735
967
|
sql += ` ORDER BY t.${sortName} ${order.toUpperCase()}, t.cid ${order.toUpperCase()} LIMIT $${paramIdx++}`;
|
|
736
968
|
params.push(limit + 1); // fetch one extra for cursor
|
|
737
|
-
const rows = await all(sql,
|
|
969
|
+
const rows = await all(sql, params);
|
|
738
970
|
const hasMore = rows.length > limit;
|
|
739
971
|
if (hasMore)
|
|
740
972
|
rows.pop();
|
|
@@ -774,7 +1006,7 @@ export async function queryRecords(collection, opts = {}) {
|
|
|
774
1006
|
}
|
|
775
1007
|
export async function getRecordByUri(uri) {
|
|
776
1008
|
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);
|
|
1009
|
+
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
1010
|
if (rows.length > 0) {
|
|
779
1011
|
const row = rows[0];
|
|
780
1012
|
if (schema.children.length > 0) {
|
|
@@ -811,7 +1043,7 @@ export async function getRecordsByUris(collection, uris) {
|
|
|
811
1043
|
if (!schema)
|
|
812
1044
|
return [];
|
|
813
1045
|
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')`,
|
|
1046
|
+
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
1047
|
// Batch-fetch child rows for all URIs
|
|
816
1048
|
const childData = new Map();
|
|
817
1049
|
for (const child of schema.children) {
|
|
@@ -839,6 +1071,19 @@ export async function getRecordsByUris(collection, uris) {
|
|
|
839
1071
|
const byUri = new Map(rows.map((r) => [r.uri, r]));
|
|
840
1072
|
return uris.map((u) => byUri.get(u)).filter(Boolean);
|
|
841
1073
|
}
|
|
1074
|
+
/** Fetch records by URIs and return as a shaped Map keyed by URI. */
|
|
1075
|
+
export async function getRecordsMap(collection, uris) {
|
|
1076
|
+
if (uris.length === 0)
|
|
1077
|
+
return new Map();
|
|
1078
|
+
const records = await getRecordsByUris(collection, uris);
|
|
1079
|
+
const map = new Map();
|
|
1080
|
+
for (const r of records) {
|
|
1081
|
+
const shaped = reshapeRow(r, r?.__childData, r?.__unionData);
|
|
1082
|
+
if (shaped)
|
|
1083
|
+
map.set(shaped.uri, shaped);
|
|
1084
|
+
}
|
|
1085
|
+
return map;
|
|
1086
|
+
}
|
|
842
1087
|
/**
|
|
843
1088
|
* Multi-phase search across any collection's records.
|
|
844
1089
|
*
|
|
@@ -859,8 +1104,8 @@ export async function searchRecords(collection, query, opts = {}) {
|
|
|
859
1104
|
if (!schema)
|
|
860
1105
|
throw new Error(`Unknown collection: ${collection}`);
|
|
861
1106
|
const elapsed = timer();
|
|
862
|
-
const { limit = 20,
|
|
863
|
-
const textCols = schema.columns.filter((c) => c.
|
|
1107
|
+
const { limit = 20, fuzzy = true } = opts;
|
|
1108
|
+
const textCols = schema.columns.filter((c) => c.sqlType === 'TEXT');
|
|
864
1109
|
// Also check if FTS has indexed any columns (including derived JSON columns)
|
|
865
1110
|
const ftsSearchCols = getSearchColumns(collection);
|
|
866
1111
|
if (textCols.length === 0 && ftsSearchCols.length === 0) {
|
|
@@ -868,149 +1113,67 @@ export async function searchRecords(collection, query, opts = {}) {
|
|
|
868
1113
|
}
|
|
869
1114
|
// FTS shadow table name (dots replaced with underscores)
|
|
870
1115
|
const safeName = '_fts_' + collection.replace(/\./g, '_');
|
|
871
|
-
const ftsSchema = `fts_main_${safeName}`;
|
|
872
1116
|
const phaseErrors = [];
|
|
873
1117
|
const phasesUsed = [];
|
|
874
|
-
// Phase 1: BM25 ranked search
|
|
1118
|
+
// Phase 1: BM25 ranked search via SearchPort
|
|
875
1119
|
let bm25Results = [];
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
1120
|
+
const sp = getSearchPort();
|
|
1121
|
+
if (sp)
|
|
1122
|
+
try {
|
|
1123
|
+
const ftsQuery = stripStopWords(query);
|
|
1124
|
+
const ftsSearchColNames = getSearchColumns(collection);
|
|
1125
|
+
// Get ranked URIs from the search port
|
|
1126
|
+
const hits = await sp.search(safeName, ftsQuery, ftsSearchColNames, limit + 1, 0);
|
|
1127
|
+
if (hits.length > 0) {
|
|
1128
|
+
const uriList = hits.map((h) => h.uri);
|
|
1129
|
+
const scoreMap = new Map(hits.map((h) => [h.uri, h.score]));
|
|
1130
|
+
// Fetch full records for matched URIs
|
|
1131
|
+
const placeholders = uriList.map((_, i) => `$${i + 1}`).join(', ');
|
|
1132
|
+
const rows = await all(`SELECT m.* FROM ${schema.tableName} m
|
|
1133
|
+
LEFT JOIN _repos r ON m.did = r.did
|
|
1134
|
+
WHERE m.uri IN (${placeholders})
|
|
1135
|
+
AND (r.status IS NULL OR r.status != 'takendown')`, uriList);
|
|
1136
|
+
// Re-attach scores and sort
|
|
1137
|
+
bm25Results = rows
|
|
1138
|
+
.map((r) => ({ ...r, score: scoreMap.get(r.uri) ?? 0 }))
|
|
1139
|
+
.sort((a, b) => b.score - a.score);
|
|
896
1140
|
}
|
|
1141
|
+
phasesUsed.push('bm25');
|
|
1142
|
+
}
|
|
1143
|
+
catch (err) {
|
|
1144
|
+
phaseErrors.push(`bm25: ${err.message}`);
|
|
897
1145
|
}
|
|
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
1146
|
const bm25Count = bm25Results.length;
|
|
907
1147
|
const hasMore = bm25Results.length > limit;
|
|
908
1148
|
if (hasMore)
|
|
909
1149
|
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
1150
|
const existingUris = new Set(bm25Results.map((r) => r.uri));
|
|
957
|
-
|
|
958
|
-
|
|
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)
|
|
1151
|
+
// Phase 2: Fuzzy fallback for typo tolerance (if still under limit)
|
|
1152
|
+
// Only available on dialects with jaro_winkler_similarity (DuckDB)
|
|
991
1153
|
let fuzzyCount = 0;
|
|
992
|
-
if (fuzzy && bm25Results.length < limit) {
|
|
1154
|
+
if (fuzzy && dialect.jaroWinklerSimilarity && bm25Results.length < limit) {
|
|
993
1155
|
const remaining = limit - bm25Results.length;
|
|
1156
|
+
const jwFn = dialect.jaroWinklerSimilarity;
|
|
994
1157
|
const simExprs = [
|
|
995
|
-
...textCols.map((c) =>
|
|
996
|
-
|
|
1158
|
+
...textCols.map((c) => `${jwFn}(lower(t.${q(c.name)}), lower($1))`),
|
|
1159
|
+
`${jwFn}(lower(r.handle), lower($1))`,
|
|
997
1160
|
];
|
|
998
1161
|
// Include child table TEXT columns via correlated subquery
|
|
999
1162
|
for (const child of schema.children) {
|
|
1000
1163
|
for (const col of child.columns) {
|
|
1001
|
-
if (col.
|
|
1002
|
-
simExprs.push(`COALESCE((SELECT MAX(
|
|
1164
|
+
if (col.sqlType === 'TEXT') {
|
|
1165
|
+
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
1166
|
}
|
|
1004
1167
|
}
|
|
1005
1168
|
}
|
|
1006
|
-
const greatestExpr =
|
|
1169
|
+
const greatestExpr = dialect.greatest(simExprs);
|
|
1007
1170
|
const fuzzySQL = `SELECT t.*, ${greatestExpr} AS fuzzy_score
|
|
1008
1171
|
FROM ${schema.tableName} t LEFT JOIN _repos r ON t.did = r.did
|
|
1009
1172
|
WHERE ${greatestExpr} >= 0.8
|
|
1010
1173
|
ORDER BY fuzzy_score DESC
|
|
1011
1174
|
LIMIT $2`;
|
|
1012
1175
|
try {
|
|
1013
|
-
const fuzzyRows = await all(fuzzySQL, query, remaining + existingUris.size);
|
|
1176
|
+
const fuzzyRows = await all(fuzzySQL, [query, remaining + existingUris.size]);
|
|
1014
1177
|
phasesUsed.push('fuzzy');
|
|
1015
1178
|
for (const row of fuzzyRows) {
|
|
1016
1179
|
if (bm25Results.length >= limit)
|
|
@@ -1025,16 +1188,17 @@ export async function searchRecords(collection, query, opts = {}) {
|
|
|
1025
1188
|
phaseErrors.push(`fuzzy: ${err.message}`);
|
|
1026
1189
|
}
|
|
1027
1190
|
}
|
|
1028
|
-
// Remove score columns
|
|
1029
|
-
const
|
|
1191
|
+
// Remove score columns and reshape into Row<T> with value
|
|
1192
|
+
const rawRecords = bm25Results.map(({ score: _score, fuzzy_score: _fuzzy_score, ...rest }) => rest);
|
|
1193
|
+
const records = rawRecords
|
|
1194
|
+
.map((r) => reshapeRow(r, r?.__childData, r?.__unionData))
|
|
1195
|
+
.filter((r) => r != null);
|
|
1030
1196
|
const lastRow = bm25Results[bm25Results.length - 1];
|
|
1031
1197
|
const nextCursor = hasMore && lastRow?.score != null ? packCursor(lastRow.score, lastRow.cid) : undefined;
|
|
1032
1198
|
emit('search', 'query', {
|
|
1033
1199
|
collection,
|
|
1034
1200
|
query,
|
|
1035
1201
|
bm25_count: bm25Count > limit ? bm25Count - 1 : bm25Count,
|
|
1036
|
-
exact_count: exactMatchResults.length,
|
|
1037
|
-
recent_count: recentCount,
|
|
1038
1202
|
fuzzy_count: fuzzyCount,
|
|
1039
1203
|
total_results: records.length,
|
|
1040
1204
|
duration_ms: elapsed(),
|
|
@@ -1045,10 +1209,13 @@ export async function searchRecords(collection, query, opts = {}) {
|
|
|
1045
1209
|
}
|
|
1046
1210
|
// Raw SQL for script feeds
|
|
1047
1211
|
export async function querySQL(sql, params = []) {
|
|
1048
|
-
return all(sql,
|
|
1212
|
+
return all(sql, params);
|
|
1213
|
+
}
|
|
1214
|
+
export async function runSQL(sql, params = []) {
|
|
1215
|
+
return run(sql, params);
|
|
1049
1216
|
}
|
|
1050
|
-
export async function
|
|
1051
|
-
return
|
|
1217
|
+
export async function createBulkInserterSQL(table, columns, options) {
|
|
1218
|
+
return port.createBulkInserter(table, columns, options);
|
|
1052
1219
|
}
|
|
1053
1220
|
export function getSchema(collection) {
|
|
1054
1221
|
return schemas.get(collection);
|
|
@@ -1057,7 +1224,7 @@ export async function countByField(collection, field, value) {
|
|
|
1057
1224
|
const schema = schemas.get(collection);
|
|
1058
1225
|
if (!schema)
|
|
1059
1226
|
return 0;
|
|
1060
|
-
const rows = await all(`SELECT COUNT(*) as count FROM ${schema.tableName} WHERE ${field} = $1`, value);
|
|
1227
|
+
const rows = await all(`SELECT COUNT(*) as count FROM ${schema.tableName} WHERE ${field} = $1`, [value]);
|
|
1061
1228
|
return Number(rows[0]?.count || 0);
|
|
1062
1229
|
}
|
|
1063
1230
|
export async function countByFieldBatch(collection, field, values) {
|
|
@@ -1067,7 +1234,7 @@ export async function countByFieldBatch(collection, field, values) {
|
|
|
1067
1234
|
if (!schema)
|
|
1068
1235
|
return new Map();
|
|
1069
1236
|
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}`,
|
|
1237
|
+
const rows = await all(`SELECT ${field}, COUNT(*) as count FROM ${schema.tableName} WHERE ${field} IN (${placeholders}) GROUP BY ${field}`, values);
|
|
1071
1238
|
const result = new Map();
|
|
1072
1239
|
for (const row of rows) {
|
|
1073
1240
|
result.set(row[field], Number(row.count));
|
|
@@ -1078,7 +1245,7 @@ export async function findByField(collection, field, value) {
|
|
|
1078
1245
|
const schema = schemas.get(collection);
|
|
1079
1246
|
if (!schema)
|
|
1080
1247
|
return null;
|
|
1081
|
-
const rows = await all(`SELECT * FROM ${schema.tableName} WHERE ${field} = $1 LIMIT 1`, value);
|
|
1248
|
+
const rows = await all(`SELECT * FROM ${schema.tableName} WHERE ${field} = $1 LIMIT 1`, [value]);
|
|
1082
1249
|
return rows[0] || null;
|
|
1083
1250
|
}
|
|
1084
1251
|
export async function findByFieldBatch(collection, field, values) {
|
|
@@ -1088,7 +1255,7 @@ export async function findByFieldBatch(collection, field, values) {
|
|
|
1088
1255
|
if (!schema)
|
|
1089
1256
|
return new Map();
|
|
1090
1257
|
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})`,
|
|
1258
|
+
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
1259
|
// Attach child data if this collection has decomposed arrays
|
|
1093
1260
|
if (schema.children.length > 0 && rows.length > 0) {
|
|
1094
1261
|
const uris = rows.map((r) => r.uri);
|
|
@@ -1098,7 +1265,6 @@ export async function findByFieldBatch(collection, field, values) {
|
|
|
1098
1265
|
childData.set(child.fieldName, childRows);
|
|
1099
1266
|
}
|
|
1100
1267
|
for (const row of rows) {
|
|
1101
|
-
;
|
|
1102
1268
|
row.__childData = childData;
|
|
1103
1269
|
}
|
|
1104
1270
|
}
|
|
@@ -1129,7 +1295,7 @@ export async function findUriByFields(collection, conditions) {
|
|
|
1129
1295
|
return null;
|
|
1130
1296
|
const where = conditions.map((c, i) => `${c.field} = $${i + 1}`).join(' AND ');
|
|
1131
1297
|
const params = conditions.map((c) => c.value);
|
|
1132
|
-
const rows = await all(`SELECT uri FROM ${schema.tableName} WHERE ${where} LIMIT 1`,
|
|
1298
|
+
const rows = await all(`SELECT uri FROM ${schema.tableName} WHERE ${where} LIMIT 1`, params);
|
|
1133
1299
|
return rows[0]?.uri || null;
|
|
1134
1300
|
}
|
|
1135
1301
|
const ENVELOPE_KEYS = new Set(['uri', 'cid', 'did', 'handle', 'indexed_at']);
|
|
@@ -1145,7 +1311,7 @@ export async function getChildRows(childTableName, parentUris) {
|
|
|
1145
1311
|
if (parentUris.length === 0)
|
|
1146
1312
|
return new Map();
|
|
1147
1313
|
const placeholders = parentUris.map((_, i) => `$${i + 1}`).join(',');
|
|
1148
|
-
const rows = await all(`SELECT * FROM ${childTableName} WHERE parent_uri IN (${placeholders})`,
|
|
1314
|
+
const rows = await all(`SELECT * FROM ${childTableName} WHERE parent_uri IN (${placeholders})`, parentUris);
|
|
1149
1315
|
const result = new Map();
|
|
1150
1316
|
for (const row of rows) {
|
|
1151
1317
|
const key = row.parent_uri;
|
|
@@ -1167,7 +1333,7 @@ export function reshapeRow(row, childData, unionData) {
|
|
|
1167
1333
|
if (schema) {
|
|
1168
1334
|
for (const col of schema.columns) {
|
|
1169
1335
|
nameMap.set(col.name, col.originalName);
|
|
1170
|
-
if (col.
|
|
1336
|
+
if (col.isJson)
|
|
1171
1337
|
jsonCols.add(col.name);
|
|
1172
1338
|
}
|
|
1173
1339
|
}
|
|
@@ -1273,15 +1439,17 @@ export function unpackCursor(cursor) {
|
|
|
1273
1439
|
}
|
|
1274
1440
|
}
|
|
1275
1441
|
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)`,
|
|
1442
|
+
return all(`SELECT * FROM _labels WHERE uri LIKE $1 AND neg = false AND (exp IS NULL OR exp > CURRENT_TIMESTAMP)`, [
|
|
1443
|
+
`at://${did}/%`,
|
|
1444
|
+
]);
|
|
1277
1445
|
}
|
|
1278
1446
|
export async function searchAccounts(query, limit = 20) {
|
|
1279
|
-
return all(`SELECT did, handle, status FROM _repos WHERE did
|
|
1447
|
+
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
1448
|
}
|
|
1281
1449
|
export async function getAccountRecordCount(did) {
|
|
1282
1450
|
let total = 0;
|
|
1283
1451
|
for (const [, schema] of schemas) {
|
|
1284
|
-
const rows = await all(`SELECT COUNT(*) as count FROM ${schema.tableName} WHERE did = $1`, did);
|
|
1452
|
+
const rows = await all(`SELECT COUNT(*) as count FROM ${schema.tableName} WHERE did = $1`, [did]);
|
|
1285
1453
|
total += Number(rows[0]?.count || 0);
|
|
1286
1454
|
}
|
|
1287
1455
|
return total;
|
|
@@ -1289,17 +1457,17 @@ export async function getAccountRecordCount(did) {
|
|
|
1289
1457
|
export async function getAllRecordUrisForDid(did) {
|
|
1290
1458
|
const uris = [];
|
|
1291
1459
|
for (const [, schema] of schemas) {
|
|
1292
|
-
const rows = await all(`SELECT uri FROM ${schema.tableName} WHERE did = $1`, did);
|
|
1460
|
+
const rows = await all(`SELECT uri FROM ${schema.tableName} WHERE did = $1`, [did]);
|
|
1293
1461
|
uris.push(...rows.map((r) => r.uri));
|
|
1294
1462
|
}
|
|
1295
1463
|
return uris;
|
|
1296
1464
|
}
|
|
1297
1465
|
export async function isTakendownDid(did) {
|
|
1298
|
-
const rows = await all(`SELECT 1 FROM _repos WHERE did = $1 AND status = 'takendown' LIMIT 1`, did);
|
|
1466
|
+
const rows = await all(`SELECT 1 FROM _repos WHERE did = $1 AND status = 'takendown' LIMIT 1`, [did]);
|
|
1299
1467
|
return rows.length > 0;
|
|
1300
1468
|
}
|
|
1301
1469
|
export async function getPreferences(did) {
|
|
1302
|
-
const rows = await all(`SELECT key, value FROM _preferences WHERE did = $1`, did);
|
|
1470
|
+
const rows = await all(`SELECT key, value FROM _preferences WHERE did = $1`, [did]);
|
|
1303
1471
|
const prefs = {};
|
|
1304
1472
|
for (const row of rows) {
|
|
1305
1473
|
try {
|
|
@@ -1312,50 +1480,60 @@ export async function getPreferences(did) {
|
|
|
1312
1480
|
return prefs;
|
|
1313
1481
|
}
|
|
1314
1482
|
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)`,
|
|
1483
|
+
await run(`INSERT OR REPLACE INTO _preferences (did, key, value, updated_at) VALUES ($1, $2, $3, $4)`, [
|
|
1484
|
+
did,
|
|
1485
|
+
key,
|
|
1486
|
+
JSON.stringify(value),
|
|
1487
|
+
new Date().toISOString(),
|
|
1488
|
+
]);
|
|
1316
1489
|
}
|
|
1317
1490
|
export async function filterTakendownDids(dids) {
|
|
1318
1491
|
if (dids.length === 0)
|
|
1319
1492
|
return new Set();
|
|
1320
1493
|
const placeholders = dids.map((_, i) => `$${i + 1}`).join(',');
|
|
1321
|
-
const rows = await all(`SELECT did FROM _repos WHERE did IN (${placeholders}) AND status = 'takendown'`,
|
|
1494
|
+
const rows = await all(`SELECT did FROM _repos WHERE did IN (${placeholders}) AND status = 'takendown'`, dids);
|
|
1322
1495
|
return new Set(rows.map((r) => r.did));
|
|
1323
1496
|
}
|
|
1324
|
-
export async function
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
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
|
-
}
|
|
1497
|
+
export async function insertReport(report) {
|
|
1498
|
+
const createdAt = new Date().toISOString();
|
|
1499
|
+
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]);
|
|
1500
|
+
return { id: rows[0].id };
|
|
1501
|
+
}
|
|
1502
|
+
export async function queryReports(opts) {
|
|
1503
|
+
const conditions = [];
|
|
1504
|
+
const params = [];
|
|
1505
|
+
let idx = 1;
|
|
1506
|
+
if (opts.status) {
|
|
1507
|
+
conditions.push(`r.status = $${idx++}`);
|
|
1508
|
+
params.push(opts.status);
|
|
1360
1509
|
}
|
|
1510
|
+
if (opts.label) {
|
|
1511
|
+
conditions.push(`r.label = $${idx++}`);
|
|
1512
|
+
params.push(opts.label);
|
|
1513
|
+
}
|
|
1514
|
+
const where = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
1515
|
+
const limit = opts.limit || 50;
|
|
1516
|
+
const offset = opts.offset || 0;
|
|
1517
|
+
const countRows = await all(`SELECT ${dialect.countAsInteger} as count FROM _reports r ${where}`, params);
|
|
1518
|
+
const total = Number(countRows[0]?.count || 0);
|
|
1519
|
+
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]);
|
|
1520
|
+
return { reports: rows, total };
|
|
1521
|
+
}
|
|
1522
|
+
export async function resolveReport(id, action, resolvedBy) {
|
|
1523
|
+
const rows = await all(`SELECT subject_uri, label, status FROM _reports WHERE id = $1`, [id]);
|
|
1524
|
+
if (!rows.length)
|
|
1525
|
+
return null;
|
|
1526
|
+
if (rows[0].status !== 'open')
|
|
1527
|
+
return null;
|
|
1528
|
+
await run(`UPDATE _reports SET status = $1, resolved_by = $2, resolved_at = $3 WHERE id = $4`, [
|
|
1529
|
+
action,
|
|
1530
|
+
resolvedBy,
|
|
1531
|
+
new Date().toISOString(),
|
|
1532
|
+
id,
|
|
1533
|
+
]);
|
|
1534
|
+
return { subjectUri: rows[0].subject_uri, label: rows[0].label };
|
|
1535
|
+
}
|
|
1536
|
+
export async function getOpenReportCount() {
|
|
1537
|
+
const rows = await all(`SELECT ${dialect.countAsInteger} as count FROM _reports WHERE status = 'open'`);
|
|
1538
|
+
return Number(rows[0]?.count || 0);
|
|
1361
1539
|
}
|