Skip to content

feat: add Cypher query support and port missing Python sgraph methods#24

Merged
villelaitila merged 1 commit intosoftagram:mainfrom
villelaitila:feat/cypher-and-python-parity
Mar 17, 2026
Merged

feat: add Cypher query support and port missing Python sgraph methods#24
villelaitila merged 1 commit intosoftagram:mainfrom
villelaitila:feat/cypher-and-python-parity

Conversation

@villelaitila
Copy link
Contributor

Summary

  • Cypher query interpreter (src/cypher/) — lightweight, zero-dependency Cypher engine for querying SGraph models. Supports MATCH patterns, WHERE filtering, RETURN with aggregation (COUNT, COLLECT, type()), ORDER BY, LIMIT, DISTINCT. Includes CLI with table/json/csv output and interactive REPL.
  • 22 new methods ported from the Python sgraph library for API parity: getLevel, getRoot, isDescendantOf, getAncestorOfType, getDescendants, getNodeCount, remove, rename, getDepth, calculateModelStats, filter, getChildrenByType, and more.
  • 2 bug fixes: SElementAssociation.remove() was removing from wrong element's incoming list; ModelApi.getUsedElements was returning fromElement instead of toElement.
  • PYTHON_MAPPING.md — bidirectional API mapping between Python and TypeScript implementations for ongoing maintenance.

Test plan

  • All 159 tests pass (npm test)
  • CLI smoke tests: npm run compile && node dist/cypher/cli.js test/modelfile.xml "MATCH (n:file) RETURN n.name"
  • Verify softagram-web-frontend compiles with updated sgraph.js (bug fixes are safe — remove() fix aligns with how frontend already uses it, getUsedElements is unused by frontend)

This adds two major features to sgraph.js:

1. Cypher Query Interpreter (src/cypher/)
   A lightweight, zero-dependency Cypher query engine for querying
   SGraph models. Supports MATCH patterns with node labels and
   relationship types, WHERE filtering (=, <>, CONTAINS, STARTS WITH,
   ENDS WITH, AND, OR, NOT), RETURN with property access and aliases,
   COUNT/COLLECT aggregation, type() function, ORDER BY, LIMIT, and
   DISTINCT. Includes a CLI with table/json/csv output and interactive
   REPL mode.

   This mirrors the Python sgraph cypher.py module but uses a hand-
   written parser+executor instead of the sPyCy dependency.

   Usage:
     import { cypherQuery } from 'sgraph.js';
     const result = cypherQuery(model, 'MATCH (n:file) RETURN n.name');

   CLI:
     node dist/cypher/cli.js model.xml "MATCH (n:file) RETURN n.name"

2. Python sgraph Method Parity
   Ports 22 methods from the Python sgraph library to close the API
   gap between the two implementations:

   SElement: getLevel, getRoot, isDescendantOf, getAncestorOfType,
   getAncestorOfTypes, getAncestorOfLevel, getDescendants, getNodeCount,
   getEACount, getEATypes, getEATypeCounts, getMaxDepth, hasType,
   remove, rename, removeDescendantsIf

   SElementAssociation: addAttribute

   SGraph: getDepth, calculateModelStats

   ModelApi: getElementByPath, filter, getChildrenByType

Bug fixes:
- SElementAssociation.remove(): fixed removing from fromElement.incoming
  instead of toElement.incoming
- ModelApi.getUsedElements(): fixed mapping to ea.fromElement instead
  of ea.toElement for outgoing dependencies

Also adds CLAUDE.md, PYTHON_MAPPING.md (bidirectional API mapping
between Python and TypeScript), and design/plan documentation.

Tests: 159 total (37 existing + 50 new method tests + 72 Cypher tests)
@villelaitila villelaitila requested a review from NotMixu March 16, 2026 07:42
@villelaitila villelaitila merged commit fd60e65 into softagram:main Mar 17, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant