0.0.2 • Published 5 years ago

mongorestorm-server v0.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

mongorestorm-server

NPM Build Status Coverage Status License

A REST Server abstraction of the commonly used mongodb@3.2.1 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(),
    }),
  },
};
MongoRestOrmServerConfigTypeDescriptionDefault
mongoConfigobjectMongoDB Server connection configurations.{ mongoUri: 'mongodb://127.0.0.1:27017/', dbName: 'test', clientOptions: { useNewUrlParser: true, useUnifiedTopology: true } }
mongoConfig.mongoUristringMongoDB Server connection string.'mongodb://127.0.0.1:27017/'
mongoConfig.dbNamestringDatabase name.'test'
mongoConfig.clientOptionsobjectPlease refer to: MongoClientOptions{ useNewUrlParser: true, useUnifiedTopology: true }
mongoConfig.dbOptionsobjectPlease refer to: MongoClientCommonOption{}
corsConfigobjectPlease refer to: CorsOptions{ origin: '*', methods: 'GET,HEAD,PUT,PATCH,POST,DELETE', preflightContinue: false, optionsSuccessStatus: 204 }
apiDocsConfigstringMongoRestOrm 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.apiDocsstringPath to access OpenAPI 3.0 specification JSON.'/api-docs'
apiDocsConfig.swaggerUistringPath to access OpenAPI 3.0 specification Swagger UI.'/docs'
authConfigobjectConfigurations for built-in authentication.null
authConfig.secretstringSecret to be used for validating Bearer token passed in the Authorization header.'secret'
logLevelstringLog level of the application. Log levels: info, warn, error, and debug. Use custom to turn off logging.'info'
basePathstringPrefix path of the MongoRestOrm Server enpoints.''
schemasstringDefine 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

JSONEJSONBSON
{ _id: 'aaaaaaaaaaaaaaaaaaaaaaaa' }{ _id: { $oid: 'aaaaaaaaaaaaaaaaaaaaaaaa' } }{ _id: ObjectId('aaaaaaaaaaaaaaaaaaaaaaaa') }

Packages that deserialize EJSON to a plain json/dict/struct with native/BSON types.

LanguagePackage
Javascriptbson
Pythonpymongo
Gogomongo

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.