Skip to main content

2025-09-28

1. Document Relation Graph

This hybrid approach, often called “polyglot persistence,” is a powerful and modern way to build systems. You’re using each database for what it excels at:
  • MongoDB: Storing and querying rich, self-contained document metadata. It’s fast, flexible, and perfect for the “what is this thing?” questions.
  • Neo4j: Storing and querying complex, interconnected relationships. It’s unparalleled for the “how does this thing relate to other things?” questions.
Let’s design this system from the ground up.

1. System Architecture Overview

Here’s how the components will interact:
Workflow: When a user performs an action (e.g., creates a new version, links two documents), the Laravel backend acts as the orchestrator:
  1. It writes the file to the archive repository.
  2. It writes the full metadata to MongoDB.
  3. It then creates/updates the corresponding nodes and relationships in Neo4j.

2. Neo4j Graph Model Design

This model is focused purely on the identities of documents and their relationships.

Nodes

We’ll use two types of nodes to represent the document and its versions.
  1. :Document Node: Represents the conceptual document, identified by its unifying callCode.
  • Properties:
  • callCode: (string, unique) e.g., “INV-2023-042”
  • title: (string) The current title of the document.
  1. :Version Node: Represents a specific, immutable version of a document. This is the node that will participate in most relationships.
  • Properties:
  • versionId: (string, unique) A composite key like callCode + ':' + revisionNumber. e.g., “INV-2023-042:1”. This makes lookups easy.
  • revisionNumber: (integer)
  • mongoId: (string) The _id of the corresponding record in your MongoDB documentVersions collection. This is the crucial link back to the full metadata.
  • createdAt: (datetime)

Relationships

These define how the nodes connect.
  • HAS_VERSION: Connects a :Document to one of its :Version nodes. (d:Document)-[:HAS_VERSION]->(v:Version)
  • LATEST_VERSION: A special, single relationship from a :Document to its most current :Version. This is an optimization for finding the latest version quickly. (d:Document)-[:LATEST_VERSION]->(v:Version)
  • PREVIOUS_VERSION: The chronological link. (v2:Version)-[:PREVIOUS_VERSION]->(v1:Version)
  • Custom Referential Relationships:
  • REFERS_TO: For generic references. (pr:Version)-[:REFERS_TO]->(inv:Version)
  • INITIATED_BY: A more semantically rich relationship. (pr:Version)-[:INITIATED_BY]->(inv:Version)
  • SUPERSEDES: For when one document explicitly replaces another.
  • You can add properties to relationships! For example: (:Version)-[:REFERS_TO { comment: "Supporting data for audit" }]->(:Version)
Example Graph:

3. Laravel Backend Implementation

You’ll need a Neo4j driver for PHP. A popular choice is laudis/neo4j-php-client.

Key Logic: Creating a New Version

Let’s imagine a DocumentService class.

API Endpoint for Visualization

This endpoint will query Neo4j and format the data for the frontend.

4. Simple Vue 2 Component for Visualization

We’ll use the excellent vis-network library for this. Installation: npm install vis-network Component: DocumentGraph.vue
How to use it in another component:
This complete setup gives you a powerful, scalable, and visually intuitive way to manage not just document versions, but the entire web of relationships within your DMS.