@type32/tauri-sqlite-orm 0.1.10 → 0.1.12
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/cli.d.mts +1 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +63 -0
- package/dist/cli.mjs +62 -0
- package/dist/index.js +43 -11
- package/dist/index.mjs +43 -11
- package/package.json +1 -1
package/dist/cli.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
// src/cli.ts
|
|
5
|
+
var import_node_fs = require("fs");
|
|
6
|
+
var import_node_path = require("path");
|
|
7
|
+
var help = `
|
|
8
|
+
tauriorm-kit
|
|
9
|
+
|
|
10
|
+
Usage:
|
|
11
|
+
bunx tauriorm-kit generate --out ./src/lib/db-client.ts --schema ./src/lib/schema.ts
|
|
12
|
+
|
|
13
|
+
Options:
|
|
14
|
+
--out <file> Output file where a typed client factory will be written
|
|
15
|
+
--schema <file> Path to a module that exports your tables and optional relations
|
|
16
|
+
--driver <uri> Database URI (default: sqlite:app.db)
|
|
17
|
+
`;
|
|
18
|
+
async function main() {
|
|
19
|
+
const args = process.argv.slice(2);
|
|
20
|
+
if (args.length === 0 || args.includes("-h") || args.includes("--help")) {
|
|
21
|
+
console.log(help);
|
|
22
|
+
process.exit(0);
|
|
23
|
+
}
|
|
24
|
+
const cmd = args[0];
|
|
25
|
+
if (cmd !== "generate") {
|
|
26
|
+
console.error("Unknown command.\n" + help);
|
|
27
|
+
process.exit(1);
|
|
28
|
+
}
|
|
29
|
+
const outIdx = args.indexOf("--out");
|
|
30
|
+
const schemaIdx = args.indexOf("--schema");
|
|
31
|
+
const driverIdx = args.indexOf("--driver");
|
|
32
|
+
if (outIdx === -1 || schemaIdx === -1) {
|
|
33
|
+
console.error("Missing --out or --schema.\n" + help);
|
|
34
|
+
process.exit(1);
|
|
35
|
+
}
|
|
36
|
+
const outPath = (0, import_node_path.resolve)(process.cwd(), args[outIdx + 1]);
|
|
37
|
+
const schemaPath = (0, import_node_path.resolve)(process.cwd(), args[schemaIdx + 1]);
|
|
38
|
+
const driverUri = driverIdx !== -1 ? args[driverIdx + 1] : "sqlite:app.db";
|
|
39
|
+
const content = `
|
|
40
|
+
import { TauriORM } from "@type32/tauri-sqlite-orm";
|
|
41
|
+
import * as Schema from ${JSON.stringify(schemaPath)};
|
|
42
|
+
|
|
43
|
+
export function createDb() {
|
|
44
|
+
const db = new TauriORM(${JSON.stringify(driverUri)}).configure(
|
|
45
|
+
// collect tables: any export that looks like a table (has _tableName)
|
|
46
|
+
Object.fromEntries(
|
|
47
|
+
Object.entries(Schema).filter(([, v]) => v && typeof v === 'object' && '_tableName' in v)
|
|
48
|
+
) as any,
|
|
49
|
+
// optional relations export
|
|
50
|
+
(Schema as any).relations || {}
|
|
51
|
+
);
|
|
52
|
+
return db;
|
|
53
|
+
}
|
|
54
|
+
`;
|
|
55
|
+
const dir = (0, import_node_path.resolve)(outPath, "..");
|
|
56
|
+
if (!(0, import_node_fs.existsSync)(dir)) (0, import_node_fs.mkdirSync)(dir, { recursive: true });
|
|
57
|
+
(0, import_node_fs.writeFileSync)(outPath, content, "utf8");
|
|
58
|
+
console.log(`\u2714 Wrote ${outPath}`);
|
|
59
|
+
}
|
|
60
|
+
main().catch((err) => {
|
|
61
|
+
console.error(err);
|
|
62
|
+
process.exit(1);
|
|
63
|
+
});
|
package/dist/cli.mjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/cli.ts
|
|
4
|
+
import { writeFileSync, mkdirSync, existsSync } from "fs";
|
|
5
|
+
import { resolve } from "path";
|
|
6
|
+
var help = `
|
|
7
|
+
tauriorm-kit
|
|
8
|
+
|
|
9
|
+
Usage:
|
|
10
|
+
bunx tauriorm-kit generate --out ./src/lib/db-client.ts --schema ./src/lib/schema.ts
|
|
11
|
+
|
|
12
|
+
Options:
|
|
13
|
+
--out <file> Output file where a typed client factory will be written
|
|
14
|
+
--schema <file> Path to a module that exports your tables and optional relations
|
|
15
|
+
--driver <uri> Database URI (default: sqlite:app.db)
|
|
16
|
+
`;
|
|
17
|
+
async function main() {
|
|
18
|
+
const args = process.argv.slice(2);
|
|
19
|
+
if (args.length === 0 || args.includes("-h") || args.includes("--help")) {
|
|
20
|
+
console.log(help);
|
|
21
|
+
process.exit(0);
|
|
22
|
+
}
|
|
23
|
+
const cmd = args[0];
|
|
24
|
+
if (cmd !== "generate") {
|
|
25
|
+
console.error("Unknown command.\n" + help);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
const outIdx = args.indexOf("--out");
|
|
29
|
+
const schemaIdx = args.indexOf("--schema");
|
|
30
|
+
const driverIdx = args.indexOf("--driver");
|
|
31
|
+
if (outIdx === -1 || schemaIdx === -1) {
|
|
32
|
+
console.error("Missing --out or --schema.\n" + help);
|
|
33
|
+
process.exit(1);
|
|
34
|
+
}
|
|
35
|
+
const outPath = resolve(process.cwd(), args[outIdx + 1]);
|
|
36
|
+
const schemaPath = resolve(process.cwd(), args[schemaIdx + 1]);
|
|
37
|
+
const driverUri = driverIdx !== -1 ? args[driverIdx + 1] : "sqlite:app.db";
|
|
38
|
+
const content = `
|
|
39
|
+
import { TauriORM } from "@type32/tauri-sqlite-orm";
|
|
40
|
+
import * as Schema from ${JSON.stringify(schemaPath)};
|
|
41
|
+
|
|
42
|
+
export function createDb() {
|
|
43
|
+
const db = new TauriORM(${JSON.stringify(driverUri)}).configure(
|
|
44
|
+
// collect tables: any export that looks like a table (has _tableName)
|
|
45
|
+
Object.fromEntries(
|
|
46
|
+
Object.entries(Schema).filter(([, v]) => v && typeof v === 'object' && '_tableName' in v)
|
|
47
|
+
) as any,
|
|
48
|
+
// optional relations export
|
|
49
|
+
(Schema as any).relations || {}
|
|
50
|
+
);
|
|
51
|
+
return db;
|
|
52
|
+
}
|
|
53
|
+
`;
|
|
54
|
+
const dir = resolve(outPath, "..");
|
|
55
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
56
|
+
writeFileSync(outPath, content, "utf8");
|
|
57
|
+
console.log(`\u2714 Wrote ${outPath}`);
|
|
58
|
+
}
|
|
59
|
+
main().catch((err) => {
|
|
60
|
+
console.error(err);
|
|
61
|
+
process.exit(1);
|
|
62
|
+
});
|
package/dist/index.js
CHANGED
|
@@ -504,6 +504,10 @@ var asc = (column) => `${getQualifiedName(column)} ASC`;
|
|
|
504
504
|
var desc = (column) => `${getQualifiedName(column)} DESC`;
|
|
505
505
|
|
|
506
506
|
// src/orm.ts
|
|
507
|
+
function getTableName(table) {
|
|
508
|
+
const anyTable = table;
|
|
509
|
+
return anyTable._tableName || anyTable.tableName || anyTable.name || "";
|
|
510
|
+
}
|
|
507
511
|
var SelectQueryBuilder = class {
|
|
508
512
|
_table = null;
|
|
509
513
|
_selectedColumns = [];
|
|
@@ -593,9 +597,15 @@ var SelectQueryBuilder = class {
|
|
|
593
597
|
throw new Error("Cannot execute select query without a 'from' table.");
|
|
594
598
|
}
|
|
595
599
|
const db = await this._dbProvider();
|
|
600
|
+
const baseTableName = getTableName(this._table);
|
|
601
|
+
if (!baseTableName) {
|
|
602
|
+
throw new Error(
|
|
603
|
+
"Invalid table passed to select.from(): missing table name"
|
|
604
|
+
);
|
|
605
|
+
}
|
|
596
606
|
const bindings = [];
|
|
597
|
-
const selectList = this._selectedColumns.length > 0 ? this._selectedColumns.map((c) => c.alias ? `${c.sql} AS ${c.alias}` : c.sql).join(", ") : Object.values(this._table._schema).map((c) => `${
|
|
598
|
-
let query = `SELECT ${this._distinct ? "DISTINCT " : ""}${selectList} FROM ${
|
|
607
|
+
const selectList = this._selectedColumns.length > 0 ? this._selectedColumns.map((c) => c.alias ? `${c.sql} AS ${c.alias}` : c.sql).join(", ") : Object.values(this._table._schema).map((c) => `${baseTableName}.${c.name}`).join(", ");
|
|
608
|
+
let query = `SELECT ${this._distinct ? "DISTINCT " : ""}${selectList} FROM ${baseTableName}`;
|
|
599
609
|
if (this._joins.length > 0) query += ` ${this._joins.join(" ")}`;
|
|
600
610
|
if (this._where.length > 0) {
|
|
601
611
|
const whereClauses = this._where.map((condition) => {
|
|
@@ -722,9 +732,14 @@ var TauriORM = class {
|
|
|
722
732
|
}
|
|
723
733
|
async execute() {
|
|
724
734
|
const db = await self.getDb();
|
|
735
|
+
const tableName = getTableName(this._table);
|
|
736
|
+
if (!tableName)
|
|
737
|
+
throw new Error(
|
|
738
|
+
"Invalid table passed to insert(): missing table name"
|
|
739
|
+
);
|
|
725
740
|
if (this._selectSql) {
|
|
726
741
|
const cols = Object.keys(this._table._schema);
|
|
727
|
-
let query = `INSERT INTO ${
|
|
742
|
+
let query = `INSERT INTO ${tableName} (${cols.join(", ")}) ${this._selectSql.clause}`;
|
|
728
743
|
const bindings = [...this._selectSql.bindings];
|
|
729
744
|
query += this._buildConflictClause();
|
|
730
745
|
const ret = await this._executeWithReturning(db, query, bindings);
|
|
@@ -736,7 +751,8 @@ var TauriORM = class {
|
|
|
736
751
|
return value ? 1 : 0;
|
|
737
752
|
}
|
|
738
753
|
if (value instanceof Date) {
|
|
739
|
-
if (col && col.mode === "timestamp_ms")
|
|
754
|
+
if (col && col.mode === "timestamp_ms")
|
|
755
|
+
return value.getTime();
|
|
740
756
|
if (col && col.mode === "timestamp")
|
|
741
757
|
return Math.floor(value.getTime() / 1e3);
|
|
742
758
|
}
|
|
@@ -758,7 +774,9 @@ var TauriORM = class {
|
|
|
758
774
|
const keys = entries.map(([k]) => schema[k]?.name ?? k);
|
|
759
775
|
const values = entries.map(([k, v]) => coerceValue2(schema[k], v));
|
|
760
776
|
const placeholders = values.map(() => "?").join(", ");
|
|
761
|
-
let query = `INSERT INTO ${
|
|
777
|
+
let query = `INSERT INTO ${tableName} (${keys.join(
|
|
778
|
+
", "
|
|
779
|
+
)}) VALUES (${placeholders})`;
|
|
762
780
|
const bindings = [...values];
|
|
763
781
|
query += this._buildConflictClause();
|
|
764
782
|
const ret = await this._executeWithReturning(db, query, bindings);
|
|
@@ -850,6 +868,11 @@ var TauriORM = class {
|
|
|
850
868
|
if (!this._data)
|
|
851
869
|
throw new Error("Update requires set() before execute()");
|
|
852
870
|
const db = await self.getDb();
|
|
871
|
+
const tableName = getTableName(this._table);
|
|
872
|
+
if (!tableName)
|
|
873
|
+
throw new Error(
|
|
874
|
+
"Invalid table passed to update(): missing table name"
|
|
875
|
+
);
|
|
853
876
|
const schema = this._table._schema;
|
|
854
877
|
const dataToSet = { ...this._data };
|
|
855
878
|
for (const [key, col] of Object.entries(schema)) {
|
|
@@ -857,8 +880,10 @@ var TauriORM = class {
|
|
|
857
880
|
const v = col.onUpdateFn();
|
|
858
881
|
if (col.mode === "boolean") dataToSet[key] = v ? 1 : 0;
|
|
859
882
|
else if (v instanceof Date) {
|
|
860
|
-
if (col.mode === "timestamp_ms")
|
|
861
|
-
|
|
883
|
+
if (col.mode === "timestamp_ms")
|
|
884
|
+
dataToSet[key] = v.getTime();
|
|
885
|
+
else if (col.mode === "timestamp")
|
|
886
|
+
dataToSet[key] = Math.floor(v.getTime() / 1e3);
|
|
862
887
|
else dataToSet[key] = v;
|
|
863
888
|
} else dataToSet[key] = v;
|
|
864
889
|
}
|
|
@@ -878,14 +903,16 @@ var TauriORM = class {
|
|
|
878
903
|
const col = schema[k];
|
|
879
904
|
if (col && col.mode === "boolean") val = v ? 1 : 0;
|
|
880
905
|
else if (v instanceof Date) {
|
|
881
|
-
if (col && col.mode === "timestamp_ms")
|
|
882
|
-
|
|
906
|
+
if (col && col.mode === "timestamp_ms")
|
|
907
|
+
val = v.getTime();
|
|
908
|
+
else if (col && col.mode === "timestamp")
|
|
909
|
+
val = Math.floor(v.getTime() / 1e3);
|
|
883
910
|
}
|
|
884
911
|
setParts.push(`${colName} = ?`);
|
|
885
912
|
bindings.push(val);
|
|
886
913
|
}
|
|
887
914
|
}
|
|
888
|
-
let query = `UPDATE ${
|
|
915
|
+
let query = `UPDATE ${tableName} SET ${setParts.join(", ")}`;
|
|
889
916
|
if (this._from) query += ` FROM ${this._from._tableName}`;
|
|
890
917
|
if (this._where) {
|
|
891
918
|
if (typeof this._where.toSQL === "function") {
|
|
@@ -961,7 +988,12 @@ var TauriORM = class {
|
|
|
961
988
|
}
|
|
962
989
|
async execute() {
|
|
963
990
|
const db = await self.getDb();
|
|
964
|
-
|
|
991
|
+
const tableName = getTableName(this._table);
|
|
992
|
+
if (!tableName)
|
|
993
|
+
throw new Error(
|
|
994
|
+
"Invalid table passed to delete(): missing table name"
|
|
995
|
+
);
|
|
996
|
+
let query = `DELETE FROM ${tableName}`;
|
|
965
997
|
const bindings = [];
|
|
966
998
|
if (this._where) {
|
|
967
999
|
if (typeof this._where.toSQL === "function") {
|
package/dist/index.mjs
CHANGED
|
@@ -426,6 +426,10 @@ var asc = (column) => `${getQualifiedName(column)} ASC`;
|
|
|
426
426
|
var desc = (column) => `${getQualifiedName(column)} DESC`;
|
|
427
427
|
|
|
428
428
|
// src/orm.ts
|
|
429
|
+
function getTableName(table) {
|
|
430
|
+
const anyTable = table;
|
|
431
|
+
return anyTable._tableName || anyTable.tableName || anyTable.name || "";
|
|
432
|
+
}
|
|
429
433
|
var SelectQueryBuilder = class {
|
|
430
434
|
_table = null;
|
|
431
435
|
_selectedColumns = [];
|
|
@@ -515,9 +519,15 @@ var SelectQueryBuilder = class {
|
|
|
515
519
|
throw new Error("Cannot execute select query without a 'from' table.");
|
|
516
520
|
}
|
|
517
521
|
const db = await this._dbProvider();
|
|
522
|
+
const baseTableName = getTableName(this._table);
|
|
523
|
+
if (!baseTableName) {
|
|
524
|
+
throw new Error(
|
|
525
|
+
"Invalid table passed to select.from(): missing table name"
|
|
526
|
+
);
|
|
527
|
+
}
|
|
518
528
|
const bindings = [];
|
|
519
|
-
const selectList = this._selectedColumns.length > 0 ? this._selectedColumns.map((c) => c.alias ? `${c.sql} AS ${c.alias}` : c.sql).join(", ") : Object.values(this._table._schema).map((c) => `${
|
|
520
|
-
let query = `SELECT ${this._distinct ? "DISTINCT " : ""}${selectList} FROM ${
|
|
529
|
+
const selectList = this._selectedColumns.length > 0 ? this._selectedColumns.map((c) => c.alias ? `${c.sql} AS ${c.alias}` : c.sql).join(", ") : Object.values(this._table._schema).map((c) => `${baseTableName}.${c.name}`).join(", ");
|
|
530
|
+
let query = `SELECT ${this._distinct ? "DISTINCT " : ""}${selectList} FROM ${baseTableName}`;
|
|
521
531
|
if (this._joins.length > 0) query += ` ${this._joins.join(" ")}`;
|
|
522
532
|
if (this._where.length > 0) {
|
|
523
533
|
const whereClauses = this._where.map((condition) => {
|
|
@@ -644,9 +654,14 @@ var TauriORM = class {
|
|
|
644
654
|
}
|
|
645
655
|
async execute() {
|
|
646
656
|
const db = await self.getDb();
|
|
657
|
+
const tableName = getTableName(this._table);
|
|
658
|
+
if (!tableName)
|
|
659
|
+
throw new Error(
|
|
660
|
+
"Invalid table passed to insert(): missing table name"
|
|
661
|
+
);
|
|
647
662
|
if (this._selectSql) {
|
|
648
663
|
const cols = Object.keys(this._table._schema);
|
|
649
|
-
let query = `INSERT INTO ${
|
|
664
|
+
let query = `INSERT INTO ${tableName} (${cols.join(", ")}) ${this._selectSql.clause}`;
|
|
650
665
|
const bindings = [...this._selectSql.bindings];
|
|
651
666
|
query += this._buildConflictClause();
|
|
652
667
|
const ret = await this._executeWithReturning(db, query, bindings);
|
|
@@ -658,7 +673,8 @@ var TauriORM = class {
|
|
|
658
673
|
return value ? 1 : 0;
|
|
659
674
|
}
|
|
660
675
|
if (value instanceof Date) {
|
|
661
|
-
if (col && col.mode === "timestamp_ms")
|
|
676
|
+
if (col && col.mode === "timestamp_ms")
|
|
677
|
+
return value.getTime();
|
|
662
678
|
if (col && col.mode === "timestamp")
|
|
663
679
|
return Math.floor(value.getTime() / 1e3);
|
|
664
680
|
}
|
|
@@ -680,7 +696,9 @@ var TauriORM = class {
|
|
|
680
696
|
const keys = entries.map(([k]) => schema[k]?.name ?? k);
|
|
681
697
|
const values = entries.map(([k, v]) => coerceValue2(schema[k], v));
|
|
682
698
|
const placeholders = values.map(() => "?").join(", ");
|
|
683
|
-
let query = `INSERT INTO ${
|
|
699
|
+
let query = `INSERT INTO ${tableName} (${keys.join(
|
|
700
|
+
", "
|
|
701
|
+
)}) VALUES (${placeholders})`;
|
|
684
702
|
const bindings = [...values];
|
|
685
703
|
query += this._buildConflictClause();
|
|
686
704
|
const ret = await this._executeWithReturning(db, query, bindings);
|
|
@@ -772,6 +790,11 @@ var TauriORM = class {
|
|
|
772
790
|
if (!this._data)
|
|
773
791
|
throw new Error("Update requires set() before execute()");
|
|
774
792
|
const db = await self.getDb();
|
|
793
|
+
const tableName = getTableName(this._table);
|
|
794
|
+
if (!tableName)
|
|
795
|
+
throw new Error(
|
|
796
|
+
"Invalid table passed to update(): missing table name"
|
|
797
|
+
);
|
|
775
798
|
const schema = this._table._schema;
|
|
776
799
|
const dataToSet = { ...this._data };
|
|
777
800
|
for (const [key, col] of Object.entries(schema)) {
|
|
@@ -779,8 +802,10 @@ var TauriORM = class {
|
|
|
779
802
|
const v = col.onUpdateFn();
|
|
780
803
|
if (col.mode === "boolean") dataToSet[key] = v ? 1 : 0;
|
|
781
804
|
else if (v instanceof Date) {
|
|
782
|
-
if (col.mode === "timestamp_ms")
|
|
783
|
-
|
|
805
|
+
if (col.mode === "timestamp_ms")
|
|
806
|
+
dataToSet[key] = v.getTime();
|
|
807
|
+
else if (col.mode === "timestamp")
|
|
808
|
+
dataToSet[key] = Math.floor(v.getTime() / 1e3);
|
|
784
809
|
else dataToSet[key] = v;
|
|
785
810
|
} else dataToSet[key] = v;
|
|
786
811
|
}
|
|
@@ -800,14 +825,16 @@ var TauriORM = class {
|
|
|
800
825
|
const col = schema[k];
|
|
801
826
|
if (col && col.mode === "boolean") val = v ? 1 : 0;
|
|
802
827
|
else if (v instanceof Date) {
|
|
803
|
-
if (col && col.mode === "timestamp_ms")
|
|
804
|
-
|
|
828
|
+
if (col && col.mode === "timestamp_ms")
|
|
829
|
+
val = v.getTime();
|
|
830
|
+
else if (col && col.mode === "timestamp")
|
|
831
|
+
val = Math.floor(v.getTime() / 1e3);
|
|
805
832
|
}
|
|
806
833
|
setParts.push(`${colName} = ?`);
|
|
807
834
|
bindings.push(val);
|
|
808
835
|
}
|
|
809
836
|
}
|
|
810
|
-
let query = `UPDATE ${
|
|
837
|
+
let query = `UPDATE ${tableName} SET ${setParts.join(", ")}`;
|
|
811
838
|
if (this._from) query += ` FROM ${this._from._tableName}`;
|
|
812
839
|
if (this._where) {
|
|
813
840
|
if (typeof this._where.toSQL === "function") {
|
|
@@ -883,7 +910,12 @@ var TauriORM = class {
|
|
|
883
910
|
}
|
|
884
911
|
async execute() {
|
|
885
912
|
const db = await self.getDb();
|
|
886
|
-
|
|
913
|
+
const tableName = getTableName(this._table);
|
|
914
|
+
if (!tableName)
|
|
915
|
+
throw new Error(
|
|
916
|
+
"Invalid table passed to delete(): missing table name"
|
|
917
|
+
);
|
|
918
|
+
let query = `DELETE FROM ${tableName}`;
|
|
887
919
|
const bindings = [];
|
|
888
920
|
if (this._where) {
|
|
889
921
|
if (typeof this._where.toSQL === "function") {
|