Giter VIP home page Giter VIP logo

cloudant-toolkit's Introduction

cloudant-toolkit

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.


Installation

npm install cloudant-toolkit

Usage

var Cloudant = require('cloudant-toolkit');
var db;
Cloudant(connectionString , function(err, cloudant){
	if(err)
		throw err;
	db = cloudant.use('your-table')	
});

Executable methods

executable methods are methods that will immediately executed against the query. See Query methods sections.

execute()

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

single([selector])

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

singleOrDefault([selector])

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

first([selector])

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

firstOrDefault([selector])

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

Query methods

these methods returns Db object. you can chain these methods to build up your query and then call execute method to get the result.

where (selector)

query data with selector

parameters

  • selector - cloudant [selector syntax][selector-syntax] criteria to query.

returns Db

	db
		.where({_id:{$gt:0}})
		.execute()

skip (offset)

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()

take (size)

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()

page (page ,size)

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()

orderBy (sort)

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()

select (fields)

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()

Insert, Update, Delete

insert (doc)

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`
	})

update (doc)

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`
	})

delete (doc)

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`
	})

Overridden methods

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.

Dependencies

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.