@type32/tauri-sqlite-orm 0.1.18-3 → 0.1.18-4

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/dist/index.js CHANGED
@@ -254,19 +254,36 @@ var InsertQueryBuilder = class extends BaseQueryBuilder {
254
254
  if (this.dataSets.length === 0) {
255
255
  throw new Error("No data provided for insert");
256
256
  }
257
- const columns = Object.keys(
258
- this.dataSets[0]
259
- );
257
+ const processedDataSets = this.dataSets.map((dataSet) => {
258
+ const finalData = { ...dataSet };
259
+ for (const [key, column] of Object.entries(this.table._.columns)) {
260
+ if (finalData[key] === void 0) {
261
+ if (column.options.$defaultFn) {
262
+ finalData[key] = column.options.$defaultFn();
263
+ }
264
+ }
265
+ }
266
+ return finalData;
267
+ });
268
+ const allKeys = /* @__PURE__ */ new Set();
269
+ for (const dataSet of processedDataSets) {
270
+ for (const key of Object.keys(dataSet)) {
271
+ allKeys.add(key);
272
+ }
273
+ }
274
+ const columns = Array.from(allKeys);
260
275
  const columnNames = columns.map(
261
- (c) => this.table._.columns[c]._.name
276
+ (key) => this.table._.columns[key]._.name
262
277
  );
263
- const placeholders = `(${columnNames.map(() => "?").join(", ")})`;
264
- const valuesSql = this.dataSets.map(() => placeholders).join(", ");
265
- this.query += ` (${columnNames.join(", ")}) VALUES ${valuesSql}`;
266
- const params = this.dataSets.flatMap(
267
- (data) => columns.map((col) => data[col])
278
+ const placeholders = `(${columns.map(() => "?").join(", ")})`;
279
+ const valuesSql = processedDataSets.map(() => placeholders).join(", ");
280
+ const finalQuery = `${this.query} (${columnNames.join(
281
+ ", "
282
+ )}) VALUES ${valuesSql}`;
283
+ const params = processedDataSets.flatMap(
284
+ (data) => columns.map((col) => data[col] ?? null)
268
285
  );
269
- const result = await this.db.execute(this.query, params);
286
+ const result = await this.db.execute(finalQuery, params);
270
287
  return result.lastInsertId ?? 0;
271
288
  }
272
289
  };
@@ -282,6 +299,12 @@ var UpdateQueryBuilder = class extends BaseQueryBuilder {
282
299
  return this;
283
300
  }
284
301
  build() {
302
+ const finalUpdateData = { ...this.updateData };
303
+ for (const [key, column] of Object.entries(this.table._.columns)) {
304
+ if (finalUpdateData[key] === void 0 && column.options.$onUpdateFn) {
305
+ finalUpdateData[key] = column.options.$onUpdateFn();
306
+ }
307
+ }
285
308
  const baseQuery = this.query;
286
309
  const whereParams = this.params;
287
310
  let tablePart = baseQuery;
@@ -291,7 +314,7 @@ var UpdateQueryBuilder = class extends BaseQueryBuilder {
291
314
  tablePart = baseQuery.substring(0, whereIndex);
292
315
  whereClause = baseQuery.substring(whereIndex);
293
316
  }
294
- const entries = Object.entries(this.updateData);
317
+ const entries = Object.entries(finalUpdateData);
295
318
  if (entries.length === 0) {
296
319
  throw new Error("Cannot execute an update query without a .set() call.");
297
320
  }
package/dist/index.mjs CHANGED
@@ -204,19 +204,36 @@ var InsertQueryBuilder = class extends BaseQueryBuilder {
204
204
  if (this.dataSets.length === 0) {
205
205
  throw new Error("No data provided for insert");
206
206
  }
207
- const columns = Object.keys(
208
- this.dataSets[0]
209
- );
207
+ const processedDataSets = this.dataSets.map((dataSet) => {
208
+ const finalData = { ...dataSet };
209
+ for (const [key, column] of Object.entries(this.table._.columns)) {
210
+ if (finalData[key] === void 0) {
211
+ if (column.options.$defaultFn) {
212
+ finalData[key] = column.options.$defaultFn();
213
+ }
214
+ }
215
+ }
216
+ return finalData;
217
+ });
218
+ const allKeys = /* @__PURE__ */ new Set();
219
+ for (const dataSet of processedDataSets) {
220
+ for (const key of Object.keys(dataSet)) {
221
+ allKeys.add(key);
222
+ }
223
+ }
224
+ const columns = Array.from(allKeys);
210
225
  const columnNames = columns.map(
211
- (c) => this.table._.columns[c]._.name
226
+ (key) => this.table._.columns[key]._.name
212
227
  );
213
- const placeholders = `(${columnNames.map(() => "?").join(", ")})`;
214
- const valuesSql = this.dataSets.map(() => placeholders).join(", ");
215
- this.query += ` (${columnNames.join(", ")}) VALUES ${valuesSql}`;
216
- const params = this.dataSets.flatMap(
217
- (data) => columns.map((col) => data[col])
228
+ const placeholders = `(${columns.map(() => "?").join(", ")})`;
229
+ const valuesSql = processedDataSets.map(() => placeholders).join(", ");
230
+ const finalQuery = `${this.query} (${columnNames.join(
231
+ ", "
232
+ )}) VALUES ${valuesSql}`;
233
+ const params = processedDataSets.flatMap(
234
+ (data) => columns.map((col) => data[col] ?? null)
218
235
  );
219
- const result = await this.db.execute(this.query, params);
236
+ const result = await this.db.execute(finalQuery, params);
220
237
  return result.lastInsertId ?? 0;
221
238
  }
222
239
  };
@@ -232,6 +249,12 @@ var UpdateQueryBuilder = class extends BaseQueryBuilder {
232
249
  return this;
233
250
  }
234
251
  build() {
252
+ const finalUpdateData = { ...this.updateData };
253
+ for (const [key, column] of Object.entries(this.table._.columns)) {
254
+ if (finalUpdateData[key] === void 0 && column.options.$onUpdateFn) {
255
+ finalUpdateData[key] = column.options.$onUpdateFn();
256
+ }
257
+ }
235
258
  const baseQuery = this.query;
236
259
  const whereParams = this.params;
237
260
  let tablePart = baseQuery;
@@ -241,7 +264,7 @@ var UpdateQueryBuilder = class extends BaseQueryBuilder {
241
264
  tablePart = baseQuery.substring(0, whereIndex);
242
265
  whereClause = baseQuery.substring(whereIndex);
243
266
  }
244
- const entries = Object.entries(this.updateData);
267
+ const entries = Object.entries(finalUpdateData);
245
268
  if (entries.length === 0) {
246
269
  throw new Error("Cannot execute an update query without a .set() call.");
247
270
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@type32/tauri-sqlite-orm",
3
- "version": "0.1.18-3",
3
+ "version": "0.1.18-4",
4
4
  "description": "A Drizzle-like ORM for Tauri v2's SQL JS API plugin.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",