unterrible it

This commit is contained in:
Rowan 2024-11-23 14:21:49 -06:00
parent d53bab5890
commit efe4971ccc
2 changed files with 36 additions and 49 deletions

View file

@ -130,7 +130,7 @@ export const fromEntries = Object.fromEntries
// lenses // lenses
export const lens = curry((get, set) => ({ get, set })) export const lens = curry((get, set) => ({ get, set }))
export const lensPath = path => lens(path(path), assocPath(path)) export const lensPath = value => lens(path(value), assocPath(value))
export const lensIndex = index => lens(nth(index), update(index)) export const lensIndex = index => lens(nth(index), update(index))
export const view = curry(({ get }, obj) => get(obj)) export const view = curry(({ get }, obj) => get(obj))
export const set = curry((l, value, obj) => over(l, always(value), obj)) export const set = curry((l, value, obj) => over(l, always(value), obj))

View file

@ -33,20 +33,12 @@ const matches = (world, type, query, set = new Set()) => entity => {
} }
} }
const propFrom = flip(prop)
const getNode = when(prop('relationship'), prop('from'))
const queryLens = lens(prop('query'), assoc('results'))
const getQueryResults = curry((world, node) => over(queryLens, applyTo(world), node))
const queryRelationship = (query, world) => { const queryRelationship = (query, world) => {
const [from, edge, to] = map( const [from, edge, to] = ['from', 'edge', 'to'].map(name => {
pipe( let node = query[name]
propFrom(query), node = node.relationship ? node.from : node
getNode, return { ...node, results: node.query(world) }
getQueryResults(world) })
),
['from', 'edge', 'to']
)
const matchesFrom = matches(world, from.type, from.results) const matchesFrom = matches(world, from.type, from.results)
const matchesTo = matches(world, to.type, to.results) const matchesTo = matches(world, to.type, to.results)
@ -65,7 +57,7 @@ const queryRelationship = (query, world) => {
return result return result
} }
const assembleQuery = curry((query, entities) => map(entity => of({ entity, query }), entities)) const assembleQuery = (query, entities) => entities.map(entity => ([{ entity, query }]))
const executeQuery = curry((query, world) => { const executeQuery = curry((query, world) => {
if (!query.relationship) { if (!query.relationship) {
@ -75,27 +67,23 @@ const executeQuery = curry((query, world) => {
const edges = queryRelationship(query, world) const edges = queryRelationship(query, world)
if (query.to.relationship) { if (query.to.relationship) {
return reduce((acc, eid) => { return edges.reduce((acc, eid) => {
const next = assocPath( const next = assocPath(
'from.query', 'from.query',
always(of(query.edge.type.to[eid])), always(of(query.edge.type.to[eid])),
query.to query.to
) )
return pipe( return executeQuery(next, world)
executeQuery(next), .map(child => [eid, ...child])
map(prepend(eid)), .map(([l, r]) => [{ entity: l, query }, r])
map(([l, r]) => ([{ entity: l, query }, r])), .concat(acc)
concat(acc) }, [])
)(world)
}, [], edges)
} else { } else {
return assembleQuery(query, edges) return assembleQuery(query, edges)
} }
}) })
const returnValues = pipe(path('query.schema.returnValues.values'), map(prop('value')))
const nodeName = path('query.schema.match.name.value.value')
const includes = curry((val, arr) => !isNil(val) && arr.includes(val)) const includes = curry((val, arr) => !isNil(val) && arr.includes(val))
const maybeAssoc = curry((pred, key, value, obj) => const maybeAssoc = curry((pred, key, value, obj) =>
@ -103,8 +91,8 @@ const maybeAssoc = curry((pred, key, value, obj) =>
) )
const resolveNode = curry((node, obj) => { const resolveNode = curry((node, obj) => {
const name = nodeName(node) const name = path('query.schema.match.name.value.value', node)
const returns = returnValues(node) const returns = path('query.schema.returnValues.values', node).map(r => r.value)
return maybeAssoc(always(includes(name, returns)), name, node.entity, obj) return maybeAssoc(always(includes(name, returns)), name, node.entity, obj)
}) })
@ -119,27 +107,26 @@ const resolveRelationship = edges => {
)({}) )({})
} }
const resolveReturns = map( const resolveReturns = query => {
pipe( return query.map(edges => {
map(ifElse( const returns = edges.map(edge => {
path('query.relationship'), if (edge.query.relationship) {
resolveRelationship, return resolveRelationship(edge)
flip(resolveNode)({}) } else {
)), return resolveNode(edge, {})
reduce(mergeLeft, {}) }
) })
)
export const query = curry((input, { world, component }) => return Object.assign({}, ...returns)
map(({ use, ...rest }) => })
pipe( }
prop('value'),
orDefault('default'), export const query = curry((input, { world, component }) => {
flip(prop)(world), return parseAll(q, input).map(({ use, ...rest }) => {
executeQuery(prepareQuery(rest, component)), const useWorld = world[use?.value ?? 'default']
resolveReturns const preparedQuery = prepareQuery(rest, component)
)(use), const results = executeQuery(preparedQuery, useWorld)
parseAll(q, input) return resolveReturns(results)
) })
) })