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

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 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
@@ -337,25 +337,46 @@ var TauriORM = class {
337
337
  }
338
338
  }
339
339
  tables = /* @__PURE__ */ new Map();
340
+ buildColumnDefinition(col, forAlterTable = false) {
341
+ let sql = `${col._.name} ${col.type}`;
342
+ if (col.options.primaryKey && !forAlterTable) {
343
+ sql += " PRIMARY KEY";
344
+ if (col._.autoincrement) {
345
+ sql += " AUTOINCREMENT";
346
+ }
347
+ }
348
+ if (col._.notNull) sql += " NOT NULL";
349
+ if (col.options.unique) sql += " UNIQUE";
350
+ if (col.options.default !== void 0) {
351
+ const value = col.options.default;
352
+ sql += ` DEFAULT ${typeof value === "string" ? `'${value.replace(/'/g, "''")}'` : value}`;
353
+ }
354
+ if (col.options.references) {
355
+ sql += ` REFERENCES ${col.options.references.table._.name}(${col.options.references.column._.name})`;
356
+ }
357
+ return sql;
358
+ }
340
359
  async migrate() {
341
360
  for (const table of this.tables.values()) {
342
- const columnsSql = Object.entries(table._.columns).map(([name, col]) => {
343
- let sql = `${col._.name} ${col.type}`;
344
- if (col.options.primaryKey) sql += " PRIMARY KEY";
345
- if (col._.autoincrement) sql += " AUTOINCREMENT";
346
- if (col._.notNull) sql += " NOT NULL";
347
- if (col.options.unique) sql += " UNIQUE";
348
- if (col.options.default !== void 0) {
349
- const value = col.options.default;
350
- sql += ` DEFAULT ${typeof value === "string" ? `'${value}'` : value}`;
351
- }
352
- if (col.options.references) {
353
- sql += ` REFERENCES ${col.options.references.table._.name}(${col.options.references.column._.name})`;
361
+ const existingTableInfo = await this.db.select(
362
+ `PRAGMA table_info('${table._.name}')`
363
+ );
364
+ if (existingTableInfo.length === 0) {
365
+ const columnsSql = Object.values(table._.columns).map((col) => this.buildColumnDefinition(col)).join(", ");
366
+ const createSql = `CREATE TABLE ${table._.name} (${columnsSql})`;
367
+ await this.db.execute(createSql);
368
+ } else {
369
+ const existingColumnNames = new Set(
370
+ existingTableInfo.map((c) => c.name)
371
+ );
372
+ for (const column of Object.values(table._.columns)) {
373
+ if (!existingColumnNames.has(column._.name)) {
374
+ const columnSql = this.buildColumnDefinition(column, true);
375
+ const alterSql = `ALTER TABLE ${table._.name} ADD COLUMN ${columnSql}`;
376
+ await this.db.execute(alterSql);
377
+ }
354
378
  }
355
- return sql;
356
- }).join(", ");
357
- const createSql = `CREATE TABLE IF NOT EXISTS ${table._.name} (${columnsSql})`;
358
- await this.db.execute(createSql);
379
+ }
359
380
  }
360
381
  }
361
382
  select(table, columns) {
@@ -409,7 +430,10 @@ var TauriORM = class {
409
430
  pk: !!col.options.primaryKey,
410
431
  ai: !!col._.autoincrement,
411
432
  nn: !!col._.notNull,
412
- dv: col.options.default && typeof col.options.default === "object" && col.options.default.raw ? { raw: col.options.default.raw } : col.options.default ?? null
433
+ unique: !!col.options.unique,
434
+ dv: col.options.default && typeof col.options.default === "object" && col.options.default.raw ? { raw: col.options.default.raw } : col.options.default ?? null,
435
+ hasDefaultFn: col.options.$defaultFn !== void 0,
436
+ hasOnUpdateFn: col.options.$onUpdateFn !== void 0
413
437
  };
414
438
  }
415
439
  computeModelSignature() {
package/dist/index.mjs CHANGED
@@ -287,25 +287,46 @@ var TauriORM = class {
287
287
  }
288
288
  }
289
289
  tables = /* @__PURE__ */ new Map();
290
+ buildColumnDefinition(col, forAlterTable = false) {
291
+ let sql = `${col._.name} ${col.type}`;
292
+ if (col.options.primaryKey && !forAlterTable) {
293
+ sql += " PRIMARY KEY";
294
+ if (col._.autoincrement) {
295
+ sql += " AUTOINCREMENT";
296
+ }
297
+ }
298
+ if (col._.notNull) sql += " NOT NULL";
299
+ if (col.options.unique) sql += " UNIQUE";
300
+ if (col.options.default !== void 0) {
301
+ const value = col.options.default;
302
+ sql += ` DEFAULT ${typeof value === "string" ? `'${value.replace(/'/g, "''")}'` : value}`;
303
+ }
304
+ if (col.options.references) {
305
+ sql += ` REFERENCES ${col.options.references.table._.name}(${col.options.references.column._.name})`;
306
+ }
307
+ return sql;
308
+ }
290
309
  async migrate() {
291
310
  for (const table of this.tables.values()) {
292
- const columnsSql = Object.entries(table._.columns).map(([name, col]) => {
293
- let sql = `${col._.name} ${col.type}`;
294
- if (col.options.primaryKey) sql += " PRIMARY KEY";
295
- if (col._.autoincrement) sql += " AUTOINCREMENT";
296
- if (col._.notNull) sql += " NOT NULL";
297
- if (col.options.unique) sql += " UNIQUE";
298
- if (col.options.default !== void 0) {
299
- const value = col.options.default;
300
- sql += ` DEFAULT ${typeof value === "string" ? `'${value}'` : value}`;
301
- }
302
- if (col.options.references) {
303
- sql += ` REFERENCES ${col.options.references.table._.name}(${col.options.references.column._.name})`;
311
+ const existingTableInfo = await this.db.select(
312
+ `PRAGMA table_info('${table._.name}')`
313
+ );
314
+ if (existingTableInfo.length === 0) {
315
+ const columnsSql = Object.values(table._.columns).map((col) => this.buildColumnDefinition(col)).join(", ");
316
+ const createSql = `CREATE TABLE ${table._.name} (${columnsSql})`;
317
+ await this.db.execute(createSql);
318
+ } else {
319
+ const existingColumnNames = new Set(
320
+ existingTableInfo.map((c) => c.name)
321
+ );
322
+ for (const column of Object.values(table._.columns)) {
323
+ if (!existingColumnNames.has(column._.name)) {
324
+ const columnSql = this.buildColumnDefinition(column, true);
325
+ const alterSql = `ALTER TABLE ${table._.name} ADD COLUMN ${columnSql}`;
326
+ await this.db.execute(alterSql);
327
+ }
304
328
  }
305
- return sql;
306
- }).join(", ");
307
- const createSql = `CREATE TABLE IF NOT EXISTS ${table._.name} (${columnsSql})`;
308
- await this.db.execute(createSql);
329
+ }
309
330
  }
310
331
  }
311
332
  select(table, columns) {
@@ -359,7 +380,10 @@ var TauriORM = class {
359
380
  pk: !!col.options.primaryKey,
360
381
  ai: !!col._.autoincrement,
361
382
  nn: !!col._.notNull,
362
- dv: col.options.default && typeof col.options.default === "object" && col.options.default.raw ? { raw: col.options.default.raw } : col.options.default ?? null
383
+ unique: !!col.options.unique,
384
+ dv: col.options.default && typeof col.options.default === "object" && col.options.default.raw ? { raw: col.options.default.raw } : col.options.default ?? null,
385
+ hasDefaultFn: col.options.$defaultFn !== void 0,
386
+ hasOnUpdateFn: col.options.$onUpdateFn !== void 0
363
387
  };
364
388
  }
365
389
  computeModelSignature() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@type32/tauri-sqlite-orm",
3
- "version": "0.1.18-2",
3
+ "version": "0.1.18-3",
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",