cloudant-toolkit
is a wrapper library for cloudant
. this library overrides some method implementation of cloudant
please refer to overridden methods section for more details.
npm install cloudant-toolkit
var Cloudant = require('cloudant-toolkit');
var db;
Cloudant(connectionString , function(err, cloudant){
if(err)
throw err;
db = cloudant.use('your-table')
});
executable methods are methods that will immediately executed against the query
. See Query methods sections.
load data with build up query. if no query is build, it will return data with {_id:{$gt:0}}
as criteria.
returns Promise
db
.execute()
.then(docs=>{
// Process your docs here.
})
.catch(e=>{
// Handle your error here.
});
get a single data, throw exception if no data returned or the returned data is more than one. If no query is build, it will use{_id:{$gt:0}}
as selector.
parameters
- selector - optional,
cloudant
[selector syntax][selector-syntax].
returns Promise
db
.single({_id: someId})
.then(docs=>{
// Process your docs here.
})
.catch(e=>{
// Handle your error here.
});
get a single data, throw exception if n returned data is more than one. If no query is build, it will use{_id:{$gt:0}}
as selector.
parameters
- selector - optional,
cloudant
[selector syntax][selector-syntax].
returns Promise
db
.single({_id: someId})
.then(docs=>{
// Process your docs here.
})
.catch(e=>{
// Handle your error here.
});
get first data, throw exception if no data returned or the returned data is more than one. If no query is build, it will use{_id:{$gt:0}}
as selector.
parameters
- selector - optional,
cloudant
[selector syntax][selector-syntax].
returns Promise
db
.first({_id: {$gt:0}})
.then(docs=>{
// Process your docs here.
})
.catch(e=>{
// Handle your error here.
});
get first data, throw exception if n returned data is more than one. If no query is build, it will use{_id:{$gt:0}}
as selector.
parameters
- selector - optional,
cloudant
[selector syntax][selector-syntax].
returns Promise
db
.firstOrDefault({_id: {$gt:0}})
.then(docs=>{
// Process your docs here.
})
.catch(e=>{
// Handle your error here.
});
these methods returns Db
object. you can chain these methods to build up your query and then call execute
method to get the result.
query data with selector
parameters
- selector -
cloudant
[selector syntax][selector-syntax] criteria to query.
returns Db
db
.where({_id:{$gt:0}})
.execute()
skip the first N result.
paramete rs
- offset - offset to start return data from.
returns Db
db
.where({_id:{$gt:0}})
.skip(10)
.take(10)
.execute()
set number of results to return.
parameters
- size - number of results to get
returns Db
db
.where({_id:{$gt:0}})
.skip(10)
.take(10)
.execute()
data pagination, shorthand for skip
and take
.
parameters
- page - 1 based index.
- size - number of results to get
returns Db
db
.where({_id:{$gt:0}})
.page(2 , 10)
.execute()
sort results, please refer to [sort syntax][sort-syntax] for more detail.
parameters
- sort -
cloudant
sort syntax.
returns Db
db
.where({_id:{$gt:0}})
.page(2 , 10)
.execute()
return result with specified fields.
parameters
- fields - array of string.
returns Db
db
.where({_id:{$gt:0}})
.page(2 , 10)
.select(['id', 'name', 'age'])
.execute()
return newly created document _id
.
parameters
- doc - document to be inserted.
returns
Promise, resolving {_id, _rev}
of inserted document.
db
.insert(doc)
.then(result=>{
//result is newly created document `_id` and `_rev`
})
return updated document {_id, _rev}
.
parameters
- doc - document to be updated, must contains
_id
and_rev
fields
returns
Promise, resolving {_id, _rev}
of updated document.
db
.update(doc)
.then(result=>{
//result is updated document `_id` and `_rev`
})
return deleted document {_id, _rev}
.
parameters
- doc - document to be deleted, must contains
_id
and_rev
fields
returns
Promise, resolving {_id, _rev}
of deleted document.
db
.delete(doc)
.then(result=>{
//result is updated document `_id` and `_rev`
})
here is a list of overridden methods by this library. you can always use cloudant
implementation by prefixing the method with underscore _
- insert - use _insert to use
cloudant
implementation.
- [cloudant][cloudant] [cloudant]:https://github.com/cloudant/nodejs-cloudant [selector-syntax]:https://docs.cloudant.com/cloudant_query.html#selector-syntax [sort-syntax]:https://docs.cloudant.com/cloudant_query.html#sort-syntax