@super-line/store-sqlite 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +42 -3
- package/dist/index.js +43 -4
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -35,6 +35,7 @@ __export(index_exports, {
|
|
|
35
35
|
module.exports = __toCommonJS(index_exports);
|
|
36
36
|
var import_better_sqlite3 = __toESM(require("better-sqlite3"), 1);
|
|
37
37
|
var import_core = require("@super-line/core");
|
|
38
|
+
var SERVER_ORIGIN = "server";
|
|
38
39
|
function sqliteStoreServer(opts) {
|
|
39
40
|
const table = opts.table ?? "resources";
|
|
40
41
|
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(table)) throw new Error(`Invalid table name: ${table}`);
|
|
@@ -52,6 +53,15 @@ function sqliteStoreServer(opts) {
|
|
|
52
53
|
list: db.prepare(`SELECT id FROM "${table}"`)
|
|
53
54
|
};
|
|
54
55
|
const listeners = /* @__PURE__ */ new Set();
|
|
56
|
+
const readData = (id) => {
|
|
57
|
+
const row = stmt.get.get(id);
|
|
58
|
+
return row ? JSON.parse(row.data) : void 0;
|
|
59
|
+
};
|
|
60
|
+
const commit = (change) => {
|
|
61
|
+
const res = stmt.setData.run(JSON.stringify(change.update ?? null), change.id);
|
|
62
|
+
if (res.changes === 0) throw new import_core.SuperLineError("NOT_FOUND", `No resource: ${change.id}`);
|
|
63
|
+
for (const cb of listeners) cb(change);
|
|
64
|
+
};
|
|
55
65
|
return {
|
|
56
66
|
clustering: "relay",
|
|
57
67
|
read(id) {
|
|
@@ -64,9 +74,38 @@ function sqliteStoreServer(opts) {
|
|
|
64
74
|
stmt.insert.run(id, JSON.stringify(data ?? null), JSON.stringify(accessRules));
|
|
65
75
|
},
|
|
66
76
|
apply(change) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
77
|
+
commit(change);
|
|
78
|
+
},
|
|
79
|
+
open(id, openOpts) {
|
|
80
|
+
if (!stmt.has.get(id)) throw new import_core.SuperLineError("NOT_FOUND", `No resource: ${id}`);
|
|
81
|
+
const origin = openOpts?.origin ?? SERVER_ORIGIN;
|
|
82
|
+
const subs = /* @__PURE__ */ new Set();
|
|
83
|
+
return {
|
|
84
|
+
getSnapshot: () => readData(id),
|
|
85
|
+
subscribe: (cb) => {
|
|
86
|
+
const wrap = (c) => {
|
|
87
|
+
if (c.id === id) cb();
|
|
88
|
+
};
|
|
89
|
+
listeners.add(wrap);
|
|
90
|
+
const off = () => void listeners.delete(wrap);
|
|
91
|
+
subs.add(off);
|
|
92
|
+
return () => {
|
|
93
|
+
off();
|
|
94
|
+
subs.delete(off);
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
set: (data) => commit({ id, update: data, origin }),
|
|
98
|
+
update: (partial) => {
|
|
99
|
+
const base = readData(id);
|
|
100
|
+
const merged = typeof base === "object" && base !== null ? { ...base, ...partial } : partial;
|
|
101
|
+
commit({ id, update: merged, origin });
|
|
102
|
+
},
|
|
103
|
+
delete: (path) => commit({ id, update: (0, import_core.removeAtPath)(readData(id), path), origin }),
|
|
104
|
+
close: () => {
|
|
105
|
+
for (const off of subs) off();
|
|
106
|
+
subs.clear();
|
|
107
|
+
}
|
|
108
|
+
};
|
|
70
109
|
},
|
|
71
110
|
setAccess(id, accessRules) {
|
|
72
111
|
const res = stmt.setAccess.run(JSON.stringify(accessRules), id);
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import Database from "better-sqlite3";
|
|
3
|
-
import { SuperLineError } from "@super-line/core";
|
|
3
|
+
import { SuperLineError, removeAtPath } from "@super-line/core";
|
|
4
|
+
var SERVER_ORIGIN = "server";
|
|
4
5
|
function sqliteStoreServer(opts) {
|
|
5
6
|
const table = opts.table ?? "resources";
|
|
6
7
|
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(table)) throw new Error(`Invalid table name: ${table}`);
|
|
@@ -18,6 +19,15 @@ function sqliteStoreServer(opts) {
|
|
|
18
19
|
list: db.prepare(`SELECT id FROM "${table}"`)
|
|
19
20
|
};
|
|
20
21
|
const listeners = /* @__PURE__ */ new Set();
|
|
22
|
+
const readData = (id) => {
|
|
23
|
+
const row = stmt.get.get(id);
|
|
24
|
+
return row ? JSON.parse(row.data) : void 0;
|
|
25
|
+
};
|
|
26
|
+
const commit = (change) => {
|
|
27
|
+
const res = stmt.setData.run(JSON.stringify(change.update ?? null), change.id);
|
|
28
|
+
if (res.changes === 0) throw new SuperLineError("NOT_FOUND", `No resource: ${change.id}`);
|
|
29
|
+
for (const cb of listeners) cb(change);
|
|
30
|
+
};
|
|
21
31
|
return {
|
|
22
32
|
clustering: "relay",
|
|
23
33
|
read(id) {
|
|
@@ -30,9 +40,38 @@ function sqliteStoreServer(opts) {
|
|
|
30
40
|
stmt.insert.run(id, JSON.stringify(data ?? null), JSON.stringify(accessRules));
|
|
31
41
|
},
|
|
32
42
|
apply(change) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
43
|
+
commit(change);
|
|
44
|
+
},
|
|
45
|
+
open(id, openOpts) {
|
|
46
|
+
if (!stmt.has.get(id)) throw new SuperLineError("NOT_FOUND", `No resource: ${id}`);
|
|
47
|
+
const origin = openOpts?.origin ?? SERVER_ORIGIN;
|
|
48
|
+
const subs = /* @__PURE__ */ new Set();
|
|
49
|
+
return {
|
|
50
|
+
getSnapshot: () => readData(id),
|
|
51
|
+
subscribe: (cb) => {
|
|
52
|
+
const wrap = (c) => {
|
|
53
|
+
if (c.id === id) cb();
|
|
54
|
+
};
|
|
55
|
+
listeners.add(wrap);
|
|
56
|
+
const off = () => void listeners.delete(wrap);
|
|
57
|
+
subs.add(off);
|
|
58
|
+
return () => {
|
|
59
|
+
off();
|
|
60
|
+
subs.delete(off);
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
set: (data) => commit({ id, update: data, origin }),
|
|
64
|
+
update: (partial) => {
|
|
65
|
+
const base = readData(id);
|
|
66
|
+
const merged = typeof base === "object" && base !== null ? { ...base, ...partial } : partial;
|
|
67
|
+
commit({ id, update: merged, origin });
|
|
68
|
+
},
|
|
69
|
+
delete: (path) => commit({ id, update: removeAtPath(readData(id), path), origin }),
|
|
70
|
+
close: () => {
|
|
71
|
+
for (const off of subs) off();
|
|
72
|
+
subs.clear();
|
|
73
|
+
}
|
|
74
|
+
};
|
|
36
75
|
},
|
|
37
76
|
setAccess(id, accessRules) {
|
|
38
77
|
const res = stmt.setAccess.run(JSON.stringify(accessRules), id);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@super-line/store-sqlite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Durable last-writer-wins Store for super-line — SQLite-backed server persistence (better-sqlite3).",
|
|
6
6
|
"license": "MIT",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
49
|
"better-sqlite3": "^11.8.1",
|
|
50
|
-
"@super-line/core": "^0.
|
|
50
|
+
"@super-line/core": "^0.6.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/better-sqlite3": "^7.6.12"
|