1.3.0 • Published 4 years ago
qs2mongo v1.3.0
qs2mongo
Querystring to mongo mapper
Builds filters, projections and options for mongoose queries
- In your querystring:
attributes=comma,separated,fieldsmaps to projection{ comma: 1, separated: 1, fields: 1 }limit=1,offset=2maps to options{ limit: 1, offset: 2 }- Sorts
- Ascending:
sort=fieldmaps to options{ sort: { field: 1 } } - Descending:
sort=-fieldmaps to options{ sort: { field: -1 } }
- Ascending:
- Any other key maps to a mongo filter. Available options are:
- strict:
key=valueis{ key: value } - not strict:
key=valueis{ key: /value/gi } - $or:
key,anotherKey=valueis$or: [{key:value}, {anotherKey:value}] - $in :
key__in=a,b,ciskey: $in: ["a","b","c"] - unary operators:
key__gt=valueis{key: { $gt: "value"} }
- strict:
Basic usage
Qs2Mongo = require ("qs2mongo")
#Bind types to mongoose schema
qs2mongo = Qs2Mongo.MongooseSchema YourMongooseSchema, {...otherOptions...}
#Or manually specify types
qs2mongo = Qs2Mongo.ManualSchema {
filterableBooleans,
filterableNumbers,
filterableDates,
filterableObjectIds
}, {...otherOptions...}#...
anEndpoint: (req, res): =>
{ filters, projection, options } = qs2mongo.parse reqUsage as middleware
Qs2Mongo = require ("qs2mongo")
config = ...
qs2mongo = new Qs2Mongo config
#...
router.use qs2mongo.middleware
#...
anEndpoint: (req, res): =>
{ filters, projection, options } = req.mongoConfig
{
defaultSort,
idField = "id",
multigetIdField = "_id",
omitableProperties = Qs2Mongo.defaultOmitableProperties
}Advanced
Default driver is mongodb. If you'd like to use a custom mongodb driver, you must provide your own type conversion implementation as follows:
{
toObjectId: (it) -> #your conversion
toNumber: (it) -> #your conversion
toBoolean: (it) -> #your conversion
toDate: (it) -> #your conversion
}Note: Your conversion must return the properly converted value if it is valid or undefined otherwise.
Specify the path to your implementation in process.env.QS_TRANSFORMER_DRIVER
