@type32/tauri-sqlite-orm 0.1.18-17 → 0.1.18-18
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 +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +47 -35
- package/dist/index.mjs +47 -35
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -246,7 +246,7 @@ declare class OneRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
|
246
246
|
declare class ManyRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
247
247
|
constructor(foreignTable: T);
|
|
248
248
|
}
|
|
249
|
-
declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(
|
|
249
|
+
declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
|
|
250
250
|
declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
|
|
251
251
|
declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
|
|
252
252
|
|
package/dist/index.d.ts
CHANGED
|
@@ -246,7 +246,7 @@ declare class OneRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
|
246
246
|
declare class ManyRelation<T extends AnyTable = AnyTable> extends Relation<T> {
|
|
247
247
|
constructor(foreignTable: T);
|
|
248
248
|
}
|
|
249
|
-
declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(
|
|
249
|
+
declare const relations: <T extends AnyTable, R extends Record<string, Relation>>(table: T, relationsCallback: (helpers: RelationsBuilder) => R) => R;
|
|
250
250
|
declare const getTableColumns: <T extends AnyTable>(table: T) => Record<string, AnySQLiteColumn>;
|
|
251
251
|
declare const alias: <T extends AnyTable>(table: T, alias: string) => Table<T["_"]["columns"], T["_"]["name"]>;
|
|
252
252
|
|
package/dist/index.js
CHANGED
|
@@ -296,9 +296,18 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
296
296
|
return rawResults;
|
|
297
297
|
}
|
|
298
298
|
processRelationResults(rawResults) {
|
|
299
|
-
if (!rawResults.length) return
|
|
300
|
-
|
|
301
|
-
|
|
299
|
+
if (!rawResults.length) return [];
|
|
300
|
+
const mainTablePks = Object.values(this.table._.columns).filter((c) => c.options.primaryKey).map((c) => c._.name);
|
|
301
|
+
if (mainTablePks.length === 0) {
|
|
302
|
+
return rawResults;
|
|
303
|
+
}
|
|
304
|
+
const groupedResults = /* @__PURE__ */ new Map();
|
|
305
|
+
for (const row of rawResults) {
|
|
306
|
+
const mainTableKey = mainTablePks.map((pk) => row[`${this.selectedTableAlias}.${pk}`] ?? row[pk]).join("_");
|
|
307
|
+
if (!groupedResults.has(mainTableKey)) {
|
|
308
|
+
groupedResults.set(mainTableKey, {});
|
|
309
|
+
}
|
|
310
|
+
const result = groupedResults.get(mainTableKey);
|
|
302
311
|
const relations2 = {};
|
|
303
312
|
for (const [key, value] of Object.entries(row)) {
|
|
304
313
|
if (key.includes(".")) {
|
|
@@ -321,15 +330,25 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
321
330
|
}
|
|
322
331
|
}
|
|
323
332
|
for (const [relName, relData] of Object.entries(relations2)) {
|
|
333
|
+
const relationConfig = this.table.relations[relName];
|
|
334
|
+
if (!relationConfig) continue;
|
|
324
335
|
const hasData = Object.values(relData).some(
|
|
325
336
|
(v) => v !== null && v !== void 0 && v !== ""
|
|
326
337
|
);
|
|
327
|
-
if (hasData)
|
|
338
|
+
if (!hasData) continue;
|
|
339
|
+
if (relationConfig.type === "many") {
|
|
340
|
+
if (!result[relName]) result[relName] = [];
|
|
341
|
+
const relatedPks = Object.values(relationConfig.foreignTable._.columns).filter((c) => c.options.primaryKey).map((c) => c._.name);
|
|
342
|
+
const relDataKey = relatedPks.map((pk) => relData[pk]).join("_");
|
|
343
|
+
if (relatedPks.length === 0 || !result[relName].some((r) => relatedPks.map((pk) => r[pk]).join("_") === relDataKey)) {
|
|
344
|
+
result[relName].push(relData);
|
|
345
|
+
}
|
|
346
|
+
} else {
|
|
328
347
|
result[relName] = relData;
|
|
329
348
|
}
|
|
330
349
|
}
|
|
331
|
-
|
|
332
|
-
|
|
350
|
+
}
|
|
351
|
+
return Array.from(groupedResults.values());
|
|
333
352
|
}
|
|
334
353
|
// Update the return type signatures
|
|
335
354
|
async all() {
|
|
@@ -719,29 +738,6 @@ var TauriORM = class {
|
|
|
719
738
|
this.tables.set(value._.name, value);
|
|
720
739
|
}
|
|
721
740
|
}
|
|
722
|
-
for (const [key, value] of Object.entries(schema)) {
|
|
723
|
-
if (!(value instanceof Table) && typeof value === "object") {
|
|
724
|
-
const tableName = key.replace("Relations", "");
|
|
725
|
-
const table = Array.from(this.tables.values()).find((t) => t._.name === tableName);
|
|
726
|
-
if (table) {
|
|
727
|
-
for (const [relName, rel] of Object.entries(value)) {
|
|
728
|
-
if (rel instanceof OneRelation) {
|
|
729
|
-
table.relations[relName] = {
|
|
730
|
-
type: "one",
|
|
731
|
-
foreignTable: rel.foreignTable,
|
|
732
|
-
fields: rel.config?.fields,
|
|
733
|
-
references: rel.config?.references
|
|
734
|
-
};
|
|
735
|
-
} else if (rel instanceof ManyRelation) {
|
|
736
|
-
table.relations[relName] = {
|
|
737
|
-
type: "many",
|
|
738
|
-
foreignTable: rel.foreignTable
|
|
739
|
-
};
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
}
|
|
744
|
-
}
|
|
745
741
|
}
|
|
746
742
|
}
|
|
747
743
|
tables = /* @__PURE__ */ new Map();
|
|
@@ -920,15 +916,31 @@ var ManyRelation = class extends Relation {
|
|
|
920
916
|
super(foreignTable);
|
|
921
917
|
}
|
|
922
918
|
};
|
|
923
|
-
var relations = (
|
|
924
|
-
|
|
925
|
-
one: (
|
|
926
|
-
return new OneRelation(
|
|
919
|
+
var relations = (table, relationsCallback) => {
|
|
920
|
+
const builtRelations = relationsCallback({
|
|
921
|
+
one: (foreignTable, config) => {
|
|
922
|
+
return new OneRelation(foreignTable, config);
|
|
927
923
|
},
|
|
928
|
-
many: (
|
|
929
|
-
return new ManyRelation(
|
|
924
|
+
many: (foreignTable) => {
|
|
925
|
+
return new ManyRelation(foreignTable);
|
|
930
926
|
}
|
|
931
927
|
});
|
|
928
|
+
for (const [name, relation] of Object.entries(builtRelations)) {
|
|
929
|
+
if (relation instanceof OneRelation) {
|
|
930
|
+
table.relations[name] = {
|
|
931
|
+
type: "one",
|
|
932
|
+
foreignTable: relation.foreignTable,
|
|
933
|
+
fields: relation.config?.fields,
|
|
934
|
+
references: relation.config?.references
|
|
935
|
+
};
|
|
936
|
+
} else if (relation instanceof ManyRelation) {
|
|
937
|
+
table.relations[name] = {
|
|
938
|
+
type: "many",
|
|
939
|
+
foreignTable: relation.foreignTable
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
return builtRelations;
|
|
932
944
|
};
|
|
933
945
|
var getTableColumns = (table) => {
|
|
934
946
|
return table._.columns;
|
package/dist/index.mjs
CHANGED
|
@@ -229,9 +229,18 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
229
229
|
return rawResults;
|
|
230
230
|
}
|
|
231
231
|
processRelationResults(rawResults) {
|
|
232
|
-
if (!rawResults.length) return
|
|
233
|
-
|
|
234
|
-
|
|
232
|
+
if (!rawResults.length) return [];
|
|
233
|
+
const mainTablePks = Object.values(this.table._.columns).filter((c) => c.options.primaryKey).map((c) => c._.name);
|
|
234
|
+
if (mainTablePks.length === 0) {
|
|
235
|
+
return rawResults;
|
|
236
|
+
}
|
|
237
|
+
const groupedResults = /* @__PURE__ */ new Map();
|
|
238
|
+
for (const row of rawResults) {
|
|
239
|
+
const mainTableKey = mainTablePks.map((pk) => row[`${this.selectedTableAlias}.${pk}`] ?? row[pk]).join("_");
|
|
240
|
+
if (!groupedResults.has(mainTableKey)) {
|
|
241
|
+
groupedResults.set(mainTableKey, {});
|
|
242
|
+
}
|
|
243
|
+
const result = groupedResults.get(mainTableKey);
|
|
235
244
|
const relations2 = {};
|
|
236
245
|
for (const [key, value] of Object.entries(row)) {
|
|
237
246
|
if (key.includes(".")) {
|
|
@@ -254,15 +263,25 @@ var SelectQueryBuilder = class extends BaseQueryBuilder {
|
|
|
254
263
|
}
|
|
255
264
|
}
|
|
256
265
|
for (const [relName, relData] of Object.entries(relations2)) {
|
|
266
|
+
const relationConfig = this.table.relations[relName];
|
|
267
|
+
if (!relationConfig) continue;
|
|
257
268
|
const hasData = Object.values(relData).some(
|
|
258
269
|
(v) => v !== null && v !== void 0 && v !== ""
|
|
259
270
|
);
|
|
260
|
-
if (hasData)
|
|
271
|
+
if (!hasData) continue;
|
|
272
|
+
if (relationConfig.type === "many") {
|
|
273
|
+
if (!result[relName]) result[relName] = [];
|
|
274
|
+
const relatedPks = Object.values(relationConfig.foreignTable._.columns).filter((c) => c.options.primaryKey).map((c) => c._.name);
|
|
275
|
+
const relDataKey = relatedPks.map((pk) => relData[pk]).join("_");
|
|
276
|
+
if (relatedPks.length === 0 || !result[relName].some((r) => relatedPks.map((pk) => r[pk]).join("_") === relDataKey)) {
|
|
277
|
+
result[relName].push(relData);
|
|
278
|
+
}
|
|
279
|
+
} else {
|
|
261
280
|
result[relName] = relData;
|
|
262
281
|
}
|
|
263
282
|
}
|
|
264
|
-
|
|
265
|
-
|
|
283
|
+
}
|
|
284
|
+
return Array.from(groupedResults.values());
|
|
266
285
|
}
|
|
267
286
|
// Update the return type signatures
|
|
268
287
|
async all() {
|
|
@@ -652,29 +671,6 @@ var TauriORM = class {
|
|
|
652
671
|
this.tables.set(value._.name, value);
|
|
653
672
|
}
|
|
654
673
|
}
|
|
655
|
-
for (const [key, value] of Object.entries(schema)) {
|
|
656
|
-
if (!(value instanceof Table) && typeof value === "object") {
|
|
657
|
-
const tableName = key.replace("Relations", "");
|
|
658
|
-
const table = Array.from(this.tables.values()).find((t) => t._.name === tableName);
|
|
659
|
-
if (table) {
|
|
660
|
-
for (const [relName, rel] of Object.entries(value)) {
|
|
661
|
-
if (rel instanceof OneRelation) {
|
|
662
|
-
table.relations[relName] = {
|
|
663
|
-
type: "one",
|
|
664
|
-
foreignTable: rel.foreignTable,
|
|
665
|
-
fields: rel.config?.fields,
|
|
666
|
-
references: rel.config?.references
|
|
667
|
-
};
|
|
668
|
-
} else if (rel instanceof ManyRelation) {
|
|
669
|
-
table.relations[relName] = {
|
|
670
|
-
type: "many",
|
|
671
|
-
foreignTable: rel.foreignTable
|
|
672
|
-
};
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
}
|
|
676
|
-
}
|
|
677
|
-
}
|
|
678
674
|
}
|
|
679
675
|
}
|
|
680
676
|
tables = /* @__PURE__ */ new Map();
|
|
@@ -853,15 +849,31 @@ var ManyRelation = class extends Relation {
|
|
|
853
849
|
super(foreignTable);
|
|
854
850
|
}
|
|
855
851
|
};
|
|
856
|
-
var relations = (
|
|
857
|
-
|
|
858
|
-
one: (
|
|
859
|
-
return new OneRelation(
|
|
852
|
+
var relations = (table, relationsCallback) => {
|
|
853
|
+
const builtRelations = relationsCallback({
|
|
854
|
+
one: (foreignTable, config) => {
|
|
855
|
+
return new OneRelation(foreignTable, config);
|
|
860
856
|
},
|
|
861
|
-
many: (
|
|
862
|
-
return new ManyRelation(
|
|
857
|
+
many: (foreignTable) => {
|
|
858
|
+
return new ManyRelation(foreignTable);
|
|
863
859
|
}
|
|
864
860
|
});
|
|
861
|
+
for (const [name, relation] of Object.entries(builtRelations)) {
|
|
862
|
+
if (relation instanceof OneRelation) {
|
|
863
|
+
table.relations[name] = {
|
|
864
|
+
type: "one",
|
|
865
|
+
foreignTable: relation.foreignTable,
|
|
866
|
+
fields: relation.config?.fields,
|
|
867
|
+
references: relation.config?.references
|
|
868
|
+
};
|
|
869
|
+
} else if (relation instanceof ManyRelation) {
|
|
870
|
+
table.relations[name] = {
|
|
871
|
+
type: "many",
|
|
872
|
+
foreignTable: relation.foreignTable
|
|
873
|
+
};
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
return builtRelations;
|
|
865
877
|
};
|
|
866
878
|
var getTableColumns = (table) => {
|
|
867
879
|
return table._.columns;
|