Generates a rest api with mongodb middleware from a json file
npm install iblokz-rest-api
// dependencies
const express = require('express');
const mongoose = require('mongoose');
// rest api const restApi = require('iblokz-rest-api'); const restMap = require('./rest.json');
// create app and db instances const app = express(); const db = mongoose.connect('mongodb://localhost/rest_api_example');
// loads the models from json schema restApi.loadModel(restMap, db);
// initis rest endpoints restApi.initRoutes(app, restMap, {}, db);
- rest.json
```json
{
"collections": {
"users": {
"model": "User",
"schema": {
"name": "String",
"email": { "type":"String", "isEmail":true, "unique": true},
"password": "String"
}
},
"articles": {
"model": "Article",
"schema": {
"title" : { "type": "String", "required": true, "minLength": 1, "maxLength": 5, "unique": true },
"viewCount": { "type": "Number", "min": 1},
"body" : "String",
"createdBy" : { "type": "ObjectId", "ref":"User", "required": false},
"dateCreated": { "type": "Date", "default": "Date.now" }
}
}
},
"routes": {
"api" : {
"_meta": {
"virtual": true,
"crud": true,
"contentType": "json"
},
"users": {},
"articles": {}
}
}
}