Coder Social home page Coder Social logo

cachedb-node's Introduction

Node.js client for Cachedb

Installing the client library

npm install @cachedb-node/cachedb

Using the client library

// Imports the CacheDB client library
const cachedb = require('@cachedb-node/cachedb');

/*
const cdbport = 9898;
const cdbuser = 'cachedb';
const cdbpass = 'cachedb';
*/

// Creates a client
const cdb = new cachedb(cdbport, cdbuser, cdbpass);
const sets = cdb.sets;
const lists = cdb.lists;

Sets API

Set

Insert a key value pair

set(key,value,ttl?) ttl : time to live, in seconds

sets.set('foo','bar').then(data=>{
    console.log(data); // OK
}).catch(err){
    console.log(err);
}

// Or

sets.set('foo','bar',60) //stores the value for 60 seconds
  .then(console.log) // OK
  .catch(console.log)

// Or

const setFoo = async () => {
  try {
	let resp = await sets.set('foo', 'bar');
	console.log(resp);   // OK
  }catch (error) {
	console.log(error);.
  }
}
setFoo()

Get

Get a value corresponding to a particlar key.

get(key)

sets.get('foo').then(data=>{
    console.log(data); // bar
}).catch(err){
    console.log(err);
}

// Or

sets.get('foo')
  .then(console.log) // bar
  .catch(console.log)

// Or

const getFoo = async () => {
  try {
	let resp = await sets.get('foo');
	console.log(resp);   // bar
  }catch (error) {
	console.log(error);.
  }
}
getFoo()

Del

Delete a particular key-value pair

del(key)

sets.del('foo').then(data=>{
    console.log(data); // OK
}).catch(err){
    console.log(err);
}

// Or

sets.del('foo')
  .then(console.log) // OK
  .catch(console.log)

// Or

const getFoo = async () => {
  try {
	let resp = await sets.del('foo');
	console.log(resp);   // OK
  }catch (error) {
	console.log(error);.
  }
}
detFoo()

Lists API

Append

Inserts the data to the end of the list.

append(listName, value)

const appendData = async () => {
  try {
	let resp = await lists.append('foo','bar');
	console.log(resp);   // OK ... data 'bar' is added to the list 'foo'
  }catch (error) {
	console.log(error);.
  }
}
appendData()

Prepend

Inserts the data to the front of the list.

prepend(listName, value)

const prependData = async () => {
  try {
	let resp = await lists.prepend('foo','bar');
	console.log(resp);   // OK ... data 'bar' is added to the front of list 'foo'
  }catch (error) {
	console.log(error);.
  }
}
prependData()

Values

Returns the entire list as an Array.

values(listName)

const getList = async () => {
  try {
	let resp = await lists.values('foo');
	console.log(resp);   // ['bar','bar']
  }catch (error) {
	console.log(error);.
  }
}
getList()

Remove Last

Removes and returns the data from the end of the list.

removeLast(listName)

const pollLast = async () => {
  try {
	let resp = await lists.removeLast('foo');
	console.log(resp);   // 'bar'
  }catch (error) {
	console.log(error);.
  }
}
pollLast()

Remove First

Removes and returns the data from the end of the list.

removeFirst(listName)

const pollFirst = async () => {
  try {
	let resp = await lists.removeFirst('foo');
	console.log(resp);   // 'bar'
  }catch (error) {
	console.log(error);.
  }
}
pollFirst()

cachedb-node's People

Contributors

touristt avatar thetinygoat avatar

Watchers

James Cloos avatar

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.