@fedify/sqlite 2.0.0-dev.1604 → 2.0.0-dev.161
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 +1 -1
- package/README.md +3 -3
- package/deno.json +2 -3
- package/dist/_virtual/{rolldown_runtime.js → rolldown_runtime.cjs} +9 -6
- package/dist/adapter.d.cts +45 -0
- package/dist/dist/sqlite.node.d.cts +2 -0
- package/dist/kv.cjs +218 -0
- package/dist/kv.d.cts +81 -0
- package/dist/kv.d.ts +6 -1
- package/dist/kv.js +41 -12
- package/dist/mod.cjs +6 -0
- package/dist/mod.d.cts +2 -0
- package/dist/mod.js +2 -2
- package/dist/sqlite.bun.cjs +42 -0
- package/dist/sqlite.bun.d.cts +23 -0
- package/dist/sqlite.bun.js +2 -2
- package/dist/sqlite.node.cjs +44 -0
- package/dist/sqlite.node.d.cts +23 -0
- package/dist/sqlite.node.js +2 -2
- package/package.json +18 -8
- package/src/kv.test.ts +96 -3
- package/src/kv.ts +56 -7
- package/tsdown.config.ts +12 -4
- package/dist/node_modules/.pnpm/@js-temporal_polyfill@0.5.1/node_modules/@js-temporal/polyfill/dist/index.esm.js +0 -5795
- package/dist/node_modules/.pnpm/jsbi@4.3.2/node_modules/jsbi/dist/jsbi-cjs.js +0 -1139
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -6,12 +6,12 @@
|
|
|
6
6
|
|
|
7
7
|
This package provides a SQLite-based [`KvStore`] implementation for [Fedify].
|
|
8
8
|
|
|
9
|
-
[JSR]: https://jsr.io/@fedify/sqlite
|
|
10
9
|
[JSR badge]: https://jsr.io/badges/@fedify/sqlite
|
|
11
|
-
[
|
|
10
|
+
[JSR]: https://jsr.io/@fedify/sqlite
|
|
12
11
|
[npm badge]: https://img.shields.io/npm/v/@fedify/sqlite?logo=npm
|
|
13
|
-
[
|
|
12
|
+
[npm]: https://www.npmjs.com/package/@fedify/sqlite
|
|
14
13
|
[`KvStore`]: https://jsr.io/@fedify/fedify/doc/federation/~/KvStore
|
|
14
|
+
[Fedify]: https://fedify.dev/
|
|
15
15
|
|
|
16
16
|
|
|
17
17
|
Usage
|
package/deno.json
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fedify/sqlite",
|
|
3
|
-
"version": "2.0.0-dev.
|
|
3
|
+
"version": "2.0.0-dev.161+b505ad7a",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./src/mod.ts",
|
|
7
7
|
"./kv": "./src/kv.ts"
|
|
8
8
|
},
|
|
9
9
|
"imports": {
|
|
10
|
-
"@fedify/sqlite": "./src/mod.ts",
|
|
11
|
-
"@fedify/sqlite/": "./src/",
|
|
12
10
|
"#sqlite": "./src/sqlite.node.ts"
|
|
13
11
|
},
|
|
14
12
|
"exclude": [
|
|
@@ -18,6 +16,7 @@
|
|
|
18
16
|
],
|
|
19
17
|
"publish": {
|
|
20
18
|
"exclude": [
|
|
19
|
+
"**/*.test.ts",
|
|
21
20
|
"!dist/"
|
|
22
21
|
]
|
|
23
22
|
},
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
const { Temporal } = require("@js-temporal/polyfill");
|
|
3
|
+
|
|
4
4
|
//#region rolldown:runtime
|
|
5
5
|
var __create = Object.create;
|
|
6
6
|
var __defProp = Object.defineProperty;
|
|
@@ -8,9 +8,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
8
8
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
9
9
|
var __getProtoOf = Object.getPrototypeOf;
|
|
10
10
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
11
|
-
var __commonJS = (cb, mod) => function() {
|
|
12
|
-
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
|
|
13
|
-
};
|
|
14
11
|
var __copyProps = (to, from, except, desc) => {
|
|
15
12
|
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
16
13
|
key = keys[i];
|
|
@@ -27,4 +24,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
27
24
|
}) : target, mod));
|
|
28
25
|
|
|
29
26
|
//#endregion
|
|
30
|
-
|
|
27
|
+
|
|
28
|
+
Object.defineProperty(exports, '__toESM', {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
get: function () {
|
|
31
|
+
return __toESM;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
//#region src/adapter.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* SQLite database adapter.
|
|
4
|
+
*
|
|
5
|
+
* An abstract interface for SQLite database for different runtime environments.
|
|
6
|
+
*/
|
|
7
|
+
interface SqliteDatabaseAdapter {
|
|
8
|
+
/**
|
|
9
|
+
* Prepares a SQL statement.
|
|
10
|
+
* @param sql - The SQL statement to prepare.
|
|
11
|
+
*/
|
|
12
|
+
prepare(sql: string): SqliteStatementAdapter;
|
|
13
|
+
/**
|
|
14
|
+
* Executes a SQL statement.
|
|
15
|
+
* @param sql - The SQL statement to execute.
|
|
16
|
+
*/
|
|
17
|
+
exec(sql: string): void;
|
|
18
|
+
/**
|
|
19
|
+
* Closes the database connection.
|
|
20
|
+
*/
|
|
21
|
+
close(): void;
|
|
22
|
+
}
|
|
23
|
+
interface SqliteStatementAdapter {
|
|
24
|
+
/**
|
|
25
|
+
* Executes a SQL statement and returns the number of changes made to the database.
|
|
26
|
+
* @param params - The parameters to bind to the SQL statement.
|
|
27
|
+
*/
|
|
28
|
+
run(...params: unknown[]): {
|
|
29
|
+
changes: number;
|
|
30
|
+
lastInsertRowid: number;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Executes a SQL statement and returns the first row of the result set.
|
|
34
|
+
* @param params - The parameters to bind to the SQL statement.
|
|
35
|
+
* @returns The first row of the result set, or `undefined` if the result set is empty.
|
|
36
|
+
*/
|
|
37
|
+
get(...params: unknown[]): unknown | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Executes a SQL statement and returns all rows of the result set.
|
|
40
|
+
* @param params - The parameters to bind to the SQL statement.
|
|
41
|
+
*/
|
|
42
|
+
all(...params: unknown[]): unknown[];
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
export { SqliteDatabaseAdapter, SqliteStatementAdapter };
|
package/dist/kv.cjs
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
|
|
2
|
+
const { Temporal } = require("@js-temporal/polyfill");
|
|
3
|
+
|
|
4
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
5
|
+
const __sqlite = require_rolldown_runtime.__toESM(require("#sqlite"));
|
|
6
|
+
const __logtape_logtape = require_rolldown_runtime.__toESM(require("@logtape/logtape"));
|
|
7
|
+
const es_toolkit = require_rolldown_runtime.__toESM(require("es-toolkit"));
|
|
8
|
+
|
|
9
|
+
//#region src/kv.ts
|
|
10
|
+
const logger = (0, __logtape_logtape.getLogger)([
|
|
11
|
+
"fedify",
|
|
12
|
+
"sqlite",
|
|
13
|
+
"kv"
|
|
14
|
+
]);
|
|
15
|
+
/**
|
|
16
|
+
* A key–value store that uses SQLite as the underlying storage.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { createFederation } from "@fedify/fedify";
|
|
21
|
+
* import { SqliteKvStore } from "@fedify/sqlite";
|
|
22
|
+
* import { DatabaseSync } from "node:sqlite";
|
|
23
|
+
*
|
|
24
|
+
* const db = new DatabaseSync(":memory:");
|
|
25
|
+
* const federation = createFederation({
|
|
26
|
+
* // ...
|
|
27
|
+
* kv: new SqliteKvStore(db),
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
var SqliteKvStore = class SqliteKvStore {
|
|
32
|
+
static #defaultTableName = "fedify_kv";
|
|
33
|
+
static #tableNameRegex = /^[A-Za-z_][A-Za-z0-9_]{0,63}$/;
|
|
34
|
+
#db;
|
|
35
|
+
#tableName;
|
|
36
|
+
#initialized;
|
|
37
|
+
/**
|
|
38
|
+
* Creates a new SQLite key–value store.
|
|
39
|
+
* @param db The SQLite database to use. Supports `node:sqlite` and `bun:sqlite`.
|
|
40
|
+
* @param options The options for the key–value store.
|
|
41
|
+
*/
|
|
42
|
+
constructor(db, options = {}) {
|
|
43
|
+
this.db = db;
|
|
44
|
+
this.options = options;
|
|
45
|
+
this.#db = new __sqlite.SqliteDatabase(db);
|
|
46
|
+
this.#initialized = options.initialized ?? false;
|
|
47
|
+
this.#tableName = options.tableName ?? SqliteKvStore.#defaultTableName;
|
|
48
|
+
if (!SqliteKvStore.#tableNameRegex.test(this.#tableName)) throw new Error(`Invalid table name for the key–value store: ${this.#tableName}`);
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* {@inheritDoc KvStore.get}
|
|
52
|
+
*/
|
|
53
|
+
async get(key) {
|
|
54
|
+
this.initialize();
|
|
55
|
+
const encodedKey = this.#encodeKey(key);
|
|
56
|
+
const now = Temporal.Now.instant().epochMilliseconds;
|
|
57
|
+
const result = this.#db.prepare(`
|
|
58
|
+
SELECT value
|
|
59
|
+
FROM "${this.#tableName}"
|
|
60
|
+
WHERE key = ? AND (expires IS NULL OR expires > ?)
|
|
61
|
+
`).get(encodedKey, now);
|
|
62
|
+
if (!result) return void 0;
|
|
63
|
+
return this.#decodeValue(result.value);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* {@inheritDoc KvStore.set}
|
|
67
|
+
*/
|
|
68
|
+
async set(key, value, options) {
|
|
69
|
+
this.initialize();
|
|
70
|
+
if (value === void 0) return;
|
|
71
|
+
const encodedKey = this.#encodeKey(key);
|
|
72
|
+
const encodedValue = this.#encodeValue(value);
|
|
73
|
+
const now = Temporal.Now.instant().epochMilliseconds;
|
|
74
|
+
const expiresAt = options?.ttl !== void 0 ? now + options.ttl.total({ unit: "milliseconds" }) : null;
|
|
75
|
+
this.#db.prepare(`INSERT INTO "${this.#tableName}" (key, value, created, expires)
|
|
76
|
+
VALUES (?, ?, ?, ?)
|
|
77
|
+
ON CONFLICT(key) DO UPDATE SET
|
|
78
|
+
value = excluded.value,
|
|
79
|
+
expires = excluded.expires`).run(encodedKey, encodedValue, now, expiresAt);
|
|
80
|
+
this.#expire();
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* {@inheritDoc KvStore.delete}
|
|
85
|
+
*/
|
|
86
|
+
async delete(key) {
|
|
87
|
+
this.initialize();
|
|
88
|
+
const encodedKey = this.#encodeKey(key);
|
|
89
|
+
this.#db.prepare(`
|
|
90
|
+
DELETE FROM "${this.#tableName}" WHERE key = ?
|
|
91
|
+
`).run(encodedKey);
|
|
92
|
+
this.#expire();
|
|
93
|
+
return Promise.resolve();
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* {@inheritDoc KvStore.cas}
|
|
97
|
+
*/
|
|
98
|
+
async cas(key, expectedValue, newValue, options) {
|
|
99
|
+
this.initialize();
|
|
100
|
+
const encodedKey = this.#encodeKey(key);
|
|
101
|
+
const now = Temporal.Now.instant().epochMilliseconds;
|
|
102
|
+
const expiresAt = options?.ttl !== void 0 ? now + options.ttl.total({ unit: "milliseconds" }) : null;
|
|
103
|
+
try {
|
|
104
|
+
this.#db.exec("BEGIN IMMEDIATE");
|
|
105
|
+
const currentResult = this.#db.prepare(`
|
|
106
|
+
SELECT value
|
|
107
|
+
FROM "${this.#tableName}"
|
|
108
|
+
WHERE key = ? AND (expires IS NULL OR expires > ?)
|
|
109
|
+
`).get(encodedKey, now);
|
|
110
|
+
const currentValue = currentResult === void 0 ? void 0 : this.#decodeValue(currentResult.value);
|
|
111
|
+
if (!(0, es_toolkit.isEqual)(currentValue, expectedValue)) {
|
|
112
|
+
this.#db.exec("ROLLBACK");
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
if (newValue === void 0) this.#db.prepare(`
|
|
116
|
+
DELETE FROM "${this.#tableName}" WHERE key = ?
|
|
117
|
+
`).run(encodedKey);
|
|
118
|
+
else {
|
|
119
|
+
const newValueJson = this.#encodeValue(newValue);
|
|
120
|
+
this.#db.prepare(`
|
|
121
|
+
INSERT INTO "${this.#tableName}" (key, value, created, expires)
|
|
122
|
+
VALUES (?, ?, ?, ?)
|
|
123
|
+
ON CONFLICT(key) DO UPDATE SET
|
|
124
|
+
value = excluded.value,
|
|
125
|
+
expires = excluded.expires
|
|
126
|
+
`).run(encodedKey, newValueJson, now, expiresAt);
|
|
127
|
+
}
|
|
128
|
+
this.#db.exec("COMMIT");
|
|
129
|
+
this.#expire();
|
|
130
|
+
return true;
|
|
131
|
+
} catch (error) {
|
|
132
|
+
this.#db.exec("ROLLBACK");
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* {@inheritDoc KvStore.list}
|
|
138
|
+
* @since 1.10.0
|
|
139
|
+
*/
|
|
140
|
+
async *list(prefix) {
|
|
141
|
+
this.initialize();
|
|
142
|
+
const now = Temporal.Now.instant().epochMilliseconds;
|
|
143
|
+
let results;
|
|
144
|
+
if (prefix == null || prefix.length === 0) results = this.#db.prepare(`
|
|
145
|
+
SELECT key, value
|
|
146
|
+
FROM "${this.#tableName}"
|
|
147
|
+
WHERE expires IS NULL OR expires > ?
|
|
148
|
+
ORDER BY key
|
|
149
|
+
`).all(now);
|
|
150
|
+
else {
|
|
151
|
+
const pattern = JSON.stringify(prefix).slice(0, -1) + ",%";
|
|
152
|
+
const exactKey = JSON.stringify(prefix);
|
|
153
|
+
results = this.#db.prepare(`
|
|
154
|
+
SELECT key, value
|
|
155
|
+
FROM "${this.#tableName}"
|
|
156
|
+
WHERE (key LIKE ? ESCAPE '\\' OR key = ?)
|
|
157
|
+
AND (expires IS NULL OR expires > ?)
|
|
158
|
+
ORDER BY key
|
|
159
|
+
`).all(pattern, exactKey, now);
|
|
160
|
+
}
|
|
161
|
+
for (const row of results) yield {
|
|
162
|
+
key: this.#decodeKey(row.key),
|
|
163
|
+
value: this.#decodeValue(row.value)
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Creates the table used by the key–value store if it does not already exist.
|
|
168
|
+
* Does nothing if the table already exists.
|
|
169
|
+
*/
|
|
170
|
+
initialize() {
|
|
171
|
+
if (this.#initialized) return;
|
|
172
|
+
logger.debug("Initializing the key–value store table {tableName}...", { tableName: this.#tableName });
|
|
173
|
+
this.#db.exec(`
|
|
174
|
+
CREATE TABLE IF NOT EXISTS "${this.#tableName}" (
|
|
175
|
+
key TEXT PRIMARY KEY,
|
|
176
|
+
value TEXT NOT NULL,
|
|
177
|
+
created INTEGER NOT NULL,
|
|
178
|
+
expires INTEGER
|
|
179
|
+
)
|
|
180
|
+
`);
|
|
181
|
+
this.#db.exec(`
|
|
182
|
+
CREATE INDEX IF NOT EXISTS "idx_${this.#tableName}_expires"
|
|
183
|
+
ON "${this.#tableName}" (expires)
|
|
184
|
+
`);
|
|
185
|
+
this.#initialized = true;
|
|
186
|
+
logger.debug("Initialized the key–value store table {tableName}.", { tableName: this.#tableName });
|
|
187
|
+
}
|
|
188
|
+
#expire() {
|
|
189
|
+
const now = Temporal.Now.instant().epochMilliseconds;
|
|
190
|
+
this.#db.prepare(`
|
|
191
|
+
DELETE FROM "${this.#tableName}"
|
|
192
|
+
WHERE expires IS NOT NULL AND expires <= ?
|
|
193
|
+
`).run(now);
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Drops the table used by the key–value store. Does nothing if the table
|
|
197
|
+
* does not exist.
|
|
198
|
+
*/
|
|
199
|
+
drop() {
|
|
200
|
+
this.#db.exec(`DROP TABLE IF EXISTS "${this.#tableName}"`);
|
|
201
|
+
this.#initialized = false;
|
|
202
|
+
}
|
|
203
|
+
#encodeKey(key) {
|
|
204
|
+
return JSON.stringify(key);
|
|
205
|
+
}
|
|
206
|
+
#decodeKey(key) {
|
|
207
|
+
return JSON.parse(key);
|
|
208
|
+
}
|
|
209
|
+
#encodeValue(value) {
|
|
210
|
+
return JSON.stringify(value);
|
|
211
|
+
}
|
|
212
|
+
#decodeValue(value) {
|
|
213
|
+
return JSON.parse(value);
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
//#endregion
|
|
218
|
+
exports.SqliteKvStore = SqliteKvStore;
|
package/dist/kv.d.cts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { DatabaseSync } from "./dist/sqlite.node.cjs";
|
|
2
|
+
import { KvKey, KvStore, KvStoreListEntry, KvStoreSetOptions } from "@fedify/fedify";
|
|
3
|
+
|
|
4
|
+
//#region src/kv.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Options for the SQLite key–value store.
|
|
7
|
+
*/
|
|
8
|
+
interface SqliteKvStoreOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The table name to use for the key–value store.
|
|
11
|
+
* Only letters, digits, and underscores are allowed.
|
|
12
|
+
* `"fedify_kv"` by default.
|
|
13
|
+
* @default `"fedify_kv"`
|
|
14
|
+
*/
|
|
15
|
+
tableName?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Whether the table has been initialized. `false` by default.
|
|
18
|
+
* @default `false`
|
|
19
|
+
*/
|
|
20
|
+
initialized?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A key–value store that uses SQLite as the underlying storage.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { createFederation } from "@fedify/fedify";
|
|
28
|
+
* import { SqliteKvStore } from "@fedify/sqlite";
|
|
29
|
+
* import { DatabaseSync } from "node:sqlite";
|
|
30
|
+
*
|
|
31
|
+
* const db = new DatabaseSync(":memory:");
|
|
32
|
+
* const federation = createFederation({
|
|
33
|
+
* // ...
|
|
34
|
+
* kv: new SqliteKvStore(db),
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
declare class SqliteKvStore implements KvStore {
|
|
39
|
+
#private;
|
|
40
|
+
readonly db: DatabaseSync;
|
|
41
|
+
readonly options: SqliteKvStoreOptions;
|
|
42
|
+
/**
|
|
43
|
+
* Creates a new SQLite key–value store.
|
|
44
|
+
* @param db The SQLite database to use. Supports `node:sqlite` and `bun:sqlite`.
|
|
45
|
+
* @param options The options for the key–value store.
|
|
46
|
+
*/
|
|
47
|
+
constructor(db: DatabaseSync, options?: SqliteKvStoreOptions);
|
|
48
|
+
/**
|
|
49
|
+
* {@inheritDoc KvStore.get}
|
|
50
|
+
*/
|
|
51
|
+
get<T = unknown>(key: KvKey): Promise<T | undefined>;
|
|
52
|
+
/**
|
|
53
|
+
* {@inheritDoc KvStore.set}
|
|
54
|
+
*/
|
|
55
|
+
set(key: KvKey, value: unknown, options?: KvStoreSetOptions): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* {@inheritDoc KvStore.delete}
|
|
58
|
+
*/
|
|
59
|
+
delete(key: KvKey): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* {@inheritDoc KvStore.cas}
|
|
62
|
+
*/
|
|
63
|
+
cas(key: KvKey, expectedValue: unknown, newValue: unknown, options?: KvStoreSetOptions): Promise<boolean>;
|
|
64
|
+
/**
|
|
65
|
+
* {@inheritDoc KvStore.list}
|
|
66
|
+
* @since 1.10.0
|
|
67
|
+
*/
|
|
68
|
+
list(prefix?: KvKey): AsyncIterable<KvStoreListEntry>;
|
|
69
|
+
/**
|
|
70
|
+
* Creates the table used by the key–value store if it does not already exist.
|
|
71
|
+
* Does nothing if the table already exists.
|
|
72
|
+
*/
|
|
73
|
+
initialize(): void;
|
|
74
|
+
/**
|
|
75
|
+
* Drops the table used by the key–value store. Does nothing if the table
|
|
76
|
+
* does not exist.
|
|
77
|
+
*/
|
|
78
|
+
drop(): void;
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
export { SqliteKvStore, SqliteKvStoreOptions };
|
package/dist/kv.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Temporal } from "@js-temporal/polyfill";
|
|
2
2
|
import { PlatformDatabase } from "#sqlite";
|
|
3
|
-
import { KvKey, KvStore, KvStoreSetOptions } from "@fedify/fedify";
|
|
3
|
+
import { KvKey, KvStore, KvStoreListEntry, KvStoreSetOptions } from "@fedify/fedify";
|
|
4
4
|
|
|
5
5
|
//#region src/kv.d.ts
|
|
6
6
|
/**
|
|
@@ -62,6 +62,11 @@ declare class SqliteKvStore implements KvStore {
|
|
|
62
62
|
* {@inheritDoc KvStore.cas}
|
|
63
63
|
*/
|
|
64
64
|
cas(key: KvKey, expectedValue: unknown, newValue: unknown, options?: KvStoreSetOptions): Promise<boolean>;
|
|
65
|
+
/**
|
|
66
|
+
* {@inheritDoc KvStore.list}
|
|
67
|
+
* @since 1.10.0
|
|
68
|
+
*/
|
|
69
|
+
list(prefix?: KvKey): AsyncIterable<KvStoreListEntry>;
|
|
65
70
|
/**
|
|
66
71
|
* Creates the table used by the key–value store if it does not already exist.
|
|
67
72
|
* Does nothing if the table already exists.
|
package/dist/kv.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import { qi } from "./node_modules/.pnpm/@js-temporal_polyfill@0.5.1/node_modules/@js-temporal/polyfill/dist/index.esm.js";
|
|
2
|
+
import { Temporal } from "@js-temporal/polyfill";
|
|
3
|
+
|
|
5
4
|
import { SqliteDatabase } from "#sqlite";
|
|
6
5
|
import { getLogger } from "@logtape/logtape";
|
|
7
6
|
import { isEqual } from "es-toolkit";
|
|
@@ -53,10 +52,10 @@ var SqliteKvStore = class SqliteKvStore {
|
|
|
53
52
|
async get(key) {
|
|
54
53
|
this.initialize();
|
|
55
54
|
const encodedKey = this.#encodeKey(key);
|
|
56
|
-
const now =
|
|
55
|
+
const now = Temporal.Now.instant().epochMilliseconds;
|
|
57
56
|
const result = this.#db.prepare(`
|
|
58
|
-
SELECT value
|
|
59
|
-
FROM "${this.#tableName}"
|
|
57
|
+
SELECT value
|
|
58
|
+
FROM "${this.#tableName}"
|
|
60
59
|
WHERE key = ? AND (expires IS NULL OR expires > ?)
|
|
61
60
|
`).get(encodedKey, now);
|
|
62
61
|
if (!result) return void 0;
|
|
@@ -70,7 +69,7 @@ var SqliteKvStore = class SqliteKvStore {
|
|
|
70
69
|
if (value === void 0) return;
|
|
71
70
|
const encodedKey = this.#encodeKey(key);
|
|
72
71
|
const encodedValue = this.#encodeValue(value);
|
|
73
|
-
const now =
|
|
72
|
+
const now = Temporal.Now.instant().epochMilliseconds;
|
|
74
73
|
const expiresAt = options?.ttl !== void 0 ? now + options.ttl.total({ unit: "milliseconds" }) : null;
|
|
75
74
|
this.#db.prepare(`INSERT INTO "${this.#tableName}" (key, value, created, expires)
|
|
76
75
|
VALUES (?, ?, ?, ?)
|
|
@@ -98,13 +97,13 @@ var SqliteKvStore = class SqliteKvStore {
|
|
|
98
97
|
async cas(key, expectedValue, newValue, options) {
|
|
99
98
|
this.initialize();
|
|
100
99
|
const encodedKey = this.#encodeKey(key);
|
|
101
|
-
const now =
|
|
100
|
+
const now = Temporal.Now.instant().epochMilliseconds;
|
|
102
101
|
const expiresAt = options?.ttl !== void 0 ? now + options.ttl.total({ unit: "milliseconds" }) : null;
|
|
103
102
|
try {
|
|
104
103
|
this.#db.exec("BEGIN IMMEDIATE");
|
|
105
104
|
const currentResult = this.#db.prepare(`
|
|
106
|
-
SELECT value
|
|
107
|
-
FROM "${this.#tableName}"
|
|
105
|
+
SELECT value
|
|
106
|
+
FROM "${this.#tableName}"
|
|
108
107
|
WHERE key = ? AND (expires IS NULL OR expires > ?)
|
|
109
108
|
`).get(encodedKey, now);
|
|
110
109
|
const currentValue = currentResult === void 0 ? void 0 : this.#decodeValue(currentResult.value);
|
|
@@ -134,6 +133,36 @@ var SqliteKvStore = class SqliteKvStore {
|
|
|
134
133
|
}
|
|
135
134
|
}
|
|
136
135
|
/**
|
|
136
|
+
* {@inheritDoc KvStore.list}
|
|
137
|
+
* @since 1.10.0
|
|
138
|
+
*/
|
|
139
|
+
async *list(prefix) {
|
|
140
|
+
this.initialize();
|
|
141
|
+
const now = Temporal.Now.instant().epochMilliseconds;
|
|
142
|
+
let results;
|
|
143
|
+
if (prefix == null || prefix.length === 0) results = this.#db.prepare(`
|
|
144
|
+
SELECT key, value
|
|
145
|
+
FROM "${this.#tableName}"
|
|
146
|
+
WHERE expires IS NULL OR expires > ?
|
|
147
|
+
ORDER BY key
|
|
148
|
+
`).all(now);
|
|
149
|
+
else {
|
|
150
|
+
const pattern = JSON.stringify(prefix).slice(0, -1) + ",%";
|
|
151
|
+
const exactKey = JSON.stringify(prefix);
|
|
152
|
+
results = this.#db.prepare(`
|
|
153
|
+
SELECT key, value
|
|
154
|
+
FROM "${this.#tableName}"
|
|
155
|
+
WHERE (key LIKE ? ESCAPE '\\' OR key = ?)
|
|
156
|
+
AND (expires IS NULL OR expires > ?)
|
|
157
|
+
ORDER BY key
|
|
158
|
+
`).all(pattern, exactKey, now);
|
|
159
|
+
}
|
|
160
|
+
for (const row of results) yield {
|
|
161
|
+
key: this.#decodeKey(row.key),
|
|
162
|
+
value: this.#decodeValue(row.value)
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
137
166
|
* Creates the table used by the key–value store if it does not already exist.
|
|
138
167
|
* Does nothing if the table already exists.
|
|
139
168
|
*/
|
|
@@ -149,14 +178,14 @@ var SqliteKvStore = class SqliteKvStore {
|
|
|
149
178
|
)
|
|
150
179
|
`);
|
|
151
180
|
this.#db.exec(`
|
|
152
|
-
CREATE INDEX IF NOT EXISTS "idx_${this.#tableName}_expires"
|
|
181
|
+
CREATE INDEX IF NOT EXISTS "idx_${this.#tableName}_expires"
|
|
153
182
|
ON "${this.#tableName}" (expires)
|
|
154
183
|
`);
|
|
155
184
|
this.#initialized = true;
|
|
156
185
|
logger.debug("Initialized the key–value store table {tableName}.", { tableName: this.#tableName });
|
|
157
186
|
}
|
|
158
187
|
#expire() {
|
|
159
|
-
const now =
|
|
188
|
+
const now = Temporal.Now.instant().epochMilliseconds;
|
|
160
189
|
this.#db.prepare(`
|
|
161
190
|
DELETE FROM "${this.#tableName}"
|
|
162
191
|
WHERE expires IS NOT NULL AND expires <= ?
|
package/dist/mod.cjs
ADDED
package/dist/mod.d.cts
ADDED
package/dist/mod.js
CHANGED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
|
|
2
|
+
const { Temporal } = require("@js-temporal/polyfill");
|
|
3
|
+
|
|
4
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
5
|
+
const bun_sqlite = require_rolldown_runtime.__toESM(require("bun:sqlite"));
|
|
6
|
+
|
|
7
|
+
//#region src/sqlite.bun.ts
|
|
8
|
+
var SqliteDatabase = class {
|
|
9
|
+
constructor(db) {
|
|
10
|
+
this.db = db;
|
|
11
|
+
}
|
|
12
|
+
prepare(sql) {
|
|
13
|
+
return new SqliteStatement(this.db.query(sql));
|
|
14
|
+
}
|
|
15
|
+
exec(sql) {
|
|
16
|
+
this.db.exec(sql);
|
|
17
|
+
}
|
|
18
|
+
close() {
|
|
19
|
+
this.db.close(false);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
var SqliteStatement = class {
|
|
23
|
+
constructor(stmt) {
|
|
24
|
+
this.stmt = stmt;
|
|
25
|
+
}
|
|
26
|
+
run(...params) {
|
|
27
|
+
return this.stmt.run(...params);
|
|
28
|
+
}
|
|
29
|
+
get(...params) {
|
|
30
|
+
const result = this.stmt.get(...params);
|
|
31
|
+
if (result === null) return void 0;
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
all(...params) {
|
|
35
|
+
return this.stmt.all(...params);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
//#endregion
|
|
40
|
+
exports.PlatformDatabase = bun_sqlite.Database;
|
|
41
|
+
exports.SqliteDatabase = SqliteDatabase;
|
|
42
|
+
exports.SqliteStatement = SqliteStatement;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SqliteDatabaseAdapter, SqliteStatementAdapter } from "./adapter.cjs";
|
|
2
|
+
import { Database, Statement } from "bun:sqlite";
|
|
3
|
+
|
|
4
|
+
//#region src/sqlite.bun.d.ts
|
|
5
|
+
declare class SqliteDatabase implements SqliteDatabaseAdapter {
|
|
6
|
+
private readonly db;
|
|
7
|
+
constructor(db: Database);
|
|
8
|
+
prepare(sql: string): SqliteStatementAdapter;
|
|
9
|
+
exec(sql: string): void;
|
|
10
|
+
close(): void;
|
|
11
|
+
}
|
|
12
|
+
declare class SqliteStatement implements SqliteStatementAdapter {
|
|
13
|
+
private readonly stmt;
|
|
14
|
+
constructor(stmt: Statement);
|
|
15
|
+
run(...params: unknown[]): {
|
|
16
|
+
changes: number;
|
|
17
|
+
lastInsertRowid: number;
|
|
18
|
+
};
|
|
19
|
+
get(...params: unknown[]): unknown | undefined;
|
|
20
|
+
all(...params: unknown[]): unknown[];
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { Database as PlatformDatabase, Statement as PlatformStatement, SqliteDatabase, SqliteStatement };
|
package/dist/sqlite.bun.js
CHANGED