Giter Site home page Giter Site logo

backbone-redis-store's Introduction

Work in Progress

This is a work in progress, it's not been thoroughly tested in development, let alone production. You have been warned.

Alternatives

There are npm modules that serve the backbone/redis storage problem, but they seem to include socket.io or similar transports to synchronize to the browser. This is great if you need that functionality, but I simply what a backbone.js redis store that neither knows about nor cares about the client.

backbone-redis-store

backbone-redis-store is intended to be a simple (yet powerful) bridge between backbone.js and redis.

It is written in CoffeeScript, so you'll need CoffeeScript to run it.

Features

  • MIT license
  • Written in CoffeeScript
  • Overrides Backbone.sync to check for redisStore and take appropriate action (should revert back to old Backbone.sync if no redisStore is found)
  • Stores models to redis (as values to a model.id keyed hash)
  • Auto-increment ids (optional, can be overridden)
  • Can enforce unique values for arbitrary keys (optional)
  • Indexing (currently only indexes unique keys)
  • Find model by unique key (asynchronously loads if not already loaded)
  • Sets (e.g. foreign key lookups)

Storage

Stores the model as JSON encoded data into a redis hash named options.key, where the id of the model is the key to the hash.

Auto-increment counter is stored as next|{options.key}

Unique indexes are stored as a normal redis key: unique|{options.key}:{field}|{value.toLowerCase()} with value: model.id. They are not stored in a hash so that we can avoid the use of WATCH and instead use MSETNX to check and set the unique keys.

Why avoid WATCH?

Simply because we're sharing one redis connection across all clients (probably) - we don't want various other WATCHs to conflict, or an UNWATCH to cancel our watch. An alternate solution would be to use a separate redis connection per transaction - this may be implemented in future.

Usage

Dependencies:

To use it, you would:

# Create a new redisClient (npm install redis)
redisClient = require('redis').createClient()

# Require backbone (npm install backbone)
Backbone = require 'backbone'

# Import backbone-redis-store
RedisStore = require './backbone-redis-store'

# Implement RedisStore's Backbone.sync method
RedisStore.infect Backbone

# Define yourself a new model
class MyModel extends Backbone.Model
  username: null
  uniqueNumber: null
  somethingElse: null

# Define a collection, setting a `redisStore` property
class MyModelCollection extends Backbone.Collection
  redisStore: new RedisStore
    key: 'mymodel'
    redisClient: redisClient
    unique: ['username','uniqueNumber']
  model: MyModel

# Create a new instance of this collection and populate it
myModelCollection = new MyModelCollection()
myModelCollection.fetch()

Note: I would not recommend doing the last line for large stores - I would lazily fetch data as you need it using myModelCollection.getByUnique or similar.

RedisStore options

new RedisStore takes the following options:

  • key - the redis key under which to store the model data
  • redisClient - the connection to redis to use for storage
  • unique - an array of columns of the model that should be treated as unique indexes. An empty array is perfectly valid.

Backbone.Collection.getByUnique

Backbone.Collection.getByUnique(key, value, options)

  • key - the model key to look for
  • value - the value of said key to look for
  • options - accepts success and error callbacks like many Backbone methods

Future

I intend to add the following features in time:

  • Non-unique indexes
  • Redis pub/sub notification of updates (allows a cluster of NodeJS/Backbone instances to stay in sync)
  • Better error handling
  • Package as an npm module
  • Redis connection pooling (maybe)

If you fancy contributing, please get in touch! :)

backbone-redis-store's People

Contributors

benjie avatar gerrit avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.