couchbaselabs / react-native-couchbase-lite

Couchbase Lite binding for React Native Android & iOS
MIT License
110 stars 53 forks source link

How to create database and documenet #112

Closed Suprit-S-M closed 7 years ago

Suprit-S-M commented 7 years ago

Hello ,

I wanted to create database and document in simple step , Means I just wanted syntax to create DB. Could you please provide me sample steps to create it .

1 . Create DB 2 . Create Document 3 . Insert initial values

I wanted something like this , below code is in C#

var database = Manager.SharedInstance.GetDatabase("mydb");

// Create a new document (i.e. a record) in the database. var document = database.CreateDocument(); document.PutProperties(new Dictionary { { "firstName", "John" } });

// Update a document. document.Update(rev => { var props = rev.UserProperties; props["firstName"] = "Johnny"; rev.SetUserProperties(props); return true; });

I want the same thing in React-Native format .

If you provide this is simple steps that would be a great help for me , Thank you

jamesnocentini commented 7 years ago

If you following the Getting Started instructions and the Documentation section you will see that the API is self documented by using the help() method.

Based on that the code would be:

import Couchbase from "react-native-couchbase-lite";

Couchbase.initRESTClient(manager => {
    manager.database.put_db({db: DB_NAME})
      .then(res => manager.document.post({db: DB_NAME, body: {city: 'brighton'}}))
      .then(res => console.log(`Response object :: ${res.obj}`));
});

I'll close this ticket but let us know if you have any questions regarding this.

Suprit-S-M commented 7 years ago

Yes , thank you but I am getting status : 400 error : Invalid database/document/revision ID .

Could you please help me to solve this issue . I am new to Couchbase-lite

jamesnocentini commented 7 years ago

Database names can't contain certain characters. See https://developer.couchbase.com/documentation/mobile/1.3/guides/couchbase-lite/native-api/database/index.html

Suprit-S-M commented 7 years ago

Thank you

Suprit-S-M commented 7 years ago

How to get inserted values from document . Is following code right ..?

manager.document.get({db:DB_NAME}).then(res => { })

jamesnocentini commented 7 years ago

You need to specify a document ID as the manager.document.get.help() method would indicate in the console. So it would be:

manager.document.get({db: 'todos', doc: 'docid'})
  .then(res => console.log(res.obj));

You can query all document in the database too. Look for the get_db_all_docs method with manager.query.help();.

Suprit-S-M commented 7 years ago

Thank you , thanks for your time .

Suprit-S-M commented 7 years ago

How to get the document id , for me manager.document.get.help() is giving error . Lets say for example if I have multiple documents in one bucket , how to get doc it.

Thanks

jamesnocentini commented 7 years ago

This is what manager.document.get.help() prints to the console

image

It helps you to write the code, not find the document ID. You need to get the document ID from another method (like _all_docs).

See README Documentation and portal documentation for the REST API.

Suprit-S-M commented 7 years ago

Okay , many thanks ,

Suprit-S-M commented 7 years ago

Hello jamiltz ,

Thanks for your guidance and your time . All working fine for me .
Now I want to replicate my local DB data into Remote Couchbase DB , how can we do this . I tried manager.server.post_replicate({}); but it did not worked .

jamesnocentini commented 7 years ago

Here's how its done in the sample app https://github.com/couchbaselabs/mobile-training-todo/blob/master/react-native/app/DataManager.js#L94-L97

This should help you get replication up and running.

Suprit-S-M commented 7 years ago

This I tried but I am not able to get local db document on remote db . The code I followed . Please help me to solve this issue . I am not getting how to push created document from local remote db.

jamesnocentini commented 7 years ago

What's the config file you used for Sync Gateway? Are you running the app on Android or iOS?

Suprit-S-M commented 7 years ago

I am running it on ios and the sync gateway file is

{ "log":[""], "databases":{ "otc2": "http://localhost:8091", "users":{ "GUEST" : {"disabled":false , "admin_channels":[""] } } } }

Suprit-S-M commented 7 years ago

I have created DB called otc2 on local and remote CB and created document called docid on local DB and inserted some values . These values I want to replicate to remote DB .

Suprit-S-M commented 7 years ago

Hello could you please help me .

jamesnocentini commented 7 years ago

Take a look at the Example section on this page https://github.com/couchbaselabs/couchbase-mobile-portal/blob/master/md-docs/ready/installation/react-native.md. Hopefully that helps.

Suprit-S-M commented 7 years ago

This I tried but it is not working , is there anything to configure extra..?

jamesnocentini commented 7 years ago

There shouldn't be. It worked fine for me this morning with the iOS simulator.

Suprit-S-M commented 7 years ago

I am getting following error in remote CB , I do verified in Logs

invalid post received: {mochiweb_request, [#Port<>,'POST',"/otc222/_revs_diff", {1,1}, {8, {"host", {'Host',"ipaddress:8091"}, {"content-type", {'Content-Type',"application/json"}, {"connection", {'Connection',"keep-alive"}, {"accept", {'Accept',"application/json"}, nil, {"accept-language", {'Accept-Language',"en-us"}, {"accept-encoding", {'Accept-Encoding',"gzip, deflate"}, nil,nil}, nil}}, {"content-length", {'Content-Length',"326"}, nil,nil}}, nil}, {"user-agent", {'User-Agent', "CouchbaseLite/1.3 (iOS 1.3.1/)"}, nil,nil}}}]}

jamesnocentini commented 7 years ago

Hard to say what's going on from this snippet, sorry :(

Suprit-S-M commented 7 years ago

Thats okay ,

Thank you

Suprit-S-M commented 7 years ago

will this manager.server.post_replicate () do two-way sync . Means from remote to local and local to remote .

Because couchbase documentation provides push and pull api's . But these will be in different language .

jamesnocentini commented 7 years ago

As long as you call the manager.server.post_replicate method twice swapping around the source and target you will have bi-directional sync. You're right, the native API on the dev portal is a totally different syntax.

Suprit-S-M commented 7 years ago

Okay , then you meant say following code will be a bi-directional sync

return manager.server.post_replicate({body: {source: SG_URL, target: DB_NAME, continuous: true}}) .then(res => manager.server.post_replicate({body: {source: DB_NAME, target: SG_URL, continuous: true}})) .catch(e => console.log('ERROR', e));

If yes , first call is for local to remote and second one is for remote to local . am I right ..?

jamesnocentini commented 7 years ago

Yes, you're right

Suprit-S-M commented 7 years ago

Okay , but I am confused , In first call we have {body:{source:SG_URL,target:DB_NAME}} this from local to remote .

And I expect following syntax for remote to local

{body:{target:DB_NAME,source:SG_URL}} but this is not the case . Could you please explain me

Suprit-S-M commented 7 years ago

Yeah , I got it working and thanks for your time . I have one more doubt

How to register for remote DB changes in react native and how to import DeviceEventEmitter

jamesnocentini commented 7 years ago

See the code in https://github.com/couchbaselabs/mobile-training-todo/tree/master/react-native. If you want to register for changes on the local db, check out Feed.js.

Suprit-S-M commented 7 years ago

Yeah that i got it , thanks . But what if I want to register for changes on the remote db . As soon as changes happen to remote DB I want get callback , for that i need to register listener . But I am not getting how to add listener for that .

Suprit-S-M commented 7 years ago

Is it possible to register change listener for remote DB changes ...?

jamesnocentini commented 7 years ago

You can use the swagger client against the public Sync Gateway REST API. For example, in the training app this is done to check the validity of the username/password. You could use the swagger client to subscribe to changes on the server side as well.