prisma-sql 1.36.0 → 1.38.0

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.cjs CHANGED
@@ -196,7 +196,7 @@ function getArrayType(prismaType, dialect) {
196
196
  case "DateTime":
197
197
  return "timestamptz[]";
198
198
  default:
199
- return "text[]";
199
+ return `"${baseType}"[]`;
200
200
  }
201
201
  }
202
202
  function jsonAgg(content, dialect) {
@@ -1841,7 +1841,7 @@ function buildLogical(operator, value, ctx, builder) {
1841
1841
  }
1842
1842
  function buildScalarField(fieldName, value, ctx) {
1843
1843
  const field = assertFieldExists(fieldName, ctx.model, ctx.path);
1844
- const expr = col(ctx.alias, fieldName);
1844
+ const expr = col(ctx.alias, fieldName, ctx.model);
1845
1845
  if (value === null) {
1846
1846
  return freezeResult(`${expr} ${SQL_TEMPLATES.IS_NULL}`, EMPTY_JOINS);
1847
1847
  }
@@ -3329,8 +3329,8 @@ function replaceOrderByAlias(orderBy, fromAlias, outerAlias) {
3329
3329
  const replacement = `${outerAlias}.`;
3330
3330
  return orderBy.split(needle).join(replacement);
3331
3331
  }
3332
- function buildDistinctColumns(distinct, fromAlias) {
3333
- return distinct.map((f) => col(fromAlias, f)).join(SQL_SEPARATORS.FIELD_LIST);
3332
+ function buildDistinctColumns(distinct, fromAlias, model) {
3333
+ return distinct.map((f) => col(fromAlias, f, model)).join(SQL_SEPARATORS.FIELD_LIST);
3334
3334
  }
3335
3335
  function buildOutputColumns(scalarNames, includeNames, hasCount) {
3336
3336
  const outputCols = [...scalarNames, ...includeNames];
@@ -3344,13 +3344,13 @@ function buildOutputColumns(scalarNames, includeNames, hasCount) {
3344
3344
  return formatted;
3345
3345
  }
3346
3346
  function buildWindowOrder(args) {
3347
- const { baseOrder, idField, fromAlias } = args;
3347
+ const { baseOrder, idField, fromAlias, model } = args;
3348
3348
  const orderFields = baseOrder.split(SQL_SEPARATORS.ORDER_BY).map((s) => s.trim().toLowerCase());
3349
3349
  const hasIdInOrder = orderFields.some(
3350
3350
  (f) => f.startsWith(`${fromAlias}.id `) || f.startsWith(`${fromAlias}."id" `)
3351
3351
  );
3352
3352
  if (hasIdInOrder) return baseOrder;
3353
- const idTiebreaker = idField ? `, ${col(fromAlias, "id")} ASC` : "";
3353
+ const idTiebreaker = idField ? `, ${col(fromAlias, "id", model)} ASC` : "";
3354
3354
  return `${baseOrder}${idTiebreaker}`;
3355
3355
  }
3356
3356
  function buildSqliteDistinctQuery(spec, selectWithIncludes, countJoins) {
@@ -3367,14 +3367,17 @@ function buildSqliteDistinctQuery(spec, selectWithIncludes, countJoins) {
3367
3367
  includeNames,
3368
3368
  hasCount
3369
3369
  );
3370
- const distinctCols = buildDistinctColumns([...distinct], from.alias);
3371
- const fallbackOrder = [...distinct].map((f) => `${col(from.alias, f)} ASC`).join(SQL_SEPARATORS.FIELD_LIST);
3372
- const idField = model.fields.find((f) => f.name === "id" && !f.isRelation);
3370
+ const distinctCols = buildDistinctColumns([...distinct], from.alias, model);
3371
+ const fallbackOrder = [...distinct].map((f) => `${col(from.alias, f, model)} ASC`).join(SQL_SEPARATORS.FIELD_LIST);
3372
+ const idField = model.fields.find(
3373
+ (f) => f.name === "id" && !f.isRelation
3374
+ );
3373
3375
  const baseOrder = isNonEmptyString(orderBy) ? orderBy : fallbackOrder;
3374
3376
  const windowOrder = buildWindowOrder({
3375
3377
  baseOrder,
3376
3378
  idField,
3377
- fromAlias: from.alias
3379
+ fromAlias: from.alias,
3380
+ model
3378
3381
  });
3379
3382
  const outerOrder = isNonEmptyString(orderBy) ? replaceOrderByAlias(orderBy, from.alias, `"__tp_distinct"`) : replaceOrderByAlias(fallbackOrder, from.alias, `"__tp_distinct"`);
3380
3383
  const joins = buildJoinsSql(whereJoins, countJoins);
@@ -3485,9 +3488,9 @@ function withCountJoins(spec, countJoins, whereJoins) {
3485
3488
  whereJoins: [...whereJoins || [], ...countJoins || []]
3486
3489
  });
3487
3490
  }
3488
- function buildPostgresDistinctOnClause(fromAlias, distinct) {
3491
+ function buildPostgresDistinctOnClause(fromAlias, distinct, model) {
3489
3492
  if (!isNonEmptyArray(distinct)) return null;
3490
- const distinctCols = buildDistinctColumns([...distinct], fromAlias);
3493
+ const distinctCols = buildDistinctColumns([...distinct], fromAlias, model);
3491
3494
  return `${SQL_TEMPLATES.DISTINCT_ON} (${distinctCols})`;
3492
3495
  }
3493
3496
  function pushJoinGroups(parts, ...groups) {
@@ -3517,7 +3520,8 @@ function constructFinalSql(spec) {
3517
3520
  method,
3518
3521
  cursorClause,
3519
3522
  params,
3520
- dialect
3523
+ dialect,
3524
+ model
3521
3525
  } = spec;
3522
3526
  const useWindowDistinct = hasWindowDistinct(spec);
3523
3527
  assertDistinctAllowed(method, useWindowDistinct);
@@ -3531,7 +3535,7 @@ function constructFinalSql(spec) {
3531
3535
  return finalizeSql(sql2, params);
3532
3536
  }
3533
3537
  const parts = [SQL_TEMPLATES.SELECT];
3534
- const distinctOn = dialect === "postgres" ? buildPostgresDistinctOnClause(from.alias, distinct) : null;
3538
+ const distinctOn = dialect === "postgres" ? buildPostgresDistinctOnClause(from.alias, distinct, model) : null;
3535
3539
  if (distinctOn) parts.push(distinctOn);
3536
3540
  const baseSelect = (select != null ? select : "").trim();
3537
3541
  const fullSelectList = buildSelectList(baseSelect, includeCols);
@@ -3791,17 +3795,17 @@ function getModelFieldMap(model) {
3791
3795
  function isTruthySelection(v) {
3792
3796
  return v === true;
3793
3797
  }
3794
- function aggExprForField(aggKey, field, alias) {
3798
+ function aggExprForField(aggKey, field, alias, model) {
3795
3799
  if (aggKey === "_count") {
3796
- return field === "_all" ? `COUNT(*)` : `COUNT(${col(alias, field)})`;
3800
+ return field === "_all" ? `COUNT(*)` : `COUNT(${col(alias, field, model)})`;
3797
3801
  }
3798
3802
  if (field === "_all") {
3799
3803
  throw new Error(`'${aggKey}' does not support '_all'`);
3800
3804
  }
3801
- if (aggKey === "_sum") return `SUM(${col(alias, field)})`;
3802
- if (aggKey === "_avg") return `AVG(${col(alias, field)})`;
3803
- if (aggKey === "_min") return `MIN(${col(alias, field)})`;
3804
- return `MAX(${col(alias, field)})`;
3805
+ if (aggKey === "_sum") return `SUM(${col(alias, field, model)})`;
3806
+ if (aggKey === "_avg") return `AVG(${col(alias, field, model)})`;
3807
+ if (aggKey === "_min") return `MIN(${col(alias, field, model)})`;
3808
+ return `MAX(${col(alias, field, model)})`;
3805
3809
  }
3806
3810
  function buildComparisonOp(op) {
3807
3811
  const sqlOp = COMPARISON_OPS[op];
@@ -3982,7 +3986,7 @@ function buildHavingForAggregateFirstShape(aggKey, target, alias, params, dialec
3982
3986
  for (const [field, filter] of Object.entries(target)) {
3983
3987
  assertHavingAggTarget(aggKey, field, model);
3984
3988
  if (!isPlainObject(filter) || Object.keys(filter).length === 0) continue;
3985
- const expr = aggExprForField(aggKey, field, alias);
3989
+ const expr = aggExprForField(aggKey, field, alias, model);
3986
3990
  out.push(...buildHavingOpsForExpr(expr, filter, params, dialect));
3987
3991
  }
3988
3992
  return out;
@@ -3999,7 +4003,7 @@ function buildHavingForFieldFirstShape(fieldName, target, alias, params, dialect
3999
4003
  assertAggregateFieldType(aggKey, field.type, field.name, model.name);
4000
4004
  const entries = Object.entries(aggFilter);
4001
4005
  if (entries.length === 0) continue;
4002
- const expr = aggExprForField(aggKey, fieldName, alias);
4006
+ const expr = aggExprForField(aggKey, fieldName, alias, model);
4003
4007
  for (const [op, val] of entries) {
4004
4008
  if (op === "mode") continue;
4005
4009
  const built = buildSimpleComparison(expr, op, val, params, dialect);
@@ -4038,10 +4042,10 @@ function assertCountableScalarField(fieldMap, model, fieldName) {
4038
4042
  );
4039
4043
  }
4040
4044
  }
4041
- function pushCountField(fields, alias, fieldName) {
4045
+ function pushCountField(fields, alias, fieldName, model) {
4042
4046
  const outAlias = `_count.${fieldName}`;
4043
4047
  fields.push(
4044
- `COUNT(${col(alias, fieldName)}) ${SQL_TEMPLATES.AS} ${quote(outAlias)}`
4048
+ `COUNT(${col(alias, fieldName, model)}) ${SQL_TEMPLATES.AS} ${quote(outAlias)}`
4045
4049
  );
4046
4050
  }
4047
4051
  function addCountFields(fields, countArg, alias, model, fieldMap) {
@@ -4059,7 +4063,7 @@ function addCountFields(fields, countArg, alias, model, fieldMap) {
4059
4063
  );
4060
4064
  for (const [f] of selected) {
4061
4065
  assertCountableScalarField(fieldMap, model, f);
4062
- pushCountField(fields, alias, f);
4066
+ pushCountField(fields, alias, f, model);
4063
4067
  }
4064
4068
  }
4065
4069
  function getAggregateSelectionObject(args, agg) {
@@ -4080,10 +4084,10 @@ function assertAggregatableScalarField(fieldMap, model, agg, fieldName) {
4080
4084
  }
4081
4085
  return field;
4082
4086
  }
4083
- function pushAggregateFieldSql(fields, aggFn, alias, agg, fieldName) {
4087
+ function pushAggregateFieldSql(fields, aggFn, alias, agg, fieldName, model) {
4084
4088
  const outAlias = `${agg}.${fieldName}`;
4085
4089
  fields.push(
4086
- `${aggFn}(${col(alias, fieldName)}) ${SQL_TEMPLATES.AS} ${quote(outAlias)}`
4090
+ `${aggFn}(${col(alias, fieldName, model)}) ${SQL_TEMPLATES.AS} ${quote(outAlias)}`
4087
4091
  );
4088
4092
  }
4089
4093
  function addAggregateFields(fields, args, alias, model, fieldMap) {
@@ -4101,7 +4105,7 @@ function addAggregateFields(fields, args, alias, model, fieldMap) {
4101
4105
  fieldName
4102
4106
  );
4103
4107
  assertAggregateFieldType(agg, field.type, fieldName, model.name);
4104
- pushAggregateFieldSql(fields, aggFn, alias, agg, fieldName);
4108
+ pushAggregateFieldSql(fields, aggFn, alias, agg, fieldName, model);
4105
4109
  }
4106
4110
  }
4107
4111
  }
@@ -4164,7 +4168,7 @@ function assertGroupByBy(args, model) {
4164
4168
  return byFields;
4165
4169
  }
4166
4170
  function buildGroupBySelectParts(args, alias, model, byFields) {
4167
- const groupCols = byFields.map((f) => col(alias, f));
4171
+ const groupCols = byFields.map((f) => col(alias, f, model));
4168
4172
  const groupFields = groupCols.join(SQL_SEPARATORS.FIELD_LIST);
4169
4173
  const aggFields = buildAggregateFields(args, alias, model);
4170
4174
  const selectFields = isNonEmptyArray(aggFields) ? groupCols.concat(aggFields).join(SQL_SEPARATORS.FIELD_LIST) : groupCols.join(SQL_SEPARATORS.FIELD_LIST);