Code Monkey home page Code Monkey logo

nativescript-couchbase's Introduction

Couchbase Lite Plugin for Telerik NativeScript

Couchbase Lite is a NoSQL embedded database for mobile devices. It is a replacement for common database technologies like SQLite and Core Data.

Configuration

To add this plugin to your Angular 2 or vanilla JavaScript NativeScript project, execute the following from the Terminal or Command Prompt:

tns plugin add nativescript-couchbase

If you wish to try either of the demo applications that are bundled with this project, execute the following after cloning the repository:

npm run setup
npm run prepare
npm run android-angular

For the third line, the list of options are:

npm run android-angular
npm run android-vanilla
npm run ios-angular
npm run ios-vanilla

If you're using TypeScript and wish to make use of the type definitions for this plugin, add the following line to your project's references.d.ts file:

/// <reference path="./node_modules/nativescript-couchbase/couchbase.d.ts" />

Without the above line included, your TypeScript compiler may throw errors during the build.

Usage

Including the Plugin in Your Project

var couchbaseModule = require("nativescript-couchbase");

Creating or Opening an Existing Database

var database = new couchbaseModule.Couchbase("test-database");

Managing the Data with CRUD Operations

Creating a New Document

var documentId = database.createDocument({
    "firstname": "Nic",
    "lastname": "Raboy",
    "address": {
        "city": "San Francisco",
        "state": "CA",
        "country": "USA"
    }
    "twitter": "https://www.twitter.com/nraboy"
});

Retrieving an Existing Document

var person = database.getDocument(documentId);

Updating an Existing Document

database.updateDocument(documentId, {
    "firstname": "Nicolas",
    "lastname": "Raboy",
    "twitter": "https://www.twitter.com/nraboy"
});

Deleting a Document

var isDeleted = database.deleteDocument(documentId);

Querying with MapReduce Views

Knowing the document id isn't always an option. With this in mind, multiple documents can be queried using criteria defined in a view.

Creating a MapReduce View

A MapReduce View will emit a key-value pair. Logic can be placed around the emitter to make the views more specific.

database.createView("people", "1", function(document, emitter) {
    emitter.emit(document._id, document);
});

Querying a MapReduce View

var rows = database.executeQuery("people");
for(var i = 0; i < rows.length; i++) {
    personList.push(rows[i]);
}

Synchronization with Couchbase Sync Gateway and Couchbase Server

Couchbase Lite can work in combination with Couchbase Sync Gateway to offer synchronization support between devices and platforms. Couchbase Sync Gateway is not a requirement to use Couchbase Lite if the goal is to only use it for offline purposes.

Couchbase Sync Gateway can be downloaded via the Couchbase Downloads Portal in the mobile section.

A demo configuration file for Sync Gateway is included in the demo directory. It can be started by executing the following from a Command Prompt or Terminal:

/path/to/sync/gateway/bin/sync_gateway /path/to/demo/sync-gateway-config.json

In the demo configuration file, Couchbase Server is not used, but instead an in-memory database good for prototyping. It can be accessed via http://localhost:4985/_admin/ in your web browser.

To replicate between the local device and server, the following must be added to your project:

var couchbaseModule = require("nativescript-couchbase");
database = new couchbaseModule.Couchbase("test-database");

var push = database.createPushReplication("http://sync-gateway-host:4984/test-database");
var pull = database.createPullReplication("http://sync-gateway-host:4984/test-database");
push.setContinuous(true);
pull.setContinuous(true);
push.start();
pull.start();

Data will now continuously be replicated between the local device and Sync Gateway.

Listening for Changes

database.addDatabaseChangeListener(function(changes) {
    for(var i = 0; i < changes.length; i++) {
        console.log(changes[i].getDocumentId());
    }
});

nativescript-couchbase's People

Contributors

nraboy avatar mehfuzh avatar nathanwalker avatar triniwiz avatar sebawita avatar tjvantoll avatar

Watchers

James Cloos avatar Matthias Spohn 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.