1

如何使用 orientjs 在事务中插入优势?我当前的实现 upserts 两个顶点并总是创建一个新的边缘:

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);
}
4

1 回答 1

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);
    })

如果发现相同的$source$destinationdb level error之间已经存在边缘,将抛出。

我希望这肯定会帮助你:)

于 2017-09-14T12:15:00.153 回答