Skip to content

Indirect JavaScript Binding

Jens Alfke edited this page Jan 30, 2019 · 2 revisions

A JavaScript binding would of course be very useful. However, some popular JavaScript-based app environments like PhoneGap / Cordova and React Native don't support direct bindings to native code; instead, they define an RPC-like mechanism where native plugins define named functions that take and return JSON objects:

Thus, to support these we'll need to define an internal API that uses such a mechanism.

In particular, since there's no intrinsic notion of an object, it probably makes more sense to use a stateless API as much as we can. That's a lot like the REST API; we can borrow some ideas from it.

API Concepts

Databases are identified by name. A database is implicitly opened when first referred to. (Do we need API to close a db?)

Document bodies do not have "magic" properties like _id. Metadata is outside the body.

An error is a two-element array [domain, code, message], matching the C CBLError struct.

Database API

CreateDB

Parameters: db
Result: true or error

DeleteDB

Parameters: db
Result: true or error

EnableDbChangedListener

Parameters: db, enabled

Document API

GetDoc

Parameters: db, id
Result: Document body

GetDocs

Parameters: db, ids
Result: Object mapping ids to document bodies

PutDoc

Parameters: db, id, body, delete
Result: true, or [error code, message]

PutDocs

Parameters: db, docs
Result: Object mapping ids to errors

docs is an array of {id:, body:, delete:} objects.

PurgeDocs

Parameters: db, ids
Result: Object mapping ids to errors

Query API

CreateQuery

Parameters: db, spec Result: Number (query ID) or error

SetQueryParams

Parameters: db, query
Result: true or error

ExecuteQuery

Parameters: db, query
Result: {rows:[]} or error

CreateIndex

DeleteIndex

Listeners

These use the reverse mechanism where PhoneGap/React lets native code invoke a named function that's handled by a callback registered by JS code.

DbChanged

parameters: db, docs

docs is an array of docIDs.