LowlaDB intro March 2015

28
LowlaDB - a low-latency JSON database that syncs March 2015

Transcript of LowlaDB intro March 2015

Page 1: LowlaDB intro March 2015

LowlaDB - a low-latency JSON database that syncs

March 2015

Page 2: LowlaDB intro March 2015

Why should we care about latency?

Graphic by Modify - http://www.mobify.com/blog/web-performance-optimization

Page 3: LowlaDB intro March 2015

Developing your app with

LowlaDB

App

Lowla DBSyncer Adapter

App

back end

Back end

Datastore

API

AngularJS

or other

frameworkSyncer API Adapter

API

Page 4: LowlaDB intro March 2015

Architecture

• Syncer manages sync-related data so that the

Adapter can focus on reading and writing data for

the specific back end.

• Adapters available for both SQL and NoSQL

databases.

• All components communicate using JSON over

HTTP.

Page 5: LowlaDB intro March 2015

When Clients change Data

• The client saves the change locally

• Changes are POSTed to the Adapter

• The Adapter saves the changes and confirms

receipt to the Client

• The Adapter notifies the Syncer

Page 6: LowlaDB intro March 2015

When Clients Refresh

• From the Syncer, the Client GETs some IDs and

metadata for any modified records since the last

sync

• The Client then POSTs a request for the full records

to the Adapter

Page 7: LowlaDB intro March 2015

Conflicts

• Rare in most real-world applications

• Always handled in the adapter

• Customizable, but first edit wins by default

• There are ways to code around them

Page 8: LowlaDB intro March 2015

Real Time Sync

• LowlaDB will automatically detect Socket.IO in the

browser

• Local changes are automatically pushed

• The Syncer notifies clients via Socket.IO that

changes are available. This triggers a refresh

• Socket.IO doesn’t carry data itself. This makes it

simple to replace it with a different real-time notifier

such as Pusher

Page 9: LowlaDB intro March 2015

Scheduled Sync

• If Socket.IO is not available, applications can specify

the number of seconds between syncs

• Socket.IO is preferred

Page 10: LowlaDB intro March 2015

Javascript database API

• API is based on MongoDB

• Most calls are asynchronous

• Asynchronous calls support both callbacks and

promises

Page 11: LowlaDB intro March 2015

Callbacks• You provide a function parameter called when the

operation completes

• coll.find().toArray(function (err, docs)

{

if (err) {

// an error occurred

}

else {

// do something with docs

}

});

Page 12: LowlaDB intro March 2015

Promises• The operation returns a promise object that can be chained

using the then and catch methods

• coll.find().toArray()

.then(function (docs) {

// do something with docs

})

.catch(function (err) {

// an error occurred

})

• Much simpler syntax, especially if you need to chain several

asynchronous steps

Page 13: LowlaDB intro March 2015

Data Overview

• LowlaDB stores regular Javascript objects

• Objects are stored in named collections

• Collections are stored in named databases

Page 14: LowlaDB intro March 2015

Collections

• Most APIs start with a collection object

• To create a collection object

• var lowla = new LowlaDB();

var coll = lowla.collection(‘dbname’,

‘collname’);

Page 15: LowlaDB intro March 2015

Storing A Document• To insert a single document into a collection

• coll.insert({a:2});

• You can provide a callback to confirm success

• coll.insert({a:2}, function (err, doc) {

});

• Or use a promise

• coll.insert({a:2}).then(function (doc) {

});

Page 16: LowlaDB intro March 2015

Storing Multiple Documents

• You can pass an array of documents to insert

• coll.insert([{a:2}, {b:3}]);

Page 17: LowlaDB intro March 2015

Removing Documents

• To delete documents, you provide a query

document.

• coll.remove({a:2});

• This removes all documents with a property “a”

having the value 2

Page 18: LowlaDB intro March 2015

Finding Documents

• The find method takes a query object and returns a

cursor

• var cursor = coll.find({a:2});

• You can convert cursors to an array of documents

• cursor.toArray().then(function (docs) {

});

Page 19: LowlaDB intro March 2015

More on Cursors

• You can iterate cursors with a callback

• cursor.each(function (err, doc) {

});

• You can count the documents in a cursor

• cursor.count().then(function (docCount) {

});

Page 20: LowlaDB intro March 2015

More on Cursors

• You can sort cursors

• var cursor = coll.find().sort(‘title’);

• You can limit the number of documents returned

• var cursor = coll.find().sort(‘title’).limit(3);

Page 21: LowlaDB intro March 2015

Pending Documents

• A pending document is a document with local

changes that have not yet been synced

• Calling cursor.showPending() will cause the

cursor to insert a $pending property into each

returned document set to true or false

Page 22: LowlaDB intro March 2015

Live Cursors

• You can be notified whenever a cursor may have

changed

• cursor.on(function (err, cursor) {

});

• The callback is called whenever the underlying

collection changes

• This gives you a single point to update your UI for all

data changes, both local and synced

Page 23: LowlaDB intro March 2015

Event Notifications

• LowlaDB emits events when interesting things

happen

• To update an activity indicator during sync

• lowla.on(‘syncBegin’, showBusy);

lowla.on(‘syncEnd’, hideBusy);

Page 24: LowlaDB intro March 2015

Installing LowlaDB

• Demo project at:

http://github.com/lowla/lowladb-demo-node

• Demo contains an Adapter and Syncer and a simple

client app based on the TodoMVC sample

• Demo uses the embedded NeDB database

(lightweight MongoDB-like database for Node.js)

Page 25: LowlaDB intro March 2015

Supported Databases

• Adapters are available for MongoDB and

PostgreSQL

• Available at

https://github.com/lowla/lowladb-node-mongo

and

https://github.com/lowla/lowladb-node-postgresql

Page 26: LowlaDB intro March 2015

Documentation

• Documentation for the APIs and the underlying

HTTP protocols is available at

• http://lowla.github.io/lowladb-json-

database/index.html

Page 27: LowlaDB intro March 2015

Coming Next

• Cordova (PhoneGap) plugins for iOS and Android

• 10-20x faster than Javascript (written in C++)

• Apple Push Notification support so that apps can

sync even while they’re not running

• Embedded Linux client: currently in test on

Raspberry Pi

• Objective-C client for iOS native apps

Page 28: LowlaDB intro March 2015

Contacting us

[email protected]

• twitter.com/sives

• github.com/lowla