@type32/tauri-sqlite-orm 0.1.18-11 → 0.1.18-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/index.d.mts +6 -4
- package/dist/index.d.ts +6 -4
- package/dist/index.js +112 -32
- package/dist/index.mjs +112 -32
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -49,7 +49,7 @@ declare class BaseQueryBuilder {
|
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
declare class SelectQueryBuilder<TTable extends AnyTable, TSelectedColumns extends (keyof TTable[
|
|
52
|
+
declare class SelectQueryBuilder<TTable extends AnyTable, TSelectedColumns extends (keyof TTable['_']['columns'])[] | undefined = undefined> extends BaseQueryBuilder {
|
|
53
53
|
private table;
|
|
54
54
|
private columns?;
|
|
55
55
|
private isDistinct;
|
|
@@ -57,15 +57,17 @@ declare class SelectQueryBuilder<TTable extends AnyTable, TSelectedColumns exten
|
|
|
57
57
|
private havingCondition;
|
|
58
58
|
private joins;
|
|
59
59
|
private includeRelations;
|
|
60
|
+
private selectedTableAlias;
|
|
60
61
|
constructor(db: Database, table: TTable, columns?: TSelectedColumns | undefined);
|
|
61
62
|
distinct(): this;
|
|
62
63
|
groupBy(...columns: AnySQLiteColumn[]): this;
|
|
63
64
|
having(condition: SQLCondition): this;
|
|
64
|
-
leftJoin<T extends AnyTable>(table: T, condition: SQLCondition, alias
|
|
65
|
-
innerJoin<T extends AnyTable>(table: T, condition: SQLCondition, alias
|
|
65
|
+
leftJoin<T extends AnyTable>(table: T, condition: SQLCondition, alias: string): this;
|
|
66
|
+
innerJoin<T extends AnyTable>(table: T, condition: SQLCondition, alias: string): this;
|
|
66
67
|
include(relations: Record<string, boolean>): this;
|
|
67
68
|
private buildJoins;
|
|
68
69
|
execute(): Promise<any[]>;
|
|
70
|
+
private processRelationResults;
|
|
69
71
|
all(): Promise<any[]>;
|
|
70
72
|
get(): Promise<any | undefined>;
|
|
71
73
|
}
|
|
@@ -247,7 +249,7 @@ declare const relations: <T extends AnyTable, R extends Record<string, Relation>
|
|
|
247
249
|
declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
|
|
248
250
|
declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
|
|
249
251
|
|
|
250
|
-
declare const eq: <T>(column: AnySQLiteColumn, value: T) => SQLCondition;
|
|
252
|
+
declare const eq: <T>(column: AnySQLiteColumn, value: T, tableAlias?: string) => SQLCondition;
|
|
251
253
|
declare const and: (...conditions: SQLCondition[]) => SQLCondition;
|
|
252
254
|
declare const or: (...conditions: SQLCondition[]) => SQLCondition;
|
|
253
255
|
declare const not: (condition: SQLCondition) => SQLCondition;
|
package/dist/index.d.ts
CHANGED
|
@@ -49,7 +49,7 @@ declare class BaseQueryBuilder {
|
|
|
49
49
|
};
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
declare class SelectQueryBuilder<TTable extends AnyTable, TSelectedColumns extends (keyof TTable[
|
|
52
|
+
declare class SelectQueryBuilder<TTable extends AnyTable, TSelectedColumns extends (keyof TTable['_']['columns'])[] | undefined = undefined> extends BaseQueryBuilder {
|
|
53
53
|
private table;
|
|
54
54
|
private columns?;
|
|
55
55
|
private isDistinct;
|
|
@@ -57,15 +57,17 @@ declare class SelectQueryBuilder<TTable extends AnyTable, TSelectedColumns exten
|
|
|
57
57
|
private havingCondition;
|
|
58
58
|
private joins;
|
|
59
59
|
private includeRelations;
|
|
60
|
+
private selectedTableAlias;
|
|
60
61
|
constructor(db: Database, table: TTable, columns?: TSelectedColumns | undefined);
|
|
61
62
|
distinct(): this;
|
|
62
63
|
groupBy(...columns: AnySQLiteColumn[]): this;
|
|
63
64
|
having(condition: SQLCondition): this;
|
|
64
|
-
leftJoin<T extends AnyTable>(table: T, condition: SQLCondition, alias
|
|
65
|
-
innerJoin<T extends AnyTable>(table: T, condition: SQLCondition, alias
|
|
65
|
+
leftJoin<T extends AnyTable>(table: T, condition: SQLCondition, alias: string): this;
|
|
66
|
+
innerJoin<T extends AnyTable>(table: T, condition: SQLCondition, alias: string): this;
|
|
66
67
|
include(relations: Record<string, boolean>): this;
|
|
67
68
|
private buildJoins;
|
|
68
69
|
execute(): Promise<any[]>;
|
|
70
|
+
private processRelationResults;
|
|
69
71
|
all(): Promise<any[]>;
|
|
70
72
|
get(): Promise<any | undefined>;
|
|
71
73
|
}
|
|
@@ -247,7 +249,7 @@ declare const relations: <T extends AnyTable, R extends Record<string, Relation>
|
|
|
247
249
|
declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
|
|
248
250
|
declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
|
|
249
251
|
|
|
250
|
-
declare const eq: <T>(column: AnySQLiteColumn, value: T) => SQLCondition;
|
|
252
|
+
declare const eq: <T>(column: AnySQLiteColumn, value: T, tableAlias?: string) => SQLCondition;
|
|
251
253
|
declare const and: (...conditions: SQLCondition[]) => SQLCondition;
|
|
252
254
|
declare const or: (...conditions: SQLCondition[]) => SQLCondition;
|
|
253
255
|
declare const not: (condition: SQLCondition) => SQLCondition;
|
package/dist/index.js
CHANGED
|
@@ -103,10 +103,13 @@ var BaseQueryBuilder = class {
|
|
|
103
103
|
};
|
|
104
104
|
|
|
105
105
|
// src/operators.ts
|
|
106
|
-
var eq = (column, value) =>
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
106
|
+
var eq = (column, value, tableAlias) => {
|
|
107
|
+
const columnName = tableAlias ? `${tableAlias}.${column._.name}` : column._.name;
|
|
108
|
+
return {
|
|
109
|
+
sql: `${columnName} = ?`,
|
|
110
|
+
params: [value]
|
|
111
|
+
};
|
|
112
|
+
};
|
|
110
113
|
var and = (...conditions) => ({
|
|
111
114
|
sql: conditions.map((c) => `(${c.sql})`).join(" AND "),
|
|
112
115
|
params: conditions.flatMap((c) => c.params)
|
|
@@ -182,14 +185,17 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
182
185
|
super(db);
|
|
183
186
|
this.table = table;
|
|
184
187
|
this.columns = columns;
|
|
185
|
-
|
|
186
|
-
|
|
188
|
+
this.selectedTableAlias = table._.name;
|
|
189
|
+
const columnNames = columns ? columns.map((c) => `${this.selectedTableAlias}.${table._.columns[c]._.name}`) : [`${this.selectedTableAlias}.*`];
|
|
190
|
+
this.query = `SELECT ${columnNames.join(", ")}
|
|
191
|
+
FROM ${table._.name} ${this.selectedTableAlias}`;
|
|
187
192
|
}
|
|
188
193
|
isDistinct = false;
|
|
189
194
|
groupByColumns = [];
|
|
190
195
|
havingCondition = null;
|
|
191
196
|
joins = [];
|
|
192
197
|
includeRelations = {};
|
|
198
|
+
selectedTableAlias;
|
|
193
199
|
distinct() {
|
|
194
200
|
this.isDistinct = true;
|
|
195
201
|
this.query = this.query.replace("SELECT", "SELECT DISTINCT");
|
|
@@ -197,7 +203,7 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
197
203
|
}
|
|
198
204
|
groupBy(...columns) {
|
|
199
205
|
this.groupByColumns.push(...columns);
|
|
200
|
-
const columnNames = columns.map((col) => col._.name).join(", ");
|
|
206
|
+
const columnNames = columns.map((col) => `${this.selectedTableAlias}.${col._.name}`).join(", ");
|
|
201
207
|
this.query += ` GROUP BY ${columnNames}`;
|
|
202
208
|
return this;
|
|
203
209
|
}
|
|
@@ -223,48 +229,104 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
223
229
|
let sql2 = "";
|
|
224
230
|
const params = [];
|
|
225
231
|
for (const join of this.joins) {
|
|
226
|
-
|
|
227
|
-
sql2 += ` ${join.type} JOIN ${join.table._.name} ${tableAlias} ON ${join.condition.sql}`;
|
|
232
|
+
sql2 += ` ${join.type} JOIN ${join.table._.name} ${join.alias} ON ${join.condition.sql}`;
|
|
228
233
|
params.push(...join.condition.params);
|
|
229
234
|
}
|
|
230
|
-
for (const [relationName, include] of Object.entries(
|
|
231
|
-
this.includeRelations
|
|
232
|
-
)) {
|
|
235
|
+
for (const [relationName, include] of Object.entries(this.includeRelations)) {
|
|
233
236
|
if (!include) continue;
|
|
234
237
|
const relation = this.table.relations[relationName];
|
|
235
238
|
if (!relation) continue;
|
|
236
239
|
const foreignTable = relation.foreignTable;
|
|
237
|
-
const foreignAlias = `${this.
|
|
240
|
+
const foreignAlias = `${this.selectedTableAlias}_${relationName}`;
|
|
238
241
|
if (relation.type === "one" && relation.fields && relation.references) {
|
|
239
|
-
const conditions = relation.fields.map(
|
|
240
|
-
|
|
241
|
-
|
|
242
|
+
const conditions = relation.fields.map((field, i) => {
|
|
243
|
+
const localColumn = `${this.selectedTableAlias}.${field._.name}`;
|
|
244
|
+
const foreignColumn = `${foreignAlias}.${relation.references[i]._.name}`;
|
|
245
|
+
return {
|
|
246
|
+
sql: `${localColumn} = ${foreignColumn}`,
|
|
247
|
+
params: []
|
|
248
|
+
};
|
|
249
|
+
});
|
|
242
250
|
const condition = conditions.length > 1 ? and(...conditions) : conditions[0];
|
|
243
251
|
sql2 += ` LEFT JOIN ${foreignTable._.name} ${foreignAlias} ON ${condition.sql}`;
|
|
244
252
|
params.push(...condition.params);
|
|
245
253
|
} else if (relation.type === "many") {
|
|
246
|
-
const refRelation = Object.
|
|
247
|
-
(r) => r.foreignTable === this.table
|
|
254
|
+
const refRelation = Object.entries(foreignTable.relations).find(
|
|
255
|
+
([_, r]) => r.foreignTable === this.table
|
|
248
256
|
);
|
|
249
|
-
if (refRelation && refRelation.fields && refRelation.references) {
|
|
250
|
-
const
|
|
251
|
-
|
|
252
|
-
|
|
257
|
+
if (refRelation && refRelation[1].fields && refRelation[1].references) {
|
|
258
|
+
const [_, relationConfig] = refRelation;
|
|
259
|
+
const conditions = relationConfig.fields.map((field, i) => {
|
|
260
|
+
const localColumn = `${foreignAlias}.${field._.name}`;
|
|
261
|
+
const foreignColumn = `${this.selectedTableAlias}.${relationConfig.references[i]._.name}`;
|
|
262
|
+
return {
|
|
263
|
+
sql: `${localColumn} = ${foreignColumn}`,
|
|
264
|
+
params: []
|
|
265
|
+
};
|
|
266
|
+
});
|
|
253
267
|
const condition = conditions.length > 1 ? and(...conditions) : conditions[0];
|
|
254
268
|
sql2 += ` LEFT JOIN ${foreignTable._.name} ${foreignAlias} ON ${condition.sql}`;
|
|
255
269
|
params.push(...condition.params);
|
|
270
|
+
this.query = this.query.replace("SELECT ", `SELECT ${this.selectedTableAlias}.*, ${foreignAlias}.* `).replace(
|
|
271
|
+
`SELECT ${this.selectedTableAlias}.*, `,
|
|
272
|
+
`SELECT ${this.selectedTableAlias}.*, ${foreignAlias}.*, `
|
|
273
|
+
);
|
|
256
274
|
}
|
|
257
275
|
}
|
|
258
276
|
}
|
|
259
277
|
return { sql: sql2, params };
|
|
260
278
|
}
|
|
279
|
+
// Enhanced execute method that handles relation data mapping
|
|
261
280
|
async execute() {
|
|
262
281
|
const { sql: joinSql, params: joinParams } = this.buildJoins();
|
|
263
282
|
this.query += joinSql;
|
|
264
283
|
this.params.push(...joinParams);
|
|
265
284
|
const { sql: sql2, params } = this.build();
|
|
266
|
-
|
|
285
|
+
console.log("Executing SQL:", sql2, "with params:", params);
|
|
286
|
+
const rawResults = await this.db.select(sql2, params);
|
|
287
|
+
if (Object.keys(this.includeRelations).some((key) => this.includeRelations[key])) {
|
|
288
|
+
const processed = this.processRelationResults(rawResults);
|
|
289
|
+
return processed;
|
|
290
|
+
}
|
|
291
|
+
return rawResults;
|
|
292
|
+
}
|
|
293
|
+
processRelationResults(rawResults) {
|
|
294
|
+
if (!rawResults.length) return rawResults;
|
|
295
|
+
return rawResults.map((row) => {
|
|
296
|
+
const result = {};
|
|
297
|
+
const relations2 = {};
|
|
298
|
+
for (const [key, value] of Object.entries(row)) {
|
|
299
|
+
if (key.includes(".")) {
|
|
300
|
+
const [tableAlias, columnName] = key.split(".");
|
|
301
|
+
if (tableAlias === this.selectedTableAlias) {
|
|
302
|
+
result[columnName] = value;
|
|
303
|
+
} else {
|
|
304
|
+
const parts = tableAlias.split("_");
|
|
305
|
+
if (parts.length >= 2 && parts[0] === this.selectedTableAlias) {
|
|
306
|
+
const relationName = parts.slice(1).join("_");
|
|
307
|
+
if (!relations2[relationName]) relations2[relationName] = {};
|
|
308
|
+
relations2[relationName][columnName] = value;
|
|
309
|
+
} else {
|
|
310
|
+
if (!result[tableAlias]) result[tableAlias] = {};
|
|
311
|
+
result[tableAlias][columnName] = value;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
} else {
|
|
315
|
+
result[key] = value;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
for (const [relName, relData] of Object.entries(relations2)) {
|
|
319
|
+
const hasData = Object.values(relData).some(
|
|
320
|
+
(v) => v !== null && v !== void 0 && v !== ""
|
|
321
|
+
);
|
|
322
|
+
if (hasData) {
|
|
323
|
+
result[relName] = relData;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return result;
|
|
327
|
+
});
|
|
267
328
|
}
|
|
329
|
+
// Update the return type signatures
|
|
268
330
|
async all() {
|
|
269
331
|
return this.execute();
|
|
270
332
|
}
|
|
@@ -477,7 +539,8 @@ var DeleteQueryBuilder = class extends BaseQueryBuilder {
|
|
|
477
539
|
constructor(db, table) {
|
|
478
540
|
super(db);
|
|
479
541
|
this.table = table;
|
|
480
|
-
this.query = `DELETE
|
|
542
|
+
this.query = `DELETE
|
|
543
|
+
FROM ${table._.name}`;
|
|
481
544
|
}
|
|
482
545
|
returningColumns = [];
|
|
483
546
|
returning(...columns) {
|
|
@@ -687,9 +750,7 @@ var TauriORM = class {
|
|
|
687
750
|
for (const [key, value] of Object.entries(schema)) {
|
|
688
751
|
if (!(value instanceof Table) && typeof value === "object") {
|
|
689
752
|
const tableName = key.replace("Relations", "");
|
|
690
|
-
const table = Array.from(this.tables.values()).find(
|
|
691
|
-
(t) => t._.name === tableName
|
|
692
|
-
);
|
|
753
|
+
const table = Array.from(this.tables.values()).find((t) => t._.name === tableName);
|
|
693
754
|
if (table) {
|
|
694
755
|
for (const [relName, rel] of Object.entries(value)) {
|
|
695
756
|
if (rel instanceof OneRelation) {
|
|
@@ -738,7 +799,10 @@ var TauriORM = class {
|
|
|
738
799
|
);
|
|
739
800
|
if (existingTableInfo.length === 0) {
|
|
740
801
|
const columnsSql = Object.values(table._.columns).map((col) => this.buildColumnDefinition(col)).join(", ");
|
|
741
|
-
const createSql = `CREATE TABLE ${table._.name}
|
|
802
|
+
const createSql = `CREATE TABLE ${table._.name}
|
|
803
|
+
(
|
|
804
|
+
${columnsSql}
|
|
805
|
+
)`;
|
|
742
806
|
await this.db.execute(createSql);
|
|
743
807
|
} else {
|
|
744
808
|
const existingColumnNames = new Set(
|
|
@@ -747,7 +811,8 @@ var TauriORM = class {
|
|
|
747
811
|
for (const column of Object.values(table._.columns)) {
|
|
748
812
|
if (!existingColumnNames.has(column._.name)) {
|
|
749
813
|
const columnSql = this.buildColumnDefinition(column, true);
|
|
750
|
-
const alterSql = `ALTER TABLE ${table._.name}
|
|
814
|
+
const alterSql = `ALTER TABLE ${table._.name}
|
|
815
|
+
ADD COLUMN ${columnSql}`;
|
|
751
816
|
await this.db.execute(alterSql);
|
|
752
817
|
}
|
|
753
818
|
}
|
|
@@ -792,13 +857,25 @@ var TauriORM = class {
|
|
|
792
857
|
// --- Schema detection / signature ---
|
|
793
858
|
async ensureSchemaMeta() {
|
|
794
859
|
await this.db.execute(
|
|
795
|
-
`CREATE TABLE IF NOT EXISTS _schema_meta
|
|
860
|
+
`CREATE TABLE IF NOT EXISTS _schema_meta
|
|
861
|
+
(
|
|
862
|
+
key
|
|
863
|
+
TEXT
|
|
864
|
+
PRIMARY
|
|
865
|
+
KEY,
|
|
866
|
+
value
|
|
867
|
+
TEXT
|
|
868
|
+
NOT
|
|
869
|
+
NULL
|
|
870
|
+
)`
|
|
796
871
|
);
|
|
797
872
|
}
|
|
798
873
|
async getSchemaMeta(key) {
|
|
799
874
|
await this.ensureSchemaMeta();
|
|
800
875
|
const rows = await this.db.select(
|
|
801
|
-
`SELECT value
|
|
876
|
+
`SELECT value
|
|
877
|
+
FROM _schema_meta
|
|
878
|
+
WHERE key = ?`,
|
|
802
879
|
[key]
|
|
803
880
|
);
|
|
804
881
|
return rows?.[0]?.value ?? null;
|
|
@@ -806,7 +883,10 @@ var TauriORM = class {
|
|
|
806
883
|
async setSchemaMeta(key, value) {
|
|
807
884
|
await this.ensureSchemaMeta();
|
|
808
885
|
await this.db.execute(
|
|
809
|
-
`INSERT INTO _schema_meta(key, value)
|
|
886
|
+
`INSERT INTO _schema_meta(key, value)
|
|
887
|
+
VALUES (?, ?) ON CONFLICT(key) DO
|
|
888
|
+
UPDATE
|
|
889
|
+
SET value = excluded.value`,
|
|
810
890
|
[key, value]
|
|
811
891
|
);
|
|
812
892
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -36,10 +36,13 @@ var BaseQueryBuilder = class {
|
|
|
36
36
|
};
|
|
37
37
|
|
|
38
38
|
// src/operators.ts
|
|
39
|
-
var eq = (column, value) =>
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
39
|
+
var eq = (column, value, tableAlias) => {
|
|
40
|
+
const columnName = tableAlias ? `${tableAlias}.${column._.name}` : column._.name;
|
|
41
|
+
return {
|
|
42
|
+
sql: `${columnName} = ?`,
|
|
43
|
+
params: [value]
|
|
44
|
+
};
|
|
45
|
+
};
|
|
43
46
|
var and = (...conditions) => ({
|
|
44
47
|
sql: conditions.map((c) => `(${c.sql})`).join(" AND "),
|
|
45
48
|
params: conditions.flatMap((c) => c.params)
|
|
@@ -115,14 +118,17 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
115
118
|
super(db);
|
|
116
119
|
this.table = table;
|
|
117
120
|
this.columns = columns;
|
|
118
|
-
|
|
119
|
-
|
|
121
|
+
this.selectedTableAlias = table._.name;
|
|
122
|
+
const columnNames = columns ? columns.map((c) => `${this.selectedTableAlias}.${table._.columns[c]._.name}`) : [`${this.selectedTableAlias}.*`];
|
|
123
|
+
this.query = `SELECT ${columnNames.join(", ")}
|
|
124
|
+
FROM ${table._.name} ${this.selectedTableAlias}`;
|
|
120
125
|
}
|
|
121
126
|
isDistinct = false;
|
|
122
127
|
groupByColumns = [];
|
|
123
128
|
havingCondition = null;
|
|
124
129
|
joins = [];
|
|
125
130
|
includeRelations = {};
|
|
131
|
+
selectedTableAlias;
|
|
126
132
|
distinct() {
|
|
127
133
|
this.isDistinct = true;
|
|
128
134
|
this.query = this.query.replace("SELECT", "SELECT DISTINCT");
|
|
@@ -130,7 +136,7 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
130
136
|
}
|
|
131
137
|
groupBy(...columns) {
|
|
132
138
|
this.groupByColumns.push(...columns);
|
|
133
|
-
const columnNames = columns.map((col) => col._.name).join(", ");
|
|
139
|
+
const columnNames = columns.map((col) => `${this.selectedTableAlias}.${col._.name}`).join(", ");
|
|
134
140
|
this.query += ` GROUP BY ${columnNames}`;
|
|
135
141
|
return this;
|
|
136
142
|
}
|
|
@@ -156,48 +162,104 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
156
162
|
let sql2 = "";
|
|
157
163
|
const params = [];
|
|
158
164
|
for (const join of this.joins) {
|
|
159
|
-
|
|
160
|
-
sql2 += ` ${join.type} JOIN ${join.table._.name} ${tableAlias} ON ${join.condition.sql}`;
|
|
165
|
+
sql2 += ` ${join.type} JOIN ${join.table._.name} ${join.alias} ON ${join.condition.sql}`;
|
|
161
166
|
params.push(...join.condition.params);
|
|
162
167
|
}
|
|
163
|
-
for (const [relationName, include] of Object.entries(
|
|
164
|
-
this.includeRelations
|
|
165
|
-
)) {
|
|
168
|
+
for (const [relationName, include] of Object.entries(this.includeRelations)) {
|
|
166
169
|
if (!include) continue;
|
|
167
170
|
const relation = this.table.relations[relationName];
|
|
168
171
|
if (!relation) continue;
|
|
169
172
|
const foreignTable = relation.foreignTable;
|
|
170
|
-
const foreignAlias = `${this.
|
|
173
|
+
const foreignAlias = `${this.selectedTableAlias}_${relationName}`;
|
|
171
174
|
if (relation.type === "one" && relation.fields && relation.references) {
|
|
172
|
-
const conditions = relation.fields.map(
|
|
173
|
-
|
|
174
|
-
|
|
175
|
+
const conditions = relation.fields.map((field, i) => {
|
|
176
|
+
const localColumn = `${this.selectedTableAlias}.${field._.name}`;
|
|
177
|
+
const foreignColumn = `${foreignAlias}.${relation.references[i]._.name}`;
|
|
178
|
+
return {
|
|
179
|
+
sql: `${localColumn} = ${foreignColumn}`,
|
|
180
|
+
params: []
|
|
181
|
+
};
|
|
182
|
+
});
|
|
175
183
|
const condition = conditions.length > 1 ? and(...conditions) : conditions[0];
|
|
176
184
|
sql2 += ` LEFT JOIN ${foreignTable._.name} ${foreignAlias} ON ${condition.sql}`;
|
|
177
185
|
params.push(...condition.params);
|
|
178
186
|
} else if (relation.type === "many") {
|
|
179
|
-
const refRelation = Object.
|
|
180
|
-
(r) => r.foreignTable === this.table
|
|
187
|
+
const refRelation = Object.entries(foreignTable.relations).find(
|
|
188
|
+
([_, r]) => r.foreignTable === this.table
|
|
181
189
|
);
|
|
182
|
-
if (refRelation && refRelation.fields && refRelation.references) {
|
|
183
|
-
const
|
|
184
|
-
|
|
185
|
-
|
|
190
|
+
if (refRelation && refRelation[1].fields && refRelation[1].references) {
|
|
191
|
+
const [_, relationConfig] = refRelation;
|
|
192
|
+
const conditions = relationConfig.fields.map((field, i) => {
|
|
193
|
+
const localColumn = `${foreignAlias}.${field._.name}`;
|
|
194
|
+
const foreignColumn = `${this.selectedTableAlias}.${relationConfig.references[i]._.name}`;
|
|
195
|
+
return {
|
|
196
|
+
sql: `${localColumn} = ${foreignColumn}`,
|
|
197
|
+
params: []
|
|
198
|
+
};
|
|
199
|
+
});
|
|
186
200
|
const condition = conditions.length > 1 ? and(...conditions) : conditions[0];
|
|
187
201
|
sql2 += ` LEFT JOIN ${foreignTable._.name} ${foreignAlias} ON ${condition.sql}`;
|
|
188
202
|
params.push(...condition.params);
|
|
203
|
+
this.query = this.query.replace("SELECT ", `SELECT ${this.selectedTableAlias}.*, ${foreignAlias}.* `).replace(
|
|
204
|
+
`SELECT ${this.selectedTableAlias}.*, `,
|
|
205
|
+
`SELECT ${this.selectedTableAlias}.*, ${foreignAlias}.*, `
|
|
206
|
+
);
|
|
189
207
|
}
|
|
190
208
|
}
|
|
191
209
|
}
|
|
192
210
|
return { sql: sql2, params };
|
|
193
211
|
}
|
|
212
|
+
// Enhanced execute method that handles relation data mapping
|
|
194
213
|
async execute() {
|
|
195
214
|
const { sql: joinSql, params: joinParams } = this.buildJoins();
|
|
196
215
|
this.query += joinSql;
|
|
197
216
|
this.params.push(...joinParams);
|
|
198
217
|
const { sql: sql2, params } = this.build();
|
|
199
|
-
|
|
218
|
+
console.log("Executing SQL:", sql2, "with params:", params);
|
|
219
|
+
const rawResults = await this.db.select(sql2, params);
|
|
220
|
+
if (Object.keys(this.includeRelations).some((key) => this.includeRelations[key])) {
|
|
221
|
+
const processed = this.processRelationResults(rawResults);
|
|
222
|
+
return processed;
|
|
223
|
+
}
|
|
224
|
+
return rawResults;
|
|
225
|
+
}
|
|
226
|
+
processRelationResults(rawResults) {
|
|
227
|
+
if (!rawResults.length) return rawResults;
|
|
228
|
+
return rawResults.map((row) => {
|
|
229
|
+
const result = {};
|
|
230
|
+
const relations2 = {};
|
|
231
|
+
for (const [key, value] of Object.entries(row)) {
|
|
232
|
+
if (key.includes(".")) {
|
|
233
|
+
const [tableAlias, columnName] = key.split(".");
|
|
234
|
+
if (tableAlias === this.selectedTableAlias) {
|
|
235
|
+
result[columnName] = value;
|
|
236
|
+
} else {
|
|
237
|
+
const parts = tableAlias.split("_");
|
|
238
|
+
if (parts.length >= 2 && parts[0] === this.selectedTableAlias) {
|
|
239
|
+
const relationName = parts.slice(1).join("_");
|
|
240
|
+
if (!relations2[relationName]) relations2[relationName] = {};
|
|
241
|
+
relations2[relationName][columnName] = value;
|
|
242
|
+
} else {
|
|
243
|
+
if (!result[tableAlias]) result[tableAlias] = {};
|
|
244
|
+
result[tableAlias][columnName] = value;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
} else {
|
|
248
|
+
result[key] = value;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
for (const [relName, relData] of Object.entries(relations2)) {
|
|
252
|
+
const hasData = Object.values(relData).some(
|
|
253
|
+
(v) => v !== null && v !== void 0 && v !== ""
|
|
254
|
+
);
|
|
255
|
+
if (hasData) {
|
|
256
|
+
result[relName] = relData;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return result;
|
|
260
|
+
});
|
|
200
261
|
}
|
|
262
|
+
// Update the return type signatures
|
|
201
263
|
async all() {
|
|
202
264
|
return this.execute();
|
|
203
265
|
}
|
|
@@ -410,7 +472,8 @@ var DeleteQueryBuilder = class extends BaseQueryBuilder {
|
|
|
410
472
|
constructor(db, table) {
|
|
411
473
|
super(db);
|
|
412
474
|
this.table = table;
|
|
413
|
-
this.query = `DELETE
|
|
475
|
+
this.query = `DELETE
|
|
476
|
+
FROM ${table._.name}`;
|
|
414
477
|
}
|
|
415
478
|
returningColumns = [];
|
|
416
479
|
returning(...columns) {
|
|
@@ -620,9 +683,7 @@ var TauriORM = class {
|
|
|
620
683
|
for (const [key, value] of Object.entries(schema)) {
|
|
621
684
|
if (!(value instanceof Table) && typeof value === "object") {
|
|
622
685
|
const tableName = key.replace("Relations", "");
|
|
623
|
-
const table = Array.from(this.tables.values()).find(
|
|
624
|
-
(t) => t._.name === tableName
|
|
625
|
-
);
|
|
686
|
+
const table = Array.from(this.tables.values()).find((t) => t._.name === tableName);
|
|
626
687
|
if (table) {
|
|
627
688
|
for (const [relName, rel] of Object.entries(value)) {
|
|
628
689
|
if (rel instanceof OneRelation) {
|
|
@@ -671,7 +732,10 @@ var TauriORM = class {
|
|
|
671
732
|
);
|
|
672
733
|
if (existingTableInfo.length === 0) {
|
|
673
734
|
const columnsSql = Object.values(table._.columns).map((col) => this.buildColumnDefinition(col)).join(", ");
|
|
674
|
-
const createSql = `CREATE TABLE ${table._.name}
|
|
735
|
+
const createSql = `CREATE TABLE ${table._.name}
|
|
736
|
+
(
|
|
737
|
+
${columnsSql}
|
|
738
|
+
)`;
|
|
675
739
|
await this.db.execute(createSql);
|
|
676
740
|
} else {
|
|
677
741
|
const existingColumnNames = new Set(
|
|
@@ -680,7 +744,8 @@ var TauriORM = class {
|
|
|
680
744
|
for (const column of Object.values(table._.columns)) {
|
|
681
745
|
if (!existingColumnNames.has(column._.name)) {
|
|
682
746
|
const columnSql = this.buildColumnDefinition(column, true);
|
|
683
|
-
const alterSql = `ALTER TABLE ${table._.name}
|
|
747
|
+
const alterSql = `ALTER TABLE ${table._.name}
|
|
748
|
+
ADD COLUMN ${columnSql}`;
|
|
684
749
|
await this.db.execute(alterSql);
|
|
685
750
|
}
|
|
686
751
|
}
|
|
@@ -725,13 +790,25 @@ var TauriORM = class {
|
|
|
725
790
|
// --- Schema detection / signature ---
|
|
726
791
|
async ensureSchemaMeta() {
|
|
727
792
|
await this.db.execute(
|
|
728
|
-
`CREATE TABLE IF NOT EXISTS _schema_meta
|
|
793
|
+
`CREATE TABLE IF NOT EXISTS _schema_meta
|
|
794
|
+
(
|
|
795
|
+
key
|
|
796
|
+
TEXT
|
|
797
|
+
PRIMARY
|
|
798
|
+
KEY,
|
|
799
|
+
value
|
|
800
|
+
TEXT
|
|
801
|
+
NOT
|
|
802
|
+
NULL
|
|
803
|
+
)`
|
|
729
804
|
);
|
|
730
805
|
}
|
|
731
806
|
async getSchemaMeta(key) {
|
|
732
807
|
await this.ensureSchemaMeta();
|
|
733
808
|
const rows = await this.db.select(
|
|
734
|
-
`SELECT value
|
|
809
|
+
`SELECT value
|
|
810
|
+
FROM _schema_meta
|
|
811
|
+
WHERE key = ?`,
|
|
735
812
|
[key]
|
|
736
813
|
);
|
|
737
814
|
return rows?.[0]?.value ?? null;
|
|
@@ -739,7 +816,10 @@ var TauriORM = class {
|
|
|
739
816
|
async setSchemaMeta(key, value) {
|
|
740
817
|
await this.ensureSchemaMeta();
|
|
741
818
|
await this.db.execute(
|
|
742
|
-
`INSERT INTO _schema_meta(key, value)
|
|
819
|
+
`INSERT INTO _schema_meta(key, value)
|
|
820
|
+
VALUES (?, ?) ON CONFLICT(key) DO
|
|
821
|
+
UPDATE
|
|
822
|
+
SET value = excluded.value`,
|
|
743
823
|
[key, value]
|
|
744
824
|
);
|
|
745
825
|
}
|