express-mongodb-rest
v3.1.0
Published
Express middleware for MongoDB REST APIs
Downloads
9
Maintainers
Readme
express-mongodb-rest
Richard Wen
[email protected]
Express middleware for MongoDB REST APIs
Install
- Install MongoDB
- Install Node.js
- Install express and express-mongodb-rest via
npm
- Recommended: Install dotenv to load environmental variables
npm install --save express express-mongodb-rest
npm install --save dotenv
For the latest developer version, see Developer Install.
Usage
This package provides a flexible, customizable, and low-dependency Representational State Transfer (REST) Application Programming Interface (API) for mongodb using express.
It is recommended to use a .env
file at the root of your project directory with the following contents:
MONGODB_CONNECTION
: MongoDB connection stringMONGODB_DATABASE
: MongoDB database name
MONGODB_CONNECTION=mongodb://localhost:27017
MONGODB_DATABASE=test
The .env
file above can be loaded using dotenv:
require('dotenv').config();
See Documentation for more details.
GET (Default)
Given the route /api/:collection/:method
:
Method | Route | Function | Query | Description
--- | --- | --- | --- | ---
GET | /api/:collection/find?q={"field":"value"} | find | {field: "value"} | Find all documents with field=value
GET | /api/:collection/find?q={"field":{"$exists":"value"}} | find | {field: {$exists: "value"}} | Find all documents where field
exists
GET | /api/:collection/find?q={"$or":[{"field1":"value1"},{"field2":"value2"}]} | find | {$or: [{field1: value1}, {field2: value2}]} | Find all documents with field1=value1
or field2=value2
A simple GET
API can be created with the following file named app.js
:
require('dotenv').config();
// (packages) Load required packages
var express = require('express');
var api = require('express-mongodb-rest')();
// (app) Create express app
var app = express();
app.use('/api/:collection', api); // add MongoDB REST API
app.listen(3000);
Run the file app.js
defined above:
node app.js
Go to localhost:3000/api/collection/find?q={"field":"value"}
with a web browser to use the API, where:
collection
is the name of the collection in your MongoDB databasefind
is the MongoDB collection method for queryingfield
is a field or key name in thecollection
value
is the value infield
to query for
REST (Custom)
Given the route /api/:collection/:method
:
Method | Route | Function | Query | Description
--- | --- | --- | --- | ---
GET | /api/:collection/find | find | {} | Find all documents in collection
POST | /api/:collection/insertMany?docs=[{"field":"value"}]| insertMany| [{field: "value"}]| Insert [{field: "value"}]
into :collection
PUT | /api/:collection/updateMany?q={"field":{"$exists":1}}&update={"$set":{"field":"newvalue"}} | updateMany | {$set: {field: "newvalue"}} | Update [{field: value}]
with [{field: newvalue}]
DELETE | /api/:collection/deleteMany?q={"field":{"$exists":1}} | deleteMany | {field: {$exists: 1}} | Delete all documents where field
exists
A custom RESTful API can be created with the following file named app.js
:
require('dotenv').config();
// (packages) Load required packages
var express = require('express');
var api = require('express-mongodb-rest');
// (options) Initialize options object
var options = {rest: {}, mongodb: {}};
// (options_get) GET options
options.rest.GET = {};
options.rest.GET.method = 'find';
options.rest.GET.query = {q: {}}; // return all if no query string provided
// (options_post) POST options
options.rest.POST = {};
options.rest.POST.method = 'insertMany';
// (options_put) PUT options
options.rest.PUT = {};
options.rest.PUT.method = 'updateMany';
// (options_delete) DELETE options
options.rest.DELETE = {};
options.rest.DELETE.method = 'deleteMany';
// (app) Create express app
var app = express();
app.use('/api/:collection', api(options)); // add MongoDB REST API
app.listen(3000);
Run the file app.js
defined above:
node app.js
Go to localhost:3000/api/collection/method
with a web browser to use the API, where:
collection
is the name of the collection in your MongoDB databasemethod
is the MongoDB collection method for querying
GET (Query String Format)
Given the route /api/:collection/:method
:
Method | Route | Function | Query | Description
--- | --- | --- | --- | ---
GET | /api/:collection/find?q[field]=value | find | {field: "value"} | Find all documents with field=value
GET | /api/:collection/find?q[field][$exists]=value | find | {field: {$exists: "value"}} | Find all documents where field
exists
GET | /api/:collection/find?q[$or][0][field1]=value1&q[$or][1][field2]=value2 | find | {$or: [{field1: value1}, {field2: value2}]} | Find all documents with field1=value1
or field2=value2
A simple GET
API with the Express query string format can be created with the following file named app.js
:
- Recommended: Install express-query-int for numeric support
npm install --save express-query-int
require('dotenv').config();
// (packages) Load required packages
var express = require('express');
var api = require('express-mongodb-rest');
var queryInt = require('express-query-int');
// (options_qs) Use query string format
options = {express: {}};
options.express.parse = function(query) {return(query)};
// (app) Create express app
var app = express();
app.use(queryInt()); // allow queries with numbers (optional)
app.use('/api/:collection', api(options)); // add MongoDB REST API
app.listen(3000);
Run the file app.js
defined above:
node app.js
Go to localhost:3000/api/collection/find?q[field]=value
with a web browser to use the API, where:
collection
is the name of the collection in your MongoDB databasefind
is the MongoDB collection method for queryingfield
is a field or key name in the<collection>
value
is the value infield
to query for
REST (Query String Format)
Given the route /api/:collection/:method
:
Method | Route | Function | Query | Description
--- | --- | --- | --- | ---
GET | /api/:collection/find | find | {} | Find all documents in collection
POST | /api/:collection/insertMany?docs[0][field]=value| insertMany| [{field: "value"}]| Insert [{field: "value"}]
into :collection
PUT | /api/:collection/update?q[field][$exists]=1&update[$set][field]=newvalue | updateMany | {$set: {field: "newvalue"}} | Update [{field: value}]
with [{field: newvalue}]
DELETE | /api/:collection/deleteMany?q[field][$exists]=1 | deleteMany | {field: {$exists: 1}} | Delete all documents where field
exists
A custom RESTful API with the Express query string format can be created with the following file named app.js
:
- Recommended: Install express-query-int for numeric support
npm install --save express-query-int
require('dotenv').config();
// (packages) Load required packages
var express = require('express');
var api = require('express-mongodb-rest');
var queryInt = require('express-query-int');
// (options) Initialize options object
var options = {express: {}, rest: {}, mongodb: {}};
// (options_qs) Use query string format
options.express.parse = function(query) {return(query)};
// (options_get) GET options
options.rest.GET = {};
options.rest.GET.method = 'find';
options.rest.GET.keys = ['q', 'options'];
options.rest.GET.query = {q: {}}; // return all if no query string provided
// (options_post) POST options
options.rest.POST = {};
options.rest.POST.method = 'insertMany';
// (options_put) PUT options
options.rest.PUT = {};
options.rest.PUT.method = 'updateMany';
// (options_delete) DELETE options
options.rest.DELETE = {};
options.rest.DELETE.method = 'deleteMany';
// (app) Create express app
var app = express();
app.use(queryInt()); // allow queries with numbers (optional)
app.use('/api/:collection', api(options)); // add MongoDB REST API
app.listen(3000);
Run the file app.js
defined above:
node app.js
Go to localhost:3000/api/collection/method
with a web browser to use the API, where:
collection
is the name of the collection in your MongoDB databasemethod
is the MongoDB collection method for querying
Contributions
Report Contributions
Reports for issues and suggestions can be made using the issue submission interface.
When possible, ensure that your submission is:
- Descriptive: has informative title, explanations, and screenshots
- Specific: has details of environment (such as operating system and hardware) and software used
- Reproducible: has steps, code, and examples to reproduce the issue
Code Contributions
Code contributions are submitted via pull requests:
- Ensure that you pass the Tests
- Create a new pull request
- Provide an explanation of the changes
A template of the code contribution explanation is provided below:
## Purpose
The purpose can mention goals that include fixes to bugs, addition of features, and other improvements, etc.
## Description
The description is a short summary of the changes made such as improved speeds or features, and implementation details.
## Changes
The changes are a list of general edits made to the files and their respective components.
* `file_path1`:
* `function_module_etc`: changed loop to map
* `function_module_etc`: changed variable value
* `file_path2`:
* `function_module_etc`: changed loop to map
* `function_module_etc`: changed variable value
## Notes
The notes provide any additional text that do not fit into the above sections.
For more information, see Developer Install and Implementation.
Developer Notes
Developer Install
Install the latest developer version with npm
from github:
npm install git+https://github.com/rrwen/express-mongodb-rest
Install from git
cloned source:
- Ensure git is installed
- Clone into current path
- Install via
npm
git clone https://github.com/rrwen/express-mongodb-rest
cd express-mongodb-rest
npm install
Tests
- Clone into current path
git clone https://github.com/rrwen/express-mongodb-rest
- Enter into folder
cd express-mongodb-rest
- Ensure devDependencies are installed and available
- Run tests with a
.env
file (see tests/README.md) - Results are saved to tests/log with each file corresponding to a version tested
npm install
npm test
Documentation
Use documentationjs to generate html documentation in the docs
folder:
npm run docs
See JSDoc style for formatting syntax.
Upload to Github
- Ensure git is installed
- Inside the
express-mongodb-rest
folder, add all files and commit changes - Push to github
git add .
git commit -a -m "Generic update"
git push
Upload to npm
- Update the version in
package.json
- Run tests and check for OK status (see tests/README.md)
- Generate documentation
- Login to npm
- Publish to npm
npm test
npm run docs
npm login
npm publish
Implementation
The module express-mongodb-rest uses the following npm packages for its implementation:
npm | Purpose --- | --- express | Serve REST API application to handle query string requests and data responses mongodb | Query MongoDB database using query string requests
express <-- Handle query requests and JSON responses
|
mongodb <-- query data using request
Changes
v3.1.0
- Now uses route
/:method
handlers withoptions.express.handler
andoptions.rest.<METHOD>.handler
- Added
options.express.handler
- Added
options.express.allow.method
andoptions.express.deny.method
- Removed
options.mongodb.keys
andoptions.rest.<METHOD>.keys
- Removed
options.mongodb.methods
andoptions.rest.<METHOD>.methods
- Removed
options.mongodb.method
andoptions.mongodb.query
v3.0.0
- Methods are now available in the route parameters (
/:method
) - Databases are re-added to route parameters (
/:database
) with connection pooling - Modified
options.mongodb.method
andoptions.rest.<METHOD>.method
to be the default methods - Added
options.mongodb.methods
andoptions.rest.<METHOD>.methods
as Objects defining functions that are called before and after a query - Added
options.express.method
for defining the route parameter name (such as/:method
) for the collection method used - Added
options.express.database
andoptions.rest.<METHOD>.database
- Added
options.express.deny.database
andoptions.express.allow.database
- Removed
options.mongodb.callback
andoptions.rest.<METHOD>.callback
- Removed
options.mongodb.parse
andoptions.rest.<METHOD>.parse
v2.5.0
- Now uses connection pooling for faster queries
- Removed
options.express.database
, you can no longer access at the database level through urls - Removed
options.express.deny.database
andoptions.express.allow.database
as user now has to manually set the database - Added
options.mongodb.options
for connection options
v2.0.0
- Default now uses JSON object format for url queries (
q={"field":"value"}
instead ofq[field]=value
) - Both JSON object format and query string formats supported
- Added option to change response codes for
options.express.deny.code
andoptions.express.allow.code
(See Documentation)
v1.0.0
- Initial release
- Default is query string format for url queries (
q[field]=value
)