Reputation: 51
What is the idiomatic way for doing an 'insert if not exists'?
Can this be done without transactions?
Upvotes: 5
Views: 956
Reputation: 3125
If the data is quite small (that you can hold in memory in the server), you can do 1 get call to fetch all the data and do a batch insert only for those which are not already there.
If your creation data is very huge (that exceeds the runtime memory), you will have to follow Alessandro's method (which will be slow because for each insertion you'll have to check).
Upvotes: 0
Reputation: 3570
You can use this code for example
db.query('select from v where rid = 23')
.then(function (record) {
if(record.length==0){
db.query('insert into v(rid) values (23)');
}
});
Hope it helps.
Upvotes: 1
Reputation: 2632
Try this:
with upsert creates a record if it doesn't exists, unless it updates
var OrientDB = require('orientjs');
var server = OrientDB({
host: 'localhost',
port: 2424,
username: 'root',
password: 'root'
});
var db = server.use({
name: 'GratefulDeadConcerts',
username: 'root',
password: 'root'
})
db.query('UPDATE V SET id = 23 UPSERT WHERE id = 23')
.then(function (response) {
console.log(response);
});
server.close();
Hope it helps.
Regards
Upvotes: 3