Simple mongoose helpers, that:
- provide connection settings reasonable for most use cases;
- ensure all new indexes are created before closing a connection;
- simplify use of spatial functionality;
- supports fully streamable schema definition syntax;
- allow you to write more compact code.
Note: this is code is used in my private projects, but I am happy to share and to receive any contributions and feedback.
$ yarn add git://github.com/valango/mongooz.git
or
$ npm i -S git://github.com/valango/mongooz.git
.
The module exports the mongoose native Schema and the functions described below.
Creates a new mongoose connection and returns a promise to it.
service : string | {[dbName]: string, [dbURI]: string}
A service URI to use.
If dbName
is not URI string, it will be appended to dbURI
.
connectOptions : ConnectOptions
As described in Mongoose docs.
The defaults are:
autoIndex: false
keepAlive: true
keepAliveInitialDelay: 300000
useCreateIndex: true
useNewUrlParser: true
useUnifiedTopology: true
errorHandler : function(err:Error):*
The 'error'
event handler. In non-production environment, it defaults
to console.error()
.
Creates a spatial schema of typeName
type with '2dsphere' index.
typeName : string
one of ('LineString', 'MultiLineString', 'Point', 'Polygon').
Creates a model and returns it.
name : string
The name for both the model and collection (will not be pluralized).
schema : Schema | Object
connection : Connection
Creates a schema and returns it. This wrapper function around the native Schema constructor supports a simplified schema definition suitable for JSON presentation:
definition.$index
optional array of objects will be applied toschema.index()
.- a field
type
attribute (or whole value) can also be a string of ('boolean', 'date', 'number', 'string'); - for a field named 'geometry', the
createGeoSchema()
is applied; - all occurrences of
index: true
are turned intoindex: {background: false}
.
definition : Object
options : Object
As described in Mongoose docs.
NB: array types are supported, but only unstructured ones, e.g.:
field: [[number]]
- ok;
field: [{type: number}]
- will not be translated.
Creates a spatial intersect query. Needs at least two coordinate pairs.
model : Promise<any>
fields : Object
- find()
conditions.
bounds : number[][]
- polygon points (a closing one will be added if not there).
geometryField : string
- return value will go to .json()
.
Sums up all numeric fields from array of data records.
records : Array<Object>
Waits for query .lean()
results and sends data
or error
via response.json()
.
promise : Promise<any>
response : Object
- like one from express.js framework.
fieldNames : string | string[]
- space-delimited name(s) of fields to return (all by default).
translate : function(Error):Object
- return value will go to .json()
.
Waits for query .lean()
results and sends data
or error
via response.json()
.
query : Query<any,any>
response : Object
- like one from express.js framework.
translate : function(Error):Object
- return value will go to .json()
.
Updates a record or inserts a new one, returning the data record.
model : Model
data : Object
will be inspected for _id
property.
Waits until indexes of all or specified models are built.
connection : Connection
model : Model | string
- explicitly say which models to check for.