I went into this expecting a challenge but it was easy. Basically, Couchbase Lite has always had an optional HTTP listener that you can connect to from inside your app at http://lite.couchbase./mydatabase/
. React Native has a fine XHR module and encourages using fetch so getting your app to sync can be as easy as adding some API calls to keep JSON in the database.
We haven't done a full example yet, but in the spirit of possiblity, here are quick instructions to connect Couchbase Lite iOS with a React Native app (generated from their cli).
- Download and unzip Couchbase Lite iOS (grep this page for
ios
) - Drag and drop BOTH frameworks (CouchbaseLite.framework and CouchbaseLiteListener.framework) into your React Native app Xcode sidebar. More about the Listener capabilities in this blog post about peer to peer apps.
- Add the library dependencies (listed in the Couchbase Lite documentation)
- In the Extras folder of the download, there are two files you need to drag over also: CBLRegisterJSViewCompiler.h and libCBLJSViewCompiler.a
- Add some header imports to your AppDelegate.m
#import "CouchbaseLite/CouchbaseLite.h"
#import "CouchbaseLiteListener/CBLListener.h"
#import "CBLRegisterJSViewCompiler.h"
- Add a function to AppDelegate.m to launch Couchbase Lite
- (void)launchCouchbaseLite
{
NSLog(@"Launching Couchbase Lite...");
CBLManager* dbmgr = [CBLManager sharedInstance];
CBLRegisterJSViewCompiler();
NSLog(@"Couchbase Lite url = %@", dbmgr.internalURL);
}
- Call this function from application didFinishLaunchingWithOptions:
[self launchCouchbaseLite];
- Now in your JavaScript code you can create a new database with a PUT (here's a fancy version that does a GET first to check if the PUT is necessary):
var dbURL = "http://lite.couchbase./mydb/"
fetch(dbURL).then((response) => {
if (response.status !== 200) {
return fetch(dbURL, {method:"PUT"})
.then((response) => response.json()).then((data) => {
console.log("create db", data)
return data;
}).catch()
}
})
- You can create documents by POSTing to the database:
fetch(dbURL, {method:"POST", body : JSON.stringify({hello:"world"})})
.then((response) => response.json()).then((data) => {
console.log("create document", data)
return data;
}).catch()
- And get all documents with a GET query:
fetch(dbURL + "_all_docs?include_docs=true")
.then((response) => response.json()).then((data) => {
var docs = data.rows.map((row) => (row.doc));
console.log("all documents", docs);
return docs;
}).catch()
- For more see the Couchbase Lite REST API documentation.
To see something like this in context, you could try my branch of marucc's demo app here.
Hi Chris,
Many thanks for this very useful gist. I'm having trouble using couchbase lite attachments api : anytime I do a PUT to /docId/attachmentName, I get a 400 "Invalid attachment" response and I can't figure out why. A discussion on couchbase forum has not helped me find a solution to this issue. This also happens to me whith your ToDo App fork.
Do you have any idea why this might be ? Many thanks for your help,
Paul