MCPcopy Create free account
hub / github.com/effect-app/libs / selectPerRow

Function selectPerRow

packages/infra/src/Store/Memory.ts:214–269  ·  view source on GitHub ↗
(r: T[])

Source from the content-addressed store, hash-verified

212 const sel = f.select
213
214 const selectPerRow = (r: T[]): M[] => {
215 if (!sel) return r as M[]
216
217 // Detect aggregate mode: any select item has `aggregate` key
218 const hasAggregates = sel.some((s) => typeof s === "object" && s !== null && "aggregate" in s)
219 if (hasAggregates) {
220 // GROUP BY + aggregate
221 const fieldItems = sel.filter((s): s is { key: string; path: string } =>
222 typeof s === "object" && s !== null && "path" in s && !("aggregate" in s)
223 )
224 const aggregateItems = sel.filter((s): s is { key: string; aggregate: AggregateIrExpression } =>
225 typeof s === "object" && s !== null && "aggregate" in s
226 )
227 const groups = new Map<string, T[]>()
228 for (const row of r) {
229 const key = fieldItems.map((fi) => JSON.stringify(get(row, fi.path))).join("\0")
230 const existing = groups.get(key) ?? []
231 existing.push(row)
232 groups.set(key, existing)
233 }
234 return [...groups.values()].map((rows) => {
235 const result: Record<string, unknown> = {}
236 for (const fi of fieldItems) result[fi.key] = get(rows[0]!, fi.path)
237 for (const ai of aggregateItems) result[ai.key] = computeAggregateValue(rows, ai.aggregate)
238 return result as M
239 })
240 }
241
242 return r.map((i) => {
243 const [keys, entries] = pipe(
244 sel,
245 Array.partition((entry) => typeof entry === "string" ? Result.fail(String(entry)) : Result.succeed(entry))
246 )
247 const subKeys = entries.filter((entry): entry is { key: string; subKeys: readonly string[] } =>
248 typeof entry === "object" && entry !== null && "subKeys" in entry
249 )
250 const computedKeys = entries.filter((entry): entry is {
251 key: string
252 computed: ComputedProjectionIrExpression
253 } => typeof entry === "object" && entry !== null && "computed" in entry)
254 const pathKeys = entries.filter((entry): entry is { key: string; path: string } =>
255 typeof entry === "object" && entry !== null && "path" in entry && !("aggregate" in entry)
256 )
257 const n = Struct.pick(i, keys)
258 subKeys.forEach((subKey) => {
259 n[subKey.key] = i[subKey.key]!.map(Struct.pick(subKey.subKeys as never[]))
260 })
261 computedKeys.forEach((entry) => {
262 ;(n as Record<string, unknown>)[entry.key] = computeProjectionValue(i, entry.computed)
263 })
264 pathKeys.forEach((entry) => {
265 ;(n as Record<string, unknown>)[entry.key] = get(i, entry.path)
266 })
267 return n as M
268 })
269 }
270
271 const skip = f?.skip

Callers 1

memFilterFunction · 0.85

Calls 15

getFunction · 0.90
computeAggregateValueFunction · 0.85
computeProjectionValueFunction · 0.85
someMethod · 0.80
filterMethod · 0.80
joinMethod · 0.80
pushMethod · 0.80
mapMethod · 0.65
getMethod · 0.65
setMethod · 0.65
pickMethod · 0.65
forEachMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…