Giter Site home page Giter Site logo

foodige / nativescript-cblite-xl Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nabil-mansouri/nativescript-couchbaselite

0.0 1.0 0.0 43.32 MB

Full fetures couchbase lite plugin for nativescript

License: Apache License 2.0

TypeScript 26.14% Objective-C 73.46% C 0.05% Ruby 0.01% HTML 0.20% CSS 0.14%

nativescript-cblite-xl's Introduction

nativescript-couchbaselite

A nativescript plugin that brings you all couchbase features:

  • CRUD (with ttl...)
  • conflict management
  • CRUD attachment (file, image...)
  • View (map, mapreduce...)
  • Queries (full query features, live queries, all docs, group levels)
  • Replication (pull and push, filters, authentication, channels, listeners...)
  • Database encryption
  • Typescript mapping object (return objects with getters/setters...)

Author

  • Nabil MANSOURI paypal

How to use it

1- Import dependency

tns plugin add nativescript-couchbaselite

2- Samples

Import lib

import {
    QueryResult, LiveQuery, QueryListener, Revision,
    DatabaseManager, Document, Database, AttachmentFactory, Emitter, AttachmentImage
} from 'nativescript-couchbaselite';

class User implements Document {
    docId: string;
    docRev: string;
    docType: string = "USER";
    name: string;
    registerAt: number;
    secure: boolean = false;
    set registerAtDate(d: Date) {
        this.registerAt = d.getTime();
    }
    get registerAtDate() {
        return this.registerAt ? new Date(this.registerAt) : null;
    }
    @Type(() => Group) group: Group = new Group;
    getName() {
        return this.name;
    }
}

Create Database encrypted

let dbTest = DatabaseManager.getOrCreate({ name: "test", encryptionKey: "SECURE", create: true });

Map Typescript Objects

let mapping = new Map<string, any>();
mapping.set("USER", User);
dbTest.setMapping(mapping);

CRUD Document

let user = new User;
user.name = "user1";
user.group.name = "group1";
user.registerAtDate = now;
user.secure = true;       
dbTest.createDocument(user, "ID", { ttl: new Date() });
let fetched: User = <User>dbTest.getDocument(user.docId);
console.log(fetched.getName()); 
fetched.group.name = "group2";
dbTest.updateDocument(fetched.docId, fetched);               
let success = dbTest.deleteDocument(user.docId);

Crud Attachment

let source = fromResource("icon");
let attach = AttachmentFactory.fromSource(source, "yeah", AttachmentImage.PNG);
dbTest.setAttachment("ID", attach);
let attachments = dbTest.getAttachmentNames("ID");
let attach = dbTest.getAttachment("ID", "yeah");
removeAttachment("ID","yeah")

Create view

dbTest.createView({
    name: "users",
    revision: "1",
    map: (doc: User, emitter) => {
        if (doc.docType == "USER") {
            emitter.emit(doc.name.toLowerCase(), null);
        }
    }
})

Query View

Querying a View returns a QueryResult object which has "documents" property that contains all the documents that matched our query. This document property will be null unless you call the getDocuments() method of the QueryResult. getDocuments() will return all the documents and will also fill up the QueryResult's "documents" property;

let founded = dbTest.queryView("users", { mapOnly: true });
founded = dbTest.queryView("users", { mapOnly: true, startKey: "user4" });
founded = dbTest.queryView("users", { mapOnly: true, endKey: "user0" });
founded = dbTest.queryView("users", { mapOnly: true, startKeyDocID: "ID4" });
founded = dbTest.queryView("users", { mapOnly: true, endKeyDocID: "ID0" });
founded = dbTest.queryView("users", { mapOnly: true, descending: true });
founded = dbTest.queryView("users", { mapOnly: true, limit: 2 });
founded = dbTest.queryView("users", { mapOnly: true, skip: 3 });
founded = dbTest.queryView("users", { mapOnly: true, keys: ["user1", "user2"] });
let docs = founded.getDocuments();

Use compound keys

dbTest.createView({
    name: "users_compound",
    revision: "1",
    map: (doc: User, emitter) => {
        if (doc.docType == "USER") { 
            emitter.emit([doc.getName().toLowerCase(), doc.group.name.toLowerCase(), doc.registerAt, doc.registerAtDate, doc.secure], null); 
        }
    }
})

Use MapReduce

dbTest.createView({
    name: "users_bygroup",
    revision: "1",
    map: (doc: User, emitter) => {
        if (doc.docType == "USER") {
            emitter.emit([doc.group.name, doc.getName().toLowerCase()], doc.name);
        }
    },
    reduce: (keys: any[], values: any[], rereduce: boolean) => {
        return values.length;
    }
});

Group by levels

let founded = dbTest.queryView("users_bygroup", { mapOnly: false, groupLevel: 1 });
let groups = founded.getValues();

Query Result manager

let founded = dbTest.queryView("users_bygroup", { mapOnly: false, groupLevel: 1 });
let groups = founded.getValues();
let value = founded.firstValue();
let docs = <User[]>founded.getDocuments();
let doc = founded.firstDocument();
let docIds = founded.getDocumentIds();

Query all docs

let founded = dbTest.queryAllDocuments({ mapOnly: true });

Use LiveQuery

let listener = { 
    onRows(rows: QueryResult) { 
    }
};
let live = dbTest.liveQuery("users_live", { mapOnly: true }, l);
live.start();
live.waitForRows();
live.stop();

Push Replication

let url = "http://localhost:4984/test/";
let push = dbTest.createPushReplication(url);
push.setBasicAuthenticator("user", "password");
dbTest.createFilter({
    name: "filter",
    filter: (doc: Revision<Document>, params: Map<string, any>) => {
        return doc.id == "ID1";
    }
});
push.setFilter("filter");
let listener = {
    count: 0,
    onChange: (p) => {
        l.count = p.changesCount;
    }
};
push.addChangeListener(listener)
push.start();

Pull replication

let url = "http://localhost:4984/test/";
let pull = dbTest.createPullReplication(url);
pull.setBasicAuthenticator("user", "password");
let pullCallback = {
    countEvent: 0,
    onChange: (p) => {
        pullCallback.countEvent++;
    }
}
pull.channels(["channel4"]);
pull.setDocIds(["ID1"]);
pull.addChangeListener(pullCallback);
pull.start();

Conflict management

let conflicts = dbTest.getConflicts("ID");
let merged = mergeConflict(conflicts);
dbTest.resolveConflict("ID", merged);

Listen database events

dbTest.addChangeListener({
    onChange:(params:DatabaseListenerParam[])=>{

    }
});

Add any kind of attachment

class CustomAttachment implements Attachment{
    getName() => {
        return name;//String
    },
    getStream () => {
        return bs;//InputStream or NSData
    },
    getType () => {
        return "any/any";//Content Type
    }
}

nativescript-cblite-xl's People

Contributors

nabil-mansouri avatar siiar avatar

Watchers

 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.