node-sqlite-kv 0.2.1 → 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 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<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 | null;
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
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
9
  var __export = (target, all) => {
@@ -16,104 +18,139 @@ var __copyProps = (to, from, except, desc) => {
16
18
  }
17
19
  return to;
18
20
  };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
19
29
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
30
 
21
31
  // src/index.ts
22
32
  var index_exports = {};
23
33
  __export(index_exports, {
24
34
  KVSync: () => KVSync,
25
- default: () => KVSync
35
+ journalModes: () => journalModes
26
36
  });
27
37
  module.exports = __toCommonJS(index_exports);
28
-
29
- // src/structures/KVSync.ts
30
38
  var import_node_sqlite = require("node:sqlite");
31
39
  var import_node_v8 = require("node:v8");
40
+ var import_node_fs = __toESM(require("node:fs"));
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
+ ];
32
50
  var KVSync = class {
33
51
  static {
34
52
  __name(this, "KVSync");
35
53
  }
36
54
  #db;
37
55
  /**
38
- * Create a new key-value store
56
+ * Instantiate a new key-value store
39
57
  * @param path Where the database is stored, or `:memory:` for in-memory storage
40
58
  */
41
59
  constructor(options) {
42
- this.#db = new import_node_sqlite.DatabaseSync(options?.path ?? ":memory:");
60
+ const dbPath = options?.path ?? ":memory:";
61
+ if (dbPath !== ":memory:") {
62
+ import_node_fs.default.mkdirSync(import_node_path.default.dirname(dbPath), { recursive: true });
63
+ }
64
+ this.#db = new import_node_sqlite.DatabaseSync(dbPath);
43
65
  this.setJournalMode(options?.journalMode ?? "DELETE");
44
66
  this.#db.exec(`
45
67
  CREATE TABLE IF NOT EXISTS kv (
46
68
  key TEXT PRIMARY KEY NOT NULL,
47
69
  value BLOB NOT NULL
48
- ) STRICT
70
+ ) STRICT;
49
71
  `);
50
72
  }
51
73
  /**
52
- * Sets a key in the database
74
+ * Set a key in the database
53
75
  * @param key Key name
54
76
  * @param value Key value
55
77
  * @returns Provided value
56
78
  */
57
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
+ }
58
89
  this.#db.prepare("INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)").run(key, (0, import_node_v8.serialize)(value));
59
90
  return value;
60
91
  }
61
92
  /**
62
- * Gets a value from the database
93
+ * Get a value from the database
63
94
  * @param key Key name
64
95
  * @returns Value or null
65
96
  */
66
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
+ }
67
103
  const row = this.#db.prepare("SELECT value FROM kv WHERE key = ?").get(key);
68
104
  return row ? (0, import_node_v8.deserialize)(row.value) : null;
69
105
  }
70
106
  /**
71
- * Deletes a key from the database
107
+ * Delete a key from the database
72
108
  * @param key Key name
73
- * @returns Deleted key or null
109
+ * @returns KVSync instance
74
110
  */
75
111
  delete(key) {
76
- const existing = this.get(key);
77
- if (existing !== null) {
78
- this.#db.prepare("DELETE FROM kv WHERE key = ?").run(key);
112
+ if (!key || typeof key !== "string") {
113
+ throw new Error(
114
+ "[KVSync]: Key must be provided and be a non-empty string."
115
+ );
79
116
  }
80
- return existing;
117
+ this.#db.prepare("DELETE FROM kv WHERE key = ?").run(key);
118
+ return this;
81
119
  }
82
120
  /**
83
121
  * Get all data in the database
84
122
  * @returns Array of objects containing keys and values
85
123
  */
86
- all() {
87
- return this.#db.prepare("SELECT key, value FROM kv").all().map((record) => ({
88
- key: record.key,
89
- value: (0, import_node_v8.deserialize)(record.value)
90
- }));
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;
91
135
  }
92
136
  /**
93
137
  * Remove all entries from the database
94
138
  */
95
139
  clear() {
96
- this.#db.exec("DELETE FROM kv");
140
+ this.#db.exec("DELETE FROM kv;");
141
+ return this;
97
142
  }
98
143
  /**
99
- * Updates the journal mode
144
+ * Update the journal mode
100
145
  * @param mode New journal mode
101
146
  */
102
147
  setJournalMode(mode) {
103
- const journalModes = [
104
- "DELETE",
105
- "MEMORY",
106
- "OFF",
107
- "PERSIST",
108
- "TRUNCATE",
109
- "WAL"
110
- ];
111
148
  if (!journalModes.includes(mode)) {
112
149
  throw new Error(
113
- `Invalid KVSync journal mode specified. Received: "${mode}", expected one of: ${journalModes.join(", ")}`
150
+ `[KVSync]: Invalid journal mode specified. Received: "${mode}", expected one of: ${journalModes.join(", ")}`
114
151
  );
115
152
  }
116
- this.#db.exec(`PRAGMA journal_mode = ${mode}`);
153
+ this.#db.exec(`PRAGMA journal_mode = ${mode};`);
117
154
  return this;
118
155
  }
119
156
  /**
@@ -122,6 +159,16 @@ var KVSync = class {
122
159
  * @returns Object containing oldValues and newValues each containing arrays of keys and values
123
160
  */
124
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
+ }
125
172
  this.#db.exec("BEGIN TRANSACTION;");
126
173
  const oldMap = /* @__PURE__ */ new Map();
127
174
  const newMap = /* @__PURE__ */ new Map();
@@ -142,7 +189,7 @@ var KVSync = class {
142
189
  }
143
190
  newMap.set(key, null);
144
191
  this.delete(key);
145
- return oldMap.get(key);
192
+ return tx;
146
193
  };
147
194
  try {
148
195
  callback(tx);
@@ -165,5 +212,6 @@ var KVSync = class {
165
212
  };
166
213
  // Annotate the CommonJS export names for ESM import in node:
167
214
  0 && (module.exports = {
168
- KVSync
215
+ KVSync,
216
+ journalModes
169
217
  });
package/dist/index.mjs CHANGED
@@ -1,94 +1,123 @@
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
+ import fs from "node:fs";
8
+ import path from "node:path";
9
+ var journalModes = [
10
+ "DELETE",
11
+ "MEMORY",
12
+ "OFF",
13
+ "PERSIST",
14
+ "TRUNCATE",
15
+ "WAL"
16
+ ];
7
17
  var KVSync = class {
8
18
  static {
9
19
  __name(this, "KVSync");
10
20
  }
11
21
  #db;
12
22
  /**
13
- * Create a new key-value store
23
+ * Instantiate a new key-value store
14
24
  * @param path Where the database is stored, or `:memory:` for in-memory storage
15
25
  */
16
26
  constructor(options) {
17
- this.#db = new DatabaseSync(options?.path ?? ":memory:");
27
+ const dbPath = options?.path ?? ":memory:";
28
+ if (dbPath !== ":memory:") {
29
+ fs.mkdirSync(path.dirname(dbPath), { recursive: true });
30
+ }
31
+ this.#db = new DatabaseSync(dbPath);
18
32
  this.setJournalMode(options?.journalMode ?? "DELETE");
19
33
  this.#db.exec(`
20
34
  CREATE TABLE IF NOT EXISTS kv (
21
35
  key TEXT PRIMARY KEY NOT NULL,
22
36
  value BLOB NOT NULL
23
- ) STRICT
37
+ ) STRICT;
24
38
  `);
25
39
  }
26
40
  /**
27
- * Sets a key in the database
41
+ * Set a key in the database
28
42
  * @param key Key name
29
43
  * @param value Key value
30
44
  * @returns Provided value
31
45
  */
32
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
+ }
33
56
  this.#db.prepare("INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)").run(key, serialize(value));
34
57
  return value;
35
58
  }
36
59
  /**
37
- * Gets a value from the database
60
+ * Get a value from the database
38
61
  * @param key Key name
39
62
  * @returns Value or null
40
63
  */
41
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
+ }
42
70
  const row = this.#db.prepare("SELECT value FROM kv WHERE key = ?").get(key);
43
71
  return row ? deserialize(row.value) : null;
44
72
  }
45
73
  /**
46
- * Deletes a key from the database
74
+ * Delete a key from the database
47
75
  * @param key Key name
48
- * @returns Deleted key or null
76
+ * @returns KVSync instance
49
77
  */
50
78
  delete(key) {
51
- const existing = this.get(key);
52
- if (existing !== null) {
53
- this.#db.prepare("DELETE FROM kv WHERE key = ?").run(key);
79
+ if (!key || typeof key !== "string") {
80
+ throw new Error(
81
+ "[KVSync]: Key must be provided and be a non-empty string."
82
+ );
54
83
  }
55
- return existing;
84
+ this.#db.prepare("DELETE FROM kv WHERE key = ?").run(key);
85
+ return this;
56
86
  }
57
87
  /**
58
88
  * Get all data in the database
59
89
  * @returns Array of objects containing keys and values
60
90
  */
61
- all() {
62
- return this.#db.prepare("SELECT key, value FROM kv").all().map((record) => ({
63
- key: record.key,
64
- value: deserialize(record.value)
65
- }));
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;
66
102
  }
67
103
  /**
68
104
  * Remove all entries from the database
69
105
  */
70
106
  clear() {
71
- this.#db.exec("DELETE FROM kv");
107
+ this.#db.exec("DELETE FROM kv;");
108
+ return this;
72
109
  }
73
110
  /**
74
- * Updates the journal mode
111
+ * Update the journal mode
75
112
  * @param mode New journal mode
76
113
  */
77
114
  setJournalMode(mode) {
78
- const journalModes = [
79
- "DELETE",
80
- "MEMORY",
81
- "OFF",
82
- "PERSIST",
83
- "TRUNCATE",
84
- "WAL"
85
- ];
86
115
  if (!journalModes.includes(mode)) {
87
116
  throw new Error(
88
- `Invalid KVSync journal mode specified. Received: "${mode}", expected one of: ${journalModes.join(", ")}`
117
+ `[KVSync]: Invalid journal mode specified. Received: "${mode}", expected one of: ${journalModes.join(", ")}`
89
118
  );
90
119
  }
91
- this.#db.exec(`PRAGMA journal_mode = ${mode}`);
120
+ this.#db.exec(`PRAGMA journal_mode = ${mode};`);
92
121
  return this;
93
122
  }
94
123
  /**
@@ -97,6 +126,16 @@ var KVSync = class {
97
126
  * @returns Object containing oldValues and newValues each containing arrays of keys and values
98
127
  */
99
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
+ }
100
139
  this.#db.exec("BEGIN TRANSACTION;");
101
140
  const oldMap = /* @__PURE__ */ new Map();
102
141
  const newMap = /* @__PURE__ */ new Map();
@@ -117,7 +156,7 @@ var KVSync = class {
117
156
  }
118
157
  newMap.set(key, null);
119
158
  this.delete(key);
120
- return oldMap.get(key);
159
+ return tx;
121
160
  };
122
161
  try {
123
162
  callback(tx);
@@ -140,5 +179,5 @@ var KVSync = class {
140
179
  };
141
180
  export {
142
181
  KVSync,
143
- KVSync as default
182
+ journalModes
144
183
  };
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.1",
4
+ "version": "0.3.0",
5
5
  "repository": {
6
6
  "url": "https://github.com/e60m5ss/node-sqlite-kv"
7
7
  },