2.1.1 • Published 6 years ago

buildreq v2.1.1

Weekly downloads
15
License
MIT
Repository
github
Last release
6 years ago

BuildREQ

npm downloads dependencies npm-issues js-standard-style Build Status js-standard-style

What is Build Response,Routing,Error & Query?

It is currently a Mongoose & Express dependent module. It can be used in a different ways like building out dynamic routes, query filter in conjuction with your schema, Error handler & Standard response. It is most useful when used as a middleware with your expressjs api.

Build.query

console.log(build.query({mongoose:mongoose,req:req})

Build.queryMiddleware

app.use(build.queryMiddleware())
//or send mongoose in if your having loading issues
app.use(build.queryMiddleware({mongoose:mongoose}))

Configs

build.config({
  query: {
  //configs go here
  }
})

Build.error

app.use(build.error())

Build.response

build.response(res, {
    method: 'json',
    query: req.queryParameters,
    hostname: req.get('host') + req.path,
    route: req.route,
    data: 'no data'
})

Build.responseMiddleware

app.use(build.responseMiddleware())
res.response(res, {
    count: results.count,
    method: 'json',
    query: req.queryParameters,
    hostname: req.get('host') + req.baseUrl,
    route: req.route,
    data: results.get,
    type: Campaign
})

Build.routing

var blogSchema = mongoose.Schema({
  created: {
    type: Date,
    default: Date.now
  }
})
Blog = mongoose.model('Blog', blogSchema)
//var route = build.routing(app)
//console.log(route)
//or
build.routing({
    app:app,
    mongoose: mongoose
},function(error,data){
    console.log(data)
})

// Routes that will be created for you
// http://localhost:3000/api/v1/blog - GET,POST | GETALL - CREATE 
// http://localhost:3000/api/v1/blog/id/:blogId - PUT,DELETE,GET | UPDATE DELETE VIEWONE
// http://localhost:3000/api/v1/blog/aggregate - GET | USE AGGREGATION FRAMEWORK
// http://localhost:3000/api/v1/blog/fields/ - GET | GETS ALL FIELDS IN SCHEMA
// http://localhost:3000/api/v1/blog/options/ - GET | GETS ALL OPTIONS IN SCHEMA
// http://localhost:3000/api/v1/blog/_indexes/ - GET | GETS ALL INDEXES IN SCHEMA

//You Can Inject mongoose in your routing if need be
//You can also wait for the connection to openbefore injecting it
var mongoose = require('mongoose')

  mongoose.connection.onOpen(function(){
    //console.log(mongoose.connection)
    console.log('open')
    //console.log(mongoose.models)
    build.routing(app,mongoose)
  })


// OR you can  build it on your end by setting the config to not build
var build = require('buildreq')(
    routing: {
        schema: true,
        url: '/api/v1/',
        build: false
    }
)

_.forEach(build.routing(app, mongoose), function (m) {
  app.use(m.route, m.app)
})

Configs

build.config({
  routing: {
  //configs go here
  }
})

Build.config

var build = require('../buildreq')
build.config({configs: 'here'}-->)

R - Response Builder

The next common way to use this module is to have it build your api response so that you have a consistent format. This response is dynamic enough right off the bat to do logic based on actions you wish to give your frontend. Great thing is if you don’t like some of the fields you can delete them in the configs.

KeyDescriptionDefault Value
methoduses the GET method by defaultget
datauses empty object by default{}
useruses empty object by default{}
countuses zero by default0
hostnameuses empty string by default''
queryuses empty object by default{}
typeuses empty string by default''
actions.prevturns on action.prev by defaulttrue
actions.nextturns on action.next by defaulttrue
actions.reloadturns on action.reload by defaulttrue
middlewareallows you to place middle ware on the routesauth: [],noauth: [],all: [],admin:[]
deletedeletes response objects[]

R - Routing Builder

It is a optional routing builder . what it does is it creates CRUD routes and interacts with the database based off of the shcema from mongoose. More to come on it later.

KeyDescriptionDefault Value
schemauses mongoose schema by default - N/A taken out for now[]
urlchange the default url that the routing is built with'/api/v1/'
buildchange the default to false to manually mount the routing'true'
middlewarechange the default to false to manually mount the routing{ auth: [], noauth: [], all: [] }
removeremove mongoose models by name if you dont want them routed ex ['Users'] |[]`
deepPopulateOptionsMongoose populate options{whitelist: [],populate: {},rewrite: {}}
deepPopulateOptions.whitelistMongoose populate options[]
deepPopulateOptions.populateMongoose populate options{}
deepPopulateOptions.rewriteMongoose populate options{}

E - Error

The error is still being worked on to make better but currently it is a base level error handler

Q - Query Builder

The most common used way is as a dynamic query builder as express middleware. It watches on the “req.query “ to see how you users interacting with it. Once it captures the data it will then check it again all of you defined mongoose schemas. By doing that it allows the builder to know what to allow and what not to all. This will give you a dynamic api query handler with out having to code anything at all . All you need to do is to tell express to use the module as middleware “app.use(buildReq.query);”

KeyDescriptionDefault Value
strictuses strict to make it follow strict to mongoosefalse
sortuses created field by default'-created'
filteruses empty object by default otherwise it finds dynamically based of schema{}
limituses empty string by default20
selectuses empty string by default''
populateIduses empty string by default''
populateItemsuses empty string by default''
limitToPopulateIduses empty string by default''
limitToPopulateItemsuses empty string by default''
deepPopulateuses empty string by default''
leanuses empty string by default''
skipuses empty string by default0
whereuses empty string by default''
gt:uses where for GreaterThanfalse
gte:uses where for GreaterThanEqualfalse
lte:uses where for LessThanEqualfalse
lt:uses where for LessThanfalse
in:uses where for IN arrayfalse
ne:uses where for NE Not Equalfalse
nin:uses where for NIN not in arrayfalse
regex:uses where with options for regexfalse
options:uses where regex requiredfalse
size:uses where for SIZE of arrayfalse
all:uses where for ALLfalse
find:uses where for FINDfalse
equals:uses where for EQUALSfalse
aggregate:uses Aggregation Framework with object you sendfalse
errorMessageuses string by default when user passes bad valueUnknown Value
deleteuses empty array by default[]
mongooseuses boolean to use mongoose to to have customtrue
schemauses empty [] if you dont use a custom schema[]
KeyExample urls
gt & lthttp://localhost:3000/api/v1/campaigns?where=created&gt=2015-11-17&lt=2015-12-30
equalshttp://localhost:3000/api/v1/campaigns?where=email&equals=john@greenpioneersolutions.com
inhttp://localhost:3000/api/v1/campaigns?where=emails&in=javier@greenpioneersolutions.com
nehttp://localhost:3000/api/v1/campaigns?where=email&ne=john@greenpioneersolutions.com
ninhttp://localhost:3000/api/v1/campaigns?where=emails&nin=javier@greenpioneersolutions.com
regex & optionshttp://localhost:3000/api/v1/campaigns?where=email&regex=\/com\/&options=%3Coptions%3E
sizehttp://localhost:3000/api/v1/campaigns?where=emails&size=2
allhttp://localhost:3000/api/v1/campaigns?where=email&all=shawn@greenpioneersolutions.com
findhttp://localhost:3000/api/v1/campaigns?where=email&find=shawn@
aggregatehttp://localhost:3000/api/v1/campaigns/task/aggregated?aggregate[$unwind]=$donations&aggregate[$group][_id]=$_id&aggregate[$group][balance][$sum]=$donations.amount

Future

-more docs -debug

Contributing

Looking for anyone that could have a use for this module in his or her daily life to help contribute .

Examples

Take a look at my examples

cd /YOURDIRECTORY/
npm install buildreq
cd ex/
node app.js

Created by Green Pioneer

This is on GitHub

Find us on GitHub

Find us on Twitter

Find us on Facebook

Find us on The Web

2.1.1

6 years ago

2.1.0

8 years ago

2.0.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago

0.9.5

8 years ago

0.9.4

8 years ago

0.9.3

8 years ago

0.9.2

8 years ago

0.9.1

8 years ago

0.9.0

8 years ago

0.4.44

8 years ago

0.4.43

8 years ago

0.4.42

8 years ago

0.4.41

8 years ago

0.4.4

8 years ago

0.4.33

8 years ago

0.4.32

8 years ago

0.4.31

8 years ago

0.4.3

8 years ago

0.4.2

8 years ago

0.4.1

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.6

8 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago