Javascript orientjs:在事务中向上插入边缘
如何使用orientjs在事务中插入边缘?我当前的实现将向上插入两个顶点,并始终创建一条新边:Javascript orientjs:在事务中向上插入边缘,javascript,orientdb,orientjs,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.attribute
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);
}
我在OrientJS中没有找到任何用于边的upsert方法,但是可以防止在同一源和目标之间创建两次边
。你只需要
- 创建边迁移时,创建一个
唯一索引
以下是用于创建具有唯一索引的边的迁移代码:
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和$destination之间已经存在边缘,将抛出db级错误
我希望这肯定会对您有所帮助:)我在OrientJS中没有找到任何用于边缘的upsert方法,但您可以防止在同一源和目标之间创建两次边缘。你只需要
- 创建边迁移时,创建一个
唯一索引
以下是用于创建具有唯一索引的边的迁移代码:
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和$destination之间已经存在边缘,将抛出db级错误
我希望这肯定会对您有所帮助:)您使用的是哪个版本?从2.2开始,您可以使用更新边缘
请参见:orientdb位于2.1.19,orientjs位于2.2.1。我可能可以升级这两个。但是在文档中,我看不到更新边缘的UPSERT
。更新边缘
会自动向上插入边缘吗?没错,更新边缘没有向上插入。如果您的边缘已经存在,您可以使用update。您使用的是哪个版本?从2.2开始,您可以使用更新边缘
请参见:orientdb位于2.1.19,orientjs位于2.2.1。我可能可以升级这两个。但是在文档中,我看不到更新边缘的UPSERT
。更新边缘
会自动向上插入边缘吗?没错,更新边缘没有向上插入。如果您的边缘已经存在,您可以使用update.and,那么如何对其中的边缘使用upsert
?以及如何对其中的边缘使用upsert
?