@prisma/client-engine-runtime 6.10.1-dev.2 → 6.11.0-dev.1

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.js CHANGED
@@ -1093,7 +1093,13 @@ var QueryInterpreter = class _QueryInterpreter {
1093
1093
  childRecords: (await this.interpretNode(joinExpr.child, queryable, scope, generators)).value
1094
1094
  }))
1095
1095
  );
1096
- return { value: attachChildrenToParents(parent, children), lastInsertId };
1096
+ if (Array.isArray(parent)) {
1097
+ for (const record of parent) {
1098
+ attachChildrenToParent(asRecord(record), children);
1099
+ }
1100
+ return { value: parent, lastInsertId };
1101
+ }
1102
+ return { value: attachChildrenToParent(asRecord(parent), children), lastInsertId };
1097
1103
  }
1098
1104
  case "transaction": {
1099
1105
  if (!this.#transactionManager.enabled) {
@@ -1235,39 +1241,34 @@ function mapField(value, field) {
1235
1241
  }
1236
1242
  return value;
1237
1243
  }
1238
- function attachChildrenToParents(parentRecords, children) {
1244
+ function attachChildrenToParent(parentRecord, children) {
1239
1245
  for (const { joinExpr, childRecords } of children) {
1240
- const parentKeys = joinExpr.on.map(([k]) => k);
1241
- const childKeys = joinExpr.on.map(([, k]) => k);
1242
- const parentMap = {};
1243
- for (const parent of Array.isArray(parentRecords) ? parentRecords : [parentRecords]) {
1244
- const parentRecord = asRecord(parent);
1245
- const key = getRecordKey(parentRecord, parentKeys);
1246
- if (!parentMap[key]) {
1247
- parentMap[key] = [];
1248
- }
1249
- parentMap[key].push(parentRecord);
1250
- if (joinExpr.isRelationUnique) {
1251
- parentRecord[joinExpr.parentField] = null;
1252
- } else {
1253
- parentRecord[joinExpr.parentField] = [];
1254
- }
1246
+ parentRecord[joinExpr.parentField] = filterChildRecords(childRecords, parentRecord, joinExpr);
1247
+ }
1248
+ return parentRecord;
1249
+ }
1250
+ function filterChildRecords(records, parentRecord, joinExpr) {
1251
+ if (Array.isArray(records)) {
1252
+ const filtered = records.filter((record) => childRecordMatchesParent(asRecord(record), parentRecord, joinExpr));
1253
+ if (joinExpr.isRelationUnique) {
1254
+ return filtered.length > 0 ? filtered[0] : null;
1255
+ } else {
1256
+ return filtered;
1255
1257
  }
1256
- for (const childRecord of Array.isArray(childRecords) ? childRecords : [childRecords]) {
1257
- if (childRecord === null) {
1258
- continue;
1259
- }
1260
- const key = getRecordKey(asRecord(childRecord), childKeys);
1261
- for (const parentRecord of parentMap[key] ?? []) {
1262
- if (joinExpr.isRelationUnique) {
1263
- parentRecord[joinExpr.parentField] = childRecord;
1264
- } else {
1265
- parentRecord[joinExpr.parentField].push(childRecord);
1266
- }
1267
- }
1258
+ } else if (records === null) {
1259
+ return null;
1260
+ } else {
1261
+ const record = asRecord(records);
1262
+ return childRecordMatchesParent(record, parentRecord, joinExpr) ? record : null;
1263
+ }
1264
+ }
1265
+ function childRecordMatchesParent(childRecord, parentRecord, joinExpr) {
1266
+ for (const [parentField, childField] of joinExpr.on) {
1267
+ if (parentRecord[parentField] !== childRecord[childField]) {
1268
+ return false;
1268
1269
  }
1269
1270
  }
1270
- return parentRecords;
1271
+ return true;
1271
1272
  }
1272
1273
  function paginate(list, { cursor, skip, take }) {
1273
1274
  const cursorIndex = cursor !== null ? list.findIndex((item) => doKeysMatch(item, cursor)) : 0;
package/dist/index.mjs CHANGED
@@ -1045,7 +1045,13 @@ var QueryInterpreter = class _QueryInterpreter {
1045
1045
  childRecords: (await this.interpretNode(joinExpr.child, queryable, scope, generators)).value
1046
1046
  }))
1047
1047
  );
1048
- return { value: attachChildrenToParents(parent, children), lastInsertId };
1048
+ if (Array.isArray(parent)) {
1049
+ for (const record of parent) {
1050
+ attachChildrenToParent(asRecord(record), children);
1051
+ }
1052
+ return { value: parent, lastInsertId };
1053
+ }
1054
+ return { value: attachChildrenToParent(asRecord(parent), children), lastInsertId };
1049
1055
  }
1050
1056
  case "transaction": {
1051
1057
  if (!this.#transactionManager.enabled) {
@@ -1187,39 +1193,34 @@ function mapField(value, field) {
1187
1193
  }
1188
1194
  return value;
1189
1195
  }
1190
- function attachChildrenToParents(parentRecords, children) {
1196
+ function attachChildrenToParent(parentRecord, children) {
1191
1197
  for (const { joinExpr, childRecords } of children) {
1192
- const parentKeys = joinExpr.on.map(([k]) => k);
1193
- const childKeys = joinExpr.on.map(([, k]) => k);
1194
- const parentMap = {};
1195
- for (const parent of Array.isArray(parentRecords) ? parentRecords : [parentRecords]) {
1196
- const parentRecord = asRecord(parent);
1197
- const key = getRecordKey(parentRecord, parentKeys);
1198
- if (!parentMap[key]) {
1199
- parentMap[key] = [];
1200
- }
1201
- parentMap[key].push(parentRecord);
1202
- if (joinExpr.isRelationUnique) {
1203
- parentRecord[joinExpr.parentField] = null;
1204
- } else {
1205
- parentRecord[joinExpr.parentField] = [];
1206
- }
1198
+ parentRecord[joinExpr.parentField] = filterChildRecords(childRecords, parentRecord, joinExpr);
1199
+ }
1200
+ return parentRecord;
1201
+ }
1202
+ function filterChildRecords(records, parentRecord, joinExpr) {
1203
+ if (Array.isArray(records)) {
1204
+ const filtered = records.filter((record) => childRecordMatchesParent(asRecord(record), parentRecord, joinExpr));
1205
+ if (joinExpr.isRelationUnique) {
1206
+ return filtered.length > 0 ? filtered[0] : null;
1207
+ } else {
1208
+ return filtered;
1207
1209
  }
1208
- for (const childRecord of Array.isArray(childRecords) ? childRecords : [childRecords]) {
1209
- if (childRecord === null) {
1210
- continue;
1211
- }
1212
- const key = getRecordKey(asRecord(childRecord), childKeys);
1213
- for (const parentRecord of parentMap[key] ?? []) {
1214
- if (joinExpr.isRelationUnique) {
1215
- parentRecord[joinExpr.parentField] = childRecord;
1216
- } else {
1217
- parentRecord[joinExpr.parentField].push(childRecord);
1218
- }
1219
- }
1210
+ } else if (records === null) {
1211
+ return null;
1212
+ } else {
1213
+ const record = asRecord(records);
1214
+ return childRecordMatchesParent(record, parentRecord, joinExpr) ? record : null;
1215
+ }
1216
+ }
1217
+ function childRecordMatchesParent(childRecord, parentRecord, joinExpr) {
1218
+ for (const [parentField, childField] of joinExpr.on) {
1219
+ if (parentRecord[parentField] !== childRecord[childField]) {
1220
+ return false;
1220
1221
  }
1221
1222
  }
1222
- return parentRecords;
1223
+ return true;
1223
1224
  }
1224
1225
  function paginate(list, { cursor, skip, take }) {
1225
1226
  const cursorIndex = cursor !== null ? list.findIndex((item) => doKeysMatch(item, cursor)) : 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prisma/client-engine-runtime",
3
- "version": "6.10.1-dev.2",
3
+ "version": "6.11.0-dev.1",
4
4
  "description": "This package is intended for Prisma's internal use",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -31,8 +31,8 @@
31
31
  "nanoid": "5.1.5",
32
32
  "ulid": "3.0.0",
33
33
  "uuid": "11.1.0",
34
- "@prisma/driver-adapter-utils": "6.10.1-dev.2",
35
- "@prisma/debug": "6.10.1-dev.2"
34
+ "@prisma/debug": "6.11.0-dev.1",
35
+ "@prisma/driver-adapter-utils": "6.11.0-dev.1"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/jest": "29.5.14",