Code Monkey home page Code Monkey logo

backbone-couch's Introduction

Backbone Couch

Overrides for Backbone to use CouchDB for Model persistence. Intended for server-side use of Backbone like in Bones.

Installation

npm install backbone-couch

Usage

var Backbone = require('backbone');

// Create a new backbone-couch handler for a database 'documents'.
var couch = require('backbone-couch')({
    host: '127.0.0.1',
    port: '5984',
    name: 'documents'
});

// Create database, push default design documents to it and
// assign sync method to Backbone.
couch.install(function(err) {
    Backbone.sync = couch.sync;
});

// Backbone.sync will now load and save models from a 'documents' couch db.

Conventions

backbone-couch stores models in using the model.url as its _id. By default, Collections retrieve models by matching the Collection url against the initial portion of the Model url. See backbone.json for the default design document and collection view.

var orange = new FruitModel({id: 'orange'});
var apple = new FruitModel({id: 'apple'});
var banana = new FruitModel({id: 'banana'});

console.log(orange.url()); // fruits/orange
console.log(apple.url());  // fruits/apple
console.log(banana.url()); // fruits/banana

var fruits = new FruitCollection();

console.log(fruits.url);   // fruits

fruits.fetch();            // retrieves orange, apple, banana

To override the default design doc with your own special sauce use the doc option when calling couch.install().

// Pass a filepath to your design doc.
couch.install({doc: '/path/to/my/doc.json'}, callback);

// Or provide a literal js object directly.
couch.install({doc: {
    _id: '_design/backbone',
    language: 'javascript'
}, callback);

Your design doc should use _design/backbone as its _id. Collections will be called against this design doc using their url. For example, if you have a Collection /blog/recent, your design doc should provide a rewrite rule to respond to this path:

{
    "_id":"_design/backbone",
    "language":"javascript",
    "views": {
        "recent": {
            "map": "function(doc) { emit(doc._id, doc._id) }"
        }
    },
    "rewrites": [
        {
            "from": "/blog/recent",
            "to": "_view/recent",
            "query": {
                "limit": "10"
                "include_docs": "true"
            }
        }
    ]
}

See the rewrite handler docs or this blog post for more info on design doc rewrites.

Run tests

You must configure CouchDB to work with HTTPS in order to run tests. See this wiki page

cd backbone-couch/
npm test

backbone-couch's People

Contributors

ianshward avatar johnsel avatar kkaefer avatar miccolis avatar mikemorris avatar wildintellect avatar willwhite avatar wrynearson avatar yhahn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.