Couchbase Lite Overview

Couchbase Lite Overview

This guide will help you understand some of Couchbase Lite features, which is a local storage system that is integrated in your application and is available across native platforms as well with REST API support. Couchbase Lite is a fully functional on-device native embedded JSON database for mobile devices that can work standalone, in a P2P network and connects with Sync Gateway.

We will begin to explore some of the features of Couchbase Lite in the Articles section.

Features


Couchbase Lite library provides data persistence directly on your device with full CRUD operations that is flexible to your mobile feature developments.

Flexible Data Model

Often times in mobile, feature specs are constantly changing and re-evaluated so Couchbase Mobile uses JSON data format throughout the stack without you having to rigidly define the data layout ahead of time. Complex design and migration time can be saved on any new updates as the data layout is essentially free-form in structure. The available native platform APIs allow JSON structures to map database documents to an application object model.

Always Available Data

Data is stored locally on the device where data is decoupled and not dependent on network connectivity. With offline functionality, the stored data model attributes are in local JSON documents where you are able to have full access and control over. This translates into complete offline support without requiring REST requests for content.

CRUD Operations


Within this overview, we will explore how Couchbase Lite library provides data persistence directly on your device with full CRUD operations support.

Data will be stored in a Document that is referenced by an unique ID within the Couchbase Lite database. Your data documents will consist of arbitrary JSON objects not enforced by rigid schemas. This provides an advantage of designing data models that can be naturally nested like a dictionary throughout the object model it is representing. This can be as natural or complex as needed when the data model is extended.

Create

When dealing with data in Couchbase Mobile, we create documents that store Keys and their Values. Once there is content within a document, we can add that document to the database.

Begin by creating an empty document through the Document class.

CBLDocument* document = [database createDocument];
let document = database.createDocument()
Document document = database.createDocument();
Document document = database.createDocument();
Document document = database.CreateDocument();

Retrieve

Once a document is populated with content, we can save it to the database. The to retrieve the document from the database, we first simply obtain the document's ID.

Assuming that the ID is stored in a variable named, 'documentID', then can programmatically call 'getDocument' to retrieve the appropriate document.

CBLDocument* retrievedDocument = [database documentWithID:documentID];
let document = database.documentWithID(documentID)
Document retrievedDocument = database.getDocument(documentID);
Document retrievedDocument = database.getDocument(documentID);
Document retrievedDocument = database.GetDocument(documentID);

Update

When entering new entries within an existing document, updating the document of interest is similar to when creating a new one.

Simply create a new object that contains the object map of keys to values and then save the object into the document that had been previously created.

NSMutableDictionary* updatedProperties = [doc.properties mutableCopy];
updatedProperties[@"new item"] = @"adding new entry into existing document";
updatedProperties[@"Database Used"] = @"My database is Couchbase Mobile";
NSError* error;
if (![document putProperties: updatedProperties error: &error]) {
    [self handleError: error];
}
var updatedProperties = doc.properties
updatedProperties["new item"] = "adding new entry into existing document";
updatedProperties["Database Used"] = "My database is Couchbase Mobile";
var error: NSError?
if doc.putProperties(updatedProperties, error: &error) == nil {
    self.handleError(error)
}
Map<String, Object> updatedProperties = new HashMap<String, Object>();
updatedProperties.putAll(retrievedDocument.getProperties());
updatedProperties.put("new item", "adding new entry into existing document");
updatedProperties.put("Database Used", "My database is Couchbase Mobile");
retrievedDocument.putProperties(updatedProperties);
Map<String, Object> updatedProperties = new HashMap<String, Object>();
updatedProperties.putAll(retrievedDocument.getProperties());
updatedProperties.put("new item", "adding new entry into existing document");
updatedProperties.put("Database Used", "My database is Couchbase Mobile");
retrievedDocument.putProperties(updatedProperties);
var updatedProperties = doc.Properties;
updatedProperties["new item"] = "adding new entry into existing document";
updatedProperties["Database Used"] = "My database is Couchbase Mobile";
var rev = doc.PutProperties(updatedProperties);
Debug.Assert(rev != null);

With the snippet of code above, we created a new HashMap object and populated the object with existing data from a document, 'retrievedDocument', by calling the 'getProperties()' method. Then we added a new key entry with the value, "adding new entry into existing document". And updating an existing key is just the same as adding a new entry. Then we save the object with new items back into our original, 'retrievedDocument'.

Delete

After a document has been entered into the database, deleting it is simply as calling the 'delete()' method on the document of interest.

NSError* error;
if (![retrievedDocument deleteDocument: &error]) {
    [self handleError: error];
}
var error: NSError?
if !retrievedDocument.deleteDocument(&error) {
    self.handleError(error)
}
retrievedDocument.delete();
retrievedDocument.delete();
retrievedDocument.Delete();

More details on CRUD operations may be found in 'Develop' tab in the 'Training' and 'Guides' sections.

Summary and Learning More


With Couchbase Lite, data can be programmatically stored locally with just simply a few additional lines of code. Couchbase Lite APIs is available across all the native mobile platforms along with desktop support.

To learn more, see the Couchbase Lite Guides section for developer descriptions on the Native APIs and how-tos. Then try building a Couchbase Lite mobile app over in the 'Training' section of the 'Develop' tab to get up and running.

Next, we explore 'What is Sync Gateway' and how to sync your data to the cloud. And we will explore some of the features on replicating and routing data to specific users.