Build your own knowledge graph database
The Bacon machine·4 min

Wire up the query command

0 / 1

Connect the pieces you built. kgdb query "<cypher>" loads the frozen graph, runs parseQuery → executor, and prints the canonicalized rows. This is parse → plan → execute — the same architecture as Postgres, just smaller. The CLI glue is given; you supply the engine underneath it.

src/cli.ts
case "query": {
  const store = loadStore(rest[0]!, rest[1]!);
  const cypher = rest[2]!;
  const rows = runQuery(store, cypher);
  // canonicalize as a SET unless the query has ORDER BY (then order is meaningful)
  const hasOrderBy = /\border\s+by\b/i.test(cypher);
  process.stdout.write(
    JSON.stringify(hasOrderBy ? rows.map(canonicalizeValue) : canonicalizeRows(rows)) + "\n",
  );
  return;
}
Order handling, one place

The CLI decides set-vs-list once: with ORDER BY, the order is meaningful, so it key-sorts each row but preserves row order; without it, it sorts rows by their stable key (a canonical set). Your executor just needs to return correct rows — the CLI handles canonical printing.