latticesql 1.6.1 → 1.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/postgres-worker.js +72 -0
- package/package.json +1 -1
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
// src/db/postgres-worker.ts
|
|
4
|
+
var import_synckit = require("synckit");
|
|
5
|
+
var { Client } = require("pg");
|
|
6
|
+
var client = null;
|
|
7
|
+
function ensureClient() {
|
|
8
|
+
if (!client) throw new Error("PostgresAdapter worker: client not opened");
|
|
9
|
+
return client;
|
|
10
|
+
}
|
|
11
|
+
(0, import_synckit.runAsWorker)(async (action) => {
|
|
12
|
+
try {
|
|
13
|
+
switch (action.type) {
|
|
14
|
+
case "open": {
|
|
15
|
+
if (client) return { ok: true };
|
|
16
|
+
client = new Client({ connectionString: action.connectionString });
|
|
17
|
+
await client.connect();
|
|
18
|
+
try {
|
|
19
|
+
await client.query("CREATE EXTENSION IF NOT EXISTS pgcrypto");
|
|
20
|
+
} catch (extErr) {
|
|
21
|
+
console.warn(
|
|
22
|
+
"[PostgresAdapter] CREATE EXTENSION pgcrypto failed (may already be enabled by your provider):",
|
|
23
|
+
extErr instanceof Error ? extErr.message : extErr
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
return { ok: true };
|
|
27
|
+
}
|
|
28
|
+
case "close": {
|
|
29
|
+
if (client) {
|
|
30
|
+
await client.end();
|
|
31
|
+
client = null;
|
|
32
|
+
}
|
|
33
|
+
return { ok: true };
|
|
34
|
+
}
|
|
35
|
+
case "run": {
|
|
36
|
+
const r = await ensureClient().query(action.sql, action.params);
|
|
37
|
+
return { ok: true, rowCount: r.rowCount ?? 0 };
|
|
38
|
+
}
|
|
39
|
+
case "get": {
|
|
40
|
+
const r = await ensureClient().query(action.sql, action.params);
|
|
41
|
+
return { ok: true, rows: r.rows.slice(0, 1) };
|
|
42
|
+
}
|
|
43
|
+
case "all": {
|
|
44
|
+
const r = await ensureClient().query(action.sql, action.params);
|
|
45
|
+
return { ok: true, rows: r.rows };
|
|
46
|
+
}
|
|
47
|
+
case "introspectColumns": {
|
|
48
|
+
const r = await ensureClient().query(
|
|
49
|
+
`SELECT column_name FROM information_schema.columns
|
|
50
|
+
WHERE table_schema = current_schema() AND table_name = $1
|
|
51
|
+
ORDER BY ordinal_position`,
|
|
52
|
+
[action.table]
|
|
53
|
+
);
|
|
54
|
+
return { ok: true, rows: r.rows };
|
|
55
|
+
}
|
|
56
|
+
case "addColumn": {
|
|
57
|
+
const upper = action.typeSpec.toUpperCase();
|
|
58
|
+
if (upper.includes("PRIMARY KEY")) return { ok: true };
|
|
59
|
+
const translated = translateTypeSpec(action.typeSpec);
|
|
60
|
+
await ensureClient().query(
|
|
61
|
+
`ALTER TABLE "${action.table}" ADD COLUMN IF NOT EXISTS "${action.column}" ${translated}`
|
|
62
|
+
);
|
|
63
|
+
return { ok: true };
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
} catch (err) {
|
|
67
|
+
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
function translateTypeSpec(typeSpec) {
|
|
71
|
+
return typeSpec.replace(/\bBLOB\b/gi, "BYTEA").replace(/\bdatetime\(\s*'now'\s*\)/gi, "NOW()").replace(/\bRANDOM\(\)/gi, "random()");
|
|
72
|
+
}
|
package/package.json
CHANGED