
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
db-to-express-rest
Advanced tools
A module to create Express 4 REST API routes automagically from MongoDB or NeDB document collections.
A module to create Express 4 REST API routes automagically from MongoDB or NeDB document collections.
db-to-express-rest allows you to create REST API routes for a document collection with a simple one-liner.
var dbtoexpress = require("db-to-express-rest");
app.use("/api", dbtoexpress("artists"));
dbtoexpress()
with a collection name.use()
the returned value on your Express app.The call to dbtoexpress()
returns an express.Router that is easy
to app.use()
on your Express 4 app.
This means that db-to-express-rest generates the usual REST API methods for you to easily expose a document collection.
npm install db-to-express-rest
Creating API endpoints and collections for several kind of documents at once.
var express = require("express"),
dbtoexpress = require("db-to-express-rest"),
app = express();
app.use("/api", dbtoexpress("records"));
app.use("/api", dbtoexpress("artists"));
app.use("/api", dbtoexpress("genres"));
app.listen(3000);
This module uses internally NeDB for simple file-based document collections and Monk for interfacing with MongoDB document collections. The trick is that these modules implement the same interface for finding, inserting, updating and removing documents from collections.
The returned express.Router
provides the following REST actions
.find()
method..findOne()
method..insert()
method..update()
method..delete()
method.See Exposed REST API for further explanation.
Using a file-based storage is the default behaviour. You can
NeDB
file-based collections.See API for more details on creation.
pass the option dbtype
to dbtoexpress()
.
var express = require("express"),
dbtoexpress = require("db-to-express-rest"),
app = express();
app.use("/api", dbtoexpress("records", {dbtype: "mongodb"}));
app.use("/api", dbtoexpress("artists", {dbtype: "mongodb"}));
app.listen(3000);
By default, it will connect to localhost
and use (create) a database named mydb
.
NeDB
file-based collections.See API for more details on creation.
Pass the option uri
to dbtoexpress
to use a specific database in a MongoDB instance
app.use("/api", dbtoexpress("records", {
dbtype: "mongodb",
uri: "190.220.8.211/mydatabase"
}));
Validating data is as simple as using a middleware like express-form2.
You just set a middleware prior to app.use()
ing the router created by db-to-express-rest.
You may think that expres-form2 is exclusive for form-submitted data but it acts
on req.body
so it will be as useful on a JOSN encoded body as with a URL encoded one.
validate = require("express-form2");
app.use("/api", expressrest("cars", {}, [
validate(
field("title").isNumeric(),
field("content").required()
),
function(req, res, next) {
if (!req.form.isValid) {
return res.status(400).json(req.form.errors);
}
next();
}
]));
The db-to-express-rest module returns a function. You specify a collection and options arguments to the main module function. For example:
var dbtoexpress = require("db-to-express-rest");
Arguments
collection
- Required - A string or an object.
{String} - If you provide a string, that string will be used as the collection name. If you're using NeDB, that will be the name of the file too. If you're using MongoDB, that will be the name of the collection.
{Object} - If you provide an instanceof nedb()
or an instance of monk.Collection
,
it will be used as the exposed collection. The collection's name for the HTTP routes will be figured out by the
filename if using NeDB or by the Mongo collection name used if you're using MongoDB.
options
- Optional - {Object}
dbtype
- {String} 'nedb'
or 'mongodb'
. Only required if the
collection
argument is a string. Default: 'nedb'
.uri
- {String} - Connection URI string for a MongoDB database. The URI is
a standard MongDB MongoURI.
For example: '190.220.8.121/mydatabase'
. Default: 'localhost/mydb'
. Only valid if using mongodb.filename
- {String} - a filename for NeDB storage. Only valid if using NeDB.
Default: join(process.cwd(), "db", "{collection}.db")
middleware
- Optional - {Array} - An array of middleware that will be attachend
on POST
and PUT
methods before saving changes.
Returned value
The module creates an express.Router() and returns it. It also adds two properties to this object:
collection
: {Object} - The collection object used for queries.collectionName
- {String} - The collection name. This is also the collection name used
for HTTP routes for the REST endpoints.Example on using the extra properties on the returned value.
var express = require("express"),
dbtoexpress = require("db-to-express-rest"),
app = express();
var dbroutes = dbtoexpress("records", {dbtype: "mongodb"});
app.use(dbroutes);
console.log("Exposing REST endpoints for collection %s", dbroutes.collectionName)
All routes respond with Content-type: application/json
.
Supported REST API requests:
Returns all documents in the specified collection.
Insert new document in collection (document in POST body).
Returns document with _id
.
Update document with _id=
(updated document in PUT body)
Delete document with _id
.
Make sure application/json
is used as Content-Type when using POST/PUT with request bodies.
Also accepts url-encoded
parameters coming from a form. (this module loads
body-parser's .json()
and
urlencoded
mechanisms )
FAQs
A module to create Express 4 REST API routes automagically from MongoDB or NeDB document collections.
The npm package db-to-express-rest receives a total of 2 weekly downloads. As such, db-to-express-rest popularity was classified as not popular.
We found that db-to-express-rest demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.