@type32/tauri-sqlite-orm 0.1.10 → 0.1.11
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 +24 -11
- package/dist/index.mjs +24 -11
- package/package.json +6 -3
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
|
@@ -593,9 +593,11 @@ var SelectQueryBuilder = class {
|
|
|
593
593
|
throw new Error("Cannot execute select query without a 'from' table.");
|
|
594
594
|
}
|
|
595
595
|
const db = await this._dbProvider();
|
|
596
|
+
const baseTableName = this._table._tableName || this._table.tableName;
|
|
597
|
+
if (!baseTableName) throw new Error("Invalid table passed to select.from(): missing _tableName");
|
|
596
598
|
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 ${
|
|
599
|
+
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(", ");
|
|
600
|
+
let query = `SELECT ${this._distinct ? "DISTINCT " : ""}${selectList} FROM ${baseTableName}`;
|
|
599
601
|
if (this._joins.length > 0) query += ` ${this._joins.join(" ")}`;
|
|
600
602
|
if (this._where.length > 0) {
|
|
601
603
|
const whereClauses = this._where.map((condition) => {
|
|
@@ -722,9 +724,11 @@ var TauriORM = class {
|
|
|
722
724
|
}
|
|
723
725
|
async execute() {
|
|
724
726
|
const db = await self.getDb();
|
|
727
|
+
const tableName = this._table._tableName || this._table.tableName;
|
|
728
|
+
if (!tableName) throw new Error("Invalid table passed to insert(): missing _tableName");
|
|
725
729
|
if (this._selectSql) {
|
|
726
730
|
const cols = Object.keys(this._table._schema);
|
|
727
|
-
let query = `INSERT INTO ${
|
|
731
|
+
let query = `INSERT INTO ${tableName} (${cols.join(", ")}) ${this._selectSql.clause}`;
|
|
728
732
|
const bindings = [...this._selectSql.bindings];
|
|
729
733
|
query += this._buildConflictClause();
|
|
730
734
|
const ret = await this._executeWithReturning(db, query, bindings);
|
|
@@ -736,7 +740,8 @@ var TauriORM = class {
|
|
|
736
740
|
return value ? 1 : 0;
|
|
737
741
|
}
|
|
738
742
|
if (value instanceof Date) {
|
|
739
|
-
if (col && col.mode === "timestamp_ms")
|
|
743
|
+
if (col && col.mode === "timestamp_ms")
|
|
744
|
+
return value.getTime();
|
|
740
745
|
if (col && col.mode === "timestamp")
|
|
741
746
|
return Math.floor(value.getTime() / 1e3);
|
|
742
747
|
}
|
|
@@ -758,7 +763,7 @@ var TauriORM = class {
|
|
|
758
763
|
const keys = entries.map(([k]) => schema[k]?.name ?? k);
|
|
759
764
|
const values = entries.map(([k, v]) => coerceValue2(schema[k], v));
|
|
760
765
|
const placeholders = values.map(() => "?").join(", ");
|
|
761
|
-
let query = `INSERT INTO ${
|
|
766
|
+
let query = `INSERT INTO ${tableName} (${keys.join(", ")}) VALUES (${placeholders})`;
|
|
762
767
|
const bindings = [...values];
|
|
763
768
|
query += this._buildConflictClause();
|
|
764
769
|
const ret = await this._executeWithReturning(db, query, bindings);
|
|
@@ -850,6 +855,8 @@ var TauriORM = class {
|
|
|
850
855
|
if (!this._data)
|
|
851
856
|
throw new Error("Update requires set() before execute()");
|
|
852
857
|
const db = await self.getDb();
|
|
858
|
+
const tableName = this._table._tableName || this._table.tableName;
|
|
859
|
+
if (!tableName) throw new Error("Invalid table passed to update(): missing _tableName");
|
|
853
860
|
const schema = this._table._schema;
|
|
854
861
|
const dataToSet = { ...this._data };
|
|
855
862
|
for (const [key, col] of Object.entries(schema)) {
|
|
@@ -857,8 +864,10 @@ var TauriORM = class {
|
|
|
857
864
|
const v = col.onUpdateFn();
|
|
858
865
|
if (col.mode === "boolean") dataToSet[key] = v ? 1 : 0;
|
|
859
866
|
else if (v instanceof Date) {
|
|
860
|
-
if (col.mode === "timestamp_ms")
|
|
861
|
-
|
|
867
|
+
if (col.mode === "timestamp_ms")
|
|
868
|
+
dataToSet[key] = v.getTime();
|
|
869
|
+
else if (col.mode === "timestamp")
|
|
870
|
+
dataToSet[key] = Math.floor(v.getTime() / 1e3);
|
|
862
871
|
else dataToSet[key] = v;
|
|
863
872
|
} else dataToSet[key] = v;
|
|
864
873
|
}
|
|
@@ -878,14 +887,16 @@ var TauriORM = class {
|
|
|
878
887
|
const col = schema[k];
|
|
879
888
|
if (col && col.mode === "boolean") val = v ? 1 : 0;
|
|
880
889
|
else if (v instanceof Date) {
|
|
881
|
-
if (col && col.mode === "timestamp_ms")
|
|
882
|
-
|
|
890
|
+
if (col && col.mode === "timestamp_ms")
|
|
891
|
+
val = v.getTime();
|
|
892
|
+
else if (col && col.mode === "timestamp")
|
|
893
|
+
val = Math.floor(v.getTime() / 1e3);
|
|
883
894
|
}
|
|
884
895
|
setParts.push(`${colName} = ?`);
|
|
885
896
|
bindings.push(val);
|
|
886
897
|
}
|
|
887
898
|
}
|
|
888
|
-
let query = `UPDATE ${
|
|
899
|
+
let query = `UPDATE ${tableName} SET ${setParts.join(", ")}`;
|
|
889
900
|
if (this._from) query += ` FROM ${this._from._tableName}`;
|
|
890
901
|
if (this._where) {
|
|
891
902
|
if (typeof this._where.toSQL === "function") {
|
|
@@ -961,7 +972,9 @@ var TauriORM = class {
|
|
|
961
972
|
}
|
|
962
973
|
async execute() {
|
|
963
974
|
const db = await self.getDb();
|
|
964
|
-
|
|
975
|
+
const tableName = this._table._tableName || this._table.tableName;
|
|
976
|
+
if (!tableName) throw new Error("Invalid table passed to delete(): missing _tableName");
|
|
977
|
+
let query = `DELETE FROM ${tableName}`;
|
|
965
978
|
const bindings = [];
|
|
966
979
|
if (this._where) {
|
|
967
980
|
if (typeof this._where.toSQL === "function") {
|
package/dist/index.mjs
CHANGED
|
@@ -515,9 +515,11 @@ var SelectQueryBuilder = class {
|
|
|
515
515
|
throw new Error("Cannot execute select query without a 'from' table.");
|
|
516
516
|
}
|
|
517
517
|
const db = await this._dbProvider();
|
|
518
|
+
const baseTableName = this._table._tableName || this._table.tableName;
|
|
519
|
+
if (!baseTableName) throw new Error("Invalid table passed to select.from(): missing _tableName");
|
|
518
520
|
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 ${
|
|
521
|
+
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(", ");
|
|
522
|
+
let query = `SELECT ${this._distinct ? "DISTINCT " : ""}${selectList} FROM ${baseTableName}`;
|
|
521
523
|
if (this._joins.length > 0) query += ` ${this._joins.join(" ")}`;
|
|
522
524
|
if (this._where.length > 0) {
|
|
523
525
|
const whereClauses = this._where.map((condition) => {
|
|
@@ -644,9 +646,11 @@ var TauriORM = class {
|
|
|
644
646
|
}
|
|
645
647
|
async execute() {
|
|
646
648
|
const db = await self.getDb();
|
|
649
|
+
const tableName = this._table._tableName || this._table.tableName;
|
|
650
|
+
if (!tableName) throw new Error("Invalid table passed to insert(): missing _tableName");
|
|
647
651
|
if (this._selectSql) {
|
|
648
652
|
const cols = Object.keys(this._table._schema);
|
|
649
|
-
let query = `INSERT INTO ${
|
|
653
|
+
let query = `INSERT INTO ${tableName} (${cols.join(", ")}) ${this._selectSql.clause}`;
|
|
650
654
|
const bindings = [...this._selectSql.bindings];
|
|
651
655
|
query += this._buildConflictClause();
|
|
652
656
|
const ret = await this._executeWithReturning(db, query, bindings);
|
|
@@ -658,7 +662,8 @@ var TauriORM = class {
|
|
|
658
662
|
return value ? 1 : 0;
|
|
659
663
|
}
|
|
660
664
|
if (value instanceof Date) {
|
|
661
|
-
if (col && col.mode === "timestamp_ms")
|
|
665
|
+
if (col && col.mode === "timestamp_ms")
|
|
666
|
+
return value.getTime();
|
|
662
667
|
if (col && col.mode === "timestamp")
|
|
663
668
|
return Math.floor(value.getTime() / 1e3);
|
|
664
669
|
}
|
|
@@ -680,7 +685,7 @@ var TauriORM = class {
|
|
|
680
685
|
const keys = entries.map(([k]) => schema[k]?.name ?? k);
|
|
681
686
|
const values = entries.map(([k, v]) => coerceValue2(schema[k], v));
|
|
682
687
|
const placeholders = values.map(() => "?").join(", ");
|
|
683
|
-
let query = `INSERT INTO ${
|
|
688
|
+
let query = `INSERT INTO ${tableName} (${keys.join(", ")}) VALUES (${placeholders})`;
|
|
684
689
|
const bindings = [...values];
|
|
685
690
|
query += this._buildConflictClause();
|
|
686
691
|
const ret = await this._executeWithReturning(db, query, bindings);
|
|
@@ -772,6 +777,8 @@ var TauriORM = class {
|
|
|
772
777
|
if (!this._data)
|
|
773
778
|
throw new Error("Update requires set() before execute()");
|
|
774
779
|
const db = await self.getDb();
|
|
780
|
+
const tableName = this._table._tableName || this._table.tableName;
|
|
781
|
+
if (!tableName) throw new Error("Invalid table passed to update(): missing _tableName");
|
|
775
782
|
const schema = this._table._schema;
|
|
776
783
|
const dataToSet = { ...this._data };
|
|
777
784
|
for (const [key, col] of Object.entries(schema)) {
|
|
@@ -779,8 +786,10 @@ var TauriORM = class {
|
|
|
779
786
|
const v = col.onUpdateFn();
|
|
780
787
|
if (col.mode === "boolean") dataToSet[key] = v ? 1 : 0;
|
|
781
788
|
else if (v instanceof Date) {
|
|
782
|
-
if (col.mode === "timestamp_ms")
|
|
783
|
-
|
|
789
|
+
if (col.mode === "timestamp_ms")
|
|
790
|
+
dataToSet[key] = v.getTime();
|
|
791
|
+
else if (col.mode === "timestamp")
|
|
792
|
+
dataToSet[key] = Math.floor(v.getTime() / 1e3);
|
|
784
793
|
else dataToSet[key] = v;
|
|
785
794
|
} else dataToSet[key] = v;
|
|
786
795
|
}
|
|
@@ -800,14 +809,16 @@ var TauriORM = class {
|
|
|
800
809
|
const col = schema[k];
|
|
801
810
|
if (col && col.mode === "boolean") val = v ? 1 : 0;
|
|
802
811
|
else if (v instanceof Date) {
|
|
803
|
-
if (col && col.mode === "timestamp_ms")
|
|
804
|
-
|
|
812
|
+
if (col && col.mode === "timestamp_ms")
|
|
813
|
+
val = v.getTime();
|
|
814
|
+
else if (col && col.mode === "timestamp")
|
|
815
|
+
val = Math.floor(v.getTime() / 1e3);
|
|
805
816
|
}
|
|
806
817
|
setParts.push(`${colName} = ?`);
|
|
807
818
|
bindings.push(val);
|
|
808
819
|
}
|
|
809
820
|
}
|
|
810
|
-
let query = `UPDATE ${
|
|
821
|
+
let query = `UPDATE ${tableName} SET ${setParts.join(", ")}`;
|
|
811
822
|
if (this._from) query += ` FROM ${this._from._tableName}`;
|
|
812
823
|
if (this._where) {
|
|
813
824
|
if (typeof this._where.toSQL === "function") {
|
|
@@ -883,7 +894,9 @@ var TauriORM = class {
|
|
|
883
894
|
}
|
|
884
895
|
async execute() {
|
|
885
896
|
const db = await self.getDb();
|
|
886
|
-
|
|
897
|
+
const tableName = this._table._tableName || this._table.tableName;
|
|
898
|
+
if (!tableName) throw new Error("Invalid table passed to delete(): missing _tableName");
|
|
899
|
+
let query = `DELETE FROM ${tableName}`;
|
|
887
900
|
const bindings = [];
|
|
888
901
|
if (this._where) {
|
|
889
902
|
if (typeof this._where.toSQL === "function") {
|
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@type32/tauri-sqlite-orm",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.11",
|
|
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",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"tauriorm-kit": "./dist/cli.mjs"
|
|
10
|
+
},
|
|
8
11
|
"exports": {
|
|
9
12
|
".": {
|
|
10
13
|
"import": "./dist/index.mjs",
|
|
@@ -15,8 +18,8 @@
|
|
|
15
18
|
"dist"
|
|
16
19
|
],
|
|
17
20
|
"scripts": {
|
|
18
|
-
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
19
|
-
"dev": "tsup src/index.ts --format esm,cjs --dts --watch",
|
|
21
|
+
"build": "tsup src/index.ts src/cli.ts --format esm,cjs --dts",
|
|
22
|
+
"dev": "tsup src/index.ts src/cli.ts --format esm,cjs --dts --watch",
|
|
20
23
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
21
24
|
},
|
|
22
25
|
"keywords": [
|