1.1.0 • Published 5 years ago

mongodb-crud-router v1.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
5 years ago

jinwookie-crud-router

Package to create CRUD routes on express. Also, create a CRUD data layer, and execute queries in mongodb.

Installation

npm install jinwookie-crud-router

Usage

executeQuery

executes queries in the function passed in. Automatically will open connection before queries are executed, and then close the connection after queries executed.

function executeQuery(func)

Parameters

func: (db) => Promise<result>

db: mongodb db object

createCrudRouter

CRUD data layer for mongodb

function createCrudRouter(options) => {
  insertOne,
  select,
  selectOne,
  updateOne,
  deleteOne,
}

options

tableName: string

Name of mongodb table

model: object

Object model

[
  {
    column: string
    type: 'string' | 'number' | 'boolean' | 'ObjectId' | 'any'
    key: boolean
  }
  ...
]

mergeParams: boolean

mergeParams parameter used for Router. Defaults to true.

useObjectId: boolean

use MongoDB generated ObjectId. Defaults to true.

idField: string

route id field. Defaults to id

customRoutes: Routes[]

Inject custom routes

appendParams: (req, res) => {object}

Function to add additional params

appendBody: (req, res) => {object}

Function to add additional data to the body

aggregate: object

Return MongoDB aggregate

Environment Variables

DB_CONN=mongodb+srv://{username}:<PASSWORD>@{cluster}?retryWrites=true
DB_NAME={dbname}
DB_PASSWORD={dbpassword}

Development

npm run dev

Run in development mode using nodemon

Production

npm start

Run in production mode