@type32/tauri-sqlite-orm 0.1.18-0 → 0.1.18-2
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.d.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +25 -8
- package/dist/index.mjs +25 -8
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -117,6 +117,10 @@ declare class UpdateQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
|
117
117
|
private updateData;
|
|
118
118
|
constructor(db: Database, table: T);
|
|
119
119
|
set(data: Partial<InferInsertModel<T>>): this;
|
|
120
|
+
build(): {
|
|
121
|
+
sql: string;
|
|
122
|
+
params: any[];
|
|
123
|
+
};
|
|
120
124
|
execute(): Promise<number>;
|
|
121
125
|
}
|
|
122
126
|
declare class DeleteQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
package/dist/index.d.ts
CHANGED
|
@@ -117,6 +117,10 @@ declare class UpdateQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
|
|
117
117
|
private updateData;
|
|
118
118
|
constructor(db: Database, table: T);
|
|
119
119
|
set(data: Partial<InferInsertModel<T>>): this;
|
|
120
|
+
build(): {
|
|
121
|
+
sql: string;
|
|
122
|
+
params: any[];
|
|
123
|
+
};
|
|
120
124
|
execute(): Promise<number>;
|
|
121
125
|
}
|
|
122
126
|
declare class DeleteQueryBuilder<T extends AnyTable> extends BaseQueryBuilder {
|
package/dist/index.js
CHANGED
|
@@ -235,7 +235,7 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
235
235
|
}
|
|
236
236
|
async execute() {
|
|
237
237
|
const { sql, params } = this.build();
|
|
238
|
-
return this.db.select(sql,
|
|
238
|
+
return this.db.select(sql, params);
|
|
239
239
|
}
|
|
240
240
|
};
|
|
241
241
|
var InsertQueryBuilder = class extends BaseQueryBuilder {
|
|
@@ -266,7 +266,7 @@ var InsertQueryBuilder = class extends BaseQueryBuilder {
|
|
|
266
266
|
const params = this.dataSets.flatMap(
|
|
267
267
|
(data) => columns.map((col) => data[col])
|
|
268
268
|
);
|
|
269
|
-
const result = await this.db.execute(this.query,
|
|
269
|
+
const result = await this.db.execute(this.query, params);
|
|
270
270
|
return result.lastInsertId ?? 0;
|
|
271
271
|
}
|
|
272
272
|
};
|
|
@@ -281,20 +281,37 @@ var UpdateQueryBuilder = class extends BaseQueryBuilder {
|
|
|
281
281
|
this.updateData = { ...this.updateData, ...data };
|
|
282
282
|
return this;
|
|
283
283
|
}
|
|
284
|
-
|
|
284
|
+
build() {
|
|
285
|
+
const baseQuery = this.query;
|
|
286
|
+
const whereParams = this.params;
|
|
287
|
+
let tablePart = baseQuery;
|
|
288
|
+
let whereClause = "";
|
|
289
|
+
const whereIndex = baseQuery.indexOf(" WHERE ");
|
|
290
|
+
if (whereIndex !== -1) {
|
|
291
|
+
tablePart = baseQuery.substring(0, whereIndex);
|
|
292
|
+
whereClause = baseQuery.substring(whereIndex);
|
|
293
|
+
}
|
|
285
294
|
const entries = Object.entries(this.updateData);
|
|
295
|
+
if (entries.length === 0) {
|
|
296
|
+
throw new Error("Cannot execute an update query without a .set() call.");
|
|
297
|
+
}
|
|
286
298
|
const setClause = entries.map(([key]) => {
|
|
287
299
|
const column = this.table._.columns[key];
|
|
288
|
-
if (!column)
|
|
300
|
+
if (!column) {
|
|
289
301
|
throw new Error(
|
|
290
302
|
`Column ${key} does not exist on table ${this.table._.name}`
|
|
291
303
|
);
|
|
304
|
+
}
|
|
292
305
|
return `${column._.name} = ?`;
|
|
293
306
|
}).join(", ");
|
|
294
|
-
|
|
295
|
-
|
|
307
|
+
const setParams = entries.map(([, value]) => value);
|
|
308
|
+
const sql = `${tablePart} SET ${setClause}${whereClause}`;
|
|
309
|
+
const params = [...setParams, ...whereParams];
|
|
310
|
+
return { sql, params };
|
|
311
|
+
}
|
|
312
|
+
async execute() {
|
|
296
313
|
const { sql, params } = this.build();
|
|
297
|
-
const result = await this.db.execute(sql,
|
|
314
|
+
const result = await this.db.execute(sql, params);
|
|
298
315
|
return result.rowsAffected;
|
|
299
316
|
}
|
|
300
317
|
};
|
|
@@ -306,7 +323,7 @@ var DeleteQueryBuilder = class extends BaseQueryBuilder {
|
|
|
306
323
|
}
|
|
307
324
|
async execute() {
|
|
308
325
|
const { sql, params } = this.build();
|
|
309
|
-
const result = await this.db.execute(sql,
|
|
326
|
+
const result = await this.db.execute(sql, params);
|
|
310
327
|
return result.rowsAffected;
|
|
311
328
|
}
|
|
312
329
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -185,7 +185,7 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
185
185
|
}
|
|
186
186
|
async execute() {
|
|
187
187
|
const { sql, params } = this.build();
|
|
188
|
-
return this.db.select(sql,
|
|
188
|
+
return this.db.select(sql, params);
|
|
189
189
|
}
|
|
190
190
|
};
|
|
191
191
|
var InsertQueryBuilder = class extends BaseQueryBuilder {
|
|
@@ -216,7 +216,7 @@ var InsertQueryBuilder = class extends BaseQueryBuilder {
|
|
|
216
216
|
const params = this.dataSets.flatMap(
|
|
217
217
|
(data) => columns.map((col) => data[col])
|
|
218
218
|
);
|
|
219
|
-
const result = await this.db.execute(this.query,
|
|
219
|
+
const result = await this.db.execute(this.query, params);
|
|
220
220
|
return result.lastInsertId ?? 0;
|
|
221
221
|
}
|
|
222
222
|
};
|
|
@@ -231,20 +231,37 @@ var UpdateQueryBuilder = class extends BaseQueryBuilder {
|
|
|
231
231
|
this.updateData = { ...this.updateData, ...data };
|
|
232
232
|
return this;
|
|
233
233
|
}
|
|
234
|
-
|
|
234
|
+
build() {
|
|
235
|
+
const baseQuery = this.query;
|
|
236
|
+
const whereParams = this.params;
|
|
237
|
+
let tablePart = baseQuery;
|
|
238
|
+
let whereClause = "";
|
|
239
|
+
const whereIndex = baseQuery.indexOf(" WHERE ");
|
|
240
|
+
if (whereIndex !== -1) {
|
|
241
|
+
tablePart = baseQuery.substring(0, whereIndex);
|
|
242
|
+
whereClause = baseQuery.substring(whereIndex);
|
|
243
|
+
}
|
|
235
244
|
const entries = Object.entries(this.updateData);
|
|
245
|
+
if (entries.length === 0) {
|
|
246
|
+
throw new Error("Cannot execute an update query without a .set() call.");
|
|
247
|
+
}
|
|
236
248
|
const setClause = entries.map(([key]) => {
|
|
237
249
|
const column = this.table._.columns[key];
|
|
238
|
-
if (!column)
|
|
250
|
+
if (!column) {
|
|
239
251
|
throw new Error(
|
|
240
252
|
`Column ${key} does not exist on table ${this.table._.name}`
|
|
241
253
|
);
|
|
254
|
+
}
|
|
242
255
|
return `${column._.name} = ?`;
|
|
243
256
|
}).join(", ");
|
|
244
|
-
|
|
245
|
-
|
|
257
|
+
const setParams = entries.map(([, value]) => value);
|
|
258
|
+
const sql = `${tablePart} SET ${setClause}${whereClause}`;
|
|
259
|
+
const params = [...setParams, ...whereParams];
|
|
260
|
+
return { sql, params };
|
|
261
|
+
}
|
|
262
|
+
async execute() {
|
|
246
263
|
const { sql, params } = this.build();
|
|
247
|
-
const result = await this.db.execute(sql,
|
|
264
|
+
const result = await this.db.execute(sql, params);
|
|
248
265
|
return result.rowsAffected;
|
|
249
266
|
}
|
|
250
267
|
};
|
|
@@ -256,7 +273,7 @@ var DeleteQueryBuilder = class extends BaseQueryBuilder {
|
|
|
256
273
|
}
|
|
257
274
|
async execute() {
|
|
258
275
|
const { sql, params } = this.build();
|
|
259
|
-
const result = await this.db.execute(sql,
|
|
276
|
+
const result = await this.db.execute(sql, params);
|
|
260
277
|
return result.rowsAffected;
|
|
261
278
|
}
|
|
262
279
|
};
|