JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 3
  • Score
    100M100P100Q58180F
  • License MIT

An object-oriented abstraction of PouchDB using Backbone models and collections.

Package Exports

  • lazybones

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (lazybones) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Lazybones

Lazybones is a modified Backbone collection that integrates with PouchDB. By combining Backbone's object-oriented approach with PouchDB's super flexible databases, Lazybones provides a highly-functional API for storing and manipulating data in the browser and Node.js.

Install

Download the latest version from our release page and use via a script tag. The variable Lazybones will be attached to window.

<script type="text/javascript" src="lazybones.js"></script>

If using Browserify or Node.js, you can install via NPM and use via require("lazybones").

$ npm install lazybones

Basic Usage

Lazybones is a modified Backbone collection with a custom sync function that allows for fetching and writing to a Pouch database. You begin using Lazybones, you only need the name of the database your are using:

var db = new Lazybones("mydb");

This will return an empty backbone collection attached to a newly initiated PouchDB instance. This instance if available via db.pouch although you should rarely need access to it.

At this point you can load in documents from the database into memory. The easiest way to do this is via a normal fetch:

db.fetch().then(function() {
   // collection now contains everything in PouchDB
});

Using fetch you can also retrieve specific subsets of documents, like only those in a specific view. All the standard Backbone sync options are available too.

Lazybones also provides a method for continuously listening to changes in the database and replicating them to the in-memory documents:

db.connect();

// later
db.disconnect();

When .connect() is called, the database will listen to the database changes feed and duplicate whatever is there. This means that connect, in a lot of ways, is like fetch with the exception that it is continuous.

API

Lazybones has a unique API, however most it is inherited from Backbone. Please read the Backbone documentation for more information on methods and concepts.

new LazyBones( name [, options ] )

The Lazybones database constructor. This returns an instance of Lazybones, which is a subclass of Backbone.Collection.

The name should be a string used to connect to a Pouch database. This can either be a local database name (indexeddb or leveldb) or a remote CouchDB url. This argument is required.

options are passed directly to the Backbone.Collection constructor, with the exception of one parameter:

  • pouch optional - An instance of PouchDB or an object of options to pass to the PouchDB constructor.

db.connect( [ options ] )

Listens to the changes feed in the PouchDB and sets all changes to the in-memory documents. This allows the models to respond to changes in the database as the happen. options is an optional argument that is passed to pouch.changes().

db.disconnect( )

Disconnects the database from the changes feeds or does nothing if not currently connected.

db.destroy( )

Destroys the Pouch database, ensuring any queued writes are properly canceled.

new Lazybones.Document( [ attrs ][, options ] )

This is the Backbone.Model subclass used to represent all documents in the database. This model has an identical API to Backbone.Model, with the exception of a few modified methods to help it work with sync.