@onurege3467/zerohelper 2.0.1 → 2.0.3

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.
@@ -1,6 +1,7 @@
1
1
  const fs = require("fs");
2
2
  const path = require("path");
3
3
  const sqlite3 = require("sqlite3").verbose();
4
+
4
5
  class Database {
5
6
  constructor(dbFilePath) {
6
7
  this.dbFilePath = dbFilePath || path.join(__dirname, "database.sqlite");
@@ -23,14 +24,45 @@ class Database {
23
24
  )
24
25
  .then(() => {
25
26
  console.log("Table initialized");
26
- i = 1;
27
27
  })
28
28
  .catch((err) => console.error(err));
29
29
  }
30
30
  });
31
31
  }
32
32
 
33
+ _parseNestedKey(key) {
34
+ return key.includes(".") ? key.split(".") : [key];
35
+ }
36
+
37
+ _buildNestedObject(keys, value) {
38
+ return keys.reverse().reduce((acc, curr) => ({ [curr]: acc }), value);
39
+ }
40
+
41
+ _mergeObjects(target, source) {
42
+ for (const key in source) {
43
+ if (source[key] instanceof Object && key in target) {
44
+ Object.assign(
45
+ source[key],
46
+ this._mergeObjects(target[key], source[key])
47
+ );
48
+ }
49
+ }
50
+ return { ...target, ...source };
51
+ }
52
+
33
53
  set(key, value) {
54
+ const keys = this._parseNestedKey(key);
55
+ if (keys.length > 1) {
56
+ return this.get(keys[0]).then((currentValue) => {
57
+ const nestedObject = this._buildNestedObject(keys.slice(1), value);
58
+ const mergedValue = this._mergeObjects(
59
+ currentValue || {},
60
+ nestedObject
61
+ );
62
+ return this.set(keys[0], mergedValue);
63
+ });
64
+ }
65
+
34
66
  return this.runQuery(
35
67
  `INSERT INTO key_value_store (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value`,
36
68
  [key, JSON.stringify(value)]
@@ -38,19 +70,40 @@ class Database {
38
70
  }
39
71
 
40
72
  get(key) {
73
+ const keys = this._parseNestedKey(key);
41
74
  return this.getQuery(`SELECT value FROM key_value_store WHERE key = ?`, [
42
- key,
43
- ]).then((row) => (row ? JSON.parse(row.value) : null));
75
+ keys[0],
76
+ ]).then((row) => {
77
+ if (!row) return null;
78
+ const value = JSON.parse(row.value);
79
+ return keys.length > 1
80
+ ? keys.slice(1).reduce((acc, curr) => (acc ? acc[curr] : null), value)
81
+ : value;
82
+ });
44
83
  }
45
84
 
46
85
  delete(key) {
86
+ const keys = this._parseNestedKey(key);
87
+ if (keys.length > 1) {
88
+ return this.get(keys[0]).then((currentValue) => {
89
+ if (!currentValue) return null;
90
+ let ref = currentValue;
91
+ for (let i = 0; i < keys.length - 1; i++) {
92
+ if (!ref[keys[i]]) return null;
93
+ if (i === keys.length - 2) {
94
+ delete ref[keys[i + 1]];
95
+ } else {
96
+ ref = ref[keys[i]];
97
+ }
98
+ }
99
+ return this.set(keys[0], currentValue);
100
+ });
101
+ }
47
102
  return this.runQuery(`DELETE FROM key_value_store WHERE key = ?`, [key]);
48
103
  }
49
104
 
50
105
  has(key) {
51
- return this.getQuery(`SELECT 1 FROM key_value_store WHERE key = ?`, [
52
- key,
53
- ]).then((row) => !!row);
106
+ return this.get(key).then((value) => value !== null);
54
107
  }
55
108
 
56
109
  push(key, value) {
@@ -121,22 +174,11 @@ module.exports = Database;
121
174
  // Example usage:
122
175
  // const db = new Database();
123
176
  // db.initialize();
124
- // db.set('foo', 'bar')
177
+ // db.set('foo.bar.baz', 'value')
178
+ // .then(() => db.get('foo.bar'))
179
+ // .then((value) => console.log('Value:', value)) // Output: { baz: 'value' }
180
+ // .then(() => db.delete('foo.bar.baz'))
125
181
  // .then(() => db.get('foo'))
126
- // .then((value) => console.log('Value:', value)) // Output: bar
127
- // .then(() => db.push('array', 'x'))
128
- // .then(() => db.get('array'))
129
- // .then((value) => console.log('Array:', value)) // Output: ['x']
130
- // .then(() => db.add('number', 1))
131
- // .then(() => db.get('number'))
132
- // .then((value) => console.log('Number:', value)) // Output: 1
133
- // .then(() => db.sub('number', 1))
134
- // .then(() => db.get('number'))
135
- // .then((value) => console.log('Number after sub:', value)) // Output: 0
136
- // .then(() => db.has('foo'))
137
- // .then((exists) => console.log('Exists:', exists)) // Output: true
138
- // .then(() => db.delete('foo'))
139
- // .then(() => db.has('foo'))
140
- // .then((exists) => console.log('Exists after delete:', exists)) // Output: false
182
+ // .then((value) => console.log('After delete:', value)) // Output: {}
141
183
  // .catch((err) => console.error(err))
142
184
  // .finally(() => db.close());
package/database/test.js CHANGED
@@ -44,8 +44,8 @@ const runSQLite = async () => {
44
44
  const { database } = require("../index");
45
45
  var db = new database.SQLiteDatabase();
46
46
  await db.initialize();
47
- await db.set("foo", "bar");
48
- console.log(await db.get("foo"));
47
+ await db.set("foo.test", "bar");
48
+ console.log(await db.get("foo.test"));
49
49
  };
50
50
 
51
51
  runSQLite();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onurege3467/zerohelper",
3
- "version": "2.0.1",
3
+ "version": "2.0.3",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "node test.js"