@type32/tauri-sqlite-orm 0.1.7 → 0.1.8

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 CHANGED
@@ -295,6 +295,7 @@ declare class TauriORM {
295
295
  execute(): Promise<unknown>;
296
296
  };
297
297
  run(query: string, bindings?: any[]): Promise<void>;
298
+ private formatDefaultValue;
298
299
  private generateCreateTableSql;
299
300
  createTableIfNotExists(table: Table<any>): Promise<void>;
300
301
  createTablesIfNotExist(tables: Table<any>[]): Promise<void>;
package/dist/index.d.ts CHANGED
@@ -295,6 +295,7 @@ declare class TauriORM {
295
295
  execute(): Promise<unknown>;
296
296
  };
297
297
  run(query: string, bindings?: any[]): Promise<void>;
298
+ private formatDefaultValue;
298
299
  private generateCreateTableSql;
299
300
  createTableIfNotExists(table: Table<any>): Promise<void>;
300
301
  createTablesIfNotExist(tables: Table<any>[]): Promise<void>;
package/dist/index.js CHANGED
@@ -975,6 +975,25 @@ var TauriORM = class {
975
975
  await db.execute(query, bindings);
976
976
  }
977
977
  // --- Migrations API ---
978
+ formatDefaultValue(col) {
979
+ const dv = col.defaultValue;
980
+ if (dv === void 0) return null;
981
+ if (dv && typeof dv === "object" && "raw" in dv) {
982
+ return dv.raw;
983
+ }
984
+ if (dv instanceof Date) {
985
+ const isMs = col.mode === "timestamp_ms";
986
+ const num = isMs ? dv.getTime() : Math.floor(dv.getTime() / 1e3);
987
+ return String(num);
988
+ }
989
+ if (col.mode === "boolean") {
990
+ return String(dv ? 1 : 0);
991
+ }
992
+ if (typeof dv === "string") {
993
+ return `'${dv.replace(/'/g, "''")}'`;
994
+ }
995
+ return String(dv);
996
+ }
978
997
  generateCreateTableSql(table) {
979
998
  const tableName = table._tableName;
980
999
  const columns = Object.values(table._schema);
@@ -985,14 +1004,8 @@ var TauriORM = class {
985
1004
  }
986
1005
  if (col.isNotNull) def += " NOT NULL";
987
1006
  if (col.defaultValue !== void 0) {
988
- const dv = col.defaultValue;
989
- if (dv && typeof dv === "object" && "raw" in dv) {
990
- def += ` DEFAULT ${dv.raw}`;
991
- } else if (typeof dv === "string") {
992
- def += ` DEFAULT '${dv.replace(/'/g, "''")}'`;
993
- } else {
994
- def += ` DEFAULT ${dv}`;
995
- }
1007
+ const formatted = this.formatDefaultValue(col);
1008
+ if (formatted !== null) def += ` DEFAULT ${formatted}`;
996
1009
  }
997
1010
  if (col.references && !col.isPrimaryKey) {
998
1011
  def += ` REFERENCES ${col.references.table} (${col.references.column})`;
@@ -1293,12 +1306,8 @@ var TauriORM = class {
1293
1306
  let clause = `${m.name} ${m.type}`;
1294
1307
  if (m.isNotNull) clause += " NOT NULL";
1295
1308
  if (m.defaultValue !== void 0) {
1296
- const dv = m.defaultValue;
1297
- if (dv && typeof dv === "object" && "raw" in dv)
1298
- clause += ` DEFAULT ${dv.raw}`;
1299
- else if (typeof dv === "string")
1300
- clause += ` DEFAULT '${dv.replace(/'/g, "''")}'`;
1301
- else clause += ` DEFAULT ${dv}`;
1309
+ const formatted = this.formatDefaultValue(m);
1310
+ if (formatted !== null) clause += ` DEFAULT ${formatted}`;
1302
1311
  }
1303
1312
  await this.run(`ALTER TABLE ${tableName} ADD COLUMN ${clause}`);
1304
1313
  }
package/dist/index.mjs CHANGED
@@ -897,6 +897,25 @@ var TauriORM = class {
897
897
  await db.execute(query, bindings);
898
898
  }
899
899
  // --- Migrations API ---
900
+ formatDefaultValue(col) {
901
+ const dv = col.defaultValue;
902
+ if (dv === void 0) return null;
903
+ if (dv && typeof dv === "object" && "raw" in dv) {
904
+ return dv.raw;
905
+ }
906
+ if (dv instanceof Date) {
907
+ const isMs = col.mode === "timestamp_ms";
908
+ const num = isMs ? dv.getTime() : Math.floor(dv.getTime() / 1e3);
909
+ return String(num);
910
+ }
911
+ if (col.mode === "boolean") {
912
+ return String(dv ? 1 : 0);
913
+ }
914
+ if (typeof dv === "string") {
915
+ return `'${dv.replace(/'/g, "''")}'`;
916
+ }
917
+ return String(dv);
918
+ }
900
919
  generateCreateTableSql(table) {
901
920
  const tableName = table._tableName;
902
921
  const columns = Object.values(table._schema);
@@ -907,14 +926,8 @@ var TauriORM = class {
907
926
  }
908
927
  if (col.isNotNull) def += " NOT NULL";
909
928
  if (col.defaultValue !== void 0) {
910
- const dv = col.defaultValue;
911
- if (dv && typeof dv === "object" && "raw" in dv) {
912
- def += ` DEFAULT ${dv.raw}`;
913
- } else if (typeof dv === "string") {
914
- def += ` DEFAULT '${dv.replace(/'/g, "''")}'`;
915
- } else {
916
- def += ` DEFAULT ${dv}`;
917
- }
929
+ const formatted = this.formatDefaultValue(col);
930
+ if (formatted !== null) def += ` DEFAULT ${formatted}`;
918
931
  }
919
932
  if (col.references && !col.isPrimaryKey) {
920
933
  def += ` REFERENCES ${col.references.table} (${col.references.column})`;
@@ -1215,12 +1228,8 @@ var TauriORM = class {
1215
1228
  let clause = `${m.name} ${m.type}`;
1216
1229
  if (m.isNotNull) clause += " NOT NULL";
1217
1230
  if (m.defaultValue !== void 0) {
1218
- const dv = m.defaultValue;
1219
- if (dv && typeof dv === "object" && "raw" in dv)
1220
- clause += ` DEFAULT ${dv.raw}`;
1221
- else if (typeof dv === "string")
1222
- clause += ` DEFAULT '${dv.replace(/'/g, "''")}'`;
1223
- else clause += ` DEFAULT ${dv}`;
1231
+ const formatted = this.formatDefaultValue(m);
1232
+ if (formatted !== null) clause += ` DEFAULT ${formatted}`;
1224
1233
  }
1225
1234
  await this.run(`ALTER TABLE ${tableName} ADD COLUMN ${clause}`);
1226
1235
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@type32/tauri-sqlite-orm",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
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",