node-sqlite-kv 0.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/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2026 Andrew (e60m5ss / wlix)
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,71 @@
1
+ <h1 align="center">node-sqlite-kv</h1>
2
+
3
+ > Key-value store with node:sqlite (Node.js v22.5.0 or higher is required)
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install node-sqlite-kv
9
+ # or
10
+ yarn add node-sqlite-kv
11
+ # or
12
+ pnpm add node-sqlite-kv
13
+ # or
14
+ bun add node-sqlite-kv
15
+ ```
16
+
17
+ ## Example
18
+
19
+ ```js
20
+ import { KVSync } from "node-sqlite-kv";
21
+
22
+ // use :memory: for in-memory storage
23
+ // path is optional, defaults to :memory:
24
+ const kv = new KVSync("./data.sqlite");
25
+
26
+ // set values
27
+ kv.set("number", 123);
28
+ kv.set("string", "hello world");
29
+ kv.set("boolean", true);
30
+ kv.set("null", null);
31
+ kv.set("array", [1, 2, 3]);
32
+ kv.set("object", { settings: { theme: "dark" } });
33
+ kv.set("date", new Date());
34
+
35
+ // get values
36
+ kv.get("number"); // 123
37
+ kv.get("string"); // "hello world"
38
+ kv.get("boolean"); // true
39
+ kv.get("null"); // null
40
+ kv.get("array"); // [1, 2, 3]
41
+ kv.get("object"); // { settings: { theme: "dark" } }
42
+ kv.get("date"); // Date
43
+
44
+ // update values
45
+ kv.set("number", 999);
46
+ kv.get("number"); // 999
47
+
48
+ // delete values
49
+ kv.delete("array"); // [1, 2, 3]
50
+ kv.get("array"); // null
51
+
52
+ // list all entries
53
+ kv.all();
54
+ // [
55
+ // { key: "string", value: "hello world" },
56
+ // { key: "number", value: 999 },
57
+ // { key: "boolean", value: true },
58
+ // // ...
59
+ // ];
60
+
61
+ // delete all entries
62
+ kv.clear();
63
+ ```
64
+
65
+ ## Contributing
66
+
67
+ Pull requests are always welcomed. For more major changes, please open an issue to discuss what you wish to change.
68
+
69
+ ## License
70
+
71
+ [MIT](LICENSE)
@@ -0,0 +1,14 @@
1
+ declare class KVSync {
2
+ #private;
3
+ constructor(path?: ":memory:" | (string & {}));
4
+ set<T = any>(key: string, value: T): T;
5
+ get<T = any>(key: string): T | null;
6
+ delete<T = any>(key: string): T | null;
7
+ all<T = any>(): {
8
+ key: string;
9
+ value: T;
10
+ }[];
11
+ clear(): void;
12
+ }
13
+
14
+ export { KVSync, KVSync as default };
@@ -0,0 +1,16 @@
1
+ declare class KVSync {
2
+ #private;
3
+ constructor(path?: ":memory:" | (string & {}));
4
+ set<T = any>(key: string, value: T): T;
5
+ get<T = any>(key: string): T | null;
6
+ delete<T = any>(key: string): T | null;
7
+ all<T = any>(): {
8
+ key: string;
9
+ value: T;
10
+ }[];
11
+ clear(): void;
12
+ }
13
+
14
+ // @ts-ignore
15
+ export = KVSync;
16
+ export { KVSync };
package/dist/index.js ADDED
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ KVSync: () => KVSync,
25
+ default: () => KVSync
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+
29
+ // src/structures/KVSync.ts
30
+ var import_node_sqlite = require("node:sqlite");
31
+ var import_node_v8 = require("node:v8");
32
+ var KVSync = class {
33
+ static {
34
+ __name(this, "KVSync");
35
+ }
36
+ #db;
37
+ /**
38
+ * Create a new key-value store
39
+ * @param path Where the database is stored, or `:memory:` for in-memory storage
40
+ */
41
+ constructor(path = ":memory:") {
42
+ this.#db = new import_node_sqlite.DatabaseSync(path);
43
+ this.#db.exec(`
44
+ CREATE TABLE IF NOT EXISTS kv (
45
+ key TEXT PRIMARY KEY NOT NULL,
46
+ value BLOB NOT NULL
47
+ ) STRICT
48
+ `);
49
+ }
50
+ /**
51
+ * Sets a key in the database
52
+ * @param key Key name
53
+ * @param value Key value
54
+ * @returns Provided value
55
+ */
56
+ set(key, value) {
57
+ this.#db.prepare("INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)").run(key, (0, import_node_v8.serialize)(value));
58
+ return value;
59
+ }
60
+ /**
61
+ * Gets a value from the database
62
+ * @param key Key name
63
+ * @returns Value or null
64
+ */
65
+ get(key) {
66
+ const row = this.#db.prepare("SELECT value FROM kv WHERE key = ?").get(key);
67
+ return row ? (0, import_node_v8.deserialize)(row.value) : null;
68
+ }
69
+ /**
70
+ * Deletes a key from the database
71
+ * @param key Key name
72
+ * @returns Deleted key or null
73
+ */
74
+ delete(key) {
75
+ const existing = this.get(key);
76
+ if (existing !== null) {
77
+ this.#db.prepare("DELETE FROM kv WHERE key = ?").run(key);
78
+ }
79
+ return existing;
80
+ }
81
+ /**
82
+ * Get all data in the database
83
+ * @returns Array of objects containing keys and values
84
+ */
85
+ all() {
86
+ return this.#db.prepare("SELECT key, value FROM kv").all().map((record) => ({
87
+ key: record.key,
88
+ value: (0, import_node_v8.deserialize)(record.value)
89
+ }));
90
+ }
91
+ /**
92
+ * Remove all entries from the database
93
+ */
94
+ clear() {
95
+ this.#db.exec("DELETE FROM kv");
96
+ }
97
+ };
98
+ // Annotate the CommonJS export names for ESM import in node:
99
+ 0 && (module.exports = {
100
+ KVSync
101
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,76 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
+
4
+ // src/structures/KVSync.ts
5
+ import { DatabaseSync } from "node:sqlite";
6
+ import { serialize, deserialize } from "node:v8";
7
+ var KVSync = class {
8
+ static {
9
+ __name(this, "KVSync");
10
+ }
11
+ #db;
12
+ /**
13
+ * Create a new key-value store
14
+ * @param path Where the database is stored, or `:memory:` for in-memory storage
15
+ */
16
+ constructor(path = ":memory:") {
17
+ this.#db = new DatabaseSync(path);
18
+ this.#db.exec(`
19
+ CREATE TABLE IF NOT EXISTS kv (
20
+ key TEXT PRIMARY KEY NOT NULL,
21
+ value BLOB NOT NULL
22
+ ) STRICT
23
+ `);
24
+ }
25
+ /**
26
+ * Sets a key in the database
27
+ * @param key Key name
28
+ * @param value Key value
29
+ * @returns Provided value
30
+ */
31
+ set(key, value) {
32
+ this.#db.prepare("INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)").run(key, serialize(value));
33
+ return value;
34
+ }
35
+ /**
36
+ * Gets a value from the database
37
+ * @param key Key name
38
+ * @returns Value or null
39
+ */
40
+ get(key) {
41
+ const row = this.#db.prepare("SELECT value FROM kv WHERE key = ?").get(key);
42
+ return row ? deserialize(row.value) : null;
43
+ }
44
+ /**
45
+ * Deletes a key from the database
46
+ * @param key Key name
47
+ * @returns Deleted key or null
48
+ */
49
+ delete(key) {
50
+ const existing = this.get(key);
51
+ if (existing !== null) {
52
+ this.#db.prepare("DELETE FROM kv WHERE key = ?").run(key);
53
+ }
54
+ return existing;
55
+ }
56
+ /**
57
+ * Get all data in the database
58
+ * @returns Array of objects containing keys and values
59
+ */
60
+ all() {
61
+ return this.#db.prepare("SELECT key, value FROM kv").all().map((record) => ({
62
+ key: record.key,
63
+ value: deserialize(record.value)
64
+ }));
65
+ }
66
+ /**
67
+ * Remove all entries from the database
68
+ */
69
+ clear() {
70
+ this.#db.exec("DELETE FROM kv");
71
+ }
72
+ };
73
+ export {
74
+ KVSync,
75
+ KVSync as default
76
+ };
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "node-sqlite-kv",
3
+ "description": "Key-value store with node:sqlite",
4
+ "version": "0.1.0",
5
+ "author": {
6
+ "name": "e60m5ss",
7
+ "url": "https://github.com/e60m5ss"
8
+ },
9
+ "main": "./dist/index.js",
10
+ "module": "./dist/index.mjs",
11
+ "types": "./dist/index.d.ts",
12
+ "engineStrict": true,
13
+ "engines": {
14
+ "node": ">=22.5.0"
15
+ },
16
+ "prettier": {
17
+ "jsxSingleQuote": false,
18
+ "printWidth": 85,
19
+ "semi": true,
20
+ "singleQuote": false,
21
+ "tabWidth": 4,
22
+ "trailingComma": "es5",
23
+ "useTabs": false
24
+ },
25
+ "devDependencies": {
26
+ "@types/node": "^25.1.0",
27
+ "prettier": "^3.8.1",
28
+ "tsup": "^8.5.1",
29
+ "typescript": "^5.9.3"
30
+ },
31
+ "scripts": {
32
+ "build": "tsup",
33
+ "format": "prettier ./src --write --ignore-path=.prettierignore",
34
+ "lint": "tsc --noEmit; prettier ./src --check --ignore-path=.prettierignore",
35
+ "prepublish": "tsup"
36
+ }
37
+ }