DataObject

DataObject

Represents a data object associated with a data model. DataObject class may be inherited by other classes that are defined as DataObjectClass of a data model.

Constructor

new DataObject(typeopt, objopt)

Parameters:
Name Type Attributes Description
type string <optional>
obj * <optional>
The object that is going to be extended
Properties:
Name Type Description
context DataContext An instance of DataContext class associated with this object.
$$type string A string that represents the type of this object.
$$model DataModel The data model which is associated with this object.
$$id * Gets the identifier of this object based on the associated model's primary key
selectors * An object that represents a collection of selectors associated with this data object e.g is(':new'), is(':valid'), is(':enabled') etc
Source:

Extends

  • EventEmitter2

Methods

attr(name, callback)

Parameters:
Name Type Description
name String
callback function
Source:

attrOf(name, callbackopt) → {Promise.<T>|*}

Gets the value of the specified attribute. If the object has already a property with the specified name and the property does not have an association mapping then returns the property value. Otherwise if attribute has an association mapping (it defines an association with another model) then returns the foreign key value
Parameters:
Name Type Attributes Description
name string The name of the attribute to retrieve
callback function <optional>
A callback function where the first argument will contain the Error object if an error occured, or null otherwise. The second argument will contain the result.
Source:
Returns:
If callback is missing then returns a promise.
Type
Promise.<T> | *

getAdditionalObject() → {Promise.<DataObject>}

Gets an instance of data object which represents the additional typed object as this is defined in additionalType attribute.
Source:
Returns:
Type
Promise.<DataObject>
Example
//get a place and afterwards get the country associated with it
 var places = context.model("Place");
 places.silent().where("name").equal("France").first().then(function(result) {
    if (result) {
        var place = places.convert(result);
        return place.getAdditionalObject().then(function(result) {
            //place your code here
            return done();
        });
    }
    return done();
}).catch(function (err) {
    return done(err);
});

getId() → {*}

Gets the identifier of this DataObject instance.
Source:
Returns:
Type
*

getModel() → {DataModel|*}

Gets the associated data model
Source:
Returns:
Type
DataModel | *

getType() → {string}

Gets the type of this data object.
Source:
Returns:
Type
string

idOf() → {*}

Gets the identifier of this data object
Deprecated:
  • This function is deprecated. Use DataObject.$$id property instead
Source:
Returns:
Type
*

is(selector) → {Promise.<T>|*}

Executes a selector and returns the result. DataObject class has default selectors for common operations. The ":new" selector checks whether current data object is new or not. The ":live" selector checks whether current data object already exists or not.
Parameters:
Name Type Description
selector string A string that represents an already registered selector
Source:
Returns:
Type
Promise.<T> | *
Example
//retrieve a user, and execute :live selector
 var users = context.model('User');
 users.where('name').equal('admin@example.com')
 .first().then(function(result) {
        var user = users.convert(result);
        user.is(":live").then(function(result) {
            if (result) {
                console.log('User already exists');
            }
            done();
        }).catch(function(err) {
            done(null, err);
        });
    }).catch(function(err) {
        done(err);
    });

property(name) → {DataQueryable|HasManyAssociation|HasOneAssociation|DataObjectJunction|DataObjectTag|HasParentJunction|Object}

Parameters:
Name Type Description
name String The relation name
Source:
Returns:
Type
DataQueryable | HasManyAssociation | HasOneAssociation | DataObjectJunction | DataObjectTag | HasParentJunction | Object

query(attr) → {DataQueryable}

Gets a DataQueryable object that is going to be used in order to get related items.
Parameters:
Name Type Description
attr string A string that contains the relation attribute
Source:
Returns:
Type
DataQueryable

remove(contextopt, callbackopt) → {Promise.<T>|*}

Deletes the current data object.
Parameters:
Name Type Attributes Description
context DataContext <optional>
The current data context.
callback function <optional>
A callback function where the first argument will contain the Error object if an error occured, or null otherwise.
Source:
Returns:
- If callback parameter is missing then returns a Promise object.
Type
Promise.<T> | *
Example
//retrieve a order, and remove it
 var orders = context.model('Order');
 orders.where('id').equal(4)
 .first().then(function(result) {
        var order = orders.convert(result);
        order.remove().then(function() {
            done();
        }).catch(function(err) {
            done(err);
        });
    }).catch(function(err) {
        done(err);
    });

save(contextopt, callbackopt) → {Promise.<T>|*}

Saves the current data object.
Parameters:
Name Type Attributes Description
context DataContext <optional>
The current data context.
callback function <optional>
A callback function where the first argument will contain the Error object if an error occured, or null otherwise.
Source:
Returns:
- If callback parameter is missing then returns a Promise object.
Type
Promise.<T> | *
Example
//retrieve an order and change paymentDue date
 var orders = context.model('Order');
 orders.where('id').equal(46)
 .first().then(function(result) {
        var order = orders.convert(result);
        order.paymentDue = moment().add(7, 'days').toDate();
        order.save().then(function() {
            done(null, order);
        }).catch(function(err) {
            done(err);
        });
    }).catch(function(err) {
        done(err);
    });

selector(name, selectoropt)

Registers a selector for the current data object
Parameters:
Name Type Attributes Description
name string
selector function <optional>
Source:
Example
//retrieve a user, register a selector for enabled and check if user is enabled or not
 var users = context.model('User');
 users.where('name').equal('admin@example.com')
 .first().then(function(result) {
        var user = users.convert(result);
        //register a selector to check whether a user is enabled or not
        user.selector('enabled', function(callback) {
            this.$$model.where('id').equal(this.id).select('enabled').value(callback);
        });
        user.is(":enabled").then(function(result) {
            if (result) {
                console.log('User is enabled');
            }
            done();
        }).catch(function(err) {
            done(null, err);
        });
    }).catch(function(err) {
        done(err);
    });

silent(valueopt)

Sets a boolean which indicates whether the next data operation will be executed in silent mode.
Parameters:
Name Type Attributes Description
value boolean <optional>
Source:
Returns:
DataObject
Example
context.model("Person").where("email").equal("alexis.rees@example.com").getTypedItem()
        .then(function(person) {
            //...
            return person.silent().save().then(function() {
                return done();
            });
        }).catch(function(err) {
            return done(err);
        });