Search code examples
node.jssails.jsorientdbwaterlinesails-orientdb

Waterline-OrientDB - Bi-Directional Edge


I have two models declared in Sails and I'm using the Waterline-Orientdb adapter and don't know how to connect them via a bi-directional edge

Questions Model

var Waterline = require('waterline');

module.exports = Waterline.Collection.extend({

  tableName: 'questionsTable',
  identity: 'questions',
  connection: 'associations',

  attributes: {
    id: { type: 'string', primaryKey: true, columnName: '@rid'},
    question  : { type: 'string'},
    user: { model: "User", required: true },
    answerOptions: {type: 'json'},
    imagefile: {type:'string'},
    answers: {
      collection: 'answer',
      via: 'questions',
      dominant:true
    }
  }

});

Answer Model

var Waterline = require('waterline');

module.exports = Waterline.Collection.extend({

    tableName: 'answerTable',
    identity: 'answer',
    connection: 'associations',

    attributes: {
        id: {
            type: 'string',
            primaryKey: true,
            columnName: '@rid'
        },
        Answer: {
            type: 'string'
        },
        questions: {
            collection: 'questions',
            via: 'answer'
        }

    }
});

I want to be able to create an edge between the two models. The user creates a question and then users can post a response.


Solution

  • There is a typo on your answer model:

    questions: {
      collection: 'questions',
      via: 'answer'
    }
    

    should be

    questions: {
      collection: 'questions',
      via: 'answers'  // answers as that is the attribute name in questions model
    }
    

    Example to create questions, answers and then link them:

    var question1;
    ontology.collections.questions.create({ question: 'question1' })
      .then(function(question){
        question1 = question;
    
        return ontology.collections.answer.create([{ answer: 'answer1' }, { answer: 'answer2' }]);
      })
      .then(function(answers){
        question1.answers.add(answers[0]);
        question1.answers.add(answers[1]);
    
        return question1.save();
      })
    

    I've created a running example at github.com/appscot/waterline-orientdb.

    Regards

    UPDATE: waterline-orientdb is now named sails-orientdb.