@effect-app/infra 4.0.0-beta.217 → 4.0.0-beta.218

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.
@@ -48,6 +48,8 @@ const emptyValueFor = (tag: ComputedProjectionIrExpression["_tag"]) => {
48
48
  return true
49
49
  case "relation-collect":
50
50
  return [] as unknown[]
51
+ case "relation-collect-fields":
52
+ return [] as unknown[]
51
53
  default:
52
54
  return assertUnreachable(tag)
53
55
  }
@@ -136,6 +138,22 @@ const computeProjectionValue = (
136
138
  }
137
139
  return out
138
140
  }
141
+ case "relation-collect-fields": {
142
+ const out: unknown[] = []
143
+ const seen = computed.distinct ? new Set<unknown>() : undefined
144
+ for (const value of relation) {
145
+ if (!matches(value)) continue
146
+ for (const field of computed.fields) {
147
+ const v = get(value, field)
148
+ if (seen) {
149
+ if (seen.has(v)) continue
150
+ seen.add(v)
151
+ }
152
+ out.push(v)
153
+ }
154
+ }
155
+ return out
156
+ }
139
157
  default:
140
158
  return assertUnreachable(computed)
141
159
  }
@@ -469,6 +469,23 @@ export function buildWhereSQLQuery(
469
469
  const aggArg = computed.distinct ? `DISTINCT ${fieldExtract}` : fieldExtract
470
470
  return `(SELECT COALESCE(jsonb_agg(${aggArg}), '[]'::jsonb) FROM ${relationFrom}${whereClause()}) AS "${key}"`
471
471
  }
472
+ case "relation-collect-fields": {
473
+ const branches = computed.fields.map((field) => {
474
+ const fieldExtract = dialect.jsonExtractElement(relationAlias, field)
475
+ return `SELECT ${fieldExtract} AS __v FROM ${relationFrom}${whereClause()}`
476
+ })
477
+ const unionQuery = branches.join(" UNION ALL ")
478
+ if (dialect.jsonColumnType === "JSON") {
479
+ if (computed.distinct) {
480
+ return `(SELECT COALESCE(json_group_array(__v), json_array()) FROM (SELECT DISTINCT __v FROM (${unionQuery}))) AS "${key}"`
481
+ }
482
+ return `(SELECT COALESCE(json_group_array(__v), json_array()) FROM (${unionQuery})) AS "${key}"`
483
+ }
484
+ if (computed.distinct) {
485
+ return `(SELECT COALESCE(jsonb_agg(__v), '[]'::jsonb) FROM (SELECT DISTINCT __v FROM (${unionQuery}) inner_q) outer_q) AS "${key}"`
486
+ }
487
+ return `(SELECT COALESCE(jsonb_agg(__v), '[]'::jsonb) FROM (${unionQuery}) t) AS "${key}"`
488
+ }
472
489
  default:
473
490
  return assertUnreachable(computed)
474
491
  }