Coder Social home page Coder Social logo

promise-repo's Introduction

promise-repo

Build Status NPM version

Map an object store with a common interface around a clean, promise-driven API.

browser support

Installation

$ npm install promise-repo

Usage

Assume we have a basic model constructor:

function User()
{
  this.id    = null;
  this.name  = null;
  this.email = null;
}

Create a new, promise-based repository of User models:

var Repository = require('promise-repo');

var users = new Repository(User);

If we attempt to call any of the accessor methods, we will get an error since there is nothing sourcing the repository:

users.get(id)
  .then(function(user) { ... })
  .catch(function(err) { console.error(err); });
> Error: No source provider for this method

Pass in a provider object that implements some or all of the accessor methods to power the repository:

var _userCache = {};
var _id       = 0;

users.source({
  get: function(id) {
    return _userCache[id] || null;
  },
  add: function(user) {
    var id = ++_id;
    _userCache[id] = user;
    user.id = id;
    return user;
  }
});

Even though our provider isn't returning a promise, the mapper in promise-repo will ensure that all responses are normalized to promises.

Now we can get and add:

var billy = new User();
billy.name = 'Billy';
billy.email = '[email protected]';

users.add(billy)
  .then(function(u) {
    return users.get(u.id);
  })
  .then(function(u) {
    console.log(u);
  })
{
  id: 1,
  name: 'Billy',
  email: '[email protected]'
}

Methods

The following methods are available on the repository object, assuming a corresponding provider has been source:

var repository = new Repository(T)

Returns a new promise-wrapped repository of models with type T.

repository.source(provider)

A provider is simply an object that has one or more of the accessor methods on it. A repository can be source by one or more providers. Any methods on provider that are not part of the accessor methods interface will be ignored.

var p = repository.get(id)

Get a model by its id. Returns a promise p for either a single instance of T with a specific id, or null.

var p = repository.getAll([skip, take])

Get all models. Returns a promise p for either an array of instances of T, or an empty array if no items are returned.

The parameters skip and take are passed to the underlying provider to allow for pagination (if implemented).

var p = repository.query(queryParams...)

Query the repository. Returns a promise p for either an array of instances of T, or an empty array if no items are returned.

All parameters passed to this function are passed to the underlying provider, and the specific format of these parameters will be determined by what provider is source for the repo.

var p = repository.add(object)

Add a new model to the repository. Returns a promise p for either a single instance of T or null.

This will pass object (which should either be of type T or similar) to the underlying provider to add it to the repository.

The returned object will be the store's representation of the model after the add operation, e.g, with a newly created id parameter filled out.

var p = repository.remove(object)

Remove an existing model from the repository. Returns a void promise p if successful.

This will pass object (which should either be of type T or similar) to the underlying provider to remove it from the repository.

No object is returned, but if the promise resolves then the item was removed.

var p = repository.update(object)

Update an existing model in the repository. Returns a promise p for either a single instance of T or null.

This will pass object (which should either be of type T or similar) to the underlying provider to update its fields in the repository.

The returned object will be the repository's representation of the model after the update operation, e.g, with all fields present if object is only a partial representation.

Provider modules

While can write your own providers by implementing some or all of the accessor methods, here are some modules for common backing stores:

  • postgres-repo
  • redis-repo (coming soon)
  • memory-repo (coming soon)
  • rest-ajax-repo (coming soon)

Testing

$ npm test

License

MIT

promise-repo's People

Contributors

bvalosek avatar

Stargazers

 avatar  avatar  avatar

Watchers

 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.