@type32/tauri-sqlite-orm 0.1.18-2 → 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/README.md +3 -3
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +75 -28
- package/dist/index.mjs +75 -28
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -134,7 +134,7 @@ import { eq } from "@type32/tauri-sqlite-orm";
|
|
|
134
134
|
await db
|
|
135
135
|
.update(users)
|
|
136
136
|
.set({ email: "new.email@example.com" })
|
|
137
|
-
.where(eq(users.id, 1));
|
|
137
|
+
.where(eq(users._.columns.id, 1));
|
|
138
138
|
```
|
|
139
139
|
|
|
140
140
|
**DELETE**
|
|
@@ -143,7 +143,7 @@ await db
|
|
|
143
143
|
import { eq } from "@type32/tauri-sqlite-orm";
|
|
144
144
|
|
|
145
145
|
// Delete a user
|
|
146
|
-
await db.delete(users).where(eq(users.id, 1));
|
|
146
|
+
await db.delete(users).where(eq(users._.columns.id, 1));
|
|
147
147
|
```
|
|
148
148
|
|
|
149
149
|
### Migrations
|
|
@@ -165,7 +165,7 @@ Run multiple database operations within a transaction to ensure atomicity.
|
|
|
165
165
|
```typescript
|
|
166
166
|
await db.transaction(async (tx) => {
|
|
167
167
|
await tx.insert(users).values({ name: "From Transaction" });
|
|
168
|
-
await tx.delete(users).where(eq(users.id, 1));
|
|
168
|
+
await tx.delete(users).where(eq(users._.columns.id, 1));
|
|
169
169
|
});
|
|
170
170
|
```
|
|
171
171
|
|
package/dist/index.d.mts
CHANGED
|
@@ -132,6 +132,7 @@ declare class TauriORM {
|
|
|
132
132
|
private db;
|
|
133
133
|
private tables;
|
|
134
134
|
constructor(db: Database, schema?: Record<string, AnyTable> | undefined);
|
|
135
|
+
private buildColumnDefinition;
|
|
135
136
|
migrate(): Promise<void>;
|
|
136
137
|
select<T extends AnyTable, C extends (keyof T["_"]["columns"])[] | undefined = undefined>(table: T, columns?: C): SelectQueryBuilder<T, C>;
|
|
137
138
|
insert<T extends AnyTable>(table: T): InsertQueryBuilder<T>;
|
package/dist/index.d.ts
CHANGED
|
@@ -132,6 +132,7 @@ declare class TauriORM {
|
|
|
132
132
|
private db;
|
|
133
133
|
private tables;
|
|
134
134
|
constructor(db: Database, schema?: Record<string, AnyTable> | undefined);
|
|
135
|
+
private buildColumnDefinition;
|
|
135
136
|
migrate(): Promise<void>;
|
|
136
137
|
select<T extends AnyTable, C extends (keyof T["_"]["columns"])[] | undefined = undefined>(table: T, columns?: C): SelectQueryBuilder<T, C>;
|
|
137
138
|
insert<T extends AnyTable>(table: T): InsertQueryBuilder<T>;
|
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
|
|
258
|
-
|
|
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
|
-
(
|
|
276
|
+
(key) => this.table._.columns[key]._.name
|
|
262
277
|
);
|
|
263
|
-
const placeholders = `(${
|
|
264
|
-
const valuesSql =
|
|
265
|
-
this.query
|
|
266
|
-
|
|
267
|
-
|
|
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(
|
|
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(
|
|
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
|
}
|
|
@@ -337,25 +360,46 @@ var TauriORM = class {
|
|
|
337
360
|
}
|
|
338
361
|
}
|
|
339
362
|
tables = /* @__PURE__ */ new Map();
|
|
363
|
+
buildColumnDefinition(col, forAlterTable = false) {
|
|
364
|
+
let sql = `${col._.name} ${col.type}`;
|
|
365
|
+
if (col.options.primaryKey && !forAlterTable) {
|
|
366
|
+
sql += " PRIMARY KEY";
|
|
367
|
+
if (col._.autoincrement) {
|
|
368
|
+
sql += " AUTOINCREMENT";
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
if (col._.notNull) sql += " NOT NULL";
|
|
372
|
+
if (col.options.unique) sql += " UNIQUE";
|
|
373
|
+
if (col.options.default !== void 0) {
|
|
374
|
+
const value = col.options.default;
|
|
375
|
+
sql += ` DEFAULT ${typeof value === "string" ? `'${value.replace(/'/g, "''")}'` : value}`;
|
|
376
|
+
}
|
|
377
|
+
if (col.options.references) {
|
|
378
|
+
sql += ` REFERENCES ${col.options.references.table._.name}(${col.options.references.column._.name})`;
|
|
379
|
+
}
|
|
380
|
+
return sql;
|
|
381
|
+
}
|
|
340
382
|
async migrate() {
|
|
341
383
|
for (const table of this.tables.values()) {
|
|
342
|
-
const
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
384
|
+
const existingTableInfo = await this.db.select(
|
|
385
|
+
`PRAGMA table_info('${table._.name}')`
|
|
386
|
+
);
|
|
387
|
+
if (existingTableInfo.length === 0) {
|
|
388
|
+
const columnsSql = Object.values(table._.columns).map((col) => this.buildColumnDefinition(col)).join(", ");
|
|
389
|
+
const createSql = `CREATE TABLE ${table._.name} (${columnsSql})`;
|
|
390
|
+
await this.db.execute(createSql);
|
|
391
|
+
} else {
|
|
392
|
+
const existingColumnNames = new Set(
|
|
393
|
+
existingTableInfo.map((c) => c.name)
|
|
394
|
+
);
|
|
395
|
+
for (const column of Object.values(table._.columns)) {
|
|
396
|
+
if (!existingColumnNames.has(column._.name)) {
|
|
397
|
+
const columnSql = this.buildColumnDefinition(column, true);
|
|
398
|
+
const alterSql = `ALTER TABLE ${table._.name} ADD COLUMN ${columnSql}`;
|
|
399
|
+
await this.db.execute(alterSql);
|
|
400
|
+
}
|
|
354
401
|
}
|
|
355
|
-
|
|
356
|
-
}).join(", ");
|
|
357
|
-
const createSql = `CREATE TABLE IF NOT EXISTS ${table._.name} (${columnsSql})`;
|
|
358
|
-
await this.db.execute(createSql);
|
|
402
|
+
}
|
|
359
403
|
}
|
|
360
404
|
}
|
|
361
405
|
select(table, columns) {
|
|
@@ -409,7 +453,10 @@ var TauriORM = class {
|
|
|
409
453
|
pk: !!col.options.primaryKey,
|
|
410
454
|
ai: !!col._.autoincrement,
|
|
411
455
|
nn: !!col._.notNull,
|
|
412
|
-
|
|
456
|
+
unique: !!col.options.unique,
|
|
457
|
+
dv: col.options.default && typeof col.options.default === "object" && col.options.default.raw ? { raw: col.options.default.raw } : col.options.default ?? null,
|
|
458
|
+
hasDefaultFn: col.options.$defaultFn !== void 0,
|
|
459
|
+
hasOnUpdateFn: col.options.$onUpdateFn !== void 0
|
|
413
460
|
};
|
|
414
461
|
}
|
|
415
462
|
computeModelSignature() {
|
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
|
|
208
|
-
|
|
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
|
-
(
|
|
226
|
+
(key) => this.table._.columns[key]._.name
|
|
212
227
|
);
|
|
213
|
-
const placeholders = `(${
|
|
214
|
-
const valuesSql =
|
|
215
|
-
this.query
|
|
216
|
-
|
|
217
|
-
|
|
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(
|
|
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(
|
|
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
|
}
|
|
@@ -287,25 +310,46 @@ var TauriORM = class {
|
|
|
287
310
|
}
|
|
288
311
|
}
|
|
289
312
|
tables = /* @__PURE__ */ new Map();
|
|
313
|
+
buildColumnDefinition(col, forAlterTable = false) {
|
|
314
|
+
let sql = `${col._.name} ${col.type}`;
|
|
315
|
+
if (col.options.primaryKey && !forAlterTable) {
|
|
316
|
+
sql += " PRIMARY KEY";
|
|
317
|
+
if (col._.autoincrement) {
|
|
318
|
+
sql += " AUTOINCREMENT";
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (col._.notNull) sql += " NOT NULL";
|
|
322
|
+
if (col.options.unique) sql += " UNIQUE";
|
|
323
|
+
if (col.options.default !== void 0) {
|
|
324
|
+
const value = col.options.default;
|
|
325
|
+
sql += ` DEFAULT ${typeof value === "string" ? `'${value.replace(/'/g, "''")}'` : value}`;
|
|
326
|
+
}
|
|
327
|
+
if (col.options.references) {
|
|
328
|
+
sql += ` REFERENCES ${col.options.references.table._.name}(${col.options.references.column._.name})`;
|
|
329
|
+
}
|
|
330
|
+
return sql;
|
|
331
|
+
}
|
|
290
332
|
async migrate() {
|
|
291
333
|
for (const table of this.tables.values()) {
|
|
292
|
-
const
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
334
|
+
const existingTableInfo = await this.db.select(
|
|
335
|
+
`PRAGMA table_info('${table._.name}')`
|
|
336
|
+
);
|
|
337
|
+
if (existingTableInfo.length === 0) {
|
|
338
|
+
const columnsSql = Object.values(table._.columns).map((col) => this.buildColumnDefinition(col)).join(", ");
|
|
339
|
+
const createSql = `CREATE TABLE ${table._.name} (${columnsSql})`;
|
|
340
|
+
await this.db.execute(createSql);
|
|
341
|
+
} else {
|
|
342
|
+
const existingColumnNames = new Set(
|
|
343
|
+
existingTableInfo.map((c) => c.name)
|
|
344
|
+
);
|
|
345
|
+
for (const column of Object.values(table._.columns)) {
|
|
346
|
+
if (!existingColumnNames.has(column._.name)) {
|
|
347
|
+
const columnSql = this.buildColumnDefinition(column, true);
|
|
348
|
+
const alterSql = `ALTER TABLE ${table._.name} ADD COLUMN ${columnSql}`;
|
|
349
|
+
await this.db.execute(alterSql);
|
|
350
|
+
}
|
|
304
351
|
}
|
|
305
|
-
|
|
306
|
-
}).join(", ");
|
|
307
|
-
const createSql = `CREATE TABLE IF NOT EXISTS ${table._.name} (${columnsSql})`;
|
|
308
|
-
await this.db.execute(createSql);
|
|
352
|
+
}
|
|
309
353
|
}
|
|
310
354
|
}
|
|
311
355
|
select(table, columns) {
|
|
@@ -359,7 +403,10 @@ var TauriORM = class {
|
|
|
359
403
|
pk: !!col.options.primaryKey,
|
|
360
404
|
ai: !!col._.autoincrement,
|
|
361
405
|
nn: !!col._.notNull,
|
|
362
|
-
|
|
406
|
+
unique: !!col.options.unique,
|
|
407
|
+
dv: col.options.default && typeof col.options.default === "object" && col.options.default.raw ? { raw: col.options.default.raw } : col.options.default ?? null,
|
|
408
|
+
hasDefaultFn: col.options.$defaultFn !== void 0,
|
|
409
|
+
hasOnUpdateFn: col.options.$onUpdateFn !== void 0
|
|
363
410
|
};
|
|
364
411
|
}
|
|
365
412
|
computeModelSignature() {
|