@super-line/store-sqlite 0.1.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/LICENSE +21 -0
- package/dist/index.cjs +93 -0
- package/dist/index.d.cts +18 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +58 -0
- package/package.json +58 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mert
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
sqliteStoreServer: () => sqliteStoreServer
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
var import_better_sqlite3 = __toESM(require("better-sqlite3"), 1);
|
|
37
|
+
var import_core = require("@super-line/core");
|
|
38
|
+
function sqliteStoreServer(opts) {
|
|
39
|
+
const table = opts.table ?? "resources";
|
|
40
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(table)) throw new Error(`Invalid table name: ${table}`);
|
|
41
|
+
const db = new import_better_sqlite3.default(opts.file);
|
|
42
|
+
db.pragma("journal_mode = WAL");
|
|
43
|
+
db.pragma("synchronous = NORMAL");
|
|
44
|
+
db.exec(`CREATE TABLE IF NOT EXISTS "${table}" (id TEXT PRIMARY KEY, data TEXT NOT NULL, access TEXT NOT NULL)`);
|
|
45
|
+
const stmt = {
|
|
46
|
+
get: db.prepare(`SELECT data, access FROM "${table}" WHERE id = ?`),
|
|
47
|
+
has: db.prepare(`SELECT 1 FROM "${table}" WHERE id = ?`),
|
|
48
|
+
insert: db.prepare(`INSERT INTO "${table}" (id, data, access) VALUES (?, ?, ?)`),
|
|
49
|
+
setData: db.prepare(`UPDATE "${table}" SET data = ? WHERE id = ?`),
|
|
50
|
+
setAccess: db.prepare(`UPDATE "${table}" SET access = ? WHERE id = ?`),
|
|
51
|
+
delete: db.prepare(`DELETE FROM "${table}" WHERE id = ?`),
|
|
52
|
+
list: db.prepare(`SELECT id FROM "${table}"`)
|
|
53
|
+
};
|
|
54
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
55
|
+
return {
|
|
56
|
+
clustering: "relay",
|
|
57
|
+
read(id) {
|
|
58
|
+
const row = stmt.get.get(id);
|
|
59
|
+
if (!row) return void 0;
|
|
60
|
+
return { id, accessRules: JSON.parse(row.access), data: JSON.parse(row.data) };
|
|
61
|
+
},
|
|
62
|
+
create(id, data, accessRules) {
|
|
63
|
+
if (stmt.has.get(id)) throw new import_core.SuperLineError("CONFLICT", `Resource already exists: ${id}`);
|
|
64
|
+
stmt.insert.run(id, JSON.stringify(data ?? null), JSON.stringify(accessRules));
|
|
65
|
+
},
|
|
66
|
+
apply(change) {
|
|
67
|
+
const res = stmt.setData.run(JSON.stringify(change.update ?? null), change.id);
|
|
68
|
+
if (res.changes === 0) throw new import_core.SuperLineError("NOT_FOUND", `No resource: ${change.id}`);
|
|
69
|
+
for (const cb of listeners) cb(change);
|
|
70
|
+
},
|
|
71
|
+
setAccess(id, accessRules) {
|
|
72
|
+
const res = stmt.setAccess.run(JSON.stringify(accessRules), id);
|
|
73
|
+
if (res.changes === 0) throw new import_core.SuperLineError("NOT_FOUND", `No resource: ${id}`);
|
|
74
|
+
},
|
|
75
|
+
delete(id) {
|
|
76
|
+
stmt.delete.run(id);
|
|
77
|
+
},
|
|
78
|
+
list() {
|
|
79
|
+
return stmt.list.all().map((r) => r.id);
|
|
80
|
+
},
|
|
81
|
+
onChange(cb) {
|
|
82
|
+
listeners.add(cb);
|
|
83
|
+
return () => listeners.delete(cb);
|
|
84
|
+
},
|
|
85
|
+
close() {
|
|
86
|
+
db.close();
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
91
|
+
0 && (module.exports = {
|
|
92
|
+
sqliteStoreServer
|
|
93
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ServerStore } from '@super-line/core';
|
|
2
|
+
|
|
3
|
+
/** Options for {@link sqliteStoreServer}. */
|
|
4
|
+
interface SqliteStoreOptions {
|
|
5
|
+
/** Path to the SQLite database file (use `:memory:` for an ephemeral store). */
|
|
6
|
+
file: string;
|
|
7
|
+
/** Table this store owns; defaults to `resources`. Use distinct tables to share one file across stores. */
|
|
8
|
+
table?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The durable, last-writer-wins **server half** — {@link "@super-line/store-memory"}'s `memoryStoreServer`,
|
|
12
|
+
* but backed by SQLite (better-sqlite3) so Resources survive a restart. A write replaces the whole `data`.
|
|
13
|
+
* `clustering: 'relay'` — it does no networking; super-line core relays its Changes across nodes and feeds
|
|
14
|
+
* remote Changes back in via {@link ServerStore.apply}. Pair it with `memoryStoreClient()` on the client.
|
|
15
|
+
*/
|
|
16
|
+
declare function sqliteStoreServer(opts: SqliteStoreOptions): ServerStore;
|
|
17
|
+
|
|
18
|
+
export { type SqliteStoreOptions, sqliteStoreServer };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ServerStore } from '@super-line/core';
|
|
2
|
+
|
|
3
|
+
/** Options for {@link sqliteStoreServer}. */
|
|
4
|
+
interface SqliteStoreOptions {
|
|
5
|
+
/** Path to the SQLite database file (use `:memory:` for an ephemeral store). */
|
|
6
|
+
file: string;
|
|
7
|
+
/** Table this store owns; defaults to `resources`. Use distinct tables to share one file across stores. */
|
|
8
|
+
table?: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The durable, last-writer-wins **server half** — {@link "@super-line/store-memory"}'s `memoryStoreServer`,
|
|
12
|
+
* but backed by SQLite (better-sqlite3) so Resources survive a restart. A write replaces the whole `data`.
|
|
13
|
+
* `clustering: 'relay'` — it does no networking; super-line core relays its Changes across nodes and feeds
|
|
14
|
+
* remote Changes back in via {@link ServerStore.apply}. Pair it with `memoryStoreClient()` on the client.
|
|
15
|
+
*/
|
|
16
|
+
declare function sqliteStoreServer(opts: SqliteStoreOptions): ServerStore;
|
|
17
|
+
|
|
18
|
+
export { type SqliteStoreOptions, sqliteStoreServer };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import Database from "better-sqlite3";
|
|
3
|
+
import { SuperLineError } from "@super-line/core";
|
|
4
|
+
function sqliteStoreServer(opts) {
|
|
5
|
+
const table = opts.table ?? "resources";
|
|
6
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(table)) throw new Error(`Invalid table name: ${table}`);
|
|
7
|
+
const db = new Database(opts.file);
|
|
8
|
+
db.pragma("journal_mode = WAL");
|
|
9
|
+
db.pragma("synchronous = NORMAL");
|
|
10
|
+
db.exec(`CREATE TABLE IF NOT EXISTS "${table}" (id TEXT PRIMARY KEY, data TEXT NOT NULL, access TEXT NOT NULL)`);
|
|
11
|
+
const stmt = {
|
|
12
|
+
get: db.prepare(`SELECT data, access FROM "${table}" WHERE id = ?`),
|
|
13
|
+
has: db.prepare(`SELECT 1 FROM "${table}" WHERE id = ?`),
|
|
14
|
+
insert: db.prepare(`INSERT INTO "${table}" (id, data, access) VALUES (?, ?, ?)`),
|
|
15
|
+
setData: db.prepare(`UPDATE "${table}" SET data = ? WHERE id = ?`),
|
|
16
|
+
setAccess: db.prepare(`UPDATE "${table}" SET access = ? WHERE id = ?`),
|
|
17
|
+
delete: db.prepare(`DELETE FROM "${table}" WHERE id = ?`),
|
|
18
|
+
list: db.prepare(`SELECT id FROM "${table}"`)
|
|
19
|
+
};
|
|
20
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
21
|
+
return {
|
|
22
|
+
clustering: "relay",
|
|
23
|
+
read(id) {
|
|
24
|
+
const row = stmt.get.get(id);
|
|
25
|
+
if (!row) return void 0;
|
|
26
|
+
return { id, accessRules: JSON.parse(row.access), data: JSON.parse(row.data) };
|
|
27
|
+
},
|
|
28
|
+
create(id, data, accessRules) {
|
|
29
|
+
if (stmt.has.get(id)) throw new SuperLineError("CONFLICT", `Resource already exists: ${id}`);
|
|
30
|
+
stmt.insert.run(id, JSON.stringify(data ?? null), JSON.stringify(accessRules));
|
|
31
|
+
},
|
|
32
|
+
apply(change) {
|
|
33
|
+
const res = stmt.setData.run(JSON.stringify(change.update ?? null), change.id);
|
|
34
|
+
if (res.changes === 0) throw new SuperLineError("NOT_FOUND", `No resource: ${change.id}`);
|
|
35
|
+
for (const cb of listeners) cb(change);
|
|
36
|
+
},
|
|
37
|
+
setAccess(id, accessRules) {
|
|
38
|
+
const res = stmt.setAccess.run(JSON.stringify(accessRules), id);
|
|
39
|
+
if (res.changes === 0) throw new SuperLineError("NOT_FOUND", `No resource: ${id}`);
|
|
40
|
+
},
|
|
41
|
+
delete(id) {
|
|
42
|
+
stmt.delete.run(id);
|
|
43
|
+
},
|
|
44
|
+
list() {
|
|
45
|
+
return stmt.list.all().map((r) => r.id);
|
|
46
|
+
},
|
|
47
|
+
onChange(cb) {
|
|
48
|
+
listeners.add(cb);
|
|
49
|
+
return () => listeners.delete(cb);
|
|
50
|
+
},
|
|
51
|
+
close() {
|
|
52
|
+
db.close();
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export {
|
|
57
|
+
sqliteStoreServer
|
|
58
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@super-line/store-sqlite",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Durable last-writer-wins Store for super-line — SQLite-backed server persistence (better-sqlite3).",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Mert",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"super-line",
|
|
10
|
+
"store",
|
|
11
|
+
"sqlite",
|
|
12
|
+
"persistence",
|
|
13
|
+
"durable",
|
|
14
|
+
"lww"
|
|
15
|
+
],
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/mertdogar/super-line.git",
|
|
19
|
+
"directory": "packages/store-sqlite"
|
|
20
|
+
},
|
|
21
|
+
"homepage": "https://mertdogar.github.io/super-line/",
|
|
22
|
+
"bugs": "https://github.com/mertdogar/super-line/issues",
|
|
23
|
+
"main": "./dist/index.cjs",
|
|
24
|
+
"module": "./dist/index.js",
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"import": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"require": {
|
|
33
|
+
"types": "./dist/index.d.cts",
|
|
34
|
+
"default": "./dist/index.cjs"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"files": [
|
|
39
|
+
"dist"
|
|
40
|
+
],
|
|
41
|
+
"sideEffects": false,
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=18"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"better-sqlite3": "^11.8.1",
|
|
50
|
+
"@super-line/core": "^0.5.0"
|
|
51
|
+
},
|
|
52
|
+
"devDependencies": {
|
|
53
|
+
"@types/better-sqlite3": "^7.6.12"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsup"
|
|
57
|
+
}
|
|
58
|
+
}
|