Saving data with Couchbase plugin


#1

Hi,
I’m starting with this plugin, but something is unclear for me.
https://www.npmjs.com/package/nativescript-couchbase?

I’m assuming that this plugin saves data in user’s device ?
if yes, in login view, im saving some data, and i’m trying to retrieve this, but this is not working, so maybe i think wrong ?

Thanks you for advice.

LOGIN
var Observable = require(“data/observable”).Observable;
var couchbaseModule = require(“nativescript-couchbase”);

exports.loaded = function(args) {

var model = new Observable();
model.welcome = 'Login';

var database = new couchbaseModule.Couchbase("test-database");
var documentId = database.createDocument({
    "firstname": "Nic",
    "lastname": "Raboy",
    "address": {
        "city": "San Francisco",
        "state": "CA",
        "country": "USA"
    },
    "twitter": "https://www.twitter.com/nraboy"
});
var page = args.object;
page.bindingContext = model;

};

REGISTER - try to get data

var Observable = require(“data/observable”).Observable;
var couchbaseModule = require(“nativescript-couchbase”);

exports.loaded = function(args) {

var model = new Observable();
model.welcome = 'Register';

var database = new couchbaseModule.Couchbase("test-database");
var person = database.getDocument(documentId);
//  This will not get any data
console.log(person.firstname);

var page = args.object;
page.bindingContext = model;

};


#2

Flagging @nraboy here as he might have some ideas!


#3

Thanks for this idea, i didnt know that it’s possible :slight_smile:


#4

Hey @Konrad,

Seems like you have the right approach. Can you dump a few variables for me?:

console.dir(documentId);
console.dir(person);

I want to make sure your documentId variable is not null and it matches in both locations. I also what to see what person is after you do your lookup.

Beyond it not working, do you get any errors in your logs?

Side note, for best results you should create a singleton for any database you use, not just Couchbase. Create one instance of it and use it everywhere rather than opening and closing the database every time you need it which is resource expensive.

Best,


#5

@Konrad If you want to pass a custom ID to the created document, then you can do it like this

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

then you can retrieve the document directly using this ID as follows:

var person = database.getDocument("MyCustomId");

Also, I highly suggest to stop using this plugin since it is lacking many features, has multiple issues, and it uses old version of couchbase. Instead use this one https://www.npmjs.com/package/nativescript-couchbaselite


#6

HI @nraboy
Login.js

var Observable = require("data/observable").Observable;
var couchbaseModule = require("nativescript-couchbase");

exports.loaded = function(args) {

var model = new Observable();
model.welcome = 'Login';

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

var person = database.getDocument(documentId);
console.dir(documentId);
console.dir(person);

var page = args.object;
page.bindingContext = model;

};

returns:
CONSOLE LOG file:///app/auth/login.js:22:16: -uSdfjIX0RbHrd4loyT-xap
CONSOLE LOG file:///app/auth/login.js:23:16: [object Object]

Register.Js

var Observable = require("data/observable").Observable;
var couchbaseModule = require("nativescript-couchbase");

exports.loaded = function(args) {

var model = new Observable();
model.welcome = 'Register';

var database = new couchbaseModule.Couchbase("test-database");
var person = database.getDocument(documentId);
//  This will not get any data

console.dir(documentId);
console.dir(person);

var page = args.object;
page.bindingContext = model;

};

returns
file:///app/auth/register.js:10:49: JS ERROR ReferenceError: Can’t find variable: documentId


#7

off course this won’t get any data and your app will crash because here documentId is undefined in Register.js
however, this will return your document Register.js

var person = database.getDocument("-uSdfjIX0RbHrd4loyT-xap");

since -uSdfjIX0RbHrd4loyT-xap is the ID that was assigned by couch base since you didn’t pass id argument when you created the document in Login.js.

If you want to set the id you need to pass it when you create the document. e.g. here I’m creating a person document1 with id =1 and document2 with id =john

var document1 = database.createDocument({
    "firstname": "Nic",
    "lastname": "Raboy",
}, "1");
var document2 = database.createDocument({
    "firstname": "John",
    "lastname": "Smith",
} ,"john");

Now in any other pages I can get document by the Id i assigned like this

var person1 = database.getDocument("1");
var person2 = database.getDocument("john");

#8

Thanks @ganas
I will check this ASAP as this looks very promising.
Thank you.


#9

@ganas great work, thanks, this works well.
regarding: ‘Also, I highly suggest to stop using this plugin since it is lacking many features, has multiple issues, and it uses old version of couchbase. Instead use this one’

Worst for is “it uses old version of couchbase”

@nraboy can . you confirm that plugin will get updates ? I relly like this plugin, but now i’m afraid a bit.
cheers.


#10

@ganas and @konrad,

The Couchbase plugin found on the couchbaselabs GitHub is a community driven plugin. I started it out with the intention of the community maintaining it.

So will it receive updates? Yes, when someone submits a PR.

Does that mean you should use one plugin over the other? That is up to you. They both work. I think the better question is, should you not use one of them. I’d say they are both fine to use.

Also, I highly suggest to stop using this plugin since it is lacking many features

I think that statement is a bit of a stretch.

When Couchbase 2.0 comes out, which is very soon, I’ll likely update the plugin because 1.X to 2.X is a complete redesign of the mobile SDKs.

Best,


#11

Hi @nraboy,

Thank you. I will wait for version 2.0 as this plugin is so easy to work with, that i will stick to this :slight_smile:
I hope not many changes - for now im leraning, but soon i’ll start using this.
Regards.


#12

@nraboy @Konrad when I said lacking many features, I mean compared to the other plugin, as the other plugin supports really major features that are really important

  • Database encryption
  • Supports attachments
  • Compound keys
  • Grouping
  • LiveQuery
  • Conflict management
  • Better error handling (many errors in nativescript-couchbase plugin are not appropritly catched e.g. u will not get conflict errors in iOS )

And I was giving my honest opinion after stress testing both plugins for two weeks, anyway both plugins have almost identical API so switching between them is not a hassle. Thus, It is good to the NativeScript ecosystem as end users it will only benefit us.

PS: I didn’t use the nativescript-couchbaselite at the beginning because its licence was AGPL, however, I contacted the maintainer and he switched it to Apache License 2.0