initial query engine workinggit status \o/

This commit is contained in:
Rowan 2024-11-20 13:17:53 -06:00
parent f843146bf2
commit 5da7eeaae5
2 changed files with 41 additions and 15 deletions

View file

@ -70,10 +70,10 @@ const cat = flip(concat)
const executeQuery = (query, world) => { const executeQuery = (query, world) => {
if (!query.relationship) { if (!query.relationship) {
return map(of, query.query(world)) return map(id => ({ [id]: query }), query.query(world))
} }
const { from, to: _to, edge, returnValues } = query const { from, to: _to, edge } = query
const to = getEndNode(_to) const to = getEndNode(_to)
const Edge = edge.type const Edge = edge.type
@ -85,7 +85,6 @@ const executeQuery = (query, world) => {
world world
) )
if (_to.relationship) { if (_to.relationship) {
return reduce((acc, eid) => { return reduce((acc, eid) => {
const next = { const next = {
@ -97,22 +96,53 @@ const executeQuery = (query, world) => {
} }
const results = map(cat([eid]), executeQuery(next, world)) const results = map(cat([eid]), executeQuery(next, world))
return [...acc, ...results] const expanded = map(
([l, r]) => ({ [l]: query, ...r }),
results
)
return [...acc, ...expanded]
}, [], edges) }, [], edges)
} else { } else {
return map(of, edges) return map(id => of({ [id]: query }), edges)
} }
} }
const val = v => v?.value?.value
const include = returns => name => name != null && returns.includes(name)
const resolveReturns = results => {
return map(result => {
return reduce((acc, [id, query]) => {
const q = query.schema.match
const inc = include(query.schema.returnValues.values.map(x => x.value))
if (is(Relationship, q)) {
const Edge = query.edge.type
const fn = val(q.from.name)
const en = val(q.edge.name)
const tn = val(q.to.name)
return {
...acc, ...{
...(inc(fn) && { [fn]: Edge.from[id] }),
...(inc(en) && { [en]: parseInt(id, 10) }),
...(inc(tn) && { [tn]: Edge.to[id] })
}
}
} else {
const name = val(q.name)
return { ...acc, ...(inc(name) && { [name]: id }) }
}
}, {}, Object.entries(result))
}, results)
}
export const query = curry((input, engine) => { export const query = curry((input, engine) => {
return parseAll(q, input).map(({ use, ...rest }) => { return parseAll(q, input).map(({ use, ...rest }) => {
const worldName = use && use.value || 'default' const worldName = use && use.value || 'default'
const world = engine.world[worldName] const world = engine.world[worldName]
const prepared = prepareQuery(rest, engine.component) const prepared = prepareQuery(rest, engine.component)
const results = executeQuery(prepared, world) const edges = executeQuery(prepared, world)
// TODO: handle return value mapping return resolveReturns(edges)
return results
}) })
}) })

View file

@ -41,10 +41,6 @@ describe('query', () => {
const a = create(NPC) // 1 const a = create(NPC) // 1
const b = create(NPC) // 2 const b = create(NPC) // 2
const c = create(NPC) // 3 const c = create(NPC) // 3
//const d = create(NPC)
//const e = create(NPC)
//const f = create(NPC)
//const g = create(NPC)
knows(player, a) // 4 knows(player, a) // 4
knows(player, c) // 5 knows(player, c) // 5
@ -56,10 +52,10 @@ describe('query', () => {
knows(b, a) // 9 knows(b, a) // 9
knows(c, player) // 10 knows(c, player) // 10
//knows(c, b) // 11
const result = query('MATCH (player:NPC) RETURN player, a, b', engine) const q = 'MATCH (player:Player)-[e1:Knows]->(a:NPC)-[e2:Knows]->(b:NPC) RETURN player, e1, a, e2, b'
console.log(result.unwrap()) const result = query(q, engine)
console.log(`parsing ${q}\n`, result.unwrap())
}) })
}) })