valango / mongooz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mongooz

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.

Installation

$ yarn add git://github.com/valango/mongooz.git

or

$ npm i -S git://github.com/valango/mongooz.git.

API

The module exports the mongoose native Schema and the functions described below.

connect(service, [connectOptions], [errorHandler]) : Promise<Connection>

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().

createGeoSchema(typeName) : Schema

Creates a spatial schema of typeName type with '2dsphere' index.

typeName : string
one of ('LineString', 'MultiLineString', 'Point', 'Polygon').

createModel(name, schema, connection) : Model

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

createSchema(definition, [options]) : Schema

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 to schema.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 into index: {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.

findBounded(model, fields, bounds, [geometryField]) : Query

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().

getTotals(records): Object

Sums up all numeric fields from array of data records.

records : Array<Object>

postJSON(promise, response, [fieldNames], [translate])

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().

postQuery(query, response, [translate])

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().

saveOne(model, data) : Promise<Object>

Updates a record or inserts a new one, returning the data record.

model : Model

data : Object will be inspected for _id property.

syncIndexes(connection, [...model]) : Promise<*[]>

Waits until indexes of all or specified models are built.

connection : Connection

model : Model | string - explicitly say which models to check for.

Links

About

License:MIT License


Languages

Language:JavaScript 100.0%