# mongoose template
This template generates server code for a mongoDB database from a model. It performs the following steps:
* generate all the files necessary to create a micro-service used to store the data into a mongoDB database using the mongoose middleware.
* A package.json is also generated and executed when all the files are written to disk. This will install all the required node modules. The modules are installed into the directory where files are generated. When all files are generated, the "npm install" is invoked to install the modules. This is part of the file generation process.

## Start the micro service
Because the microservice is node application, to start it: 
* open the terminal window
* start the micro service with the command "node index"

It is recommanded to use the client testing "postman" application which can be downloaded from: <https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop>

## Description
As described in the package.json file, the main entry point for the node executable is index.js. This file contains the REST API router. The REST methods are then passed to members of the classes contained in the classes.js file. The latter is used to perform operations on the mongoDB through the mongoose middleware.

index.js ----> classes.js -----> mongoose.js -----> mongoDB.

The file named addons.js contains two(2) classes for pre-process and post-process. The pre-process members for GET' PUT' POST' DELETE are called before any operations on mongoDB. When appropriate, It receives the data in javascript format, this data can be altered prior to be stored in mongoDB by the appropriate method invoked. If no process is needed, then it is simply ignored and the storage is performed with the very same data received from the client. If some specific methods do not need to store the data, which in this case, means that the method is simply overloaded. To do so, an ABORT value is returned. Hence a method in the preprocess class can return the data (Equivalent to OK), ABORT or ERROR (in the case the preprocess is used to perform some validations on the data)

## REST methods
To the basic REST methods included in the generated files is also included to additional endpoints used to provide more information about the model. The CRUD endpoint is identified by the collection name. For example, a collection named *payment*, if the service start at the root level (at the / ), the the endpoint will be /payment.
* The /model GET request returns the data model used to generate the code and store the data into mongoDB. It contains the full model for the collection as spécified in the ".mdl" file.
* the /querytemplate GET request is used to return a data template with only the set of property name: default values. This template is conveniently used to create queries with test clients like, for instance, PostMan.
* The GET query should contain a JSON object corresponding to a mongoose query. it should work for singletons and collections.
* The POST request should contain a JSON object with properties filled to modify the ones stored in mongoDB.
* The PUT request should contain a JSON object with properties filled to create a new entry in mongoDb. Object should be uniquely identified either by objectID or by one of the properties reserved to be a unique identifier used to retreive the entry from mongoDb.
* the DELETE request should contain a query used to identify a single entry in mongoDB.

## The addons data pre and post processor
The file named "addons" contains a class for each collection handled by the REST microservice. Data can be modified before being stored in the database. A function associated to a particular REST verb is called before storing the data. For example, "postPreProcess(content)". The data passed as parameter can then be processed or simply examined to trigger some additional processing. It is then returned to the main class to be stored in the database. In some occasions, the function can overload the default behavior without storing the data. In this case the function returns ABORT. This will stop any further process. In the same vein, after the data being stored a post process function is invoked. For example, "postPostProcess(content)" the content passed could be the data received from the client application or the data modified by the pre-process function. For example, this can be used to send an email to one or more recipients when the data is stored in the database.

## licence
Even if the magic Code generator cannot be copied, sold or modified, all code produced with the Magic Code generator can be freely used, sold, modified without any constraints.
