LowlaDB intro March 2015

Post on 17-Jul-2015

170 views 1 download

Tags:

Transcript of LowlaDB intro March 2015

LowlaDB - a low-latency JSON database that syncs

March 2015

Why should we care about latency?

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

Developing your app with

LowlaDB

App

Lowla DBSyncer Adapter

App

back end

Back end

Datastore

API

AngularJS

or other

frameworkSyncer API Adapter

API

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.

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

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

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

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

Scheduled Sync

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

the number of seconds between syncs

• Socket.IO is preferred

Javascript database API

• API is based on MongoDB

• Most calls are asynchronous

• Asynchronous calls support both callbacks and

promises

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

}

});

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

Data Overview

• LowlaDB stores regular Javascript objects

• Objects are stored in named collections

• Collections are stored in named databases

Collections

• Most APIs start with a collection object

• To create a collection object

• var lowla = new LowlaDB();

var coll = lowla.collection(‘dbname’,

‘collname’);

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) {

});

Storing Multiple Documents

• You can pass an array of documents to insert

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

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

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) {

});

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) {

});

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);

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

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

Event Notifications

• LowlaDB emits events when interesting things

happen

• To update an activity indicator during sync

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

lowla.on(‘syncEnd’, hideBusy);

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)

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

Documentation

• Documentation for the APIs and the underlying

HTTP protocols is available at

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

database/index.html

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

Contacting us

• steve@teamstudio.com

• twitter.com/sives

• github.com/lowla