Firesync - Overview
firesync is a library for seamless data synchronization between Firebase and your local data. When a FiresyncObject or FiresyncArray is created this object is observed for changes. At the same time the FirebaseRef is also observed. When a change in the one end happens, the data is immediately synchronized to the other end, eliminating the need to explicitly subscribe to events in order to update objects.
Requirements
firesync runs equally well in browsers and in node. Since it uses Object.observe
to watch for local changes a polyfill should be used in environments that do not support it.
Installation and usage
bower
bower install firesync
<script src="bower_components/firesync/dist/firesync.js"></script>
nodejs
npm install firesync-node
var firesync = require('firesync-node');
Examples
Basic
var ref = new Firebase('https://example.firebaseio.com/users/admin'); // null
ref.once('value', function (snap) {
var val = snap.val();
val.name === 'admin';
val.email === '[email protected]';
});
var admin = new firesync.FiresyncObject(ref);
admin.name = 'admin';
admin.dateCreated = new Date().valueOf().toString();
admin.email = '[email protected]';
//these changes will be automatically synced to the server
Firesync object synchronized from the database first
var ref = new Firebase('https://example.firebaseio.com/users/admin'); // {name: 'admin'}
var adminUser = new firesync.FiresyncObject(ref);
//the user might have already loaded
if (!adminUser.loaded()) {
//in most cases you would not need to explicitly attach to events
adminUser.once('loaded', function () {
var name = adminUser.name;
adminUser.once('synced', function () {
//https://example.firebaseio.com/users/admin now is - {name: '__invalid'}
});
adminUser.name = '__invalid';
});
}
Firesync object synchronized from local first
var ref = new Firebase('https://example.firebaseio.com/users/newUser'); // null
var newUser = new firesync.FiresyncObject(ref);
newUser.name = 'newUser';
newUser.once('synced', function () {
//https://example.firebaseio.com/users/newUser now is - {name: 'newUser'}
});
Firesync array synchronized from database first
var ref = new Firebase('https://example.firebaseio.com/users'); // [{name: 'admin'}, {name: 'newUser'}]
var users = new firesync.FiresyncArray(ref);
//the array can be already loaded
if (!users.loaded()) {
//we do not need to use loaded to add values
users.once('loaded', function () {
//users = [{name: 'admin'}, {name: 'newUser'}]
users[0].name === 'admin';
users[1].name === 'newUser';
users.add({
name: 'newUser2'
});
//this will immediately push newUser2 to the remote array
});
}
Firesync.create - Automatically creates a synchronized object or array based on the underlying firebase value. The created objects are guaranteed to be loaded.
var ref = new Firebase('https://example.firebaseio.com/users/fred'); //{name: 'fred'}
firesync.create(ref)
.then(function (firesyncObj) {
firesyncObj.name === 'fred';
firesyncObj.loaded() === true;
});
var usersRef = new Firebase('https://example.firebaseio.com/users'); //[{name: 'admin'}, {name: 'fred'}];
firesync.create(usersRef)
.then(function (firesyncArr) {
firesyncArr.lenght() === 2;
firesyncArr.loaded() === true;
firesyncArr[0].name === 'admin';
});
Firesync.map - Returns a non-synchronized object or array, depending on the underlying firebase value. Each object in the returned object/array is a synchronized FiresyncObject or FiresyncArray depending on the underlying firebase value. The objects are guaranteed to be loaded.
var usersRef = new Firebase('https://example.firebaseio.com/users'); //[{name: 'admin'}, {name: 'fred'}];
firesync.map(usersRef)
.then(function (arr) {
Array.isArray(arr) === true;
arr[0] instanceof firesync.FiresyncObject === true;
arr[1] instanceof firesync.FiresyncObject === true;
//changes to arr will not reflect the changes to the remote data, but changing any of the inner objects will
});
var someRef = new Firebase('https://example.firebaseio.com/someRef'); //{someObj: {name: 'admin'}, someArr: [1, 2]};
firesync.map(someRef)
.then(function (obj) {
typeof obj === 'object';
obj.someObj instanceof firesync.FiresyncObject;
obj.someArr instanceof firesync.FiresyncArray;
//changes to obj will not reflect the changes to the remote data, but changing any of the inner objects will
});
Important !!!
Whenever you are done with any FiresyncObject or FiresyncArray make sure to call the detach()
method as it will unattach the inner firebase listeners.
API Reference
Classes
- firesync
The entry point of firesync.
- FirebaseRef
FirebaseRef object
Kind: global class
- firesync
- .FiresyncArray ⇐
FiresyncBase
- .FiresyncObject ⇐
FiresyncBase
- .create(ref) ⇒
Promise
- .map(ref) ⇒
Promise
- .FiresyncArray ⇐
FiresyncBase
firesync.FiresyncArray ⇐ An array which keeps its values synchronized with the remote. One should use the FiresyncArray methods to manipulate the values.
Kind: static class of firesync
Extends: FiresyncBase
Access: protected
boolean
firesyncArray.loaded() ⇒ Indicates whether the object has loaded its data from Firebase.
Kind: instance method of FiresyncArray
FirebaseRef
firesyncArray.ref() ⇒ Returns the ref set in the constructor
Kind: instance method of FiresyncArray
firesyncArray.detach()
Detaches from the subscribed events to the FirebaseRef
Kind: instance method of FiresyncArray
firesyncArray.set(val)
Applies a value to the object.
Kind: instance method of FiresyncArray
Param | Type |
---|---|
val | object |
Example
firebaseObject.set({value: 1});
"changed"
Fired the local object changes, regardless whether it is a result of direct local change or remote change.
Kind: event emitted by FiresyncArray
Example
firesyncObject.on('changed', function(){});
"loaded"
Fired when the initial value of the object is loaded from the remote.
Kind: event emitted by FiresyncArray
Example
firesyncObject.on('loaded', function(){});
"synced" (err)
Fired when the local object's value is sucesfully set to the remote.
Kind: event emitted by FiresyncArray
Param | Type | Description |
---|---|---|
err | Error |
Synchronization error |
Example
firesyncObject.on('synced', function(err){});
FiresyncBase
firesync.FiresyncObject ⇐ An object which keeps its values synchronized with the remote.
Kind: static class of firesync
Extends: FiresyncBase
Access: protected
boolean
firesyncObject.loaded() ⇒ Indicates whether the object has loaded its data from Firebase.
Kind: instance method of FiresyncObject
FirebaseRef
firesyncObject.ref() ⇒ Returns the ref set in the constructor
Kind: instance method of FiresyncObject
firesyncObject.detach()
Detaches from the subscribed events to the FirebaseRef
Kind: instance method of FiresyncObject
firesyncObject.set(val)
Applies a value to the object.
Kind: instance method of FiresyncObject
Param | Type |
---|---|
val | object |
Example
firebaseObject.set({value: 1});
"changed"
Fired the local object changes, regardless whether it is a result of direct local change or remote change.
Kind: event emitted by FiresyncObject
Example
firesyncObject.on('changed', function(){});
"loaded"
Fired when the initial value of the object is loaded from the remote.
Kind: event emitted by FiresyncObject
Example
firesyncObject.on('loaded', function(){});
"synced" (err)
Fired when the local object's value is sucesfully set to the remote.
Kind: event emitted by FiresyncObject
Param | Type | Description |
---|---|---|
err | Error |
Synchronization error |
Example
firesyncObject.on('synced', function(err){});
Promise
firesync.create(ref) ⇒ Creates a FiresyncObject or FiresyncArray from the specified ref depending on the underlying value. The returned object is guaranteed to be loaded.
Kind: static method of firesync
Param | Type | Description |
---|---|---|
ref | FirebaseRef |
from a specified ref |
Example
firesync.create(ref).then(function(firesyncObj) {}); //if ref's underlying value is array a FiresyncArray is returned
otherwise a FiresyncObject
Promise
firesync.map(ref) ⇒ Returns a non-synchronized array or an object of FiresyncObject or FiresyncArray objects. The objects are guaranteed to be loaded.
Kind: static method of firesync
Param | Type | Description |
---|---|---|
ref | FirebaseRef |
from a specified ref |
Example
firesync.map(ref).then(function(objOrArr){});
FirebaseRef
FirebaseRef object
Kind: global external
See: https://www.firebase.com/docs/web/api/firebase/child.html
License
The MIT License (MIT)
Copyright (c) 2015 Firesync
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.