-
Notifications
You must be signed in to change notification settings - Fork 1
Model
-
db
pgo.Db instance.
-
fields
Fields collection.
If primary key is not set, it will be automatically set to
idfield.
Example:
var User = new pgo.model(db, {
"name": pgo.types.Str(),
"birthdate": pgo.types.Datetime(),
"about": pgo.types.Text()
});
See pgo.types
Find rows according to query conditions and options.
-
conditions
Query conditions.
-
Equality:
{"name": "username"} name = 'username' {"id": [1, 2, 3, 4]} id in (1, 2, 3, 4) {"$and": {"id": 1, "name": "username"}} {"id": 1, "name": "username"} id=1 and name='username' -
Inequality:
{"$not": {"name": "username"}} name != 'username' {"$not": {"id": [1, 2, 3, 4]}} id not in (1, 2, 3, 4) -
Disjunction:
{"$or": {"name": "username", "id": 1}} name = 'username' or id=1 -
Conditional operators:
{"$lt": {"age": 29}} age < 29 {"$gt": {"birthdate": "1982-01-23"}} birthdate > '1982-01-23'
Of course, you can combine them.
-
-
options
Query options.
-
Limit/offset:
{"limit": 10} limit 10 {"offset": 100} offset 100 -
Rows order:
{"order": "birthdate"} order by birthdate ascor
{"order": "-birthdate"} order by birthdate descfor ordering by multiple columns:
{"order": ["-birthdate", "name"]} order by birthdate desc, name asc
-
Returns EventEmitter instance.
-
row
function(row){}Emitted when a row is received
-
end
function(){}Emitted when the query is finished.
-
error
function(error){}Emitted when any error occurs.
Example:
Post.find({
$or: {user_id:222, id: [10, 23, 117]},
$gt: {created: '2011-04-01'}
},
{limit: 10})
.on('row', function(post){
console.log(post.id+'\t'+post.text);
})
.on('end', function(){
console.log('posts end');
})
.on('error', function(error){
console.log('Error:', error);
});
Similar to find() method, but gets a single row.
-
conditions
Query conditions. See find().
Returns EventEmitter instance.
-
end
function(row){}Emitted when the row is received.
-
error
function(error){}Emitted when any error occurs.
-
conditions
Query conditions. See find().
Returns EventEmitter instance.
-
end
function(cnt){}Emitted when the row is received.
-
error
function(error){}Emitted when any error occurs.
-
row
Either a pgo.Row instance or plain javascript object with needed values.
Returns EventEmitter instance.
-
object
Either a pgo.Row instance or conditions object (see find() method description).
Returns EventEmitter instance.
-
end
function(){}Emitted when the row is deleted.
-
error
function(error){}Emitted when any error occurs.