@ix-xs/node-comfort 1.0.54 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ix-xs/node-comfort",
3
- "version": "1.0.54",
3
+ "version": "1.0.56",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "types/index.d.ts",
@@ -106,6 +106,19 @@ module.exports = class localStorage {
106
106
  return `${firstCol} DESC`;
107
107
  }
108
108
 
109
+ /**
110
+ * @param {object} table
111
+ * @param {string} table.name
112
+ * @param {Record<string, {
113
+ * type: "INTEGER"|"TEXT"|"REAL"|"BLOB"|"NUMERIC",
114
+ * primaryKey?: boolean,
115
+ * autoincrement?: boolean,
116
+ * notNull?: boolean,
117
+ * unique?: boolean,
118
+ * defaultValue?: *,
119
+ * values?: Array<*>
120
+ * }>} table.options
121
+ */
109
122
  async createTable(table) {
110
123
  try {
111
124
  this.#defineModel(table.name, table.options);
@@ -115,6 +128,9 @@ module.exports = class localStorage {
115
128
  }
116
129
  }
117
130
 
131
+ /**
132
+ * @param {string} tableName
133
+ */
118
134
  async deleteTable(tableName) {
119
135
  try {
120
136
  if (!this.#models.has(tableName)) {
@@ -129,6 +145,9 @@ module.exports = class localStorage {
129
145
  }
130
146
  }
131
147
 
148
+ /**
149
+ * @param {string} tableName
150
+ */
132
151
  async clearTable(tableName) {
133
152
  try {
134
153
  if (!this.#models.has(tableName)) {
@@ -142,6 +161,10 @@ module.exports = class localStorage {
142
161
  }
143
162
  }
144
163
 
164
+ /**
165
+ * @param {string} tableName
166
+ * @param {object} where
167
+ */
145
168
  async get(tableName, where) {
146
169
  try {
147
170
  if (!this.#models.has(tableName)) return undefined;
@@ -157,6 +180,12 @@ module.exports = class localStorage {
157
180
  }
158
181
  }
159
182
 
183
+ /**
184
+ * @param {string} tableName
185
+ * @param {object} where
186
+ * @param {number} limit
187
+ * @param {number} offset
188
+ */
160
189
  async getAll(tableName, where = {}, limit = 50, offset = 0) {
161
190
  try {
162
191
  if (!this.#models.has(tableName)) return [];
@@ -173,6 +202,11 @@ module.exports = class localStorage {
173
202
  }
174
203
  }
175
204
 
205
+ /**
206
+ * @param {string} tableName
207
+ * @param {object} data
208
+ * @param {object|null} where
209
+ */
176
210
  async set(tableName, data, where = null) {
177
211
  try {
178
212
  if (!this.#models.has(tableName)) {
@@ -181,14 +215,24 @@ module.exports = class localStorage {
181
215
 
182
216
  const lookup = where ?? (data.id ? { id: data.id } : null);
183
217
 
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})`;
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})`;
189
233
 
190
234
  const stmt = this.#$.prepare(sql);
191
- stmt.run(...Object.values(data));
235
+ stmt.run(...Object.values(finalData));
192
236
 
193
237
  return { ok: true };
194
238
  } catch (error) {
@@ -196,6 +240,11 @@ module.exports = class localStorage {
196
240
  }
197
241
  }
198
242
 
243
+
244
+ /**
245
+ * @param {string} tableName
246
+ * @param {object} where
247
+ */
199
248
  async delete(tableName, where) {
200
249
  try {
201
250
  if (!this.#models.has(tableName)) {
@@ -212,6 +261,10 @@ module.exports = class localStorage {
212
261
  }
213
262
  }
214
263
 
264
+ /**
265
+ * @param {string} tableName
266
+ * @param {object} [where]
267
+ */
215
268
  async count(tableName, where = {}) {
216
269
  try {
217
270
  if (!this.#models.has(tableName)) {