API Docs for: 0.2.1
Show:

Hook.Collection Class

Module: Hook

Methods

avg

(
  • field
  • callback
)
Promise

Aggregate field with 'avg' values

Parameters:

  • field String
  • callback Function

    [optional]

Returns:

Promise:

Example:

Get the average value from highscore collection

client.collection('highscore').avg('score', function(data) {
  console.log("avg: ", data);
});

channel

(
  • options
)
Hook.Channel

Get channel for this collection.

Parameters:

  • options Object

    (optional)

Returns:

Hook.Channel:

Example:

Streaming collection data

client.collection('messages').where('type', 'new-game').channel().subscribe(function(event, data) {
  console.log("Received new-game message: ", data);
});

client.collection('messages').create({type: 'sad', text: "i'm sad because streaming won't catch me"});
client.collection('messages').create({type: 'new-game', text: "yey, streaming will catch me!"});

clone

() Collection

Return a new copy of the collection.

Returns:

Collection:

count

(
  • callback
)
Promise

Count the number of items on this collection

Parameters:

  • callback Function

    [optional]

Returns:

Promise:

Example:

Count the elements of the current query

client.collection('posts').where('author','Vicente').count(function(total) {
  console.log("Total:", total);
});

create

(
  • data
)
Hook.Collection

Create a new resource

Parameters:

  • data Object

Returns:

Example:

Creating an entry

client.collection('posts').create({
  title: "Post name",
  summary: "My awesome new post",
  stars: 5
});

Listening to complete event

// Verbose way
var c = client.collection('posts');
var promise = c.create({ title: "Post name", summary: "Something", stars: 5 });
promise.then(function(data) {
    console.log(data);
});

// Short way
client.collection('posts').create({ title: "Post name", summary: "Something", stars: 5 }).then(function(data) {
    console.log(data);
});

debug

() Promise

Alias for then & console.log.bind(console)

Returns:

Promise:

decrement

(
  • field
  • value
)
Promise

Decrement a value from 'field' from all rows matching current filter.

Parameters:

  • field String
  • value Number

Returns:

Promise:

Example:

Decrement user score

client.collection('users').where('_id', user_id).decrement('score', 10).then(function(numRows) {
  console.log(numRows, " users has been updated");
});

distinct

(
  • field
  • ...
)
Hook.Collection

The 'distinct' can be used to return only distinct (different) values.

Parameters:

  • field String
  • ... String

    more fields

Returns:

find

(
  • _id
  • callback
)
Promise

Find first item by _id

Parameters:

  • _id Number
  • callback Function

    [optional]

Returns:

Promise:

Example:

Finding first item by _id, with 'success' callback as param.

client.collection('posts').find(50, function(data) {
  console.log("Row:", data);
});

Catching 'not found' error.

client.collection('posts').find(128371923).then(function(data) {
  console.log("Row:", data); // will never execute this
}).otherwise(function(e) {
  console.log("Not found.");
});

first

(
  • callback
)
Promise

Query only the first result

Parameters:

  • callback Function

    [optional]

Returns:

Promise:

Example:

Return just the first element for current query

client.collection('users').sort('created_at', -1).first(function(data) {
  console.log("Last created user:", data);
});

firstOrCreate

(
  • data
  • callback
)
Promise

First or create

Parameters:

  • data Object
  • callback Function

Returns:

Promise:

example Return the first match for 'data' param, or create it.

client.collection('uniques').firstOrCreate({type: "something"}).then(function(data) {
  console.log("Unique row: ", data);
});

get

() Hook.Collection

Get collection data, based on where params.

Returns:

group

(
  • field
  • ...
)
Hook.Collection

Group results by field

Parameters:

  • field String
  • ... String

    more fields

Returns:

increment

(
  • field
  • value
)
Promise

Increment a value from 'field' from all rows matching current filter.

Parameters:

  • field String
  • value Number

Returns:

Promise:

Example:

Increment user score

client.collection('users').where('_id', user_id).increment('score', 10).then(function(numRows) {
  console.log(numRows, " users has been updated");
});

join

(
  • ...
)
Hook.Collection

Set the relationships that should be eager loaded.

Parameters:

  • ... String

Returns:

Example:

Simple relationship

client.collection('books').join('author').each(function(book) {
  console.log("Author: ", book.author.name);
});

Multiple relationships

client.collection('books').join('author', 'publisher').each(function(book) {
  console.log("Author: ", book.author.name);
  console.log("Publisher: ", book.publisher.name);
});

Nested relationships

client.collection('books').join('author.contacts').each(function(book) {
  console.log("Author: ", book.author.name);
  console.log("Contacts: ", book.author.contacts);
});

limit

(
  • int
)
Hook.Collection

Parameters:

  • int Number

Returns:

Example:

Limit the number of rows to retrieve

client.collection('posts').sort('updated_at', -1).limit(5).then(function(data) {
  console.log("Last 5 rows updated: ", data);
});

Limit and offset

client.collection('posts').sort('updated_at', -1).limit(5).offset(5).then(function(data) {
  console.log("last 5 rows updated, after 5 lastest: ", data);
});

max

(
  • field
  • callback
)
Promise

Aggregate field with 'max' values

Parameters:

  • field String
  • callback Function

    [optional]

Returns:

Promise:

Example:

Get the max value from highscore collection

client.collection('highscore').max('score', function(data) {
  console.log("max: ", data);
});

min

(
  • field
  • callback
)
Promise

Aggregate field with 'min' values

Parameters:

  • field String
  • callback Function

    [optional]

Returns:

Promise:

Example:

Get the min value from highscore collection

client.collection('highscore').min('score', function(data) {
  console.log("min: ", data);
});

offset

(
  • int
)
Hook.Collection

Parameters:

  • int Number

Returns:

orWhere

(
  • where
  • operation
  • value
)
Hook.Collection

Add OR query param

Parameters:

  • where Object | String

    params or field name

  • operation String

    '<', '<=', '>', '>=', '!=', 'in', 'between', 'not_in', 'not_between', 'like', 'not_null'

  • value String

    value

Returns:

remember

(
  • minutes
)
Hook.Collection

Indicate that the query results should be cached.

Parameters:

  • minutes Number

Returns:

Example:

Caching a query

client.collection('posts').sort('updated_at', -1).limit(5).remember(10).then(function(data) {
  // ...
});

remove

(
  • id
)
Promise

Remove a single row by id

Parameters:

  • id String

    [optional]

Returns:

Promise:

Example:

Deleting a row by id

client.collection('posts').remove(1).then(function(data) {
  console.log("Success:", data.success);
});

Deleting multiple rows

client.collection('ranking').where('score', 0).remove().then(function(data) {
  console.log("Success:", data.success);
});

reset

() Hook.Collection

Clear collection filtering state

Returns:

select

() Hook.Collection

Fields that should be retrieved from the database

Returns:

sort

(
  • field
  • direction
)
Hook.Collection

Parameters:

  • field String
  • direction Number | String

Returns:

Example:

Return just the first element for current query

// Ommit the second argument for ascending order:
client.collection('users').sort('created_at').then(function(data){ });

// Use 1 or 'asc' to specify ascending order:
client.collection('users').sort('created_at', 1).then(function(data){  });
client.collection('users').sort('created_at', 'asc').then(function(data){  });

// Use -1 or 'desc' for descending order:
client.collection('users').sort('created_at', -1).then(function(data) {  });
client.collection('users').sort('created_at', 'desc').then(function(data) {  });

sum

(
  • field
  • callback
)
Promise

Aggregate field with 'sum' values

Parameters:

  • field String
  • callback Function

    [optional]

Returns:

Promise:

Example:

Get the sum value from highscore collection

client.collection('highscore').sum('score', function(data) {
  console.log("sum: ", data);
});

then

() Promise

Alias for get & then

Returns:

Promise:

update

(
  • _id
  • data
)

Update a single collection entry

Parameters:

  • _id Number | String
  • data Object

Example:

Updating a single row

client.collection('posts').update(1, { title: "Changing post title" }).then(function(data) {
  console.log("Success:", data.success);
});

updateAll

(
  • data
)
Promise

Update all collection's data based on where params.

Parameters:

  • data Object

    key-value data to update from matched rows [optional]

Returns:

Promise:

Example:

Updating all rows of the collection

client.collection('users').updateAll({category: 'everybody'}).then(function(numRows) {
  console.log(numRows, " users has been updated");
});

Updating collection filters

client.collection('users').where('age','<',18).updateAll({category: 'baby'}).then(function(numRows) {
  console.log(numRows, " users has been updated");
});

where

(
  • where
  • operation
  • value
)
Hook.Collection

Add where param

Parameters:

  • where Object | String

    params or field name

  • operation String

    '<', '<=', '>', '>=', '!=', 'in', 'between', 'not_in', 'not_between', 'like', 'not_null'

  • value String

    value

Returns:

Example:

Multiple 'where' calls

var c = client.collection('posts');
c.where('author','Vicente'); // equal operator may be omitted
c.where('stars','>',10);     // support '<' and '>' operators
c.then(function(result) {
  console.log(result);
});

One 'where' call

client.collection('posts').where({
  author: 'Vicente',
  stars: ['>', 10]
}).then(function(result) {
  console.log(result);
})

Filtering 'in' value list.

client.collection('posts').where('author_id', 'in', [500, 501]).then(function(result) {
  console.log(result);
})

Partial String matching

client.collection('posts').where('author', 'like', '%Silva%').then(function(result) {
  console.log(result);
})