@getstrata/core 0.5.10 → 0.5.13

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.
@@ -664,6 +664,66 @@ function indexBelongsToManyRelation(parents, pivotRows, relatedRows, relation) {
664
664
  }
665
665
  return groups;
666
666
  }
667
+ function morphMany(definition) {
668
+ return {
669
+ type: "morphMany",
670
+ ...definition
671
+ };
672
+ }
673
+ function morphOne(definition) {
674
+ return {
675
+ type: "morphOne",
676
+ ...definition
677
+ };
678
+ }
679
+ function morphTo(definition) {
680
+ return {
681
+ type: "morphTo",
682
+ ...definition
683
+ };
684
+ }
685
+ function indexMorphManyRelation(parents, children, relation) {
686
+ const groups = new Map;
687
+ for (const parent of parents) {
688
+ groups.set(parent[relation.localKey], []);
689
+ }
690
+ for (const child of children) {
691
+ if (child[relation.morphTypeKey] !== relation.morphType) {
692
+ continue;
693
+ }
694
+ const key = child[relation.morphIdKey];
695
+ const group = groups.get(key);
696
+ if (!group) {
697
+ continue;
698
+ }
699
+ group.push(child);
700
+ }
701
+ return groups;
702
+ }
703
+ function indexMorphOneRelation(parents, children, relation) {
704
+ const grouped = indexMorphManyRelation(parents, children, relation);
705
+ const result = new Map;
706
+ for (const parent of parents) {
707
+ const matches = grouped.get(parent[relation.localKey]) ?? [];
708
+ result.set(parent[relation.localKey], matches[0]);
709
+ }
710
+ return result;
711
+ }
712
+ function indexMorphToRelation(children, parentsByType, relation) {
713
+ const result = new Map;
714
+ for (const child of children) {
715
+ const morphType = String(child[relation.morphTypeKey]);
716
+ const parents = parentsByType.get(morphType);
717
+ if (!parents) {
718
+ continue;
719
+ }
720
+ const parent = parents.get(child[relation.morphIdKey]);
721
+ if (parent) {
722
+ result.set(child[relation.morphIdKey], parent);
723
+ }
724
+ }
725
+ return result;
726
+ }
667
727
 
668
728
  // ../../src/config/database.ts
669
729
  function readInteger(name, fallback) {
@@ -867,6 +927,37 @@ class RepositoryQuery {
867
927
  });
868
928
  return this;
869
929
  }
930
+ withMorphMany(as, relation, childRepository, options = {}) {
931
+ this.eagerLoads.push({
932
+ kind: "morphMany",
933
+ as,
934
+ relation,
935
+ repository: childRepository,
936
+ options
937
+ });
938
+ return this;
939
+ }
940
+ withMorphOne(as, relation, childRepository, options = {}) {
941
+ this.eagerLoads.push({
942
+ kind: "morphOne",
943
+ as,
944
+ relation,
945
+ repository: childRepository,
946
+ options
947
+ });
948
+ return this;
949
+ }
950
+ withMorphTo(as, relation, repositoriesByType, options = {}) {
951
+ this.eagerLoads.push({
952
+ kind: "morphTo",
953
+ as,
954
+ relation,
955
+ repository: this.repository,
956
+ morphRepositories: repositoriesByType,
957
+ options
958
+ });
959
+ return this;
960
+ }
870
961
  async get() {
871
962
  const rows = await this.repository.findAll(this.buildOptions());
872
963
  return await this.attach(rows);
@@ -927,6 +1018,33 @@ class RepositoryQuery {
927
1018
  }));
928
1019
  continue;
929
1020
  }
1021
+ if (load.kind === "morphMany") {
1022
+ const relation2 = load.relation;
1023
+ const grouped2 = await load.repository.withConnection(this.repository.getConnection()).loadMorphManyForParents(rows, relation2, load.options);
1024
+ result = result.map((row) => ({
1025
+ ...row,
1026
+ [load.as]: grouped2.get(row[relation2.localKey]) ?? []
1027
+ }));
1028
+ continue;
1029
+ }
1030
+ if (load.kind === "morphOne") {
1031
+ const relation2 = load.relation;
1032
+ const grouped2 = await load.repository.withConnection(this.repository.getConnection()).loadMorphOneForParents(rows, relation2, load.options);
1033
+ result = result.map((row) => ({
1034
+ ...row,
1035
+ [load.as]: grouped2.get(row[relation2.localKey])
1036
+ }));
1037
+ continue;
1038
+ }
1039
+ if (load.kind === "morphTo") {
1040
+ const relation2 = load.relation;
1041
+ const grouped2 = await this.repository.loadMorphToForChildren(rows, relation2, load.morphRepositories ?? new Map, load.options);
1042
+ result = result.map((row) => ({
1043
+ ...row,
1044
+ [load.as]: grouped2.get(row[relation2.morphIdKey])
1045
+ }));
1046
+ continue;
1047
+ }
930
1048
  const relation = load.relation;
931
1049
  const grouped = await this.repository.loadBelongsToForParents(rows, relation, load.repository, load.options);
932
1050
  result = result.map((row) => ({
@@ -1203,6 +1321,56 @@ class BaseRepository {
1203
1321
  }, options);
1204
1322
  return indexBelongsToRelation(children, parents, relation);
1205
1323
  }
1324
+ async loadMorphManyForParents(parents, relation, options = {}) {
1325
+ if (parents.length === 0) {
1326
+ return indexMorphManyRelation(parents, [], relation);
1327
+ }
1328
+ const parentIds = [...new Set(parents.map((parent) => parent[relation.localKey]))];
1329
+ const children = await this.findWhere({
1330
+ [relation.morphTypeKey]: relation.morphType,
1331
+ [relation.morphIdKey]: parentIds
1332
+ }, options);
1333
+ return indexMorphManyRelation(parents, children, relation);
1334
+ }
1335
+ async loadMorphOneForParents(parents, relation, options = {}) {
1336
+ const grouped = await this.loadMorphManyForParents(parents, relation, options);
1337
+ const result = new Map;
1338
+ for (const parent of parents) {
1339
+ const matches = grouped.get(parent[relation.localKey]) ?? [];
1340
+ result.set(parent[relation.localKey], matches[0]);
1341
+ }
1342
+ return result;
1343
+ }
1344
+ async loadMorphToForChildren(children, relation, repositoriesByType, options = {}) {
1345
+ if (children.length === 0) {
1346
+ return new Map;
1347
+ }
1348
+ const idsByType = new Map;
1349
+ for (const child of children) {
1350
+ const morphType = String(child[relation.morphTypeKey]);
1351
+ const morphId = child[relation.morphIdKey];
1352
+ const ids = idsByType.get(morphType) ?? new Set;
1353
+ ids.add(morphId);
1354
+ idsByType.set(morphType, ids);
1355
+ }
1356
+ const parentsByType = new Map;
1357
+ for (const [morphType, ids] of idsByType) {
1358
+ const repository = repositoriesByType.get(morphType);
1359
+ if (!repository) {
1360
+ continue;
1361
+ }
1362
+ const ownerKey = repository.getTable().primaryKey;
1363
+ const parents = await repository.withConnection(this.connection).findWhere({
1364
+ [ownerKey]: [...ids]
1365
+ }, options);
1366
+ const indexed = new Map;
1367
+ for (const parent of parents) {
1368
+ indexed.set(parent[ownerKey], parent);
1369
+ }
1370
+ parentsByType.set(morphType, indexed);
1371
+ }
1372
+ return indexMorphToRelation(children, parentsByType, relation);
1373
+ }
1206
1374
  }
1207
1375
  var baseRepository_default = BaseRepository;
1208
1376
  // ../../src/core/database/connection.ts
@@ -2176,8 +2344,14 @@ export {
2176
2344
  quoteIdentifier,
2177
2345
  qualifyColumn,
2178
2346
  parseQualifiedColumn,
2347
+ morphTo,
2348
+ morphOne,
2349
+ morphMany,
2179
2350
  mapDatabaseError,
2180
2351
  inferReferencedTable,
2352
+ indexMorphToRelation,
2353
+ indexMorphOneRelation,
2354
+ indexMorphManyRelation,
2181
2355
  indexHasOneRelation,
2182
2356
  indexHasManyRelation,
2183
2357
  indexBelongsToRelation,
@@ -888,6 +888,66 @@ function indexBelongsToManyRelation(parents, pivotRows, relatedRows, relation) {
888
888
  }
889
889
  return groups;
890
890
  }
891
+ function morphMany(definition) {
892
+ return {
893
+ type: "morphMany",
894
+ ...definition
895
+ };
896
+ }
897
+ function morphOne(definition) {
898
+ return {
899
+ type: "morphOne",
900
+ ...definition
901
+ };
902
+ }
903
+ function morphTo(definition) {
904
+ return {
905
+ type: "morphTo",
906
+ ...definition
907
+ };
908
+ }
909
+ function indexMorphManyRelation(parents, children, relation) {
910
+ const groups = new Map;
911
+ for (const parent of parents) {
912
+ groups.set(parent[relation.localKey], []);
913
+ }
914
+ for (const child of children) {
915
+ if (child[relation.morphTypeKey] !== relation.morphType) {
916
+ continue;
917
+ }
918
+ const key = child[relation.morphIdKey];
919
+ const group = groups.get(key);
920
+ if (!group) {
921
+ continue;
922
+ }
923
+ group.push(child);
924
+ }
925
+ return groups;
926
+ }
927
+ function indexMorphOneRelation(parents, children, relation) {
928
+ const grouped = indexMorphManyRelation(parents, children, relation);
929
+ const result = new Map;
930
+ for (const parent of parents) {
931
+ const matches = grouped.get(parent[relation.localKey]) ?? [];
932
+ result.set(parent[relation.localKey], matches[0]);
933
+ }
934
+ return result;
935
+ }
936
+ function indexMorphToRelation(children, parentsByType, relation) {
937
+ const result = new Map;
938
+ for (const child of children) {
939
+ const morphType = String(child[relation.morphTypeKey]);
940
+ const parents = parentsByType.get(morphType);
941
+ if (!parents) {
942
+ continue;
943
+ }
944
+ const parent = parents.get(child[relation.morphIdKey]);
945
+ if (parent) {
946
+ result.set(child[relation.morphIdKey], parent);
947
+ }
948
+ }
949
+ return result;
950
+ }
891
951
 
892
952
  // ../../src/config/database.ts
893
953
  function readInteger(name, fallback) {
@@ -1091,6 +1151,37 @@ class RepositoryQuery {
1091
1151
  });
1092
1152
  return this;
1093
1153
  }
1154
+ withMorphMany(as, relation, childRepository, options = {}) {
1155
+ this.eagerLoads.push({
1156
+ kind: "morphMany",
1157
+ as,
1158
+ relation,
1159
+ repository: childRepository,
1160
+ options
1161
+ });
1162
+ return this;
1163
+ }
1164
+ withMorphOne(as, relation, childRepository, options = {}) {
1165
+ this.eagerLoads.push({
1166
+ kind: "morphOne",
1167
+ as,
1168
+ relation,
1169
+ repository: childRepository,
1170
+ options
1171
+ });
1172
+ return this;
1173
+ }
1174
+ withMorphTo(as, relation, repositoriesByType, options = {}) {
1175
+ this.eagerLoads.push({
1176
+ kind: "morphTo",
1177
+ as,
1178
+ relation,
1179
+ repository: this.repository,
1180
+ morphRepositories: repositoriesByType,
1181
+ options
1182
+ });
1183
+ return this;
1184
+ }
1094
1185
  async get() {
1095
1186
  const rows = await this.repository.findAll(this.buildOptions());
1096
1187
  return await this.attach(rows);
@@ -1151,6 +1242,33 @@ class RepositoryQuery {
1151
1242
  }));
1152
1243
  continue;
1153
1244
  }
1245
+ if (load.kind === "morphMany") {
1246
+ const relation2 = load.relation;
1247
+ const grouped2 = await load.repository.withConnection(this.repository.getConnection()).loadMorphManyForParents(rows, relation2, load.options);
1248
+ result = result.map((row) => ({
1249
+ ...row,
1250
+ [load.as]: grouped2.get(row[relation2.localKey]) ?? []
1251
+ }));
1252
+ continue;
1253
+ }
1254
+ if (load.kind === "morphOne") {
1255
+ const relation2 = load.relation;
1256
+ const grouped2 = await load.repository.withConnection(this.repository.getConnection()).loadMorphOneForParents(rows, relation2, load.options);
1257
+ result = result.map((row) => ({
1258
+ ...row,
1259
+ [load.as]: grouped2.get(row[relation2.localKey])
1260
+ }));
1261
+ continue;
1262
+ }
1263
+ if (load.kind === "morphTo") {
1264
+ const relation2 = load.relation;
1265
+ const grouped2 = await this.repository.loadMorphToForChildren(rows, relation2, load.morphRepositories ?? new Map, load.options);
1266
+ result = result.map((row) => ({
1267
+ ...row,
1268
+ [load.as]: grouped2.get(row[relation2.morphIdKey])
1269
+ }));
1270
+ continue;
1271
+ }
1154
1272
  const relation = load.relation;
1155
1273
  const grouped = await this.repository.loadBelongsToForParents(rows, relation, load.repository, load.options);
1156
1274
  result = result.map((row) => ({
@@ -1427,6 +1545,56 @@ class BaseRepository {
1427
1545
  }, options);
1428
1546
  return indexBelongsToRelation(children, parents, relation);
1429
1547
  }
1548
+ async loadMorphManyForParents(parents, relation, options = {}) {
1549
+ if (parents.length === 0) {
1550
+ return indexMorphManyRelation(parents, [], relation);
1551
+ }
1552
+ const parentIds = [...new Set(parents.map((parent) => parent[relation.localKey]))];
1553
+ const children = await this.findWhere({
1554
+ [relation.morphTypeKey]: relation.morphType,
1555
+ [relation.morphIdKey]: parentIds
1556
+ }, options);
1557
+ return indexMorphManyRelation(parents, children, relation);
1558
+ }
1559
+ async loadMorphOneForParents(parents, relation, options = {}) {
1560
+ const grouped = await this.loadMorphManyForParents(parents, relation, options);
1561
+ const result = new Map;
1562
+ for (const parent of parents) {
1563
+ const matches = grouped.get(parent[relation.localKey]) ?? [];
1564
+ result.set(parent[relation.localKey], matches[0]);
1565
+ }
1566
+ return result;
1567
+ }
1568
+ async loadMorphToForChildren(children, relation, repositoriesByType, options = {}) {
1569
+ if (children.length === 0) {
1570
+ return new Map;
1571
+ }
1572
+ const idsByType = new Map;
1573
+ for (const child of children) {
1574
+ const morphType = String(child[relation.morphTypeKey]);
1575
+ const morphId = child[relation.morphIdKey];
1576
+ const ids = idsByType.get(morphType) ?? new Set;
1577
+ ids.add(morphId);
1578
+ idsByType.set(morphType, ids);
1579
+ }
1580
+ const parentsByType = new Map;
1581
+ for (const [morphType, ids] of idsByType) {
1582
+ const repository = repositoriesByType.get(morphType);
1583
+ if (!repository) {
1584
+ continue;
1585
+ }
1586
+ const ownerKey = repository.getTable().primaryKey;
1587
+ const parents = await repository.withConnection(this.connection).findWhere({
1588
+ [ownerKey]: [...ids]
1589
+ }, options);
1590
+ const indexed = new Map;
1591
+ for (const parent of parents) {
1592
+ indexed.set(parent[ownerKey], parent);
1593
+ }
1594
+ parentsByType.set(morphType, indexed);
1595
+ }
1596
+ return indexMorphToRelation(children, parentsByType, relation);
1597
+ }
1430
1598
  }
1431
1599
  var baseRepository_default = BaseRepository;
1432
1600
  // ../../src/core/database/connection.ts
@@ -888,6 +888,66 @@ function indexBelongsToManyRelation(parents, pivotRows, relatedRows, relation) {
888
888
  }
889
889
  return groups;
890
890
  }
891
+ function morphMany(definition) {
892
+ return {
893
+ type: "morphMany",
894
+ ...definition
895
+ };
896
+ }
897
+ function morphOne(definition) {
898
+ return {
899
+ type: "morphOne",
900
+ ...definition
901
+ };
902
+ }
903
+ function morphTo(definition) {
904
+ return {
905
+ type: "morphTo",
906
+ ...definition
907
+ };
908
+ }
909
+ function indexMorphManyRelation(parents, children, relation) {
910
+ const groups = new Map;
911
+ for (const parent of parents) {
912
+ groups.set(parent[relation.localKey], []);
913
+ }
914
+ for (const child of children) {
915
+ if (child[relation.morphTypeKey] !== relation.morphType) {
916
+ continue;
917
+ }
918
+ const key = child[relation.morphIdKey];
919
+ const group = groups.get(key);
920
+ if (!group) {
921
+ continue;
922
+ }
923
+ group.push(child);
924
+ }
925
+ return groups;
926
+ }
927
+ function indexMorphOneRelation(parents, children, relation) {
928
+ const grouped = indexMorphManyRelation(parents, children, relation);
929
+ const result = new Map;
930
+ for (const parent of parents) {
931
+ const matches = grouped.get(parent[relation.localKey]) ?? [];
932
+ result.set(parent[relation.localKey], matches[0]);
933
+ }
934
+ return result;
935
+ }
936
+ function indexMorphToRelation(children, parentsByType, relation) {
937
+ const result = new Map;
938
+ for (const child of children) {
939
+ const morphType = String(child[relation.morphTypeKey]);
940
+ const parents = parentsByType.get(morphType);
941
+ if (!parents) {
942
+ continue;
943
+ }
944
+ const parent = parents.get(child[relation.morphIdKey]);
945
+ if (parent) {
946
+ result.set(child[relation.morphIdKey], parent);
947
+ }
948
+ }
949
+ return result;
950
+ }
891
951
 
892
952
  // ../../src/config/database.ts
893
953
  function readInteger(name, fallback) {
@@ -1091,6 +1151,37 @@ class RepositoryQuery {
1091
1151
  });
1092
1152
  return this;
1093
1153
  }
1154
+ withMorphMany(as, relation, childRepository, options = {}) {
1155
+ this.eagerLoads.push({
1156
+ kind: "morphMany",
1157
+ as,
1158
+ relation,
1159
+ repository: childRepository,
1160
+ options
1161
+ });
1162
+ return this;
1163
+ }
1164
+ withMorphOne(as, relation, childRepository, options = {}) {
1165
+ this.eagerLoads.push({
1166
+ kind: "morphOne",
1167
+ as,
1168
+ relation,
1169
+ repository: childRepository,
1170
+ options
1171
+ });
1172
+ return this;
1173
+ }
1174
+ withMorphTo(as, relation, repositoriesByType, options = {}) {
1175
+ this.eagerLoads.push({
1176
+ kind: "morphTo",
1177
+ as,
1178
+ relation,
1179
+ repository: this.repository,
1180
+ morphRepositories: repositoriesByType,
1181
+ options
1182
+ });
1183
+ return this;
1184
+ }
1094
1185
  async get() {
1095
1186
  const rows = await this.repository.findAll(this.buildOptions());
1096
1187
  return await this.attach(rows);
@@ -1151,6 +1242,33 @@ class RepositoryQuery {
1151
1242
  }));
1152
1243
  continue;
1153
1244
  }
1245
+ if (load.kind === "morphMany") {
1246
+ const relation2 = load.relation;
1247
+ const grouped2 = await load.repository.withConnection(this.repository.getConnection()).loadMorphManyForParents(rows, relation2, load.options);
1248
+ result = result.map((row) => ({
1249
+ ...row,
1250
+ [load.as]: grouped2.get(row[relation2.localKey]) ?? []
1251
+ }));
1252
+ continue;
1253
+ }
1254
+ if (load.kind === "morphOne") {
1255
+ const relation2 = load.relation;
1256
+ const grouped2 = await load.repository.withConnection(this.repository.getConnection()).loadMorphOneForParents(rows, relation2, load.options);
1257
+ result = result.map((row) => ({
1258
+ ...row,
1259
+ [load.as]: grouped2.get(row[relation2.localKey])
1260
+ }));
1261
+ continue;
1262
+ }
1263
+ if (load.kind === "morphTo") {
1264
+ const relation2 = load.relation;
1265
+ const grouped2 = await this.repository.loadMorphToForChildren(rows, relation2, load.morphRepositories ?? new Map, load.options);
1266
+ result = result.map((row) => ({
1267
+ ...row,
1268
+ [load.as]: grouped2.get(row[relation2.morphIdKey])
1269
+ }));
1270
+ continue;
1271
+ }
1154
1272
  const relation = load.relation;
1155
1273
  const grouped = await this.repository.loadBelongsToForParents(rows, relation, load.repository, load.options);
1156
1274
  result = result.map((row) => ({
@@ -1427,6 +1545,56 @@ class BaseRepository {
1427
1545
  }, options);
1428
1546
  return indexBelongsToRelation(children, parents, relation);
1429
1547
  }
1548
+ async loadMorphManyForParents(parents, relation, options = {}) {
1549
+ if (parents.length === 0) {
1550
+ return indexMorphManyRelation(parents, [], relation);
1551
+ }
1552
+ const parentIds = [...new Set(parents.map((parent) => parent[relation.localKey]))];
1553
+ const children = await this.findWhere({
1554
+ [relation.morphTypeKey]: relation.morphType,
1555
+ [relation.morphIdKey]: parentIds
1556
+ }, options);
1557
+ return indexMorphManyRelation(parents, children, relation);
1558
+ }
1559
+ async loadMorphOneForParents(parents, relation, options = {}) {
1560
+ const grouped = await this.loadMorphManyForParents(parents, relation, options);
1561
+ const result = new Map;
1562
+ for (const parent of parents) {
1563
+ const matches = grouped.get(parent[relation.localKey]) ?? [];
1564
+ result.set(parent[relation.localKey], matches[0]);
1565
+ }
1566
+ return result;
1567
+ }
1568
+ async loadMorphToForChildren(children, relation, repositoriesByType, options = {}) {
1569
+ if (children.length === 0) {
1570
+ return new Map;
1571
+ }
1572
+ const idsByType = new Map;
1573
+ for (const child of children) {
1574
+ const morphType = String(child[relation.morphTypeKey]);
1575
+ const morphId = child[relation.morphIdKey];
1576
+ const ids = idsByType.get(morphType) ?? new Set;
1577
+ ids.add(morphId);
1578
+ idsByType.set(morphType, ids);
1579
+ }
1580
+ const parentsByType = new Map;
1581
+ for (const [morphType, ids] of idsByType) {
1582
+ const repository = repositoriesByType.get(morphType);
1583
+ if (!repository) {
1584
+ continue;
1585
+ }
1586
+ const ownerKey = repository.getTable().primaryKey;
1587
+ const parents = await repository.withConnection(this.connection).findWhere({
1588
+ [ownerKey]: [...ids]
1589
+ }, options);
1590
+ const indexed = new Map;
1591
+ for (const parent of parents) {
1592
+ indexed.set(parent[ownerKey], parent);
1593
+ }
1594
+ parentsByType.set(morphType, indexed);
1595
+ }
1596
+ return indexMorphToRelation(children, parentsByType, relation);
1597
+ }
1430
1598
  }
1431
1599
  var baseRepository_default = BaseRepository;
1432
1600
  // ../../src/core/database/connection.ts