@ix-xs/node-comfort 1.0.55 → 1.0.57
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/package.json +1 -1
- package/src/localStorage.js +33 -12
package/package.json
CHANGED
package/src/localStorage.js
CHANGED
|
@@ -59,10 +59,10 @@ module.exports = class localStorage {
|
|
|
59
59
|
if (def.notNull) colDef += " NOT NULL";
|
|
60
60
|
if (def.unique) colDef += " UNIQUE";
|
|
61
61
|
if (def.defaultValue !== undefined) {
|
|
62
|
-
colDef += ` DEFAULT ${
|
|
62
|
+
colDef += ` DEFAULT ${this.#toSQLValue(def.defaultValue)}`;
|
|
63
63
|
}
|
|
64
64
|
if (def.values?.length) {
|
|
65
|
-
colDef += ` CHECK(${col} IN (${def.values.map(v =>
|
|
65
|
+
colDef += ` CHECK(${col} IN (${def.values.map(v => this.#toSQLValue(v)).join(", ")}))`;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
return colDef;
|
|
@@ -106,6 +106,16 @@ module.exports = class localStorage {
|
|
|
106
106
|
return `${firstCol} DESC`;
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
+
#toSQLValue(value) {
|
|
110
|
+
if (value === null) return "NULL";
|
|
111
|
+
if (typeof value === "number") return String(value);
|
|
112
|
+
if (typeof value === "bigint") return String(value);
|
|
113
|
+
if (typeof value === "boolean") return value ? "1" : "0";
|
|
114
|
+
if (typeof value === "string") return `'${value.replace(/'/g, "''")}'`;
|
|
115
|
+
|
|
116
|
+
return `'${JSON.stringify(value).replace(/'/g, "''")}'`;
|
|
117
|
+
}
|
|
118
|
+
|
|
109
119
|
/**
|
|
110
120
|
* @param {object} table
|
|
111
121
|
* @param {string} table.name
|
|
@@ -203,10 +213,10 @@ module.exports = class localStorage {
|
|
|
203
213
|
}
|
|
204
214
|
|
|
205
215
|
/**
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
216
|
+
* @param {string} tableName
|
|
217
|
+
* @param {object} data
|
|
218
|
+
* @param {object|null} where
|
|
219
|
+
*/
|
|
210
220
|
async set(tableName, data, where = null) {
|
|
211
221
|
try {
|
|
212
222
|
if (!this.#models.has(tableName)) {
|
|
@@ -215,14 +225,24 @@ module.exports = class localStorage {
|
|
|
215
225
|
|
|
216
226
|
const lookup = where ?? (data.id ? { id: data.id } : null);
|
|
217
227
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
228
|
+
let finalData = data;
|
|
229
|
+
|
|
230
|
+
if (lookup) {
|
|
231
|
+
const existing = await this.get(tableName, lookup);
|
|
232
|
+
|
|
233
|
+
if (existing && !existing.error) {
|
|
234
|
+
finalData = { ...existing, ...data };
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const columns = Object.keys(finalData);
|
|
239
|
+
const placeholders = columns.map(() => "?").join(", ");
|
|
240
|
+
const sql = `INSERT OR REPLACE INTO ${tableName} (${columns.join(
|
|
241
|
+
", ",
|
|
242
|
+
)}) VALUES (${placeholders})`;
|
|
223
243
|
|
|
224
244
|
const stmt = this.#$.prepare(sql);
|
|
225
|
-
stmt.run(...Object.values(
|
|
245
|
+
stmt.run(...Object.values(finalData));
|
|
226
246
|
|
|
227
247
|
return { ok: true };
|
|
228
248
|
} catch (error) {
|
|
@@ -230,6 +250,7 @@ module.exports = class localStorage {
|
|
|
230
250
|
}
|
|
231
251
|
}
|
|
232
252
|
|
|
253
|
+
|
|
233
254
|
/**
|
|
234
255
|
* @param {string} tableName
|
|
235
256
|
* @param {object} where
|