Konza
Konza

Reputation: 2163

Orient js which method should we use to create a new vertex and why

I am using orientjs version: 2.1.0 and orientdb version 2.1.6 community edition

From the github docs I found that there are 2 ways to create a new Vertex.

Here is the first method

db.create('VERTEX', 'User')
        .set(newUser)
        .one()
        .then(function (record) {
            logger.info("Created record " + record)
            return resolve(record)
        }).catch(function (err) {
            logger.error("Error in creating user ");
            logger.error(err);
            return reject(err)
        })

And here is the second method

var User = db.class.get("User").then(function (User) {
        User.create(newUser).then(function (record) {
            logger.info("Created record " + record)
            return resolve(record)
        })
    }).catch(function (err) {
        logger.error("Error in creating user ");
        logger.error(err);
        return reject(err)
    })

So which is the preferred method and why? And when to use db.create and class.create?

I think the first method is a bit more faster because there is only one call to the db. Correct me if I am wrong.

Upvotes: 2

Views: 464

Answers (1)

peak
peak

Reputation: 116740

The 2.1 documentation encourages the use of CREATE VERTEX: "By using the graph commands over the standard SQL syntax, OrientDB ensures that your graphs remain consistent." (http://orientdb.com/docs/2.1/Tutorial-Working-with-graphs.html) This remark is a bit perplexing, but I think it simply means that using CREATE VERTEX makes it completely transparent that a new vertex is being created. By contrast, using INSERT INTO <class> may or may not create a new vertex, depending on how the class hierarchy has been defined.

Unfortunately, there is a caveat -- see the top of http://orientdb.com/docs/2.1/SQL-Create-Vertex.html I believe the caveat concerns vertices in general, not whether or not CREATE VERTEX should be used. The caveat probably won't apply in most cases, but still, it's disappointing that one might have to be concerned about such things.

Upvotes: 2

Related Questions