A REST Server abstraction of the commonly used mongodb@3.2.1 CRUD operations with schema definitions using @hapi/joi for data validations.
$ npm i mongorestorm-server
const { MongoRestOrmServer } = require('mongorestorm-server');
const joi = require('@hapi/joi');
const config = {
mongoConfig: {
mongoUri: 'mongodb://127.0.0.1:27017/',
dbName: 'test',
clientOptions: {
useNewUrlParser: true,
useUnifiedTopology: true,
},
dbOptions: {},
},
corsConfig: {
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204,
},
apiDocsConfig: {
apiDocs: '/api-docs',
swaggerUi: '/docs',
},
authConfig: null,
logLevel: 'info',
basePath: '/api',
schemas: {
userCollection: joi.object().keys({
_id: joi.object().keys({
$oid: joi.string().min(24).hex(),
}),
username: joi.string().required(),
password: joi.string().required(),
}),
},
};
MongoRestOrmServerConfig | Type | Description | Default |
---|---|---|---|
mongoConfig | object | MongoDB Server connection configurations. | { mongoUri: 'mongodb://127.0.0.1:27017/', dbName: 'test', clientOptions: { useNewUrlParser: true, useUnifiedTopology: true } } |
mongoConfig.mongoUri | string | MongoDB Server connection string. | 'mongodb://127.0.0.1:27017/' |
mongoConfig.dbName | string | Database name. | 'test' |
mongoConfig.clientOptions | object | Please refer to: MongoClientOptions | { useNewUrlParser: true, useUnifiedTopology: true } |
mongoConfig.dbOptions | object | Please refer to: MongoClientCommonOption | {} |
corsConfig | object | Please refer to: CorsOptions | { origin: '*', methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', preflightContinue: false, optionsSuccessStatus: 204 } |
apiDocsConfig | string | MongoRestOrm Server uses the OpenAPI 3.0 specification to document and visualize it's API. Use null to turn off docs. |
{ apiDocs: '/api-docs', swaggerUi: '/docs' } |
apiDocsConfig.apiDocs | string | Path to access OpenAPI 3.0 specification JSON. | '/api-docs' |
apiDocsConfig.swaggerUi | string | Path to access OpenAPI 3.0 specification Swagger UI. | '/docs' |
authConfig | object | Configurations for built-in authentication. | null |
authConfig.secret | string | Secret to be used for validating Bearer token passed in the Authorization header. |
'secret' |
logLevel | string | Log level of the application. Log levels: info , warn , error , and debug . Use custom to turn off logging. |
'info' |
basePath | string | Prefix path of the MongoRestOrm Server enpoints. | '' |
schemas | string | Define database schema using @hapi/joi for data validation and visualization in docs. | {} |
const mongoRestOrmServer = new MongoRestOrmServer(config);
There are two ways of starting the MongoRestOrm Server.
Way 1
const port = 3000;
mongoRestOrmServer.startServer({ port });
Way 2
const express = require('express');
const app = express();
const port = 3000;
// Add custom middlewares here.
app.on('ready', () => {
app.listen(port, () => {
logger.info('Accepting connections at port: %d', port);
});
});
mongoRestOrmServer.applyMiddleware(app);
Way 2 will allow adding of custom express middlewares such as own authentication middlewares.
All endpoints exposed by MongoRestOrm Server will be prefixed by the basePath
that was provided in the MongoRestOrmConfig
plus /dbs/
plus the mongoConfig.dbName
. Example prefix: /basePath/dbs/dbName
To see the endpoints exposed by MongoRestOrm Server for the mongodb CRUD operations, please visit the Wiki.
Comming Soon...
MongoRestOrm Server serializes and deserializes the data it receives and sends using the MongoDB Extended JSON (v2) specification to preserve special MongoDB types such as ObjectID
.
Comparisons
JSON | EJSON | BSON |
---|---|---|
{ _id: 'aaaaaaaaaaaaaaaaaaaaaaaa' } |
{ _id: { $oid: 'aaaaaaaaaaaaaaaaaaaaaaaa' } } |
{ _id: ObjectId('aaaaaaaaaaaaaaaaaaaaaaaa') } |
Packages that deserialize EJSON to a plain json/dict/struct with native/BSON types.
Language | Package |
---|---|
Javascript | bson |
Python | pymongo |
Go | gomongo |
Usage of this package is only recommended for applications with a microservice architecture and should only have communications with other microservices within the application. Exposing the API to the client might cause some security issues.
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE file for details.
Generated using TypeDoc