New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

hapi-mongo-models

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-mongo-models - npm Package Compare versions

Comparing version 2.2.2 to 3.0.0

test/fixtures/noindex-model.js

6

index.js

@@ -37,3 +37,3 @@ var Path = require('path');

server.after(function (serverObj, done) {
server.ext('onPreStart', function (serverObj, done) {

@@ -43,3 +43,5 @@ if (autoIndex) {

models[key].ensureIndexes();
if (models[key].indexes) {
models[key].createIndexes(models[key].indexes);
}
});

@@ -46,0 +48,0 @@ }

@@ -34,24 +34,4 @@ var Joi = require('joi');

BaseModel.ensureIndexes = function (callback) {
BaseModel.createIndexes = function () {
if (!this.indexes) {
return callback && callback();
}
var self = this;
var tasks = this.indexes.map(function (index) {
return function (done) {
self.ensureIndex(index[0], index[1], done);
};
});
Async.parallel(tasks, callback);
};
BaseModel.ensureIndex = function () {
var args = new Array(arguments.length);

@@ -63,3 +43,3 @@ for (var i = 0; i < args.length; ++i) {

var collection = BaseModel.db.collection(this._collection);
collection.ensureIndex.apply(collection, args);
collection.createIndexes.apply(collection, args);
};

@@ -66,0 +46,0 @@

{
"name": "hapi-mongo-models",
"version": "2.2.2",
"version": "3.0.0",
"description": "MongoDB object models for hapi applications",

@@ -8,4 +8,3 @@ "main": "index.js",

"test": "lab -c -L",
"test-cover": "lab -c -r html -o ./test/artifacts/coverage.html && opn ./test/artifacts/coverage.html",
"update": "david update"
"test-cover": "lab -c -r html -o ./test/artifacts/coverage.html && opn ./test/artifacts/coverage.html"
},

@@ -33,5 +32,4 @@ "repository": {

"code": "^1.x.x",
"david": "^6.x.x",
"hapi": "^8.x.x",
"lab": "^5.x.x",
"hapi": "^11.x.x",
"lab": "^6.x.x",
"mongodb": "^2.x.x",

@@ -49,4 +47,4 @@ "opn-cli": "^1.x.x",

"joi": "^6.x.x",
"object-assign": "^3.x.x"
"object-assign": "^4.x.x"
}
}

@@ -50,4 +50,3 @@ # hapi-mongo-models

- [`disconnect()`](#disconnect)
- [`ensureIndexes(callback)`](#ensureindexescallback)
- [`ensureIndex(fieldOrSpec, [options], callback)`](#ensureindexfieldorspec-options-callback)
- [`createIndexes(indexSpecs, [callback])`](#createindexesindexspecs-callback)
- [`validate(input, callback)`](#validateinput-callback)

@@ -184,5 +183,5 @@ - [`validate(callback)`](#validatecallback)

- `options` - an optional object passed to MongoDB's native connect function.
- `autoIndex` - a boolean specifying if the plugin should call `ensureIndexes`
for each model. Defaults to `true`. Typically set to `false` in production
environments.
- `autoIndex` - a boolean specifying if the plugin should call `createIndexes`
for each model that has a static `indexes` property. Defaults to `true`.
Typically set to `false` in production environments.
- `models` - an object where each key is the exposed model name and each value

@@ -368,8 +367,16 @@ is the path (relative to the current working directory or absolute) of where

#### `ensureIndexes(callback)`
#### `createIndexes(indexSpecs, [callback])`
Loops over the static `indexes` array property of a model class calling
`ensureIndex`. The server plugin calls this method for each model after the
server has started.
Note: `createIndexes` is called during plugin registration for each model when
the `autoIndex` option is set to `true`.
Creates multiple indexes in the collection where:
- `indexSpecs` - an array of objects containing index specifications to be
created.
- `callback` - the callback method using the signature `function (err, result)`
where:
- `err` - if creating the indexes failed, the error reason, otherwise `null`.
- `result` - if creating the indexes succeeded, the result object.
Indexes are defined as a static property on your models like:

@@ -379,21 +386,11 @@

Kitten.indexes = [
[{ name: 1 }],
[{ email: 1 }]
{ key: { name: 1 } },
{ key: { email: -1 } }
];
```
#### `ensureIndex(fieldOrSpec, [options], callback)`
For details on all the options an index specification may have see:
Ensures that an index exists, if it does not it creates it where:
https://docs.mongodb.org/manual/reference/command/createIndexes/
- `fieldOrSpec` - an object contains the field and value pairs where the field
is the index key and the value describes the type of index for that field. For
an ascending index on a field, specify a value of `1`; for descending index,
specify a value of `-1`.
- `options` - an options object passed to MongoDB's native `ensureIndex` method.
- `callback` - the callback method using the signature `function (err, result)`
where:
- `err` - if creating the index failed, the error reason, otherwise `null`.
- `result` - if creating the index succeeded, the result object.
#### `validate(input, callback)`

@@ -400,0 +397,0 @@

@@ -14,2 +14,5 @@ var Joi = require('joi');

Dummy._collection = 'dummies';
Dummy.schema = Joi.object().keys({

@@ -21,2 +24,8 @@ name: Joi.string().required(),

Dummy.indexes = [
{ key: { name: 1 } },
{ key: { hasHat: -1 } }
];
module.exports = Dummy;

@@ -132,3 +132,3 @@ var Path = require('path');

lab.test('it successfuly connects to the db and exposes defined models and skips indexing', function (done) {
lab.test('it successfuly connects to the db, exposes defined models and skips indexing', function (done) {

@@ -171,2 +171,39 @@ var server = new Hapi.Server();

lab.test('it skips calling `createIndexes` when none are defined', function (done) {
var server = new Hapi.Server();
var Plugin = {
register: ModelsPlugin,
options: {
mongodb: Config.mongodb,
models: {
NoIndex: './test/fixtures/noindex-model'
}
}
};
server.connection({ port: 0 });
server.register(Plugin, function (err) {
if (err) {
return done(err);
}
server.start(function (err) {
if (err) {
return done(err);
}
Code.expect(server.plugins['hapi-mongo-models']).to.be.an.object();
Code.expect(server.plugins['hapi-mongo-models'].NoIndex).to.exist();
server.plugins['hapi-mongo-models'].BaseModel.disconnect();
done();
});
});
});
lab.test('it allows models to be added dynamically specifically during another plugin\'s registration', function (done) {

@@ -173,0 +210,0 @@

@@ -261,8 +261,8 @@ var Async = require('async');

lab.test('it successfully creates an index', function (done) {
lab.test('it successfully creates indexes', function (done) {
SubModel.ensureIndex({ username: 1 }, {}, function (err, indexName) {
SubModel.createIndexes([{ key: { username: 1 } }], function (err, results) {
Code.expect(err).to.not.exist();
Code.expect(indexName).to.be.a.string();
Code.expect(results).to.be.an.object();

@@ -272,45 +272,2 @@ done();

});
lab.test('it exists early when there are no indexes', function (done) {
SubModel.ensureIndexes();
SubModel.ensureIndexes(function (err, docs) {
Code.expect(err).to.not.exist();
Code.expect(docs).to.not.exist();
done();
});
});
lab.test('it successfully ensures indexes', function (done) {
SubModel.indexes = [
[{ foo: 1 }],
[{ bar: -1 }]
];
SubModel.ensureIndexes(function (err, docs) {
Code.expect(err).to.not.exist();
Code.expect(docs).to.be.an.array();
done();
});
});
lab.test('it successfully ensures indexes without a callback', function (done) {
SubModel.indexes = [
[{ foo: 1 }],
[{ bar: -1 }]
];
Code.expect(SubModel.ensureIndexes()).to.equal(undefined);
done();
});
});

@@ -317,0 +274,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc