node-sqlite-kv 0.2.2 → 0.3.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.d.mts +6 -6
- package/dist/index.d.ts +6 -8
- package/dist/index.js +63 -31
- package/dist/index.mjs +62 -29
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -4,18 +4,18 @@ interface KVSyncOptions {
|
|
|
4
4
|
journalMode?: JournalMode;
|
|
5
5
|
}
|
|
6
6
|
type SQLitePath = ":memory:" | (string & {});
|
|
7
|
-
|
|
7
|
+
declare const journalModes: JournalMode[];
|
|
8
8
|
declare class KVSync<T = any> {
|
|
9
9
|
#private;
|
|
10
10
|
constructor(options?: KVSyncOptions);
|
|
11
|
-
set<K = T>(key: string, value: K): K;
|
|
11
|
+
set<K = T>(key: string, value: K | undefined): K | null;
|
|
12
12
|
get<K = T>(key: string): K | null;
|
|
13
|
-
delete
|
|
14
|
-
all<K = T>(): {
|
|
13
|
+
delete(key: string): KVSync;
|
|
14
|
+
all<K = T>(filter?: (key: string, value: K) => boolean): {
|
|
15
15
|
key: string;
|
|
16
16
|
value: K;
|
|
17
17
|
}[];
|
|
18
|
-
clear():
|
|
18
|
+
clear(): KVSync;
|
|
19
19
|
setJournalMode(mode: JournalMode): this;
|
|
20
20
|
transaction<R>(callback: (kv: KVSync<T>) => R): {
|
|
21
21
|
oldValues: {
|
|
@@ -29,4 +29,4 @@ declare class KVSync<T = any> {
|
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
export { type JournalMode, KVSync, type KVSyncOptions, type SQLitePath,
|
|
32
|
+
export { type JournalMode, KVSync, type KVSyncOptions, type SQLitePath, journalModes };
|
package/dist/index.d.ts
CHANGED
|
@@ -4,18 +4,18 @@ interface KVSyncOptions {
|
|
|
4
4
|
journalMode?: JournalMode;
|
|
5
5
|
}
|
|
6
6
|
type SQLitePath = ":memory:" | (string & {});
|
|
7
|
-
|
|
7
|
+
declare const journalModes: JournalMode[];
|
|
8
8
|
declare class KVSync<T = any> {
|
|
9
9
|
#private;
|
|
10
10
|
constructor(options?: KVSyncOptions);
|
|
11
|
-
set<K = T>(key: string, value: K): K;
|
|
11
|
+
set<K = T>(key: string, value: K | undefined): K | null;
|
|
12
12
|
get<K = T>(key: string): K | null;
|
|
13
|
-
delete
|
|
14
|
-
all<K = T>(): {
|
|
13
|
+
delete(key: string): KVSync;
|
|
14
|
+
all<K = T>(filter?: (key: string, value: K) => boolean): {
|
|
15
15
|
key: string;
|
|
16
16
|
value: K;
|
|
17
17
|
}[];
|
|
18
|
-
clear():
|
|
18
|
+
clear(): KVSync;
|
|
19
19
|
setJournalMode(mode: JournalMode): this;
|
|
20
20
|
transaction<R>(callback: (kv: KVSync<T>) => R): {
|
|
21
21
|
oldValues: {
|
|
@@ -29,6 +29,4 @@ declare class KVSync<T = any> {
|
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
export = KVSync;
|
|
34
|
-
export { type JournalMode, KVSync, type KVSyncOptions, type SQLitePath };
|
|
32
|
+
export { type JournalMode, KVSync, type KVSyncOptions, type SQLitePath, journalModes };
|
package/dist/index.js
CHANGED
|
@@ -32,22 +32,28 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
32
32
|
var index_exports = {};
|
|
33
33
|
__export(index_exports, {
|
|
34
34
|
KVSync: () => KVSync,
|
|
35
|
-
|
|
35
|
+
journalModes: () => journalModes
|
|
36
36
|
});
|
|
37
37
|
module.exports = __toCommonJS(index_exports);
|
|
38
|
-
|
|
39
|
-
// src/structures/KVSync.ts
|
|
40
38
|
var import_node_sqlite = require("node:sqlite");
|
|
41
39
|
var import_node_v8 = require("node:v8");
|
|
42
40
|
var import_node_fs = __toESM(require("node:fs"));
|
|
43
41
|
var import_node_path = __toESM(require("node:path"));
|
|
42
|
+
var journalModes = [
|
|
43
|
+
"DELETE",
|
|
44
|
+
"MEMORY",
|
|
45
|
+
"OFF",
|
|
46
|
+
"PERSIST",
|
|
47
|
+
"TRUNCATE",
|
|
48
|
+
"WAL"
|
|
49
|
+
];
|
|
44
50
|
var KVSync = class {
|
|
45
51
|
static {
|
|
46
52
|
__name(this, "KVSync");
|
|
47
53
|
}
|
|
48
54
|
#db;
|
|
49
55
|
/**
|
|
50
|
-
*
|
|
56
|
+
* Instantiate a new key-value store
|
|
51
57
|
* @param path Where the database is stored, or `:memory:` for in-memory storage
|
|
52
58
|
*/
|
|
53
59
|
constructor(options) {
|
|
@@ -65,71 +71,86 @@ var KVSync = class {
|
|
|
65
71
|
`);
|
|
66
72
|
}
|
|
67
73
|
/**
|
|
68
|
-
*
|
|
74
|
+
* Set a key in the database
|
|
69
75
|
* @param key Key name
|
|
70
76
|
* @param value Key value
|
|
71
77
|
* @returns Provided value
|
|
72
78
|
*/
|
|
73
79
|
set(key, value) {
|
|
80
|
+
if (!key || typeof key !== "string") {
|
|
81
|
+
throw new Error(
|
|
82
|
+
"[KVSync]: Key must be provided and be a non-empty string."
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
if (value === void 0) {
|
|
86
|
+
this.delete(key);
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
74
89
|
this.#db.prepare("INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)").run(key, (0, import_node_v8.serialize)(value));
|
|
75
90
|
return value;
|
|
76
91
|
}
|
|
77
92
|
/**
|
|
78
|
-
*
|
|
93
|
+
* Get a value from the database
|
|
79
94
|
* @param key Key name
|
|
80
95
|
* @returns Value or null
|
|
81
96
|
*/
|
|
82
97
|
get(key) {
|
|
98
|
+
if (!key || typeof key !== "string") {
|
|
99
|
+
throw new Error(
|
|
100
|
+
"[KVSync]: Key must be provided and be a non-empty string."
|
|
101
|
+
);
|
|
102
|
+
}
|
|
83
103
|
const row = this.#db.prepare("SELECT value FROM kv WHERE key = ?").get(key);
|
|
84
104
|
return row ? (0, import_node_v8.deserialize)(row.value) : null;
|
|
85
105
|
}
|
|
86
106
|
/**
|
|
87
|
-
*
|
|
107
|
+
* Delete a key from the database
|
|
88
108
|
* @param key Key name
|
|
89
|
-
* @returns
|
|
109
|
+
* @returns KVSync instance
|
|
90
110
|
*/
|
|
91
111
|
delete(key) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
112
|
+
if (!key || typeof key !== "string") {
|
|
113
|
+
throw new Error(
|
|
114
|
+
"[KVSync]: Key must be provided and be a non-empty string."
|
|
115
|
+
);
|
|
95
116
|
}
|
|
96
|
-
|
|
117
|
+
this.#db.prepare("DELETE FROM kv WHERE key = ?").run(key);
|
|
118
|
+
return this;
|
|
97
119
|
}
|
|
98
120
|
/**
|
|
99
121
|
* Get all data in the database
|
|
100
122
|
* @returns Array of objects containing keys and values
|
|
101
123
|
*/
|
|
102
|
-
all() {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
124
|
+
all(filter) {
|
|
125
|
+
const rows = this.#db.prepare("SELECT key, value FROM kv").iterate();
|
|
126
|
+
const result = [];
|
|
127
|
+
for (const row of rows) {
|
|
128
|
+
const key = row.key;
|
|
129
|
+
const value = (0, import_node_v8.deserialize)(row.value);
|
|
130
|
+
if (!filter || filter(key, value)) {
|
|
131
|
+
result.push({ key, value });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
107
135
|
}
|
|
108
136
|
/**
|
|
109
137
|
* Remove all entries from the database
|
|
110
138
|
*/
|
|
111
139
|
clear() {
|
|
112
|
-
this.#db.exec("DELETE FROM kv");
|
|
140
|
+
this.#db.exec("DELETE FROM kv;");
|
|
141
|
+
return this;
|
|
113
142
|
}
|
|
114
143
|
/**
|
|
115
|
-
*
|
|
144
|
+
* Update the journal mode
|
|
116
145
|
* @param mode New journal mode
|
|
117
146
|
*/
|
|
118
147
|
setJournalMode(mode) {
|
|
119
|
-
const journalModes = [
|
|
120
|
-
"DELETE",
|
|
121
|
-
"MEMORY",
|
|
122
|
-
"OFF",
|
|
123
|
-
"PERSIST",
|
|
124
|
-
"TRUNCATE",
|
|
125
|
-
"WAL"
|
|
126
|
-
];
|
|
127
148
|
if (!journalModes.includes(mode)) {
|
|
128
149
|
throw new Error(
|
|
129
|
-
`Invalid
|
|
150
|
+
`[KVSync]: Invalid journal mode specified. Received: "${mode}", expected one of: ${journalModes.join(", ")}`
|
|
130
151
|
);
|
|
131
152
|
}
|
|
132
|
-
this.#db.exec(`PRAGMA journal_mode = ${mode}
|
|
153
|
+
this.#db.exec(`PRAGMA journal_mode = ${mode};`);
|
|
133
154
|
return this;
|
|
134
155
|
}
|
|
135
156
|
/**
|
|
@@ -138,6 +159,16 @@ var KVSync = class {
|
|
|
138
159
|
* @returns Object containing oldValues and newValues each containing arrays of keys and values
|
|
139
160
|
*/
|
|
140
161
|
transaction(callback) {
|
|
162
|
+
if (!callback) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
"[KVSync]: A callback must be provided when using transaction()."
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
if (typeof callback !== "function") {
|
|
168
|
+
throw new Error(
|
|
169
|
+
`[KVSync]: Transaction callback must be of type function. Received: ${typeof callback}`
|
|
170
|
+
);
|
|
171
|
+
}
|
|
141
172
|
this.#db.exec("BEGIN TRANSACTION;");
|
|
142
173
|
const oldMap = /* @__PURE__ */ new Map();
|
|
143
174
|
const newMap = /* @__PURE__ */ new Map();
|
|
@@ -158,7 +189,7 @@ var KVSync = class {
|
|
|
158
189
|
}
|
|
159
190
|
newMap.set(key, null);
|
|
160
191
|
this.delete(key);
|
|
161
|
-
return
|
|
192
|
+
return tx;
|
|
162
193
|
};
|
|
163
194
|
try {
|
|
164
195
|
callback(tx);
|
|
@@ -181,5 +212,6 @@ var KVSync = class {
|
|
|
181
212
|
};
|
|
182
213
|
// Annotate the CommonJS export names for ESM import in node:
|
|
183
214
|
0 && (module.exports = {
|
|
184
|
-
KVSync
|
|
215
|
+
KVSync,
|
|
216
|
+
journalModes
|
|
185
217
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,18 +1,26 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
|
-
// src/
|
|
4
|
+
// src/index.ts
|
|
5
5
|
import { DatabaseSync } from "node:sqlite";
|
|
6
6
|
import { serialize, deserialize } from "node:v8";
|
|
7
7
|
import fs from "node:fs";
|
|
8
8
|
import path from "node:path";
|
|
9
|
+
var journalModes = [
|
|
10
|
+
"DELETE",
|
|
11
|
+
"MEMORY",
|
|
12
|
+
"OFF",
|
|
13
|
+
"PERSIST",
|
|
14
|
+
"TRUNCATE",
|
|
15
|
+
"WAL"
|
|
16
|
+
];
|
|
9
17
|
var KVSync = class {
|
|
10
18
|
static {
|
|
11
19
|
__name(this, "KVSync");
|
|
12
20
|
}
|
|
13
21
|
#db;
|
|
14
22
|
/**
|
|
15
|
-
*
|
|
23
|
+
* Instantiate a new key-value store
|
|
16
24
|
* @param path Where the database is stored, or `:memory:` for in-memory storage
|
|
17
25
|
*/
|
|
18
26
|
constructor(options) {
|
|
@@ -30,71 +38,86 @@ var KVSync = class {
|
|
|
30
38
|
`);
|
|
31
39
|
}
|
|
32
40
|
/**
|
|
33
|
-
*
|
|
41
|
+
* Set a key in the database
|
|
34
42
|
* @param key Key name
|
|
35
43
|
* @param value Key value
|
|
36
44
|
* @returns Provided value
|
|
37
45
|
*/
|
|
38
46
|
set(key, value) {
|
|
47
|
+
if (!key || typeof key !== "string") {
|
|
48
|
+
throw new Error(
|
|
49
|
+
"[KVSync]: Key must be provided and be a non-empty string."
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
if (value === void 0) {
|
|
53
|
+
this.delete(key);
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
39
56
|
this.#db.prepare("INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)").run(key, serialize(value));
|
|
40
57
|
return value;
|
|
41
58
|
}
|
|
42
59
|
/**
|
|
43
|
-
*
|
|
60
|
+
* Get a value from the database
|
|
44
61
|
* @param key Key name
|
|
45
62
|
* @returns Value or null
|
|
46
63
|
*/
|
|
47
64
|
get(key) {
|
|
65
|
+
if (!key || typeof key !== "string") {
|
|
66
|
+
throw new Error(
|
|
67
|
+
"[KVSync]: Key must be provided and be a non-empty string."
|
|
68
|
+
);
|
|
69
|
+
}
|
|
48
70
|
const row = this.#db.prepare("SELECT value FROM kv WHERE key = ?").get(key);
|
|
49
71
|
return row ? deserialize(row.value) : null;
|
|
50
72
|
}
|
|
51
73
|
/**
|
|
52
|
-
*
|
|
74
|
+
* Delete a key from the database
|
|
53
75
|
* @param key Key name
|
|
54
|
-
* @returns
|
|
76
|
+
* @returns KVSync instance
|
|
55
77
|
*/
|
|
56
78
|
delete(key) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
79
|
+
if (!key || typeof key !== "string") {
|
|
80
|
+
throw new Error(
|
|
81
|
+
"[KVSync]: Key must be provided and be a non-empty string."
|
|
82
|
+
);
|
|
60
83
|
}
|
|
61
|
-
|
|
84
|
+
this.#db.prepare("DELETE FROM kv WHERE key = ?").run(key);
|
|
85
|
+
return this;
|
|
62
86
|
}
|
|
63
87
|
/**
|
|
64
88
|
* Get all data in the database
|
|
65
89
|
* @returns Array of objects containing keys and values
|
|
66
90
|
*/
|
|
67
|
-
all() {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
91
|
+
all(filter) {
|
|
92
|
+
const rows = this.#db.prepare("SELECT key, value FROM kv").iterate();
|
|
93
|
+
const result = [];
|
|
94
|
+
for (const row of rows) {
|
|
95
|
+
const key = row.key;
|
|
96
|
+
const value = deserialize(row.value);
|
|
97
|
+
if (!filter || filter(key, value)) {
|
|
98
|
+
result.push({ key, value });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
72
102
|
}
|
|
73
103
|
/**
|
|
74
104
|
* Remove all entries from the database
|
|
75
105
|
*/
|
|
76
106
|
clear() {
|
|
77
|
-
this.#db.exec("DELETE FROM kv");
|
|
107
|
+
this.#db.exec("DELETE FROM kv;");
|
|
108
|
+
return this;
|
|
78
109
|
}
|
|
79
110
|
/**
|
|
80
|
-
*
|
|
111
|
+
* Update the journal mode
|
|
81
112
|
* @param mode New journal mode
|
|
82
113
|
*/
|
|
83
114
|
setJournalMode(mode) {
|
|
84
|
-
const journalModes = [
|
|
85
|
-
"DELETE",
|
|
86
|
-
"MEMORY",
|
|
87
|
-
"OFF",
|
|
88
|
-
"PERSIST",
|
|
89
|
-
"TRUNCATE",
|
|
90
|
-
"WAL"
|
|
91
|
-
];
|
|
92
115
|
if (!journalModes.includes(mode)) {
|
|
93
116
|
throw new Error(
|
|
94
|
-
`Invalid
|
|
117
|
+
`[KVSync]: Invalid journal mode specified. Received: "${mode}", expected one of: ${journalModes.join(", ")}`
|
|
95
118
|
);
|
|
96
119
|
}
|
|
97
|
-
this.#db.exec(`PRAGMA journal_mode = ${mode}
|
|
120
|
+
this.#db.exec(`PRAGMA journal_mode = ${mode};`);
|
|
98
121
|
return this;
|
|
99
122
|
}
|
|
100
123
|
/**
|
|
@@ -103,6 +126,16 @@ var KVSync = class {
|
|
|
103
126
|
* @returns Object containing oldValues and newValues each containing arrays of keys and values
|
|
104
127
|
*/
|
|
105
128
|
transaction(callback) {
|
|
129
|
+
if (!callback) {
|
|
130
|
+
throw new Error(
|
|
131
|
+
"[KVSync]: A callback must be provided when using transaction()."
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
if (typeof callback !== "function") {
|
|
135
|
+
throw new Error(
|
|
136
|
+
`[KVSync]: Transaction callback must be of type function. Received: ${typeof callback}`
|
|
137
|
+
);
|
|
138
|
+
}
|
|
106
139
|
this.#db.exec("BEGIN TRANSACTION;");
|
|
107
140
|
const oldMap = /* @__PURE__ */ new Map();
|
|
108
141
|
const newMap = /* @__PURE__ */ new Map();
|
|
@@ -123,7 +156,7 @@ var KVSync = class {
|
|
|
123
156
|
}
|
|
124
157
|
newMap.set(key, null);
|
|
125
158
|
this.delete(key);
|
|
126
|
-
return
|
|
159
|
+
return tx;
|
|
127
160
|
};
|
|
128
161
|
try {
|
|
129
162
|
callback(tx);
|
|
@@ -146,5 +179,5 @@ var KVSync = class {
|
|
|
146
179
|
};
|
|
147
180
|
export {
|
|
148
181
|
KVSync,
|
|
149
|
-
|
|
182
|
+
journalModes
|
|
150
183
|
};
|