View on GitHub

Mangusu

sugared mongoose resources for the express framework

Download this project as a .zip file Download this project as a tar.gz file

Resource

A resource is the primary export of the module. It is the wrapper around a mongoose model that maps http verbs to mongoose model methods.

To be perfectly fair - for most use cases this is page probably all you really need to know. The rest of the pages are about internal stuff that automatically mapped from the resource.

Methods

constructor new Resource(model, resource_name[, options])

Function
Creates a new resource object.

Parameters

modelthe mongoose model that you are wrapping
resource_namethe canonical name of the resource
optionsan optional hash of configurations — see options section for details

Examples

ExampleSchema = new mongoose.Schema({ name: String });
ExampleModel = mongoose.model('ExampleModel', ExampleSchema);
/* 
 * simple use:
 * creates a resource for a given ExampleModel
 *
 * -----------------------------------------------------------------
 * NOTE: do not instantiate the model - just use the functional type
 * -----------------------------------------------------------------
 */

ExampleResource = new Resource(ExampleModel, 'example-models');


/*
 * simple with custom page sizes:
 * same as the other, except this will now default to pages of 5
 */

ExampleResource = new Resource(ExampleModel, 'example-models', { limit: 5 });

/*
 * slightly more involved example:
 * sets references
 */

CliqueSchema = new mongoose.Schema({ name: String });
Clique = mongoose.model('Clique', CliqueSchema);

PersonSchema = new mongoose.Schema({ name: String, clique: { type: ObjectId, ref: Clique } });
Person = mongoose.model('Person', PersonSchema);

CliqueResource = new Resource(Clique, 'cliques');
PersonResource = new Resource(Person, 'people', { refs: { clique: CliqueResource } });

make_routes make_routes(app)

Function
Maps HTTP verbs to model methods. It's really just a general shortcut to avoid the repetitiveness of writing routes.

Parameters

appthe express app in use

Examples

/* shortcuts the following:
 *
 * app.get('/people', ExampleResource.find());
 * app.get('/people/:_id', ExampleResource.find_one());
 * app.post('/people', ExampleResource.create());
 * app.put('/people/:_id', ExampleResource.update());
 * app.delete('/people/:_id', ExampleResource.destroy());
 *
 * nothing huge, but it saves the tedium
 */
var app = express();

// app code and stuff

ExampleSchema = new mongoose.Schema({ name: String });
ExampleModel = mongoose.model('ExampleModel', ExampleSchema);
ExampleResource = new Resource(ExampleModel, 'example-models');

ExampleResource.make_paths(app);

Options

Keys for the optional options parameter of the constructor.

limit

Function
Sets the limit/page size of the number of documents returned in the find method.

Type
The limit key must be an integer.

Default
The limit key defaults to 20.

Example

// limits page size to 10
YourResource = new Resource(YourModel, 'models', { limit: 10 });

// expands limit to 50
TonsOfResource = new Resource(YourOtherModel, 'other-models', { limit: 50 });

refs

Function
Used for generating resource pointers in place of straight _id strings.

Type
The refs key must be an object consisting of field names and their referenced resource.

Default
The refs key defaults to null.

Example

// schema/mode/resource for the reference
CliqueSchema = new mongoose.Schema({ name: String });
Clique = mongoose.model('Clique', CliqueSchema);
CliqueResource = new Resource(Clique, cliques);

// people
PersonSchema = new mongoose.Schema({ name: String, clique: { tpye: mongoose.Schema.Types.ObjectId, ref: Clique } });
Person = mongoose.model('Person', PersonSchema);
PersonResource = new Resource(Person, 'people', { refs: { clique: CliqueResource } });

// now instead of getting { ... clique: "<some long id>", .. }
// you get { ... clique: "/cliques/<some long id>", ... }

Properties

many_path

The many_path is what is used when mapping HTTP methods that return a collection of documents.

Value
The many_path is created from the resource_name parameter of the constructor. It prepends it with a forward slash.

Example

ArchEnemyResource = new Resource(ArchEnemy, 'arch-enemies');
console.log(ArchEnemyResource.many_path);
// "/arch-enemies"

single_path

The single_path is what is used when mapping HTTP methods that return a single named document.

Value
The single_path is created from the resource_name parameter of the constructor. It prepends it with a forward slash and appends another slash and an express route parameter for the document's _id.

Example

ArchEnemyResource = new Resource(ArchEnemy, 'arch-enemies');
console.log(ArchEnemyResource.single_path);
// "/arch-enemies/:_id"

emitter

There is a custom emitter that is used for the create, update, and destroy methods. It can be used to create listeners for custom actions happening post-facto of these changes.