Context()

Context(resource, req, res, server)

A Context gives access to a req and res object when passed to resource.handle(),
as well as several utility functions and properties.

Properties

  • req ServerRequest req
  • res ServerResponse res
  • url String The url of the request, stripped of the resource's base path
  • body Object The body of the request, if the body is JSON or url encoded
  • query Object The query of the request

Context.prototype.end()

Alias for ctx.res.end()

Context.prototype.done()

Context.prototype.done(err, response)

Continuous callback sugar for easily calling res.end().

Example

// instead of
store.find({foo: 'bar'}, function(err, res) {
  if(err) return res.end(JSON.stringify(err));
  res.end(JSON.stringify(res));   
})

// you can just do
store.find({foo: 'bar'}, ctx.done);

db.connect()

db.connect(options)

Create a new database connection with the given options. You can start making
database calls right away. They are internally buffered and executed once the
connection is resolved.

Options

  • name the database name
  • host the database host
  • port the database port

Example

db
  .connect({host: 'localhost', port: 27015, name: 'test'})
  .createStore('testing-store')
  .insert({foo: 'bar'}, fn)

Db.prototype.createStore()

Db.prototype.createStore(namespace)

Create a new database store (eg. a collection).

Example

db
  .connect({host: 'localhost', port: 27015, name: 'test'})
  .createStore('testing-store')
  .insert({foo: 'bar'}, fn)

Store.prototype.createUniqueIdentifier()

Create a unique identifier. Override this in derrived stores
to change the way IDs are generated.

Store.prototype.insert()

Store.prototype.insert(object, callback)

Insert an object into the store.

Example

db
  .connect({host: 'localhost', port: 27015, name: 'test'})
  .createStore('testing-store')
  .insert({foo: 'bar'}, fn)

Store.prototype.find()

Store.prototype.find(query, callback)

Find all objects in the store that match the given query.

Example

db
  .connect({host: 'localhost', port: 27015, name: 'test'})
  .createStore('testing-store')
  .find({foo: 'bar'}, fn)

Store.prototype.first()

Store.prototype.first(query, callback)

Find the first object in the store that match the given query.

Example

db
  .connect({host: 'localhost', port: 27015, name: 'test'})
  .createStore('testing-store')
  .first({foo: 'bar'}, fn)

Store.prototype.update()

Store.prototype.update(query, object, callback)

Update an object or objects in the store that match the given query.

Example

db
  .connect({host: 'localhost', port: 27015, name: 'test'})
  .createStore('testing-store')
  .update({id: '<an object id>'}, fn)

Store.prototype.remove()

Store.prototype.remove(query, callback)

Remove an object or objects in the store that match the given query.

Example

db
  .connect({host: 'localhost', port: 27015, name: 'test'})
  .createStore('testing-store')
  .remove({id: '<an object id>'}, fn)

Store.prototype.rename()

Store.prototype.rename(namespace, callback)

Rename the store.

Example

db
  .connect({host: 'localhost', port: 27015, name: 'test'})
  .createStore('testing-store')
  .rename('renamed-store', fn)

Resource.prototype.handle()

Resource.prototype.handle(ctx, next)

Handle an incoming request. This gets called by the router.
Call next() if the resource cannot handle the request.
Otherwise call cxt.done(err, res) when the resource
is ready to respond.

Example

Override the handle method to return a string:

function MyResource(settings) {
  Resource.apply(this, arguments);
}
util.inherits(MyResource, Resource);

MyResource.prototype.handle = function (ctx, next) {
  // respond with the file contents (or an error if one occurs)
  fs.readFile('myfile.txt', ctx.done);
}

Collection.prototype.handle()

Collection.prototype.handle(req, res)

Handle an incoming http req and res and execute
the correct Store proxy function based on req.method.

Collection.prototype.parseId()

Collection.prototype.parseId(ctx)

Parse the ctx.url for an id

Collection.prototype.execListener()

Collection.prototype.execListener(method, session, query, item, client, callback)

Execute a collection event listener based on the given method.

Example

var c = new Collection({
  onPost: 'error("foo", "must not be bar")'
});

var item = {foo: 'bar'};

c.execListener('Post', session, query, item, dpd, function (err, result) {
  expect(result).to.eql({"foo": "must not be bar"});
});

Collection.prototype.find()

Collection.prototype.find(session, query, callback)

Find all the objects in a collection that match the given
query. Then execute its get listener on each object.

Collection.prototype.remove()

Collection.prototype.remove(session, query, callback)

Execute the onDelete listener. If it succeeds, remove all objects in a
collection that match the given query.

Collection.prototype.save()

Collection.prototype.save(session, query, item, callback)

Execute the onPost or onPut listener. If it succeeds,
save the given item in the collection.

UserCollection.prototype.handle()

UserCollection.prototype.handle(req, res)

Handle an incoming http req and res and execute
the correct Store proxy function based on req.method.

Router.prototype.route()

Router.prototype.route(req, res)

Route requests to resources with matching root paths.
Generate a ctx object and hand it to the resource, along with the res by calling its resource.handle(ctx, next) method.
If a resource calls next(), move on to the next resource.

If all matching resources call next(), or if the router does not find a resource, respond with 404.

Server()

Server(options)

Create an http server with the given options and create a Router to handle its requests.

Options

  • db the database connection info
  • host the server's hostname
  • port the server's port

Example

var server = new Server({port: 3000, db: {host: 'localhost', port: 27015, name: 'my-db'}});

server.listen();

Server.prototype.listen()

Start listening for incoming connections.

Server.prototype.createStore()

Server.prototype.createStore(namespace)

Create a new Store for persisting data using the database info that was passed to the server when it was created.

Example

// Create a new server
var server = new Server({port: 3000, db: {host: 'localhost', port: 27015, name: 'my-db'}});

// Attach a store to the server
var todos = server.createStore('todos');

     // Use the store to CRUD data
todos.insert({name: 'go to the store', done: true}, ...); // see `Store` for more info

SessionStore()

A store for persisting sessions inbetween connection / disconnection.
Automatically creates session IDs on inserted objects.

SessionStore.prototype.createSession()

SessionStore.prototype.createSession(sid, callback)

Create a new Session based on an optional sid (session id).

Session()

Session(data, store, socket)

An in memory representation of a client or user connection that can be saved to disk.
Data will be passed around via a Context to resources.

Example

var session = new Session({id: 'my-sid', new SessionStore('sessions', db)});

session.set({uid: 'my-uid'}).save();

Session.prototype.set()

Session.prototype.set(changes)

Set properties on the in memory representation of a session.

Session.prototype.save()

Session.prototype.save(callback)

Save the in memory representation of a session to its store.

Session.prototype.fetch()

Session.prototype.fetch(callback)

Reset the session using the data in its store.

Session.prototype.remove()

Session.prototype.remove(callback)

Remove the session.