Skip to content

Repository files navigation

trace-database-response

assert a response of express if the response is raw mongodb raw object.

Install

Install with npm:

npm install trace-database-response

Usage

importmongoosefrom"mongoose";importexpressfrom"express";import{traceExpressMiddleware,traceMongoosePlugin}from"trace-database-response";constapp=express();// Enable tracing on development if(process.env.NODE_ENV!=="production"){// Express middlewareapp.use(traceExpressMiddleware());// mongoose pluginmongoose.plugin(traceMongoosePlugin());}

Example

NG: Respond mongo model directly

importmongoosefrom"mongoose";importexpressfrom"express";import{traceExpressMiddleware,traceMongoosePlugin}from"trace-database-response";// mongoose pluginmongoose.plugin(traceMongoosePlugin());constapp=express();app.use(express.json());// express middlewareapp.use(traceExpressMiddleware(options));// connect to Mongo daemonmongoose.connect("mongodb://mongo:27017/express-mongo",{useNewUrlParser: true,useUnifiedTopology: true}).catch((err)=>console.log(err));// DB schemaconstItemSchema=newmongoose.Schema({name: {type: String,required: true},date: {type: Date,default: Date.now}});constItem=mongoose.model("item",ItemSchema);app.get("/item",(req,res)=>{constname=req.query.name;if(!name){returnres.status(400).send("name query not defined");}returnItem.findOne({
name
},(error,doc)=>{if(error){res.status(400).send(error.message);}elseif(!doc){res.status(404).send("No document that match the query: "+name);}else{res.status(200).json(doc);// <= the response includes all data of mongo model}});});

OK: allow-list

importmongoosefrom"mongoose";importexpressfrom"express";import{traceExpressMiddleware,traceMongoosePlugin}from"trace-database-response";// mongoose pluginmongoose.plugin(traceMongoosePlugin());constapp=express();app.use(express.json());// express middlewareapp.use(traceExpressMiddleware(options));// connect to Mongo daemonmongoose.connect("mongodb://mongo:27017/express-mongo",{useNewUrlParser: true,useUnifiedTopology: true}).catch((err)=>console.log(err));// DB schemaconstItemSchema=newmongoose.Schema({name: {type: String,required: true},date: {type: Date,default: Date.now}});constItem=mongoose.model("item",ItemSchema);app.get("/item",(req,res)=>{constname=req.query.name;constresponseStrategy=req.query.responseStrategy;if(!name){returnres.status(400).send("name query not defined");}returnItem.findOne({
name
},(error,doc)=>{if(error){res.status(400).send(error.message);}elseif(!doc){res.status(404).send("No document that match the query: "+name);}else{// The response only includes specific datares.status(200).json({name: doc.name});}});});

Test

make up
make test

Changelog

See Releases page.

Running tests

Install devDependencies and Run npm test:

npm test

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT © azu

About

Assert: Express response raw data from MongoDB

Topics

Resources

Code of conduct

Security policy

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages