The Clouburo Modelloader
clb-modelloader is a small utility which is loading moongoose model definition files from a file directory location into a nodejs server instance and automatically creates corresponding REST request handlers for handling the Create, Read, Update and Delete (CRUD) operations.
The request handlers are expecting a JSON document and will take over the persistency interaction part with the moongoose library.
The library can be easily integrated with a client side Backbone model which results in a transparent handling of the DB interactions for CRUD operations in a Backbone backed Web Application.
Because REST is an architectural style and not a strict standard, it allows for a lot of flexibility. The library will make sure that each model will offer an interface which is following the same API design principles.
The REST API style offered is following the pragmatic (not dogmatic) REST API design guidelines as outlined in the free eBooks by apigee.
Fork me on GitHub: https://github.com/talfco/clb-modelloader
The library is as NPM module available, you have to run:
npm install clb-modelloader
Moongoose Business Object Models
Put all your moongoose business object model (= collection) into a directory from where they will be loaded into the nodejs application server. The model load will accept either coffee files ending in
.coffee or direct java script files endin in
.js. A server model file will look like the following:
<div class='bogus-wrapper'><notextile><figure class='code'><figcaption><span>Moongoose model file - customerModel.coffee </span></figcaption>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
You are passing to the super constructor the model name, collection name, as well as the schema attributes. In our sample above this will result in a mongodb collection of the name
You would store this model in under the file name
customerModel.coffee and place it for example in the directory
The following naming conventions should be followed:
- A model file should start with a small letter and must end in with the postfix Model
- E.g. customerModel.coffee, productTypeModel.js etc.
- A model class name must start with a capital letter and the rest as the fileName
- E.g. CustomerModel, ProductTypeModel
Node.js Server Integration
Within the Node.js server code part you will add something like that into the ‘index.js’
<div class='bogus-wrapper'><notextile><figure class='code'><figcaption><span>Coding Section modelloader - index.js </span></figcaption>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15