I have an object and I need to keep a history of all changes made to it. How would I implement this using neo4j?
You could also approach it from the other side:
(pages)-[:VERSION]->(V1)-[:VERSION]->(V2)-[:VERSION]->(V3)
^ ^
| |
category current
node version of page
advantage : when you create a new version, you just add it at the end of the chain, no need to "insert" it between the (page) and the current version.
disadvantage :you can't just throw away old versions, unless you reconstruct the chain. But this is probably not a frequent operation.
As with a RDBMS, it would depend on your domain and data query requirements.
Does your application require regular access to all versions of the object or usually just to the most recent, with the older versions available via the current one? An example of this could be pages on Wikipedia. As as example, let's say we have a page which is on version 3. We could then model this as follows:
(pages)-[:PAGE]->(V3)-[:PREV]->(V2)-[:PREV]->(V1)
^ ^
| |
category current
node version of page
Here, only the current version can be seen to form part of the main structure but you may wish to allow all versions to form part of that structure. In this case, you could use relationship properties to indicate the version and have all page versions link from the category node:
(V1)
^
|
[:PAGE(v=1)]
|
(pages)-[:PAGE(v=2)]->(V2)
|
[:PAGE(v=3)]
|
v
(V3)
Here, you can immediately traverse to a particular version of the page by simply specifying the version in which you are interested.
A third option could be that you wish all older versions to be completely separate from the main structure. For this you could use multiple category nodes, one for (current_pages)
and another for (old_pages)
. As each page is superseded by a new version, it becomes unlinked from the former category and instead linked to the latter. This would form more of an "archive" type of system where the older versions could even be moved into a separate database instance.
So you have these three options, plus more that I haven't thought of! Neo4j allows you great flexibility with this sort of design and there's absolutely no "right" answer. If none of these inspire you however, post a little more information about your domain so that the answer can be more tailored for your needs.
Cheers, Nige