Check if database with specified name exists or not

427 views Asked by At

As you may know that whenever we set a new database inside "sails-orientdb adapter" configurations it creates database, now on the creation time of the database of course database will be created if there is no database inside orientdb with this name, now I want to create vertices related to a class you can say those vertices are defaults of my application, whenever new database will be created those defaults will also be created, but when database already exists those defaults will also be skipped.

Now, is there any function like exists() available inside Waterline or Oriento which can check that database with the specified name inside configurations exists or not inside orientdb and return true or false?

1

There are 1 answers

0
Dário On BEST ANSWER

There is not a function .exists() but Oriento has a function named .list() which will list all DBs and allows checking if a particular DB is present. To do this from Sails-OrientDB you can use the custom method .getServer() as follows:

// Assume a model named "Post"
Post.getServer()
  .list()
  .then(function (dbs) {
    var dbExists = _.find(dbs, function(db) {
      return db.name === 'myDatabaseName';
    });
    console.log('myDatabaseName exists:', dbExists);
});

This is the logic that Sails-OrientDB uses to determine if the DB exists before creating it: https://github.com/appscot/sails-orientdb/blob/master/lib/connection.js#L604-L608