ColdMVC

ORM

Overview

Note: some of the methods listed below can accept an options argument, which can be used for pagination and sorting.

var books = _Book.list({
    sort = "title",
    order = "asc",
    max = "10",
    offset = "20"
});

count

Returns the total number of records.

var count = _Book.count();

countBy

Returns the number of records with matching parameters. Uses dynamic finders via onMissingMethod().

var count = _Book.countByTitle("The Da Vinci Code");

countWhere

Returns the number of records with matching parameters.

var count = _Book.countWhere({
    title = "The Da Vinci Code"
});

delete

Deletes a record from the database.

var book = _Book.get(id);

book.delete();

exists

If called on a singleton model, checks to see if a record exists with a certain ID.

if (_Book.exists(12)) {
    // the book with ID "12" exists in the database
}

If called on an instance of a model, checks to see if the model has already been stored in the database and has an ID.

var book = _Book.get(12);

if (book.exists()) {
    // the book with ID "12" already exists in the database
}

find

Executes a query and returns either the first result or a new instance of a model if no results are found.

var book = _Book.find("from Book where book.title = :title", {
    title = "The Da Vinci Code"
});

findAll

Executes a query and returns an array of records.

var books = _Book.findAll("from Book where book.title like :title", {
    title = "The Da Vinci Code"
});

findAllBy

Returns an array of records with matching parameters. Uses dynamic finders via onMissingMethod().

var books = _Book.findAllByTitle("The Da Vinci Code");

findAllWhere

Returns an array of records with matching parameters.

var books = _Book.findAllWhere({
    title = "The Da Vinci Code"
});

findBy

Returns a record with matching parameters or a new instance of a model if no results are found. Uses dynamic finders via onMissingMethod().

var book = _Book.findByTitle("The Da Vinci Code");

findWhere

Returns a record with matching parameters or a new instance of a model if no results are found.

var book = _Book.findWhere({
    title = "The Da Vinci Code"
});

get

Returns a record with a matching ID or a new instance of a model if no results are found. Optionally allows you to populate the result.

var book = _Book.get(1, {
    title = "The Da Vinci Code",
    author = "Dan Brown"
});

getAll

Returns an array of records within a comma-separated list or array of IDs. The results within the array will be in the same order as the IDs that were passed in unless a sort option is specified.

var books = _Book.getAll("1,2,3");

var books = _Book.getAll([1,2,3]);

list

Returns an array of records.

var books = _Book.list({
    sort = "title",
    order = "asc"
});

new

Creates a new instance of a model. Optionally allows you to populate the result.

var book = _Book.new({
    title = "The Da Vinci Code",
    author = "Dan Brown"
});

populate

Populates an instance of a model. Optionally specify a list of valid properties to populate.

var book = _Book.new();

book.populate({
    title = "The Da Vinci Code",
    author = "Dan Brown"
});

prop

Provides a multi-purpose getter/setter for properties.

If a value is specified, the property is set to the value and the instance of the model is returned.

var book = _Book.new();

book.prop("title", "The Da Vinci Code"); // setter

If a value is not specified, the current value of the property is returned.

var book = _Book.get(12);   

var title = book.prop("title"); // getter

save

Saves a record to the database.

var book = _Book.new({
    title = "The Da Vinci Code",
    author = "Dan Brown"
});

book.save();

validate

Validates a model and returns a validation result.

var book = _Book.new({
    title = "The Da Vinci Code",
    author = "Dan Brown"
});

var result = book.validate();

if (result.isValid()) {
    book.save();
}