@ix-xs/node-comfort 1.0.53 → 1.0.54

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/localStorage.js +71 -83
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ix-xs/node-comfort",
3
- "version": "1.0.53",
3
+ "version": "1.0.54",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
@@ -3,8 +3,10 @@ const checker = require("./checker");
3
3
 
4
4
  const originalEmitWarning = process.emitWarning;
5
5
  process.emitWarning = (warning, ...args) => {
6
- if ((warning.name === "ExperimentalWarning" && warning.message.includes("SQLite")) ||
7
- (typeof warning === "string" && warning.startsWith("SQLite is an experimental feature"))) {
6
+ if (
7
+ (warning.name === "ExperimentalWarning" && warning.message.includes("SQLite")) ||
8
+ (typeof warning === "string" && warning.startsWith("SQLite is an experimental feature"))
9
+ ) {
8
10
  return;
9
11
  }
10
12
  return originalEmitWarning.call(process, warning, ...args);
@@ -32,7 +34,10 @@ module.exports = class localStorage {
32
34
  #exec(sql, options = []) {
33
35
  try {
34
36
  const stmt = this.#$.prepare(sql);
35
- const result = stmt.run(...checker.isArray(options) ? options : [options]);
37
+ const result = checker.isArray(options)
38
+ ? stmt.run(...options)
39
+ : stmt.run(options);
40
+
36
41
  return {
37
42
  changes: result.changes,
38
43
  lastInsertRowid: result.lastInsertRowid,
@@ -41,26 +46,32 @@ module.exports = class localStorage {
41
46
  throw error;
42
47
  }
43
48
  }
44
- #buildCreateSQL(tableName, options) {
45
- const columns = Object.entries(options).map(([col, def]) => {
46
- let colDef = `${col} ${def.type}`;
47
49
 
48
- if (def.primaryKey) {
49
- colDef += " PRIMARY KEY";
50
- if (def.autoincrement) colDef += " AUTOINCREMENT";
51
- }
52
- if (def.notNull) colDef += " NOT NULL";
53
- if (def.unique) colDef += " UNIQUE";
54
- if (def.defaultValue !== undefined) colDef += ` DEFAULT ${JSON.stringify(def.defaultValue)}`;
55
- if (def.values?.length) {
56
- colDef += ` CHECK(${col} IN (${def.values.map(v => JSON.stringify(v)).join(", ")}))`;
57
- }
58
-
59
- return colDef;
60
- }).join(", ");
50
+ #buildCreateSQL(tableName, options) {
51
+ const columns = Object.entries(options)
52
+ .map(([col, def]) => {
53
+ let colDef = `${col} ${def.type}`;
54
+
55
+ if (def.primaryKey) {
56
+ colDef += " PRIMARY KEY";
57
+ if (def.autoincrement) colDef += " AUTOINCREMENT";
58
+ }
59
+ if (def.notNull) colDef += " NOT NULL";
60
+ if (def.unique) colDef += " UNIQUE";
61
+ if (def.defaultValue !== undefined) {
62
+ colDef += ` DEFAULT ${JSON.stringify(def.defaultValue)}`;
63
+ }
64
+ if (def.values?.length) {
65
+ colDef += ` CHECK(${col} IN (${def.values.map(v => JSON.stringify(v)).join(", ")}))`;
66
+ }
67
+
68
+ return colDef;
69
+ })
70
+ .join(", ");
61
71
 
62
72
  return `CREATE TABLE IF NOT EXISTS ${tableName} (${columns})`;
63
73
  }
74
+
64
75
  #defineModel(tableName, options) {
65
76
  if (this.#models.has(tableName)) return;
66
77
 
@@ -68,8 +79,9 @@ module.exports = class localStorage {
68
79
  this.#exec(createSQL);
69
80
  this.#models.set(tableName, { name: tableName, options });
70
81
  }
71
- #buildWhere(where) {
72
- if (Object.keys(where).length === 0) {
82
+
83
+ #buildWhere(where = {}) {
84
+ if (!where || Object.keys(where).length === 0) {
73
85
  return { where: "", params: [] };
74
86
  }
75
87
 
@@ -86,6 +98,7 @@ module.exports = class localStorage {
86
98
  params,
87
99
  };
88
100
  }
101
+
89
102
  #getOrderBy(tableName) {
90
103
  const model = this.#models.get(tableName);
91
104
  if (!model) return "id DESC";
@@ -93,17 +106,6 @@ module.exports = class localStorage {
93
106
  return `${firstCol} DESC`;
94
107
  }
95
108
 
96
- /**
97
- * @param {{name: string, options: Record<string, {
98
- * type: "TEXT"|"INTEGER"|"REAL"|"BLOB"|"NUMERIC"|"DATE"|"DATETIME"|"TIMESTAMP"|"TIME"|"VARCHAR"|"CHAR"|"CLOB",
99
- * primaryKey?: boolean,
100
- * unique?: boolean,
101
- * notNull: boolean,
102
- * defaultValue?: any,
103
- * values?: Array<string>
104
- * autoincrement?: boolean,
105
- * }>}} table
106
- */
107
109
  async createTable(table) {
108
110
  try {
109
111
  this.#defineModel(table.name, table.options);
@@ -112,12 +114,12 @@ module.exports = class localStorage {
112
114
  return { error: error.message };
113
115
  }
114
116
  }
115
- /**
116
- * @param {string} tableName
117
- */
117
+
118
118
  async deleteTable(tableName) {
119
119
  try {
120
- if (!this.#models.has(tableName)) return { error: `Table ${tableName} doesn't exist` };
120
+ if (!this.#models.has(tableName)) {
121
+ return { error: `Table ${tableName} doesn't exist` };
122
+ }
121
123
 
122
124
  this.#exec(`DROP TABLE IF EXISTS ${tableName}`);
123
125
  this.#models.delete(tableName);
@@ -126,12 +128,12 @@ module.exports = class localStorage {
126
128
  return { error: error.message };
127
129
  }
128
130
  }
129
- /**
130
- * @param {string} tableName
131
- */
131
+
132
132
  async clearTable(tableName) {
133
133
  try {
134
- if (!this.#models.has(tableName)) return { error: `Table ${tableName} doesn't exist` };
134
+ if (!this.#models.has(tableName)) {
135
+ return { error: `Table ${tableName} doesn't exist` };
136
+ }
135
137
 
136
138
  this.#exec(`DELETE FROM ${tableName}`);
137
139
  return { ok: true };
@@ -139,10 +141,7 @@ module.exports = class localStorage {
139
141
  return { error: error.message };
140
142
  }
141
143
  }
142
- /**
143
- * @param {string} tableName
144
- * @param {object} [where]
145
- */
144
+
146
145
  async get(tableName, where) {
147
146
  try {
148
147
  if (!this.#models.has(tableName)) return undefined;
@@ -150,24 +149,22 @@ module.exports = class localStorage {
150
149
  const whereClause = this.#buildWhere(where);
151
150
  const sql = `SELECT * FROM ${tableName} ${whereClause.where} LIMIT 1`;
152
151
  const stmt = this.#$.prepare(sql);
153
- const record = stmt.get(whereClause.params);
152
+ const record = stmt.get(...whereClause.params);
153
+
154
154
  return record || undefined;
155
155
  } catch (error) {
156
156
  return { error: error.message };
157
157
  }
158
158
  }
159
- /**
160
- * @param {string} tableName
161
- * @param {object} [where]
162
- * @param {number} [limit]
163
- * @param {number} [offset]
164
- */
159
+
165
160
  async getAll(tableName, where = {}, limit = 50, offset = 0) {
166
161
  try {
167
162
  if (!this.#models.has(tableName)) return [];
168
163
 
169
164
  const whereClause = this.#buildWhere(where);
170
- const sql = `SELECT * FROM ${tableName} ${whereClause.where} ORDER BY ${this.#getOrderBy(tableName)} LIMIT ? OFFSET ?`;
165
+ const sql = `SELECT * FROM ${tableName} ${whereClause.where} ORDER BY ${this.#getOrderBy(
166
+ tableName,
167
+ )} LIMIT ? OFFSET ?`;
171
168
  const stmt = this.#$.prepare(sql);
172
169
  const records = stmt.all(...whereClause.params, limit, offset);
173
170
  return records;
@@ -175,43 +172,35 @@ module.exports = class localStorage {
175
172
  return { error: error.message };
176
173
  }
177
174
  }
178
- /**
179
- * @param {string} tableName
180
- * @param {*} data
181
- * @param {object} [where]
182
- */
175
+
183
176
  async set(tableName, data, where = null) {
184
177
  try {
185
- if (!this.#models.has(tableName)) return { error: `Table ${tableName} doesn't exist` };
178
+ if (!this.#models.has(tableName)) {
179
+ return { error: `Table ${tableName} doesn't exist` };
180
+ }
186
181
 
187
182
  const lookup = where ?? (data.id ? { id: data.id } : null);
188
183
 
189
- if (lookup) {
190
- const columns = Object.keys(data).join(", ");
191
- const placeholders = Object.keys(data).map(() => "?").join(", ");
192
- const sql = `INSERT OR REPLACE INTO ${tableName} (${columns}) VALUES (${placeholders})`;
193
- const stmt = this.#$.prepare(sql);
194
- stmt.run(...Object.values(data));
195
- return { ok: true };
196
- } else {
197
- const columns = Object.keys(data).join(", ");
198
- const placeholders = Object.keys(data).map(() => "?").join(", ");
199
- const sql = `INSERT INTO ${tableName} (${columns}) VALUES (${placeholders})`;
200
- const stmt = this.#$.prepare(sql);
201
- stmt.run(...Object.values(data));
202
- return { ok: true };
203
- }
184
+ const columns = Object.keys(data).join(", ");
185
+ const placeholders = Object.keys(data).map(() => "?").join(", ");
186
+ const sql = lookup
187
+ ? `INSERT OR REPLACE INTO ${tableName} (${columns}) VALUES (${placeholders})`
188
+ : `INSERT INTO ${tableName} (${columns}) VALUES (${placeholders})`;
189
+
190
+ const stmt = this.#$.prepare(sql);
191
+ stmt.run(...Object.values(data));
192
+
193
+ return { ok: true };
204
194
  } catch (error) {
205
195
  return { error: error.message };
206
196
  }
207
197
  }
208
- /**
209
- * @param {string} tableName
210
- * @param {object} where
211
- */
198
+
212
199
  async delete(tableName, where) {
213
200
  try {
214
- if (!this.#models.has(tableName)) return { error: `Table ${tableName} doesn't exist` };
201
+ if (!this.#models.has(tableName)) {
202
+ return { error: `Table ${tableName} doesn't exist` };
203
+ }
215
204
 
216
205
  const whereClause = this.#buildWhere(where);
217
206
  const sql = `DELETE FROM ${tableName} ${whereClause.where}`;
@@ -222,13 +211,12 @@ module.exports = class localStorage {
222
211
  return { error: error.message };
223
212
  }
224
213
  }
225
- /**
226
- * @param {string} tableName
227
- * @param {object} where
228
- */
214
+
229
215
  async count(tableName, where = {}) {
230
216
  try {
231
- if (!this.#models.has(tableName)) return { error: `Table ${tableName} doesn't exist` };
217
+ if (!this.#models.has(tableName)) {
218
+ return { error: `Table ${tableName} doesn't exist` };
219
+ }
232
220
 
233
221
  const whereClause = this.#buildWhere(where);
234
222
  const sql = `SELECT COUNT(*) as total FROM ${tableName} ${whereClause.where}`;