node-sqlite-kv 0.1.0 → 0.2.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/README.md CHANGED
@@ -58,6 +58,18 @@ kv.all();
58
58
  // // ...
59
59
  // ];
60
60
 
61
+ // transactions
62
+ kv.set("user:1", { name: "Andrew", age: 19 });
63
+ kv.set("user:2", { name: "Josh", age: 22 });
64
+ kv.set("user:3", { name: "Gabe", age: 20 });
65
+
66
+ // ...store what changed in transactions
67
+ const { oldValues, newValues } = kv.transaction((tx) => {
68
+ tx.set("user:1", { name: "Andrew", age: 20 });
69
+ tx.set("user:4", { name: "Kris", age: 21 });
70
+ tx.delete("user:2");
71
+ });
72
+
61
73
  // delete all entries
62
74
  kv.clear();
63
75
  ```
package/dist/index.d.mts CHANGED
@@ -1,14 +1,32 @@
1
- declare class KVSync {
1
+ type JournalMode = "DELETE" | "MEMORY" | "OFF" | "PERSIST" | "TRUNCATE" | "WAL";
2
+ interface KVSyncOptions {
3
+ path?: SQLitePath;
4
+ journalMode?: JournalMode;
5
+ }
6
+ type SQLitePath = ":memory:" | (string & {});
7
+
8
+ declare class KVSync<T = any> {
2
9
  #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>(): {
10
+ constructor(options?: KVSyncOptions);
11
+ set<K = T>(key: string, value: K): K;
12
+ get<K = T>(key: string): K | null;
13
+ delete<K = T>(key: string): K | null;
14
+ all<K = T>(): {
8
15
  key: string;
9
- value: T;
16
+ value: K;
10
17
  }[];
11
18
  clear(): void;
19
+ setJournalMode(mode: JournalMode): this;
20
+ transaction<R>(callback: (kv: KVSync<T>) => R): {
21
+ oldValues: {
22
+ key: string;
23
+ value: T | null | undefined;
24
+ }[];
25
+ newValues: {
26
+ key: string;
27
+ value: T | null;
28
+ }[];
29
+ };
12
30
  }
13
31
 
14
- export { KVSync, KVSync as default };
32
+ export { type JournalMode, KVSync, type KVSyncOptions, type SQLitePath, KVSync as default };
package/dist/index.d.ts CHANGED
@@ -1,16 +1,34 @@
1
- declare class KVSync {
1
+ type JournalMode = "DELETE" | "MEMORY" | "OFF" | "PERSIST" | "TRUNCATE" | "WAL";
2
+ interface KVSyncOptions {
3
+ path?: SQLitePath;
4
+ journalMode?: JournalMode;
5
+ }
6
+ type SQLitePath = ":memory:" | (string & {});
7
+
8
+ declare class KVSync<T = any> {
2
9
  #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>(): {
10
+ constructor(options?: KVSyncOptions);
11
+ set<K = T>(key: string, value: K): K;
12
+ get<K = T>(key: string): K | null;
13
+ delete<K = T>(key: string): K | null;
14
+ all<K = T>(): {
8
15
  key: string;
9
- value: T;
16
+ value: K;
10
17
  }[];
11
18
  clear(): void;
19
+ setJournalMode(mode: JournalMode): this;
20
+ transaction<R>(callback: (kv: KVSync<T>) => R): {
21
+ oldValues: {
22
+ key: string;
23
+ value: T | null | undefined;
24
+ }[];
25
+ newValues: {
26
+ key: string;
27
+ value: T | null;
28
+ }[];
29
+ };
12
30
  }
13
31
 
14
32
  // @ts-ignore
15
33
  export = KVSync;
16
- export { KVSync };
34
+ export { type JournalMode, KVSync, type KVSyncOptions, type SQLitePath };
package/dist/index.js CHANGED
@@ -38,8 +38,9 @@ var KVSync = class {
38
38
  * Create a new key-value store
39
39
  * @param path Where the database is stored, or `:memory:` for in-memory storage
40
40
  */
41
- constructor(path = ":memory:") {
42
- this.#db = new import_node_sqlite.DatabaseSync(path);
41
+ constructor(options) {
42
+ this.#db = new import_node_sqlite.DatabaseSync(options?.path ?? ":memory:");
43
+ this.setJournalMode(options?.journalMode ?? "DELETE");
43
44
  this.#db.exec(`
44
45
  CREATE TABLE IF NOT EXISTS kv (
45
46
  key TEXT PRIMARY KEY NOT NULL,
@@ -94,6 +95,73 @@ var KVSync = class {
94
95
  clear() {
95
96
  this.#db.exec("DELETE FROM kv");
96
97
  }
98
+ /**
99
+ * Updates the journal mode
100
+ * @param mode New journal mode
101
+ */
102
+ setJournalMode(mode) {
103
+ const journalModes = [
104
+ "DELETE",
105
+ "MEMORY",
106
+ "OFF",
107
+ "PERSIST",
108
+ "TRUNCATE",
109
+ "WAL"
110
+ ];
111
+ if (!journalModes.includes(mode)) {
112
+ throw new Error(
113
+ `Invalid KVSync journal mode specified. Received: "${mode}", expected one of: ${journalModes.join(", ")}`
114
+ );
115
+ }
116
+ this.#db.exec(`PRAGMA journal_mode = ${mode}`);
117
+ return this;
118
+ }
119
+ /**
120
+ * Perform a transaction
121
+ * @param callback Callback with KVSync instance
122
+ * @returns Object containing oldValues and newValues each containing arrays of keys and values
123
+ */
124
+ transaction(callback) {
125
+ this.#db.exec("BEGIN TRANSACTION;");
126
+ const oldMap = /* @__PURE__ */ new Map();
127
+ const newMap = /* @__PURE__ */ new Map();
128
+ const tx = Object.create(this);
129
+ tx.set = (key, value) => {
130
+ if (!oldMap.has(key)) {
131
+ const oldValue = this.get(key);
132
+ oldMap.set(key, oldValue === null ? void 0 : oldValue);
133
+ }
134
+ const result = this.set(key, value);
135
+ newMap.set(key, result);
136
+ return result;
137
+ };
138
+ tx.delete = (key) => {
139
+ if (!oldMap.has(key)) {
140
+ const oldValue = this.get(key);
141
+ oldMap.set(key, oldValue === null ? void 0 : oldValue);
142
+ }
143
+ newMap.set(key, null);
144
+ this.delete(key);
145
+ return oldMap.get(key);
146
+ };
147
+ try {
148
+ callback(tx);
149
+ this.#db.exec("COMMIT;");
150
+ } catch (error) {
151
+ this.#db.exec("ROLLBACK;");
152
+ throw error;
153
+ }
154
+ return {
155
+ oldValues: Array.from(oldMap.entries()).map(([key, value]) => ({
156
+ key,
157
+ value
158
+ })),
159
+ newValues: Array.from(newMap.entries()).map(([key, value]) => ({
160
+ key,
161
+ value
162
+ }))
163
+ };
164
+ }
97
165
  };
98
166
  // Annotate the CommonJS export names for ESM import in node:
99
167
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -13,8 +13,9 @@ var KVSync = class {
13
13
  * Create a new key-value store
14
14
  * @param path Where the database is stored, or `:memory:` for in-memory storage
15
15
  */
16
- constructor(path = ":memory:") {
17
- this.#db = new DatabaseSync(path);
16
+ constructor(options) {
17
+ this.#db = new DatabaseSync(options?.path ?? ":memory:");
18
+ this.setJournalMode(options?.journalMode ?? "DELETE");
18
19
  this.#db.exec(`
19
20
  CREATE TABLE IF NOT EXISTS kv (
20
21
  key TEXT PRIMARY KEY NOT NULL,
@@ -69,6 +70,73 @@ var KVSync = class {
69
70
  clear() {
70
71
  this.#db.exec("DELETE FROM kv");
71
72
  }
73
+ /**
74
+ * Updates the journal mode
75
+ * @param mode New journal mode
76
+ */
77
+ setJournalMode(mode) {
78
+ const journalModes = [
79
+ "DELETE",
80
+ "MEMORY",
81
+ "OFF",
82
+ "PERSIST",
83
+ "TRUNCATE",
84
+ "WAL"
85
+ ];
86
+ if (!journalModes.includes(mode)) {
87
+ throw new Error(
88
+ `Invalid KVSync journal mode specified. Received: "${mode}", expected one of: ${journalModes.join(", ")}`
89
+ );
90
+ }
91
+ this.#db.exec(`PRAGMA journal_mode = ${mode}`);
92
+ return this;
93
+ }
94
+ /**
95
+ * Perform a transaction
96
+ * @param callback Callback with KVSync instance
97
+ * @returns Object containing oldValues and newValues each containing arrays of keys and values
98
+ */
99
+ transaction(callback) {
100
+ this.#db.exec("BEGIN TRANSACTION;");
101
+ const oldMap = /* @__PURE__ */ new Map();
102
+ const newMap = /* @__PURE__ */ new Map();
103
+ const tx = Object.create(this);
104
+ tx.set = (key, value) => {
105
+ if (!oldMap.has(key)) {
106
+ const oldValue = this.get(key);
107
+ oldMap.set(key, oldValue === null ? void 0 : oldValue);
108
+ }
109
+ const result = this.set(key, value);
110
+ newMap.set(key, result);
111
+ return result;
112
+ };
113
+ tx.delete = (key) => {
114
+ if (!oldMap.has(key)) {
115
+ const oldValue = this.get(key);
116
+ oldMap.set(key, oldValue === null ? void 0 : oldValue);
117
+ }
118
+ newMap.set(key, null);
119
+ this.delete(key);
120
+ return oldMap.get(key);
121
+ };
122
+ try {
123
+ callback(tx);
124
+ this.#db.exec("COMMIT;");
125
+ } catch (error) {
126
+ this.#db.exec("ROLLBACK;");
127
+ throw error;
128
+ }
129
+ return {
130
+ oldValues: Array.from(oldMap.entries()).map(([key, value]) => ({
131
+ key,
132
+ value
133
+ })),
134
+ newValues: Array.from(newMap.entries()).map(([key, value]) => ({
135
+ key,
136
+ value
137
+ }))
138
+ };
139
+ }
72
140
  };
73
141
  export {
74
142
  KVSync,
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
2
  "name": "node-sqlite-kv",
3
3
  "description": "Key-value store with node:sqlite",
4
- "version": "0.1.0",
4
+ "version": "0.2.0",
5
+ "repository": {
6
+ "url": "https://github.com/e60m5ss/node-sqlite-kv"
7
+ },
5
8
  "author": {
6
9
  "name": "e60m5ss",
7
10
  "url": "https://github.com/e60m5ss"