Hook.Collection Class
Item Index
Methods
avg
-
field
-
callback
Aggregate field with 'avg' values
Parameters:
-
field
String -
callback
Function[optional]
Returns:
Example:
Get the average value from highscore collection
client.collection('highscore').avg('score', function(data) {
console.log("avg: ", data);
});
channel
-
options
Get channel for this collection.
Parameters:
-
options
Object(optional)
Returns:
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:
count
-
callback
Count the number of items on this collection
Parameters:
-
callback
Function[optional]
Returns:
Example:
Count the elements of the current query
client.collection('posts').where('author','Vicente').count(function(total) {
console.log("Total:", total);
});
create
-
data
Create a new resource
Parameters:
-
data
Object
Returns:
this
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:
decrement
-
field
-
value
Decrement a value from 'field' from all rows matching current filter.
Parameters:
-
field
String -
value
Number
Returns:
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
-
...
The 'distinct' can be used to return only distinct (different) values.
Parameters:
-
field
String -
...
Stringmore fields
Returns:
this
find
-
_id
-
callback
Find first item by _id
Parameters:
-
_id
Number -
callback
Function[optional]
Returns:
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
Query only the first result
Parameters:
-
callback
Function[optional]
Returns:
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
First or create
Parameters:
-
data
Object -
callback
Function
Returns:
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);
});
group
-
field
-
...
Group results by field
Parameters:
-
field
String -
...
Stringmore fields
Returns:
this
increment
-
field
-
value
Increment a value from 'field' from all rows matching current filter.
Parameters:
-
field
String -
value
Number
Returns:
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
-
...
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
Parameters:
-
int
Number
Returns:
this
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
Aggregate field with 'max' values
Parameters:
-
field
String -
callback
Function[optional]
Returns:
Example:
Get the max value from highscore collection
client.collection('highscore').max('score', function(data) {
console.log("max: ", data);
});
min
-
field
-
callback
Aggregate field with 'min' values
Parameters:
-
field
String -
callback
Function[optional]
Returns:
Example:
Get the min value from highscore collection
client.collection('highscore').min('score', function(data) {
console.log("min: ", data);
});
orWhere
-
where
-
operation
-
value
Add OR query param
Parameters:
-
where
Object | Stringparams or field name
-
operation
String'<', '<=', '>', '>=', '!=', 'in', 'between', 'not_in', 'not_between', 'like', 'not_null'
-
value
Stringvalue
Returns:
this
remember
-
minutes
Indicate that the query results should be cached.
Parameters:
-
minutes
Number
Returns:
this
Example:
Caching a query
client.collection('posts').sort('updated_at', -1).limit(5).remember(10).then(function(data) {
// ...
});
remove
-
id
Remove a single row by id
Parameters:
-
id
String[optional]
Returns:
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);
});
select
()
Hook.Collection
Fields that should be retrieved from the database
Returns:
this
sort
-
field
-
direction
Parameters:
-
field
String -
direction
Number | String
Returns:
this
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
Aggregate field with 'sum' values
Parameters:
-
field
String -
callback
Function[optional]
Returns:
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:
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
Update all collection's data based on where
params.
Parameters:
-
data
Objectkey-value data to update from matched rows [optional]
Returns:
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
Add where
param
Parameters:
-
where
Object | Stringparams or field name
-
operation
String'<', '<=', '>', '>=', '!=', 'in', 'between', 'not_in', 'not_between', 'like', 'not_null'
-
value
Stringvalue
Returns:
this
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);
})