orientjs:在事务中upsert edge

时间:2016-10-03 21:54:01

标签: javascript orientdb orientjs

如何使用orientjs在事务中插入边缘?我当前的实现突出了两个顶点,始终创建了一个新的边缘:

function add(db, from, edge, to, cb) {
  cb = cb || function() {};
  log(
    '[' + from.clazz + ']' + JSON.stringify(from.attributes) + ' ' +
    '-[' + edge.clazz + ']' + JSON.stringify(edge.attributes) + '> ' +
    '[' + to.clazz + ']' + JSON.stringify(to.attributes)
  );
  db.let('source', function(s) {
      s.update(from.clazz)
        .set(from.attributes)
        .upsert()
        .where(from.attributes)
        .return('after @this');
    })
    .let('destination', function(d) {
      d.update(to.clazz)
        .set(to.attributes)
        .upsert()
        .where(to.attributes)
        .return('after @this');
    })
    .let('edge', function(e) {
      e.create('EDGE', edge.clazz)
        .from('$source')
        .to('$destination')
        .set(edge.attributes);
    })
    .commit()
    .return('$edge')
    .all()
    .then(cb);
}

1 个答案:

答案 0 :(得分:0)

我没有在OrientJS中找到任何边缘的upsert方法,但是你可以阻止在同一个源和目标之间创建边twice。你需要

  • 创建边缘迁移时创建UNIQUE index

以下是使用唯一索引创建边的迁移代码:

exports.up = (db) => {
  return db.class.create('HasApplied', 'E')
    .then((hasApplied) => {
      return hasApplied.property.create(
        [{
          name: 'out',
          type: 'link',
          linkedClass: 'Consultant',
          mandatory: true
        }, {
          name: 'in',
          type: 'link',
          linkedClass: 'Job',
          mandatory: true
        }, {
          name: 'technicalQuestions',
          type: 'embedded'
        }, {
          name: 'technicalAnswers',
          type: 'embedded'
        }, {
          name: 'behavioralQuestions',
          type: 'embedded'
        }, {
          name: 'behavioralAnswers',
          type: 'embedded'
        }, {
          name: 'artifacts',
          type: 'embeddedset'
        }, {
          name: 'comments',
          type: 'string',
        }, {
          name: 'createdAt',
          type: 'datetime'
        }, {
          name: 'updatedAt',
          type: 'datetime'
        }]
      );
    })
    .then(() => db.query('CREATE INDEX HasApplied.out_in ON HasApplied (out, in) UNIQUE'));
};

然后当你的代码试图运行包含let block的事务时:

.let('edge', function(e) {
      e.create('EDGE', edge.HasApplied)
        .from('$source')
        .to('$destination')
        .set(edge.attributes);
    })
如果在同一 $来源 $目的地之间找到已经存在优势,

将抛出db level error

我希望这肯定会对你有所帮助:)。

相关问题