Model Class¶
Please note: If you are using custom-built tables, you must have an id column!
model.get( id, options, callback )¶
id is the id of the object you are wishing to get
options is an optional object of options which uses the same flags as the orm.define()
function in ORM Class.
callback is a function which takes two parameters which are error
and instance
. Instance is an Instance Class.
model.find( ... )¶
This function takes the following parameters
- conditions, Object: The conditions you wish to search by
- options, object: The options of the query, Optional;
- limit, Integer: The maximum number of records to return per-query
- ordering, list: Ordering. This will generally contain 2 elements. The first is the field to sort by and the second is the method. This is
A
for Ascending order andZ
(default) descending order. Optional - callback, function: Called when the data is available. Takes two parameters:
error
anditems
(items is an array of Instance Class )
Conditions is an object which is quite limited at the moment. You can include a value to compare with (=
comparator) or a list (IN
comparator). See More
Options can contain more including (all of which are optional):
- __merge which joins two tables together and contains two values of
from
andto
which each contain atable
andfield
value - offset: Integer, the number to start loading data from
- limit: Integer, the number to limit to per query.
- only: List, a list of properties that you wish to contain if you want to restrict them
model.hasOne( type, another_model, opts )¶
Relates this item to another.
- type, String: What relationship does the current item have to another_model
- another_model, Model: Model to relate to. Optional, if ommited it defaults to itself.
- opts, Object: Options to apply to the relationship. Optional
The options available to customize are: * reverse If you add this you must provide it with a value to add to the other model to get it’s parent. For more information see ../relationships
For example:
var Person = db.define('person', {
name : String
});
var Animal = db.define('animal', {
name : String
});
Animal.hasOne("owner", Person);
With this example it assumes animal
has a field called owner_id
which is an Integer.
If you have enabled autoFetch
, then instances will have a type property with the other model instance. Otherwise you will have a function with the name of get*type* (although type is capitalized for CammelCase typing).
Note
If you are using @kennydude’s version the following applies:
This will also attach a reverse-lookup function to your model with the name of model.findBy type ( other_model, extra, callback )
Where extra is optional, and callback takes 2 arguments, error`
and item
(a Instance Class )
model.hasMany( type, extra, another_model )¶
Relates this model to another in a many-to-many fashion.
- type, String: What relationship does the current item have to another_model
- extra, Object: Extra attributes on the intermediate table you want to include. Optional
- another_model, Model: Model to relate to. Optional, if ommited it defaults to itself.
For example:
var Person = db.define('person', {
name : String
});
Person.hasMany("friends", {
rate : Number
});
Person.get(123, function (err, John) {
John.getFriends(function (err, friends) {
// assumes rate is another column on table person_friends
// you can access it by going to friends[N].extra.rate
});
});
You require an intermediate table with relationshipType_id and anotherModelName_id fields at least. The table is called thisModelName_type. For the above you would have a table called person_friends with the fields friend_id and person_id.
Note
If you are using @kennydude’s version the following applies:
This will also attach a reverse-lookup function to your model with the name of model.findBy type ( other_model, extra, callback )
Where extra is optional, and callback takes 2 arguments, error`
and items
(array of Instance Class )
model.createSQL()¶
Returns a string with the CREATE TABLE
syntax that should be used for the current database.
Note
If the current database is non-relational this may not return anything (“tables” are created on-demand)