Skip to content

What is TypeGraph?

TypeGraph is a TypeScript-first, embedded knowledge graph library that brings property graph semantics and ontological reasoning to applications using standard relational databases. Rather than introducing a separate graph database, TypeGraph lives inside your application as a library, storing graph data in your existing SQLite or PostgreSQL database.

TypeGraph Architecture: Your application imports TypeGraph as a library dependency. TypeGraph uses Drizzle ORM to store graph data (nodes, edges, schema, ontology) in your existing SQLite or PostgreSQL database. No separate graph database required.

Zod schemas are the single source of truth. From one schema definition, TypeGraph derives:

  • Runtime validation rules
  • TypeScript types (inferred, not duplicated)
  • Database storage requirements
  • Query builder type constraints
const Person = defineNode("Person", {
schema: z.object({
fullName: z.string().min(1),
email: z.string().email().optional(),
dateOfBirth: z.date().optional(),
}),
});

2. Semantic Layer with Ontological Reasoning

Section titled “2. Semantic Layer with Ontological Reasoning”

Type-level relationships enable sophisticated inference:

RelationshipMeaningUse Case
subClassOfInstance inheritance (Podcast IS-A Media)Query expansion
broaderHierarchical concept (ML broader than DL)Topic navigation
equivalentToSame concept, different nameCross-system mapping
disjointWithCannot be both (Person ≠ Organization)Constraint validation
impliesEdge entailment (marriedTo implies knows)Relationship inference
inverseOfEdge pairs (manages/managedBy)Bidirectional queries

The schema and ontology are stored in the database as data, enabling:

  • Runtime schema introspection
  • Versioned schema history
  • Self-describing exports and backups
  • Migration tooling

Queries compile to an AST before targeting SQL:

  • Consistent semantics across SQLite and PostgreSQL
  • Type-checked at compile time
  • Query results have inferred types

TypeGraph is a library dependency, not a networked service. TypeGraph initializes with your application, uses your database connection, and requires no separate deployment.

Define your schemas once with Zod, and TypeGraph handles validation, type inference, and storage. No duplicate type definitions or manual synchronization.

TypeGraph favors explicit declarations:

  • Relationships are declared, not inferred from foreign keys
  • Semantic relationships are explicit in the ontology
  • Cascade behavior is configured, not assumed

The query builder generates portable ASTs that can target different SQL dialects. The same query code works with SQLite and PostgreSQL.

TypeGraph deliberately excludes:

  • Graph algorithms: No built-in shortest path, PageRank, or community detection
  • Distributed storage: Single-database deployment only

These exclusions keep TypeGraph focused and maintainable.

Note: TypeGraph does support semantic search via database vector extensions (pgvector for PostgreSQL, sqlite-vec for SQLite). See Semantic Search for details.

Note: TypeGraph does support variable-length paths via .recursive() with configurable depth limits, optional path/depth projection, and explicit cycle policy. Cycle prevention is the default. See Recursive Traversals for details.

Compared to Graph Databases (Neo4j, Amazon Neptune)

Section titled “Compared to Graph Databases (Neo4j, Amazon Neptune)”

Graph databases are powerful but come with operational overhead:

AspectGraph DatabaseTypeGraph
DeploymentSeparate service to manage, scale, and monitorLibrary in your app, uses existing database
NetworkAdditional latency for every queryIn-process, no network hop
TransactionsSeparate transaction scope from your SQL dataSame ACID transaction as your other data
Learning curveNew query language (Cypher, Gremlin)TypeScript you already know
Graph algorithmsBuilt-in (PageRank, shortest path)Not included
ScaleOptimized for billions of nodesBest for thousands to millions

Choose TypeGraph when your graph is part of your application domain (knowledge bases, org charts, content relationships) rather than a standalone analytical system.

Compared to ORMs (Prisma, Drizzle, TypeORM)

Section titled “Compared to ORMs (Prisma, Drizzle, TypeORM)”

ORMs model relations through foreign keys, which works well for simple associations but lacks graph semantics:

AspectTraditional ORMTypeGraph
RelationshipsForeign keys, eager/lazy loadingFirst-class edges with properties
TraversalsManual joins or N+1 queriesFluent traversal API, compiled to efficient SQL
InheritanceTable-per-class or single-tableSemantic subClassOf with query expansion
ConstraintsForeign key constraintsDisjointness, cardinality, implications
SchemaMigrations alter tablesSchema versioning, JSON properties

Choose TypeGraph when you need to traverse relationships, model type hierarchies, or enforce semantic constraints beyond what foreign keys provide.

Triple stores and RDF provide rich ontological modeling but have practical challenges:

AspectTriple StoreTypeGraph
Type safetyRuntime validation, stringly-typedFull TypeScript inference
Query languageSPARQL (powerful but verbose)TypeScript fluent API
SchemaOWL/RDFS (complex specification)Zod schemas (familiar, composable)
IntegrationSeparate system, data sync requiredEmbedded in your app
InferenceFull reasoning engines availablePrecomputed closures, practical subset

Choose TypeGraph when you want ontological concepts (subclass, disjoint, implies) without the complexity of full semantic web stack.

TypeGraph is designed for applications where:

  1. The graph is your domain model — not a separate analytical system
  2. You already use SQL — and don’t want another database to manage
  3. Type safety matters — you want compile-time checking, not runtime surprises
  4. Semantic relationships help — inheritance, implications, constraints add value
  5. Scale is moderate — thousands to millions of nodes, not billions

TypeGraph is ideal for:

  • Knowledge bases with typed entities and relationships
  • Organizational structures with hierarchies and roles
  • Content graphs with topics, articles, and references
  • Domain models requiring semantic constraints
  • RAG applications combining graph traversal with vector search

TypeGraph is not ideal for:

  • Large-scale graph analytics requiring distributed processing
  • Social networks with billions of edges
  • Real-time streaming graph data
  • Applications requiring graph algorithms (use Neo4j or a graph library)