mongorestorm-server
v0.0.2
Published
A REST server for commonly used mongodb-driver operations.
Downloads
9
Maintainers
Readme
mongorestorm-server
A REST Server abstraction of the commonly used [email protected] CRUD operations with schema definitions using @hapi/joi for data validations.
Getting Started
Installation
$ npm i mongorestorm-server
Usage
1. Import mongorestorm-server
const { MongoRestOrmServer } = require('mongorestorm-server');
2. Define MongoRestOrmServerConfig
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. | {}
|
3. Create MongoRestOrm Server instance
const mongoRestOrmServer = new MongoRestOrmServer(config);
4. Start MongoRestOrm Server
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.
Endpoints
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.
MongoRestOrm Client
Comming Soon...
MongoDB Extended JSON
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 |
Recommendations
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.
Authors
- Zishran Julbert Garces
See also the list of contributors who participated in this project.
License
This project is licensed under the MIT License - see the LICENSE file for details.