@ix-xs/node-comfort 1.0.55 → 1.0.56

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 +21 -10
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ix-xs/node-comfort",
3
- "version": "1.0.55",
3
+ "version": "1.0.56",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
@@ -203,10 +203,10 @@ module.exports = class localStorage {
203
203
  }
204
204
 
205
205
  /**
206
- * @param {string} tableName
207
- * @param {*} data
208
- * @param {object} where
209
- */
206
+ * @param {string} tableName
207
+ * @param {object} data
208
+ * @param {object|null} where
209
+ */
210
210
  async set(tableName, data, where = null) {
211
211
  try {
212
212
  if (!this.#models.has(tableName)) {
@@ -215,14 +215,24 @@ module.exports = class localStorage {
215
215
 
216
216
  const lookup = where ?? (data.id ? { id: data.id } : null);
217
217
 
218
- const columns = Object.keys(data).join(", ");
219
- const placeholders = Object.keys(data).map(() => "?").join(", ");
220
- const sql = lookup
221
- ? `INSERT OR REPLACE INTO ${tableName} (${columns}) VALUES (${placeholders})`
222
- : `INSERT INTO ${tableName} (${columns}) VALUES (${placeholders})`;
218
+ let finalData = data;
219
+
220
+ if (lookup) {
221
+ const existing = await this.get(tableName, lookup);
222
+
223
+ if (existing && !existing.error) {
224
+ finalData = { ...existing, ...data };
225
+ }
226
+ }
227
+
228
+ const columns = Object.keys(finalData);
229
+ const placeholders = columns.map(() => "?").join(", ");
230
+ const sql = `INSERT OR REPLACE INTO ${tableName} (${columns.join(
231
+ ", ",
232
+ )}) VALUES (${placeholders})`;
223
233
 
224
234
  const stmt = this.#$.prepare(sql);
225
- stmt.run(...Object.values(data));
235
+ stmt.run(...Object.values(finalData));
226
236
 
227
237
  return { ok: true };
228
238
  } catch (error) {
@@ -230,6 +240,7 @@ module.exports = class localStorage {
230
240
  }
231
241
  }
232
242
 
243
+
233
244
  /**
234
245
  * @param {string} tableName
235
246
  * @param {object} where