@prisma/orm-family-sql 8.0.0-rc.11-dev.12 → 8.0.0-rc.11-dev.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.
@@ -2038,7 +2038,7 @@ function createPreparedRowsConsumer(options) {
2038
2038
  raw,
2039
2039
  mapped: mapRow(raw)
2040
2040
  }));
2041
- for (const parent of parents) for (const include of includes) parent.mapped[include.alias] = await include.consume(parent.raw[include.alias]);
2041
+ for (const parent of parents) for (const include of includes) parent.mapped[include.alias] = include.consume(parent.raw[include.alias]);
2042
2042
  for (const parent of parents) yield castRow(parent.mapped);
2043
2043
  })());
2044
2044
  }
@@ -2061,10 +2061,10 @@ function createPreparedIncludeConsumer(context, include) {
2061
2061
  }));
2062
2062
  return {
2063
2063
  alias,
2064
- consume: async (raw) => {
2064
+ consume: (raw) => {
2065
2065
  const parsed = parseCombineEnvelope(include, raw);
2066
2066
  const result = {};
2067
- for (const branch of branches) result[branch.name] = await branch.consume(parsed[branch.name]);
2067
+ for (const branch of branches) result[branch.name] = branch.consume(parsed[branch.name]);
2068
2068
  return result;
2069
2069
  }
2070
2070
  };
@@ -2080,14 +2080,62 @@ function createPreparedIncludeConsumer(context, include) {
2080
2080
  const bindingFor = (key) => bindings().get(key)?.() ?? null;
2081
2081
  const nested = include.nested.includes.map((child) => createPreparedIncludeConsumer(context, child));
2082
2082
  const toOne = isToOneCardinality(include.cardinality);
2083
+ const fused = deferResolution(() => createPreparedIncludedRowDecoder(contract, include, bindingFor));
2083
2084
  return {
2084
2085
  alias,
2085
- consume: (raw) => consumeRowInclude(parseIncludedRows(include, raw), mapRow, bindingFor, nested, toOne)
2086
+ consume: (raw) => {
2087
+ const rows = parseIncludedRows(include, raw);
2088
+ if (rows.length === 0) return toOne ? null : [];
2089
+ const decoder = fused();
2090
+ if (!decoder || !rows.every(decoder.matches)) return consumeRowInclude(rows, mapRow, bindingFor, nested, toOne);
2091
+ const mapped = rows.map((row) => {
2092
+ const result = decoder.decode(row);
2093
+ for (const child of nested) result[child.alias] = child.consume(row[child.alias]);
2094
+ return result;
2095
+ });
2096
+ return toOne ? mapped[0] ?? null : mapped;
2097
+ }
2098
+ };
2099
+ }
2100
+ function createPreparedIncludedRowDecoder(contract, include, bindingFor) {
2101
+ const namespace = include.relatedNamespaceId;
2102
+ const model = include.relatedModelName;
2103
+ if (resolvePolymorphismInfo(contract, namespace, model)) return void 0;
2104
+ const fieldToColumn = getFieldToColumnMap(contract, namespace, model);
2105
+ const columnToField = getColumnToFieldMap(contract, namespace, model);
2106
+ const columns = (include.nested.selectedFields ?? Object.keys(fieldToColumn)).map((field) => Object.hasOwn(fieldToColumn, field) ? fieldToColumn[field] ?? field : field);
2107
+ const aliases = include.nested.includes.map((child) => child.relationName);
2108
+ const table = contract.storage.namespaces[namespace]?.entries.table?.[include.relatedTableName];
2109
+ if (aliases.some((alias) => table?.columns[alias] !== void 0)) return void 0;
2110
+ const keys = [...columns, ...aliases];
2111
+ if (keys.includes("__proto__") || new Set(keys).size !== keys.length) return void 0;
2112
+ const operations = keys.map((key) => {
2113
+ const binding = deferResolution(() => bindingFor(key));
2114
+ return {
2115
+ key,
2116
+ field: Object.hasOwn(columnToField, key) ? columnToField[key] ?? key : key,
2117
+ decode(value) {
2118
+ if (value === null || value === void 0) return value;
2119
+ const resolved = binding();
2120
+ return resolved ? decodeIncludedColumnValue(resolved.ref, resolved.codecId, resolved.codec, value) : value;
2121
+ }
2122
+ };
2123
+ });
2124
+ return {
2125
+ matches(row) {
2126
+ const actual = Object.keys(row);
2127
+ return actual.length === keys.length && actual.every((key, index) => key === keys[index]);
2128
+ },
2129
+ decode(row) {
2130
+ const result = {};
2131
+ for (const operation of operations) result[operation.field] = operation.decode(row[operation.key]);
2132
+ return result;
2133
+ }
2086
2134
  };
2087
2135
  }
2088
2136
  function createPreparedScalarConsumer(context, include, scalar) {
2089
2137
  const metadata = deferResolution(() => resolveScalarIncludeMetadata(context.contract, context, include, scalar));
2090
- return async (raw) => {
2138
+ return (raw) => {
2091
2139
  const { resolved, codec } = metadata();
2092
2140
  return consumeScalarInclude(include, resolved, codec, raw);
2093
2141
  };
@@ -2135,7 +2183,7 @@ function consumeIncludeRows(context, state, namespaceId, modelName, rows) {
2135
2183
  };
2136
2184
  });
2137
2185
  const bindings = /* @__PURE__ */ new WeakMap();
2138
- for (const parent of parentRows) for (const include of state.includes) parent.mapped[include.relationName] = await decodeIncludePayload(contract, context, include, parent.raw[include.relationName], bindings);
2186
+ for (const parent of parentRows) for (const include of state.includes) parent.mapped[include.relationName] = decodeIncludePayload(contract, context, include, parent.raw[include.relationName], bindings);
2139
2187
  for (const row of parentRows) yield blindCast(row.mapped);
2140
2188
  };
2141
2189
  return new AsyncIterableResult(generator());
@@ -2218,8 +2266,8 @@ function buildIdentityInFilter(contract, namespaceId, tableName, identityColumns
2218
2266
  * each branch is dispatched to the row or scalar decoder per its
2219
2267
  * declared shape (see `decodeCombineIncludePayload`).
2220
2268
  */
2221
- async function decodeIncludePayload(contract, context, include, raw, bindings) {
2222
- if (include.scalar) return Promise.resolve(decodeScalarIncludePayload(contract, context, include, include.scalar, raw));
2269
+ function decodeIncludePayload(contract, context, include, raw, bindings) {
2270
+ if (include.scalar) return decodeScalarIncludePayload(contract, context, include, include.scalar, raw);
2223
2271
  if (include.combine) return decodeCombineIncludePayload(contract, context, include, include.combine, raw, bindings);
2224
2272
  const rawChildren = parseIncludedRows(include, raw);
2225
2273
  const polyInfo = resolvePolymorphismInfo(contract, include.relatedNamespaceId, include.relatedModelName);
@@ -2242,12 +2290,12 @@ async function decodeIncludePayload(contract, context, include, raw, bindings) {
2242
2290
  consume: (value) => decodeIncludePayload(contract, context, nested, value, bindings)
2243
2291
  })), isToOneCardinality(include.cardinality));
2244
2292
  }
2245
- async function consumeRowInclude(rawChildren, mapChildRow, bindingFor, nested, toOne) {
2293
+ function consumeRowInclude(rawChildren, mapChildRow, bindingFor, nested, toOne) {
2246
2294
  const mappedChildren = [];
2247
2295
  for (const childRow of rawChildren) {
2248
2296
  const decodedChildRow = decodeIncludedStorageRow(childRow, bindingFor);
2249
2297
  const mapped = mapChildRow(decodedChildRow);
2250
- for (const child of nested) mapped[child.alias] = await child.consume(decodedChildRow[child.alias]);
2298
+ for (const child of nested) mapped[child.alias] = child.consume(decodedChildRow[child.alias]);
2251
2299
  mappedChildren.push(mapped);
2252
2300
  }
2253
2301
  return toOne ? mappedChildren[0] ?? null : mappedChildren;
@@ -2353,12 +2401,12 @@ function wrapIncludedDecodeFailure(error, ref, codecId) {
2353
2401
  * bug — `parseCombineEnvelope` throws loudly rather than papering over
2354
2402
  * it with an empty shape.
2355
2403
  */
2356
- async function decodeCombineIncludePayload(contract, context, include, branches, raw, bindings) {
2404
+ function decodeCombineIncludePayload(contract, context, include, branches, raw, bindings) {
2357
2405
  const parsed = parseCombineEnvelope(include, raw);
2358
2406
  const result = {};
2359
2407
  for (const [branchName, branch] of Object.entries(branches)) {
2360
2408
  const branchRaw = parsed[branchName];
2361
- if (branch.kind === "rows") result[branchName] = await decodeIncludePayload(contract, context, {
2409
+ if (branch.kind === "rows") result[branchName] = decodeIncludePayload(contract, context, {
2362
2410
  ...include,
2363
2411
  nested: branch.state,
2364
2412
  scalar: void 0,