Skip to content

Commit

Permalink
Merge pull request seanemmer#19 from ohcibi/feature/add-disconnect-fu…
Browse files Browse the repository at this point in the history
…nction

Add disconnect function
  • Loading branch information
seanemmer authored Oct 24, 2017
2 parents b362ecb + ec38026 commit a2b6cdc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ var seeder = require('mongoose-seed');
// Connect to MongoDB via Mongoose
seeder.connect('mongodb://localhost/sample-dev', function() {

// Load Mongoose models
seeder.loadModels([
'app/model1File.js',
'app/model2File.js'
]);
// Load Mongoose models
seeder.loadModels([
'app/model1File.js',
'app/model2File.js'
]);

// Clear specified collections
seeder.clearModels(['Model1', 'Model2'], function() {
// Clear specified collections
seeder.clearModels(['Model1', 'Model2'], function() {

// Callback to populate DB once collections have been cleared
seeder.populateModels(data, function() {
//seeder.disconnect();
});
// Callback to populate DB once collections have been cleared
seeder.populateModels(data, function() {
seeder.disconnect();
});

});
});
});

// Data array containing seed data - documents organized by Model
Expand All @@ -44,8 +44,6 @@ var data = [
]
}
];


```

## Methods
Expand All @@ -71,3 +69,10 @@ Clears DB collection specified by each model in modelArray. Callback is execute
### seeder.populateModels(dataArray, [callback])

Populates MongoDB with documents in dataArray. dataArray consists of objects with 'model' and 'documents' keys, where 'documents' is an array of valid collection documents. Note that Mongoose Schema validation *is* enforced.

---

### seeder.disconnect()

Disconnects mongoose db-handle. Use it inside `populateModels` callback to cleanly exit the program
(see example above).
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,8 @@ Seeder.prototype.populateModels = function(seedData, cb) {
});
};

Seeder.prototype.disconnect = function disconnect() {
mongoose.disconnect();
};

module.exports = new Seeder();

0 comments on commit a2b6cdc

Please sign in to comment.