Skip to content
artss edited this page Jul 3, 2011 · 11 revisions

new pgo.Model(db, fields)

  • db

    pgo.Db instance.

  • fields

    Fields collection.

    If primary key is not set, it will be automatically set to id field.

Example:

var User = new pgo.model(db, {
    "name": pgo.types.Str(),
    "birthdate": pgo.types.Datetime(),
    "about": pgo.types.Text()
});

See pgo.types

find(conditions[, options])

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 asc
      

      or

         {"order": "-birthdate"}                     order by birthdate desc
      

      for ordering by multiple columns:

         {"order": ["-birthdate", "name"]}           order by birthdate desc, name asc
      

Returns EventEmitter instance.

Events

  • 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);
});

get(conditions)

Similar to find() method, but gets a single row.

  • conditions

    Query conditions. See find().

Returns EventEmitter instance.

Events

  • end

      function(row){}
    

    Emitted when the row is received.

  • error

      function(error){}
    

    Emitted when any error occurs.

count(conditions)

  • conditions

    Query conditions. See find().

Returns EventEmitter instance.

Events

  • end

      function(cnt){}
    

    Emitted when the row is received.

  • error

      function(error){}
    

    Emitted when any error occurs.

add(row)

  • row

    Either a pgo.Row instance or plain javascript object with needed values.

Returns EventEmitter instance.

Events

del(object)

  • object

    Either a pgo.Row instance or conditions object (see find() method description).

Returns EventEmitter instance.

Events

  • end

      function(){}
    

    Emitted when the row is deleted.

  • error

      function(error){}
    

    Emitted when any error occurs.

Clone this wiki locally