node-sqlite-kv 0.3.0 → 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
@@ -8,7 +8,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 | undefined): K | null;
11
+ set<K = T>(key: string, value: K | undefined): K;
12
12
  get<K = T>(key: string): K | null;
13
13
  delete(key: string): KVSync;
14
14
  all<K = T>(filter?: (key: string, value: K) => boolean): {
package/dist/index.d.ts CHANGED
@@ -8,7 +8,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 | undefined): K | null;
11
+ set<K = T>(key: string, value: K | undefined): K;
12
12
  get<K = T>(key: string): K | null;
13
13
  delete(key: string): KVSync;
14
14
  all<K = T>(filter?: (key: string, value: K) => boolean): {
package/dist/index.js CHANGED
@@ -63,12 +63,9 @@ var KVSync = class {
63
63
  }
64
64
  this.#db = new import_node_sqlite.DatabaseSync(dbPath);
65
65
  this.setJournalMode(options?.journalMode ?? "DELETE");
66
- this.#db.exec(`
67
- CREATE TABLE IF NOT EXISTS kv (
68
- key TEXT PRIMARY KEY NOT NULL,
69
- value BLOB NOT NULL
70
- ) STRICT;
71
- `);
66
+ this.#db.exec(
67
+ "CREATE TABLE IF NOT EXISTS kv (key TEXT PRIMARY KEY NOT NULL, value BLOB NOT NULL) STRICT;"
68
+ );
72
69
  }
73
70
  /**
74
71
  * Set a key in the database
@@ -83,8 +80,9 @@ var KVSync = class {
83
80
  );
84
81
  }
85
82
  if (value === void 0) {
86
- this.delete(key);
87
- return null;
83
+ throw new Error(
84
+ "[KVSync]: Provided value is undefined. Did you mean to use delete() instead?"
85
+ );
88
86
  }
89
87
  this.#db.prepare("INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)").run(key, (0, import_node_v8.serialize)(value));
90
88
  return value;
@@ -169,7 +167,6 @@ var KVSync = class {
169
167
  `[KVSync]: Transaction callback must be of type function. Received: ${typeof callback}`
170
168
  );
171
169
  }
172
- this.#db.exec("BEGIN TRANSACTION;");
173
170
  const oldMap = /* @__PURE__ */ new Map();
174
171
  const newMap = /* @__PURE__ */ new Map();
175
172
  const tx = Object.create(this);
@@ -178,9 +175,8 @@ var KVSync = class {
178
175
  const oldValue = this.get(key);
179
176
  oldMap.set(key, oldValue === null ? void 0 : oldValue);
180
177
  }
181
- const result = this.set(key, value);
182
- newMap.set(key, result);
183
- return result;
178
+ newMap.set(key, value);
179
+ return value ?? null;
184
180
  };
185
181
  tx.delete = (key) => {
186
182
  if (!oldMap.has(key)) {
@@ -188,11 +184,18 @@ var KVSync = class {
188
184
  oldMap.set(key, oldValue === null ? void 0 : oldValue);
189
185
  }
190
186
  newMap.set(key, null);
191
- this.delete(key);
192
187
  return tx;
193
188
  };
194
189
  try {
195
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
+ }
196
199
  this.#db.exec("COMMIT;");
197
200
  } catch (error) {
198
201
  this.#db.exec("ROLLBACK;");
package/dist/index.mjs CHANGED
@@ -30,12 +30,9 @@ var KVSync = class {
30
30
  }
31
31
  this.#db = new DatabaseSync(dbPath);
32
32
  this.setJournalMode(options?.journalMode ?? "DELETE");
33
- this.#db.exec(`
34
- CREATE TABLE IF NOT EXISTS kv (
35
- key TEXT PRIMARY KEY NOT NULL,
36
- value BLOB NOT NULL
37
- ) STRICT;
38
- `);
33
+ this.#db.exec(
34
+ "CREATE TABLE IF NOT EXISTS kv (key TEXT PRIMARY KEY NOT NULL, value BLOB NOT NULL) STRICT;"
35
+ );
39
36
  }
40
37
  /**
41
38
  * Set a key in the database
@@ -50,8 +47,9 @@ var KVSync = class {
50
47
  );
51
48
  }
52
49
  if (value === void 0) {
53
- this.delete(key);
54
- return null;
50
+ throw new Error(
51
+ "[KVSync]: Provided value is undefined. Did you mean to use delete() instead?"
52
+ );
55
53
  }
56
54
  this.#db.prepare("INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)").run(key, serialize(value));
57
55
  return value;
@@ -136,7 +134,6 @@ var KVSync = class {
136
134
  `[KVSync]: Transaction callback must be of type function. Received: ${typeof callback}`
137
135
  );
138
136
  }
139
- this.#db.exec("BEGIN TRANSACTION;");
140
137
  const oldMap = /* @__PURE__ */ new Map();
141
138
  const newMap = /* @__PURE__ */ new Map();
142
139
  const tx = Object.create(this);
@@ -145,9 +142,8 @@ var KVSync = class {
145
142
  const oldValue = this.get(key);
146
143
  oldMap.set(key, oldValue === null ? void 0 : oldValue);
147
144
  }
148
- const result = this.set(key, value);
149
- newMap.set(key, result);
150
- return result;
145
+ newMap.set(key, value);
146
+ return value ?? null;
151
147
  };
152
148
  tx.delete = (key) => {
153
149
  if (!oldMap.has(key)) {
@@ -155,11 +151,18 @@ var KVSync = class {
155
151
  oldMap.set(key, oldValue === null ? void 0 : oldValue);
156
152
  }
157
153
  newMap.set(key, null);
158
- this.delete(key);
159
154
  return tx;
160
155
  };
161
156
  try {
162
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
+ }
163
166
  this.#db.exec("COMMIT;");
164
167
  } catch (error) {
165
168
  this.#db.exec("ROLLBACK;");
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.3.0",
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"