node-sqlite-kv 1.0.0 → 1.1.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.cjs CHANGED
@@ -98,6 +98,23 @@ var KVSync = class {
98
98
  return value;
99
99
  }
100
100
  /**
101
+ * Set a key only if it doesn't already exist
102
+ * @param key Key name
103
+ * @param value Key value
104
+ * @returns True if key was set, false if already existed
105
+ */
106
+ setnx(key, value) {
107
+ if (!this.#db.isOpen) throw new KVError("setnx", "Database is not open");
108
+ if (!key || typeof key !== "string") throw new KVError("setnx", "Key must be provided and be a non-empty string");
109
+ if (value === void 0) throw new KVError("setnx", "Provided value is undefined, did you mean to use delete()?");
110
+ try {
111
+ this.#db.prepare(`INSERT INTO ${this.tableName} (key, value) VALUES (?, ?)`).run(key, (0, node_v8.serialize)(value));
112
+ return true;
113
+ } catch {
114
+ return false;
115
+ }
116
+ }
117
+ /**
101
118
  * Get a value from the database
102
119
  * @param key Key name
103
120
  * @returns Value or undefined
@@ -166,6 +183,7 @@ var KVSync = class {
166
183
  if (typeof callback !== "function") throw new KVError("transaction", `Transaction callback must be of type function. Received: ${typeof callback}`);
167
184
  const oldMap = /* @__PURE__ */ new Map();
168
185
  const newMap = /* @__PURE__ */ new Map();
186
+ const setnxKeys = /* @__PURE__ */ new Set();
169
187
  const tx = Object.create(this);
170
188
  tx.set = (key, value) => {
171
189
  if (!oldMap.has(key)) {
@@ -183,10 +201,23 @@ var KVSync = class {
183
201
  newMap.set(key, void 0);
184
202
  return tx;
185
203
  };
204
+ tx.setnx = (key, value) => {
205
+ if (!oldMap.has(key)) {
206
+ const oldValue = this.get(key);
207
+ oldMap.set(key, oldValue);
208
+ }
209
+ if (oldMap.get(key) === void 0) {
210
+ newMap.set(key, value);
211
+ setnxKeys.add(key);
212
+ return true;
213
+ }
214
+ return false;
215
+ };
186
216
  try {
187
217
  this.#db.exec("BEGIN TRANSACTION");
188
218
  callback(tx);
189
219
  for (const [key, value] of newMap.entries()) if (value === void 0) this.delete(key);
220
+ else if (setnxKeys.has(key)) this.setnx(key, value);
190
221
  else this.set(key, value);
191
222
  this.#db.exec("COMMIT");
192
223
  } catch (error) {
package/dist/index.d.cts CHANGED
@@ -56,6 +56,13 @@ declare class KVSync<T = any> {
56
56
  * @returns Provided value
57
57
  */
58
58
  set<K = T>(key: string, value: K | undefined): K;
59
+ /**
60
+ * Set a key only if it doesn't already exist
61
+ * @param key Key name
62
+ * @param value Key value
63
+ * @returns True if key was set, false if already existed
64
+ */
65
+ setnx<K = T>(key: string, value: K): boolean;
59
66
  /**
60
67
  * Get a value from the database
61
68
  * @param key Key name
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": "1.0.0",
4
+ "version": "1.1.0",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {
7
7
  "url": "https://github.com/andrewdku/node-sqlite-kv"
@@ -16,8 +16,14 @@
16
16
  "type": "module",
17
17
  "engineStrict": true,
18
18
  "engines": {
19
- "node": ">=22.5.0"
19
+ "node": ">=22"
20
20
  },
21
+ "files": [
22
+ "dist",
23
+ "LICENSE",
24
+ "package.json",
25
+ "README.md"
26
+ ],
21
27
  "devDependencies": {
22
28
  "@types/node": "^25.5.0",
23
29
  "prettier": "^3.8.1",
package/.prettierrc.json DELETED
@@ -1,11 +0,0 @@
1
- {
2
- "arrowParens": "always",
3
- "jsxSingleQuote": false,
4
- "printWidth": 90,
5
- "quoteProps": "as-needed",
6
- "semi": false,
7
- "singleQuote": false,
8
- "tabWidth": 4,
9
- "trailingComma": "es5",
10
- "useTabs": false
11
- }
@@ -1,4 +0,0 @@
1
- {
2
- "prettier.configPath": ".prettierrc.json",
3
- "prettier.prettierPath": "node_modules/prettier"
4
- }
package/tsdown.config.mjs DELETED
@@ -1,17 +0,0 @@
1
- // @ts-check
2
-
3
- import { defineConfig } from "tsdown"
4
-
5
- export default defineConfig({
6
- cjsDefault: true,
7
- clean: true,
8
- deps: { skipNodeModulesBundle: true },
9
- dts: true,
10
- entry: ["src/index.ts"],
11
- format: ["cjs"],
12
- minify: false,
13
- platform: "node",
14
- removeNodeProtocol: false,
15
- shims: true,
16
- target: "node22",
17
- })