This models extends the mongodb/mongoose Document with: * allows creating, deleting and querying all kind of incoming and outgoing relationships * native queries on neo4j with option to load Documents by default * connects each Document with corresponding Node in neo4j
TODO: check that we always get Documents as mongoose models
Check that we don't override existing functions
@param typeOfRelationship = '*' (any relationship you can query with cypher, e.g. KNOW, LOVE|KNOW ...) @param options = {} (first value is default) * direction (both|incoming|outgoing) * action: (RETURN|DELETE|...) (all other actions wich can be used in cypher) * processPart: (relationship|path|...) (depends on the result you expect from our query) * loadDocuments: (true|false) * endNode: '' (can be a node object or a nodeID)
REMOVED: options can be a cypher query as string options = { query: options } if typeof options is 'string'
build query from options
endNode can be string or node object
if no node is found
take query from options and discard build query
you can force a reloading of a node so you can ensure to get the latest existing node directly from db
Difference between doCreateIfNotExists and forceCreation:
@forceCreation: this is needed because mongoose marks each document as doc.new = true (which is checked to prevent accidently creating orphaned nodes). As long it is 'init' doc.new stays true, but we need that to complete the 'pre' 'save' hook (see -> mongraphMongoosePlugin)
Find equivalent node in graphdb
TODO: cache existing node
store document data also als in node -> untested and not recommend known issue: neo4j doesn't store deeper levels of nested objects...
store node_id on document
create a new one
do index for better queries outside mongraph e.g. people/_id/5178fb1b48c7a4ae24000001
Recommend to use this method instead of findOrCreateCorrespondingNode
shortcutmethod -> findOrCreateCorrespondingNode
Faster, because it returns directly from document if stored (see -> mongraphMongoosePlugin)
assign cb + attribute arguments
Is needed to load the records from mongodb TODO: Currently we have to store these information redundant because otherwise we would have to request each side for it's represantive node seperately to get the information wich namespace/collection the mongodb records is stored --> would increase requests to neo4j
Get both nodes: "from" node (this document) and "to" node (given as 1st argument)
alternate directions: doc -> this
both directions
we don't distinguish between incoming and outgoing relationships here would it make sense?! not sure...
if we have an error or no node found (as expected)
index if have a value
TODO: refactor -> split into more methods
relationships will be stored permanently on this document not for productive usage -> it's deactivated by default, because I'm not sure that it'a good idea to store informations redundant (CAP/syncing)
add relationships to object, sorted by type (see above for schema)
update all -> slower
remove/unset attribute
TODO: type check try to "guess" process part from last statement TODO: nice or bad feature?! ... maybe too much magic
Adding cypher query for better debugging
TODO: would it be helpful to have also the native
result?
options.graphResult = map
extract from result
return first first property otherwise
TODO: distinguish between 'Path', 'Node' etc ...
prevent undefined is not a function
if no cb is given
Extend Document