brikteknologier / seraph-model

thin model layer for seraph/neo4j (node.js)
MIT License
111 stars 28 forks source link

model.query performs a CREATE, but doesn't trigger model.on('prepare'...) #93

Closed beebase closed 8 years ago

beebase commented 9 years ago

I'm trying to create a new Song that should be linked to a logged in existing user. The query itself works fine but doesn't trigger the prepare event on the Model (=Song) Is this a bug or is my approach wrong? Should I create the Song first (Model.create) and then the relation to the existing user?

(BTW, the prepare is working ok , when updating a song)

function(req, res) {
  var cypher = ' match (u:User)  where id(u) = {userId} with u' +
               ' create (u)-[:USER_SONG]->(node:Song {song} )';
  Model.query(cypher, {
      userId: req.decoded.id,
      song  : req.body
    }
jonpacker commented 8 years ago

Yeah, unfortunately there's no support for triggering events from stuff that happens in a query. Theoretically, it would be possible to trigger 'afterSave', since neo4j can send back information about what the result of an api call is, but it's hard to see how that could be reliably used to tell if it was indeed this model that was updated.

As for validation and prepare, that's unfortunately out of the question. The only way I could see to do it would be to parse the cypher query inside seraph-model... and that's a little bit outside the scope of the project.

In your example, I would suggest that you first use Model.save to create your Song, and thereafter relate it to the user. You could also use a composition, depending on the context.