fogine/couchbase-odm

View on GitHub
tutorials/4.instances.md

Summary

Maintainability
Test Coverage
#### Saving a new Model instance to the bucket

`Model.create(data, [options])`  

````javascript
    return User.create({
        username: 'happiecat',
        email: 'bla@bla.com'
    }).then(function(user) {
        //
    });
````

or  

`Model.build(data, [options]) => <Instance>`  

> *Which builds a new `Model.Instance` object with provided data & options *

and then  

`Instance.save([options]) => {Promise<Instance>}`

> *Which performs `insert` or `update (=replace)` operation on the instance*

````javascript
    const happie = User.build({
        username: 'happiecat',
        email: 'bla@bla.com'
    });

    return happie.save().then(function(happie) {
        console.log(happie.username); // $ > happiecat

        console.log(happie.getData()); // $ > {
                                       //          _id     : '000a2644-a151-4771-98ef-132134e1606e',
                                       //          _type   : 'User',
                                       //          username: 'happiecat',
                                       //          email   : 'bla@bla.com',
                                       //     }

        console.log(happie.getData('email')); // $ > bla@bla.com
    });
````


See {@link Model Model API} and {@link Instance Instance API} for more information.