Bradley Serbu
Bradley Serbu

Reputation: 51

How do you do an 'Insert if not exists' using orientjs?

What is the idiomatic way for doing an 'insert if not exists'?

Can this be done without transactions?

Upvotes: 5

Views: 956

Answers (3)

cegprakash
cegprakash

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

Alessandro Rota
Alessandro Rota

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

Michela Bonizzi
Michela Bonizzi

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

Related Questions