Skip to content
This repository was archived by the owner on Jul 14, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var mongoose = require('mongoose'),
fs = require('fs'),
path = require('path'),
autoIncrement = require('mongoose-auto-increment'),
extend = require('extend');

module.exports = exports = {
Expand All @@ -18,15 +17,8 @@ module.exports = exports = {
connectionString: 'mongodb://localhost/test',
// The path to the directory where your dbmodels are stored.
modelsDir: path.join(__dirname, '..', '..', 'dbmodels'),
// Whether or not simpledb should auto-increment _id's of type Number.
autoIncrementNumberIds: true,
autoIncrementSettings: {
field: "_id", // Field name of id, _id by default
startAt: 0, // The number the count should start at.
incrementBy: 1 // The number by which to increment the count each time.
},
//default options of connect, can be extended, or changed
options: { server: { socketOptions: { keepAlive: 1 } } }
options: { keepAlive: 300000, connectTimeoutMS: 30000 }
};

switch (arguments.length) {
Expand Down Expand Up @@ -82,9 +74,6 @@ module.exports = exports = {

// Once the connection is open begin to load models from the database.
db.connection.once('open', function () {
// If mongoose-auto-increment plugin is installedInitialize mongoose-auto-increment plugin.
if (settings.autoIncrementNumberIds)
autoIncrement.initialize(db.connection);

// Find and load all Mongoose dbmodels from the dbmodels directory.
fs.readdir(settings.modelsDir, function (err, files) {
Expand Down Expand Up @@ -142,14 +131,12 @@ module.exports = exports = {
schema.post(key, record);
}

// If autoIncrementIds:true then utilize mongoose-auto-increment plugin for this model.
if (settings.autoIncrementNumberIds) {
var field = settings.autoIncrementSettings.field;
if (schema.paths.hasOwnProperty(field) && schema.paths[field].instance === 'Number') {
settings.autoIncrementSettings.model = modelName;
schema.plugin(autoIncrement.plugin, settings.autoIncrementSettings);
}
}
//Array of indexes
if (modelData.hasOwnProperty('indexes'))
for (var i=0; i<modelData.indexes.length; i++) {
var index = modelData.indexes[i];
schema.index(index.fields, index.options);
}

// If model name contains an underscore then camelCase it.
var propName = modelName.charAt(0).toUpperCase() + modelName.slice(1);
Expand Down Expand Up @@ -177,7 +164,9 @@ module.exports = exports = {
},

// Expose mongoose types for easy access.
Types: mongoose.Schema.Types,
schemaTypes: mongoose.Schema.Types,

Types: mongoose.Types,

// Expose all of mongoose for easy access.
mongoose: mongoose
Expand Down
281 changes: 281 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
"description": "Simple API for defining mongoose dbmodels and loading them into a single object for easy access.",
"main": "index.js",
"dependencies": {
"extend": "^3.0.0",
"mongoose-auto-increment": "^5.0.1"
"extend": "^3.0.0"
},
"peerDependencies": {
"mongoose": "^4.0.0"
"mongoose": "^5.0.0"
},
"devDependencies": {
"mocha": "*",
Expand Down