# CouchBase
| Android Device | Android Emulator | iOS Device | iOS Simulator |
|---|---|---|---|
| ✅ | ✅ | ✅ | ✅ |
Couchbase Lite (opens new window) is an embedded, NoSQL JSON Document Style database for your mobile apps.
You can use Couchbase Lite as a standalone embedded database within your mobile apps, or with Sync Gateway (opens new window) and Couchbase Server (opens new window) to provide a complete cloud to edge synchronized solution
- CouchBase(...)
- close()
- createDocument(...)
- setBlob(...)
- getBlob(...)
- getDocument(...)
- getDocuments(...)
- updateDocument(...)
- deleteDocument(...)
- destroyDatabase(...)
- query(...)
- createReplication(...)
- createPullReplication(...)
- createPushReplication(...)
- addDatabaseChangeListener(...)
- removeDatabaseChangeListener(...)
- addDocumentChangeListener(...)
- removeDocumentChangeListener(...)
- inBatch(...)
- Classes
- Intefaces
- Enums
- Types
# Android
Note
The minimum required SDK is version 22 (Lollipop 🍭)
TIP
Include the following in the android {} section of you app.gradle:
android {
// Set minimum JVM level to ensure availability of, for example, lambda expressions
compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8
}
}
# iOS
Note
The minimum required platform is version 10
# Installing
ns plugin add @triniwiz/nativescript-couchbase
# Usage
import { CouchBase, ConcurrencyMode } from '@triniwiz/nativescript-couchbase';
const database = new CouchBase('my-database');
const documentId = database.createDocument({
firstname: 'O',
lastname: 'Fortune',
address: {
country: 'Trinidad and Tobago',
},
twitter: 'https://www.twitter.com/triniwiz',
});
const person = database.getDocument(documentId);
database.updateDocument(documentId, {
firstname: 'Osei',
lastname: 'Fortune',
twitter: 'https://www.twitter.com/triniwiz',
});
// Default concurrency mode is FailOnConflict if you don't pass it
const isDeleted = database.deleteDocument(documentId, ConcurrencyMode.FailOnConflict);
# Synchronization with Couchbase Sync Gateway and Couchbase Server
import { CouchBase } from '@triniwiz/nativescript-couchbase';
const database = new CouchBase('my-database');
const push = database.createPushReplication('ws://sync-gateway-host:4984/my-database');
push.setUserNameAndPassword('user', 'password');
const pull = database.createPullReplication('ws://sync-gateway-host:4984/my-database');
pull.setSessionId('SomeId');
pull.setSessionIdAndCookieName('SomeId', 'SomeCookieName');
push.setContinuous(true);
pull.setContinuous(true);
push.start();
pull.start();
# Listening for Changes
# Datebase
database.addDatabaseChangeListener(function (changes) {
for (var i = 0; i < changes.length; i++) {
const documentId = changes[i];
console.log(documentId);
}
});
# Document
database.addDocumentChangeListener('document-id-to-watch', function (documentId) {
console.log(documentId);
});
# Query
const results = database.query({
select: [], // Leave empty to query for all
from: 'otherDatabaseName', // Omit or set null to use current db
where: [{ property: 'firstName', comparison: 'equalTo', value: 'Osei' }],
order: [{ property: 'firstName', direction: 'desc' }],
limit: 2,
});
# Transactions
Using the method inBatch to run group of database operations in a batch/transaction. Use this when performing bulk write operations like multiple inserts/updates; it saves the overhead of multiple database commits, greatly improving performance.
import { CouchBase } from '@triniwiz/nativescript-couchbase';
const database = new CouchBase('my-database');
database.inBatch(() => {
const documentId = database.createDocument({
"firstname": "O",
"lastname": "Fortune",
"address": {
"country": "Trinidad and Tobago"
}
"twitter": "https://www.twitter.com/triniwiz"
});
const person = database.getDocument(documentId);
database.updateDocument(documentId, {
"firstname": "Osei",
"lastname": "Fortune",
"twitter": "https://www.twitter.com/triniwiz"
});
const isDeleted = database.deleteDocument(documentId);
});
# API
# CouchBase(...)
new CouchBase('nsDB');
Creates or opens a database
| Param | Type |
|---|---|
| name | string |
Returns: CouchBase
# close()
close(): void;
Closes the currently opened database
# createDocument(...)
createDocument(data: Object, documentId?: string, concurrencyMode?: ConcurrencyMode): string;
Creates a new document
| Param | Type |
|---|---|
| data | Object |
| documentId | string |
| concurrencyMode | ConcurrencyMode |
Returns: string
# setBlob(...)
setBlob(id: string, name: string, blob: any, mimeType?: string, concurrencyMode?: ConcurrencyMode): void;
| Param | Type |
|---|---|
| id | string |
| name | string |
| blob | any |
| mimeType | string |
| concurrencyMode | ConcurrencyMode |
Adds a blob to a document
# getBlob(...)
getBlob(id: string, name: string): Blob;
| Param | Type |
|---|---|
| id | string |
| name | string |
Gets a blob from a document
Returns: Blob
# getDocument(...)
getDocument(documentId: string): Object;
| Param | Type |
|---|---|
| documentId | string |
Gets a document
Return: Object
# getDocuments(...)
getDocuments(documentIds: string[]): Object[];
| Param | Type |
|---|---|
| documentIds | string[] |
Get a list of documents by id
Return: Object[]
# updateDocument(...)
updateDocument(documentId: string, data: Object, concurrencyMode?: ConcurrencyMode): void;
| Param | Type |
|---|---|
| documentId | string |
| data | Object |
| concurrencyMode | ConcurrencyMode |
Updates a document
Return: Boolean
# deleteDocument(...)
deleteDocument(documentId: string, concurrencyMode?: ConcurrencyMode): void;
| Param | Type |
|---|---|
| documentId | string |
| data | ConcurrencyMode |
Deletes a document
Return: Boolean
# destroyDatabase(...)
destroyDatabase(): void;
Destroys the currently opened database
# query(...)
query(query?: Query): Array<Object>;
| Param | Type |
|---|---|
| query | Query |
Queries the currently opened database
Returns: Array<Object>
# createReplication(...)
createReplication(remoteUrl: string, direction: 'push' | 'pull' | 'both'): Replicator;
| Param | Type |
|---|---|
| remoteUrl | string |
| direction | 'push' | 'pull' | 'both' |
Creates a replicator which can be used later on to sync updates with a remote database
Returns: Replicator
# createPullReplication(...)
createPullReplication(remoteUrl: string, username?: string, password?: string): Replicator;
| Param | Type |
|---|---|
| remoteUrl | string |
| username | string |
| password | string |
Creates a pull replicator which can be used later on to sync updates with a remote database
Returns: Replicator
# createPushReplication(...)
createPushReplication(remoteUrl: string, username?: string, password?: string): Replicator;
| Param | Type |
|---|---|
| remoteUrl | string |
| username | string |
| password | string |
Creates a push replicator which can be used later on to sync updates with a remote database
Returns: Replicator
# addDatabaseChangeListener(...)
addDatabaseChangeListener(callback: (ids: string[]) => void): void;
| Param | Type |
|---|---|
| callback | (ids: string[]) => void |
Adds a database change listener
# removeDatabaseChangeListener(...)
removeDatabaseChangeListener(callback: (ids: string[]) => void): void;
| Param | Type |
|---|---|
| callback | (ids: string[]) => void |
Removes a database change listener
# addDocumentChangeListener(...)
addDocumentChangeListener(documentId: string, callback: (id: string) => void): void;
| Param | Type |
|---|---|
| callback | (documentId: string, id: string) => void |
Adds a document change listener
# removeDocumentChangeListener(...)
removeDocumentChangeListener(callback: (id: string) => void): void;
| Param | Type |
|---|---|
| callback | (id: string) => void |
Removes a document change listener
# inBatch(...)
inBatch(batch: () => void): void;
| Param | Type |
|---|---|
| batch | Function |
Runs a group of operations in a batch. Use this when performing bulk write operations like multiple create/update; it saves the overhead of multiple database commits, greatly improving performance.
# Classes
# Replicator
| Method | Type |
|---|---|
| start() | void |
| stop() | void |
| isRunning() | boolean |
| setContinuous(isContinuous: boolean) | void |
| setUserNameAndPassword(username: string, password: string) | void |
| setChannels(channels: string[]) | void |
| setSessionIdAndCookieName(sessionId: string, cookieName: string) | void |
| setSessionId(sessionId: string) | void |
# Blob
| Prop | Type |
|---|---|
| ios | any |
| android | any |
| content | any |
| contentStream | any |
| contentType | any |
| length | number |
| digest | string |
| properties | Map<string, any> |
# Interfaces
# Query
| Prop | Type |
|---|---|
| select | any[] |
| where | QueryWhereItem[] |
| groupBy | any |
| order | QueryOrderItem[] |
| limit | any |
| offset | any |
| from | string |
# QueryWhereItem
| Prop | Type |
|---|---|
| logical | QueryLogicalOperator |
| property | string |
| comparison | QueryComparisonOperator |
| value | any |
# QueryOrderItem
| Prop | Type |
|---|---|
| property | string |
| direction | 'asc' |
# Enums
# QueryMeta
| Members | Value |
|---|---|
| ALL | "COUCHBASE_ALL" |
| ID | "COUCHBASE_ID" |
# QueryLogicalOperator
| Members | Value |
|---|---|
| AND | "and" |
| OR | "or" |
# QueryArrayOperator
| Members | Value |
|---|---|
| CONTAINS | "contains" |
# ConcurrencyMode
| Members | Value | Description |
|---|---|---|
| LastWriteWins | 0 | (default) The last operation wins if there is a conflict. |
| FailOnConflict | 1 | The operation will fail if there is a conflict. |
# Types
# QueryComparisonOperator
"modulo" |
"is" |
"between" |
"isNot" |
"collate" |
"in" |
"add" |
"isNullOrMissing" |
"greaterThan" |
"divide" |
"notEqualTo" |
"like" |
"subtract" |
"lessThanOrEqualTo" |
"lessThan" |
"notNullOrMissing" |
"regex" |
"equalTo" |
"multiply";
← AWS-SDK-S3 Downloader →