Coder Social home page Coder Social logo

node-couchbase-crud's Introduction

node-couchbase-crud

Lightweight node client for Couchbase CRUD operations

NPM JS: https://www.npmjs.com/package/node-couchbase-crud

EXAMPLE CODE USAGE OF METHODS INSERT(POST), UPDATE(PUT), SELECT(GET), DELETE(DELETE)

var express=require('express');
var app = express();
var apiRoute = express.Router();

var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  res.header("Access-Control-Allow-Methods", 'POST, GET, PUT, DELETE, OPTIONS');
  next();
});

//NODE COUCHBASE CRUD MODULE REQUIRED
var couchbaseConnector = require('node-couchbase-crud');

//TEST CONFIG JSON OBJECT
var config = {
    "couchbase_cluster_url":"couchbase://TEST URL",
    "couchbase_bucket_name":"BUCKET_NAME",
    "couchbase_view_name":"ALL_BUCKET_DATA_VIEW_NAME"
}
//CALLING CONSTRUCTOR WITH CONFIGURATION DETAILS
var couchRequest = new couchbaseConnector(config);


/* FOR GETTING ALL DOCUMENTS, YOU'LL NEED TO CREATE A PUBLISHED PRODUCTION VIEW AND PASS THE NAME IN 
CONFIG COUCHBASE_VIEW_NAME, VIEW FUNCTION BELOW

    function (doc, meta) {
        emit(meta.id, doc);
    }
*/
apiRoute.get("/allDocuments", function(req, res){
    //get all documents
    couchRequest.getDocument(null, function(result){
        return res.json(result);
    });
});

//INSERTING NEW DOCUMENT
apiRoute.post("/document", function(req, res){
    couchRequest.upsertDocument(null, req.body.document_body,function(result){
        return res.json(result);
    });
});

//UPDATING EXISTING DOCUMENT
apiRoute.put("/document", function(req, res){
    couchRequest.upsertDocument(req.body.document_id, req.body.document_body,function(result){
        return res.json(result);
    });
});
//GET DOCUMENT BY ID AND GETTING ALL DOCUMENTS.
apiRoute.get("/document", function(req, res){
    //get document by id
    if(req.query.document_id){
        couchRequest.getDocument(req.query.document_id, function(result){
            return res.json(result);
        });
    }
    //getting all documents
    couchRequest.getDocument(null, function(result){
        return res.json(result);
    });

});

//DELETING DOCUMENT BY ID
apiRoute.delete("/document", function(req,res){
    //delete document by ID
    couchRequest.deleteDocument(req.query.document_id, function(result){
        return res.json(result);
    });
});

var port = process.env.port || 8080;
app.use("/", apiRoute);
app.listen(port);
console.log("Server started at port :: "+port);

node-couchbase-crud's People

Contributors

aakashthakkar avatar

Watchers

Ken Smith avatar 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.