The api of canner.
Installation
$ npm install --save canner-api
or
<script src="https://static.canner.io/canner-api/stable/sdk.js"></script>
Usage
import CannerApi from 'canner-api';
const db = new CannerApi(<api key>).connect();
db.array('article')
.find({title: 'Today is a good day.'})
.exec((err, docs) {
// ...
});
db.object('author')
.get('name')
.exec((err, docs) {
// ...
});
API
- Type
- exec
- Operators
- Conditions (Only for array)
- Options (Only for array)
Type
There are two data formats to choose. Each one of them offer the serveral methods to do operation on it.
array
Declares the specific array of the data.
db.array('posts');
object
Declares the specific object of the data.
db.object('info');
exec
Executes the request. If callback is not given, return a Promise
;
db.array('posts')
.find({title:'Today is a good day.'});
.exec(callback);
db.array('posts')
.find({title:'Today is a good day.'});
.exec()
.then(callback)
.catch(errHandler);
Operators
find
Only for array. Declares this request a find operator. Executed when give a callback.
db.array('posts')
.find()
db.array('posts')
.find({title: 'Today is a good day.'})
db.array('posts')
.find({title: 'Today is a good day.'}, function(err, docs) {
// docs = [
// {title: 'Today is a good day.'}
// ]
// ...
});
findOne
Only for array. Declares this request a findOne operator. Executed when give a callback. Return the first matched data.
db.array('posts')
.findOne()
db.array('posts')
.findOne({title: 'Today is a good day.'})
db.array('posts')
.findOne({title: 'Today is a good day.'}, function(err, doc) {
// doc = {
// title: 'Today is a good day.'
// }
// ...
});
Delete
Only for array. Declares this request a delete operator. Executed when give a callback.
db.array('posts')
.delete('<postId>')
db.array('posts')
.delete('<postId>', function(err) {
if (err) {
// do something
}
});
Update
Only for array. Declares this request a update operator. Executed when give a callback.
db.array('posts')
.update('<postId>', {title: 'This is a good day!'})
db.array('posts')
.update('<postId>', {title: 'This is a good day!'}, function(err) {
// ...;
});
Create
Only for array. Declares this request a create operator. Executed when give a callback.
db.array('posts')
.create({title: 'This is a good day.', content: 'yes'})
db.array('posts')
.create({title: 'This is a good day.', content: 'yes'}, function(err, docs) {
// ...;
// return the docs you given with its _id
});
Set
Only for object. Declares this request a set operator. Executed when give a callback.
db.object('person')
.set('name', 'canner-tools!')
db.object('person')
.set('info/city', 'Taipei', function(err) {
// ...;
});
Get
Only for object. Declares this request a get operator. Executed when give a callback.
db.object('person')
.get('', function(err, doc) {
// doc = {
// name: 'lee'
// }
})
db.object('person')
.get('name', function(err, doc) {
// doc = 'lee'
// ...;
});
Conditions
Only for array.
where
Specifies a path
for adding one or one more conditions.
db.array("posts")
.where({'title', 'title'})
.find(callback)
db.array('post').find()
.where({date: {$gt: '2017/05/04'}})
db.array('post').find()
.where('date').gt('2017/05/04')
.where('date').lt('2017/05/08')
.exec(callback)
gt
Specifies $gt
condition.
db.array("posts")
.where('viewer').gt(15)
gte
Specifies $gte
condition.
db.array("posts")
.where('viewer').gte(15)
lt
Specifies $lt
condition.
db.array("posts")
.where('viewer').lt(150)
lte
Specifies $lte
condition.
db.array("posts")
.where('viewer').lte(150)
equals
Specifies $eq
condition.
db.array("posts")
.where('viewer').equals(15)
db.array("posts")
.where('viewer').eq(15)
contains
Specifies $contains
condition.
// posts: [{
// title: 'this is a title',
// tags: ['tiger', 'elephant', ...]
// }]
db.array("posts")
.where('tags').contains('tiger')
in
Specifies $in
condition.
// posts: [{
// title: 'this is a title',
// tag: 'tiger'
// }]
db.array("posts")
.where('tag').in(['tiger', 'elephant', 'dog'])
exists
Specifies $exists
condition.
// posts: [{
// title: 'post1',
// content: 'content',
// ahthor: 'Lee'
// }, {
// title: 'post2',
// content: 'content',
// }]
// find the posts which have the author field
db.array("posts")
.where('author').exists()
Options
Only for array.
limit
Specifies the limit
option.
db.array("posts")
.find().limit(10)
skip
Specifies the skip
option.
db.array("posts")
.find().skip(10)
sort
Specifies the sort
option.
db.array("posts")
.find().sort(10)
page
Specifies the page
option.
use with perPage
db.array("posts").find().page(2)
.exec(function(err, docs) {
// docs = {
// data: [...],
// totalPage: 8
// }
});
perPage
Specifies the perPage
option.
use with page
db.array("posts").find().perPage(10)
.exec(function(err, docs) {
// docs = {
// data: [...],
// totalPage: 8
// }
});
License
Apache-2.0 © abz53378