【问题标题】:orientjs: upsert edge in transactionorientjs:在事务中插入边缘
【发布时间】:2016-10-03 21:54:01
【问题描述】:

如何使用 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);
}

【问题讨论】:

  • 您使用的是哪个版本?从 2.2 开始,您可以使用 UPDATE EDGE 参见:orientdb.com/docs/last/SQL-Update-Edge.html
  • orientdb 在 2.1.19,orientjs 在 2.2.1。我也许可以升级两者。但在文档中,我看不到UPDATE EDGEUPSERTUPDATE EDGE 会自动插入边缘吗?
  • 你是对的,更新边缘没有 upsert。如果你的边缘已经存在,你可以使用更新。

标签: javascript orientdb orientjs


【解决方案1】:

我没有在 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 块的事务时:

.let('edge', function(e) {
      e.create('EDGE', edge.HasApplied)
        .from('$source')
        .to('$destination')
        .set(edge.attributes);
    })

如果发现同一 $source$destination 之间已经存在边缘,将抛出 db level error

我希望这对你有帮助:)

【讨论】:

  • 那么你如何在其中使用upsert作为边缘?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-12
  • 1970-01-01
  • 2022-07-07
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多