1.0.11 • Published 5 years ago
@appbyte/couchsync v1.0.11
CouchSync 
A react native library for interacting with remote CouchDB instances & local document management. This library uses CloudantSync for Android & iOS
Getting started
$ npm install @appbyte/couchsync --save
Note: On iOS you have to run pod install additionally!
Features (Android & iOS):
Documents
- Create document
- Update document
- Delete document
Attachments
- Add attachment to document
- Remove attachment from document
- Retrieve attachments
Sync
- Two way sync
- Pull
- filtered Pull ()
- Push
Others
- Local storage only (sync url is not required)
- Changeable remote endpoint
Sample usage
import CouchSync from '@appbyte/couchsync';
//for authentication simply add username password combo to url.
// e.g http://test:test@127.0.0.1:5984/test
let db = new CouchSync('test', 'http://127.0.0.1:5984/test');
db.init().then(() => {
db.put({
name: 'AppByte'
}).then(() => {
db.push().then(() => {
console.log('Replicated successfully');
})
});
});
####Filtered pull
import CouchSync from '@appbyte/couchsync';
//for authentication simply add username password combo to url.
// e.g http://test:test@127.0.0.1:5984/test
let db = new CouchSync('test', 'http://127.0.0.1:5984/test');
db.init().then(() => {
db.pull({
name: "selects/myfilter",
parameters: {}
}).then(() => {
console.log('pulled changes');
})
});
Available API methods
/**
* Sets the remote url for the database.
*
* @param {string} remoteUrl Contains the new remote url.
* */
setRemoteUrl(remoteUrl: string): void;
/**
* Gets the current remote url.
* */
getRemoteUrl(): string;
/**
* Sets the name of the storage.
* Note changing the storage name after initialization requires a manual reinitialization.
*
* @param {string} storageName Contains the name of the storage.
* */
setStorageName(storageName: string): void;
/**
* Returns the name of the storage.
* */
getStorageName(): string;
/**
* Initializes the local storage.
* This method has to be called once after startup and before very first usage of the storage.
* */
init(): Promise<any>;
/**
* Checks if a document exists or not.
*
* @param {string} id Contains the id of the document.
* */
exists(id: string): Promise<boolean>
/**
* Syncs the local storage with the remote storage. First it pulls and then it pushes.
* */
sync(): Promise<any>
/**
* Pulls changes from the remote instance.
*
* @param {object} filter Specifies a filter for filtered replication.
* The filter object must contain a property name (contains the name of the filter as string) and a
* property parameters (must be an object) which contains the parameters passed to the filter.
* */
pull(filter?: any): Promise<any>
/**
* Push changes to the remote instance.
* */
push(): Promise<any>
/**
* Creates or updates an existing doc.
* */
put(doc: any): Promise<any>
/**
* Gets a document by it's id.
*
* @param {string} id Contains the id of the document.
* */
get(id: string): Promise<any>
/**
* Removes a document by it's id.
*
* @param {string} id Contains the id of the document.
* */
remove(id: string): Promise<any>
/**
* Searchs the local storage for documents.
*
* @param {object} query Contains the query.
* @param {[string]} fields Contains a list of fields which should be displayed instead of retrieving the whole document.
* */
find(query: any, fields?: any): Promise<any>
/**
* Adds an attachment to an existing document.
*
* @param {string} docId Contains the id of the document.
* @param {string} fileName Contains the name of the file which will be attached.
* @param {string} fileType Contains the mime type of the file.
* @param {string} fileData Contains the plain text or base64 encoded data.
* */
addAttachment(docId: string, fileName: string, fileType: string, fileData: any): Promise<any>;
/**
* Adds an attachment to an existing document.
*
* @param {string} docId Contains the id of the document.
* @param {string} fileName Contains the name of the file which will be attached.
* */
removeAttachment(docId: string, fileName: string): Promise<any>;
Useful tips
- Android: Check within simulator via chrome if your couchdb is accessible. If not try to run the following command: adb reverse tcp:5984 tcp:5984