node-sqlite-kv 0.2.2 → 0.3.1

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 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;
12
12
  get<K = T>(key: string): K | null;
13
- delete<K = T>(key: string): K | null;
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(): void;
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, KVSync as default };
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;
12
12
  get<K = T>(key: string): K | null;
13
- delete<K = T>(key: string): K | null;
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(): void;
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
- // @ts-ignore
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
- default: () => KVSync
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
- * Create a new key-value store
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) {
@@ -57,79 +63,92 @@ var KVSync = class {
57
63
  }
58
64
  this.#db = new import_node_sqlite.DatabaseSync(dbPath);
59
65
  this.setJournalMode(options?.journalMode ?? "DELETE");
60
- this.#db.exec(`
61
- CREATE TABLE IF NOT EXISTS kv (
62
- key TEXT PRIMARY KEY NOT NULL,
63
- value BLOB NOT NULL
64
- ) STRICT;
65
- `);
66
+ this.#db.exec(
67
+ "CREATE TABLE IF NOT EXISTS kv (key TEXT PRIMARY KEY NOT NULL, value BLOB NOT NULL) STRICT;"
68
+ );
66
69
  }
67
70
  /**
68
- * Sets a key in the database
71
+ * Set a key in the database
69
72
  * @param key Key name
70
73
  * @param value Key value
71
74
  * @returns Provided value
72
75
  */
73
76
  set(key, value) {
77
+ if (!key || typeof key !== "string") {
78
+ throw new Error(
79
+ "[KVSync]: Key must be provided and be a non-empty string."
80
+ );
81
+ }
82
+ if (value === void 0) {
83
+ throw new Error(
84
+ "[KVSync]: Provided value is undefined. Did you mean to use delete() instead?"
85
+ );
86
+ }
74
87
  this.#db.prepare("INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)").run(key, (0, import_node_v8.serialize)(value));
75
88
  return value;
76
89
  }
77
90
  /**
78
- * Gets a value from the database
91
+ * Get a value from the database
79
92
  * @param key Key name
80
93
  * @returns Value or null
81
94
  */
82
95
  get(key) {
96
+ if (!key || typeof key !== "string") {
97
+ throw new Error(
98
+ "[KVSync]: Key must be provided and be a non-empty string."
99
+ );
100
+ }
83
101
  const row = this.#db.prepare("SELECT value FROM kv WHERE key = ?").get(key);
84
102
  return row ? (0, import_node_v8.deserialize)(row.value) : null;
85
103
  }
86
104
  /**
87
- * Deletes a key from the database
105
+ * Delete a key from the database
88
106
  * @param key Key name
89
- * @returns Deleted key or null
107
+ * @returns KVSync instance
90
108
  */
91
109
  delete(key) {
92
- const existing = this.get(key);
93
- if (existing !== null) {
94
- this.#db.prepare("DELETE FROM kv WHERE key = ?").run(key);
110
+ if (!key || typeof key !== "string") {
111
+ throw new Error(
112
+ "[KVSync]: Key must be provided and be a non-empty string."
113
+ );
95
114
  }
96
- return existing;
115
+ this.#db.prepare("DELETE FROM kv WHERE key = ?").run(key);
116
+ return this;
97
117
  }
98
118
  /**
99
119
  * Get all data in the database
100
120
  * @returns Array of objects containing keys and values
101
121
  */
102
- all() {
103
- return this.#db.prepare("SELECT key, value FROM kv").all().map((record) => ({
104
- key: record.key,
105
- value: (0, import_node_v8.deserialize)(record.value)
106
- }));
122
+ all(filter) {
123
+ const rows = this.#db.prepare("SELECT key, value FROM kv").iterate();
124
+ const result = [];
125
+ for (const row of rows) {
126
+ const key = row.key;
127
+ const value = (0, import_node_v8.deserialize)(row.value);
128
+ if (!filter || filter(key, value)) {
129
+ result.push({ key, value });
130
+ }
131
+ }
132
+ return result;
107
133
  }
108
134
  /**
109
135
  * Remove all entries from the database
110
136
  */
111
137
  clear() {
112
- this.#db.exec("DELETE FROM kv");
138
+ this.#db.exec("DELETE FROM kv;");
139
+ return this;
113
140
  }
114
141
  /**
115
- * Updates the journal mode
142
+ * Update the journal mode
116
143
  * @param mode New journal mode
117
144
  */
118
145
  setJournalMode(mode) {
119
- const journalModes = [
120
- "DELETE",
121
- "MEMORY",
122
- "OFF",
123
- "PERSIST",
124
- "TRUNCATE",
125
- "WAL"
126
- ];
127
146
  if (!journalModes.includes(mode)) {
128
147
  throw new Error(
129
- `Invalid KVSync journal mode specified. Received: "${mode}", expected one of: ${journalModes.join(", ")}`
148
+ `[KVSync]: Invalid journal mode specified. Received: "${mode}", expected one of: ${journalModes.join(", ")}`
130
149
  );
131
150
  }
132
- this.#db.exec(`PRAGMA journal_mode = ${mode}`);
151
+ this.#db.exec(`PRAGMA journal_mode = ${mode};`);
133
152
  return this;
134
153
  }
135
154
  /**
@@ -138,7 +157,16 @@ var KVSync = class {
138
157
  * @returns Object containing oldValues and newValues each containing arrays of keys and values
139
158
  */
140
159
  transaction(callback) {
141
- this.#db.exec("BEGIN TRANSACTION;");
160
+ if (!callback) {
161
+ throw new Error(
162
+ "[KVSync]: A callback must be provided when using transaction()."
163
+ );
164
+ }
165
+ if (typeof callback !== "function") {
166
+ throw new Error(
167
+ `[KVSync]: Transaction callback must be of type function. Received: ${typeof callback}`
168
+ );
169
+ }
142
170
  const oldMap = /* @__PURE__ */ new Map();
143
171
  const newMap = /* @__PURE__ */ new Map();
144
172
  const tx = Object.create(this);
@@ -147,9 +175,8 @@ var KVSync = class {
147
175
  const oldValue = this.get(key);
148
176
  oldMap.set(key, oldValue === null ? void 0 : oldValue);
149
177
  }
150
- const result = this.set(key, value);
151
- newMap.set(key, result);
152
- return result;
178
+ newMap.set(key, value);
179
+ return value ?? null;
153
180
  };
154
181
  tx.delete = (key) => {
155
182
  if (!oldMap.has(key)) {
@@ -157,11 +184,18 @@ var KVSync = class {
157
184
  oldMap.set(key, oldValue === null ? void 0 : oldValue);
158
185
  }
159
186
  newMap.set(key, null);
160
- this.delete(key);
161
- return oldMap.get(key);
187
+ return tx;
162
188
  };
163
189
  try {
164
190
  callback(tx);
191
+ this.#db.exec("BEGIN TRANSACTION;");
192
+ for (const [key, value] of newMap.entries()) {
193
+ if (value === null) {
194
+ this.delete(key);
195
+ } else {
196
+ this.set(key, value);
197
+ }
198
+ }
165
199
  this.#db.exec("COMMIT;");
166
200
  } catch (error) {
167
201
  this.#db.exec("ROLLBACK;");
@@ -181,5 +215,6 @@ var KVSync = class {
181
215
  };
182
216
  // Annotate the CommonJS export names for ESM import in node:
183
217
  0 && (module.exports = {
184
- KVSync
218
+ KVSync,
219
+ journalModes
185
220
  });
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/structures/KVSync.ts
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
- * Create a new key-value store
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) {
@@ -22,79 +30,92 @@ var KVSync = class {
22
30
  }
23
31
  this.#db = new DatabaseSync(dbPath);
24
32
  this.setJournalMode(options?.journalMode ?? "DELETE");
25
- this.#db.exec(`
26
- CREATE TABLE IF NOT EXISTS kv (
27
- key TEXT PRIMARY KEY NOT NULL,
28
- value BLOB NOT NULL
29
- ) STRICT;
30
- `);
33
+ this.#db.exec(
34
+ "CREATE TABLE IF NOT EXISTS kv (key TEXT PRIMARY KEY NOT NULL, value BLOB NOT NULL) STRICT;"
35
+ );
31
36
  }
32
37
  /**
33
- * Sets a key in the database
38
+ * Set a key in the database
34
39
  * @param key Key name
35
40
  * @param value Key value
36
41
  * @returns Provided value
37
42
  */
38
43
  set(key, value) {
44
+ if (!key || typeof key !== "string") {
45
+ throw new Error(
46
+ "[KVSync]: Key must be provided and be a non-empty string."
47
+ );
48
+ }
49
+ if (value === void 0) {
50
+ throw new Error(
51
+ "[KVSync]: Provided value is undefined. Did you mean to use delete() instead?"
52
+ );
53
+ }
39
54
  this.#db.prepare("INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)").run(key, serialize(value));
40
55
  return value;
41
56
  }
42
57
  /**
43
- * Gets a value from the database
58
+ * Get a value from the database
44
59
  * @param key Key name
45
60
  * @returns Value or null
46
61
  */
47
62
  get(key) {
63
+ if (!key || typeof key !== "string") {
64
+ throw new Error(
65
+ "[KVSync]: Key must be provided and be a non-empty string."
66
+ );
67
+ }
48
68
  const row = this.#db.prepare("SELECT value FROM kv WHERE key = ?").get(key);
49
69
  return row ? deserialize(row.value) : null;
50
70
  }
51
71
  /**
52
- * Deletes a key from the database
72
+ * Delete a key from the database
53
73
  * @param key Key name
54
- * @returns Deleted key or null
74
+ * @returns KVSync instance
55
75
  */
56
76
  delete(key) {
57
- const existing = this.get(key);
58
- if (existing !== null) {
59
- this.#db.prepare("DELETE FROM kv WHERE key = ?").run(key);
77
+ if (!key || typeof key !== "string") {
78
+ throw new Error(
79
+ "[KVSync]: Key must be provided and be a non-empty string."
80
+ );
60
81
  }
61
- return existing;
82
+ this.#db.prepare("DELETE FROM kv WHERE key = ?").run(key);
83
+ return this;
62
84
  }
63
85
  /**
64
86
  * Get all data in the database
65
87
  * @returns Array of objects containing keys and values
66
88
  */
67
- all() {
68
- return this.#db.prepare("SELECT key, value FROM kv").all().map((record) => ({
69
- key: record.key,
70
- value: deserialize(record.value)
71
- }));
89
+ all(filter) {
90
+ const rows = this.#db.prepare("SELECT key, value FROM kv").iterate();
91
+ const result = [];
92
+ for (const row of rows) {
93
+ const key = row.key;
94
+ const value = deserialize(row.value);
95
+ if (!filter || filter(key, value)) {
96
+ result.push({ key, value });
97
+ }
98
+ }
99
+ return result;
72
100
  }
73
101
  /**
74
102
  * Remove all entries from the database
75
103
  */
76
104
  clear() {
77
- this.#db.exec("DELETE FROM kv");
105
+ this.#db.exec("DELETE FROM kv;");
106
+ return this;
78
107
  }
79
108
  /**
80
- * Updates the journal mode
109
+ * Update the journal mode
81
110
  * @param mode New journal mode
82
111
  */
83
112
  setJournalMode(mode) {
84
- const journalModes = [
85
- "DELETE",
86
- "MEMORY",
87
- "OFF",
88
- "PERSIST",
89
- "TRUNCATE",
90
- "WAL"
91
- ];
92
113
  if (!journalModes.includes(mode)) {
93
114
  throw new Error(
94
- `Invalid KVSync journal mode specified. Received: "${mode}", expected one of: ${journalModes.join(", ")}`
115
+ `[KVSync]: Invalid journal mode specified. Received: "${mode}", expected one of: ${journalModes.join(", ")}`
95
116
  );
96
117
  }
97
- this.#db.exec(`PRAGMA journal_mode = ${mode}`);
118
+ this.#db.exec(`PRAGMA journal_mode = ${mode};`);
98
119
  return this;
99
120
  }
100
121
  /**
@@ -103,7 +124,16 @@ var KVSync = class {
103
124
  * @returns Object containing oldValues and newValues each containing arrays of keys and values
104
125
  */
105
126
  transaction(callback) {
106
- this.#db.exec("BEGIN TRANSACTION;");
127
+ if (!callback) {
128
+ throw new Error(
129
+ "[KVSync]: A callback must be provided when using transaction()."
130
+ );
131
+ }
132
+ if (typeof callback !== "function") {
133
+ throw new Error(
134
+ `[KVSync]: Transaction callback must be of type function. Received: ${typeof callback}`
135
+ );
136
+ }
107
137
  const oldMap = /* @__PURE__ */ new Map();
108
138
  const newMap = /* @__PURE__ */ new Map();
109
139
  const tx = Object.create(this);
@@ -112,9 +142,8 @@ var KVSync = class {
112
142
  const oldValue = this.get(key);
113
143
  oldMap.set(key, oldValue === null ? void 0 : oldValue);
114
144
  }
115
- const result = this.set(key, value);
116
- newMap.set(key, result);
117
- return result;
145
+ newMap.set(key, value);
146
+ return value ?? null;
118
147
  };
119
148
  tx.delete = (key) => {
120
149
  if (!oldMap.has(key)) {
@@ -122,11 +151,18 @@ var KVSync = class {
122
151
  oldMap.set(key, oldValue === null ? void 0 : oldValue);
123
152
  }
124
153
  newMap.set(key, null);
125
- this.delete(key);
126
- return oldMap.get(key);
154
+ return tx;
127
155
  };
128
156
  try {
129
157
  callback(tx);
158
+ this.#db.exec("BEGIN TRANSACTION;");
159
+ for (const [key, value] of newMap.entries()) {
160
+ if (value === null) {
161
+ this.delete(key);
162
+ } else {
163
+ this.set(key, value);
164
+ }
165
+ }
130
166
  this.#db.exec("COMMIT;");
131
167
  } catch (error) {
132
168
  this.#db.exec("ROLLBACK;");
@@ -146,5 +182,5 @@ var KVSync = class {
146
182
  };
147
183
  export {
148
184
  KVSync,
149
- KVSync as default
185
+ journalModes
150
186
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "node-sqlite-kv",
3
3
  "description": "Key-value store with node:sqlite",
4
- "version": "0.2.2",
4
+ "version": "0.3.1",
5
5
  "repository": {
6
6
  "url": "https://github.com/e60m5ss/node-sqlite-kv"
7
7
  },
@@ -26,7 +26,7 @@
26
26
  "useTabs": false
27
27
  },
28
28
  "devDependencies": {
29
- "@types/node": "^25.1.0",
29
+ "@types/node": "^25.2.0",
30
30
  "prettier": "^3.8.1",
31
31
  "tsup": "^8.5.1",
32
32
  "typescript": "^5.9.3"