Skip to content

Commit

Permalink
Merge branch 'develop-0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
mgan59 committed Nov 19, 2013
2 parents 5fbd0a9 + 45dffa7 commit e3e1667
Show file tree
Hide file tree
Showing 30 changed files with 347 additions and 65 deletions.
11 changes: 11 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# Mongoose-Fixture Changelog


## Release 0.3.0

The mongoose/mongo connection was adjusted to use `mongoose.createConnection` instead of a direct call to `mongoose.connect` this allows the usage of multiple mongo nodes. These can be configured in the `mongoose-fixture-config.js`. Backwards compatibility was maintained for single connections. However, with the usage of `createConnect` a mongo-native db instance is created and needs to be passed around so that the bound models can be accessed. Before the models could be fetched directly from the mongoose singleton but that is no longer supported.

* `createConnection` now utilized
* support for multiple hosts in `mongoose-fixture-config`
* some more documentation
* tests added for multiple nodes
* test mongo conf files adjusted, now have replica support and multiple-mongo nodes


## Release 0.2.4

Last release for the 0.2.x line as 0.3.0 will have breaking changes in the data-fixture method signature
Expand Down
8 changes: 3 additions & 5 deletions lib/BaseFixtureLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ colors.setTheme({

// rootCallback came from the mongoose-fixture top level async.seriesEach
// it must be executed on the loaders async completion to propogate back up the chain
module.exports = function(fixtureConfig, mongoose, rootCallback){
module.exports = function(fixtureConfig, mongoose, conn, rootCallback){
//console.log('fired ', fixtureConfig);

//unpack fixtureConfig
var fixtureItem = fixtureConfig.itemName;
var fixtureSchemaKey = fixtureConfig.schema;
Expand All @@ -38,7 +37,7 @@ module.exports = function(fixtureConfig, mongoose, rootCallback){
// this callback allows one to add internal/additional async branches
// within a fixture, specifically to load embeddable documents
// hooks are available for a plugin system or future features
var fixtureData = require(fixtureConfig.dataFixturePath+fixtureDataKey)(mongoose, function(err, globalFixtureData){
var fixtureData = require(fixtureConfig.dataFixturePath+fixtureDataKey)(mongoose, conn, function(err, globalFixtureData){

// our kludge/namespace for the fixtureSet we plan to insert
var fixtureSet = null;
Expand Down Expand Up @@ -71,10 +70,9 @@ module.exports = function(fixtureConfig, mongoose, rootCallback){
// and by collection that will be determind by the correspoding model/schema

// time to move the schemas
// make a schema for 'facilities', use set-option to use correct collection name
// use model .create to staticically create each record
// then have
var fixtureModel = mongoose.model(fixtureItem, fixtureSchema);
var fixtureModel = conn.model(fixtureItem, fixtureSchema);

var ctr = 0;
// iterate over each document in the data fixture array, call a create and then propogate
Expand Down
5 changes: 3 additions & 2 deletions lib/BoilerPlates/data-fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
// @callback must be returned
// expects (err, object)
// object can be an array of data-documents, or a kwarg['dataFixtures']

module.exports = function(mongoose, callback){
// mongoose is the instance of mongoose being used (has schemas)
// conn is the current native mongodb connection (will contain models)
module.exports = function(mongoose, conn, callback){

// standard callback error
var error = null;
Expand Down
35 changes: 22 additions & 13 deletions lib/MongooseFixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ colors.setTheme({
});
// pass in an array of obj-lits that contain all our fixture info
// along with a mongoose db-handler
module.exports = function(selectedFixtures, mongoose){
module.exports = function(selectedFixtures, mongoose, conn){
var that = {};
that.hostString = conn.host+':'+conn.port;

// the add method
that.add = function(params, rootCallback){
var autoDisconnect = true;
Expand All @@ -39,17 +41,19 @@ module.exports = function(selectedFixtures, mongoose){
async.eachSeries(selectedFixtures,
function(fixtureConfig, callback){
// use our generic loader and load fixture
baseFixtureLoader(fixtureConfig, mongoose, callback);
baseFixtureLoader(fixtureConfig, mongoose, conn, callback);
},
function(err){
if(autoDisconnect){
// display interface commands
console.log('Fixtures Loaded');
mongoose.disconnect(function(err){
console.log('Fixtures Loaded on ('+that.hostString+')');

conn.close(function(err){
if(err){
console.log('WARNING: mongoose errord while disconnecting');
};
console.log('mongoose - disconnect'.mongoose);
var disconnectMessage = 'mongoose - disconnect ('+that.hostString+')';
console.log(disconnectMessage.mongoose);
}); //< end mongo-disconnect closure
}else{
if(err){
Expand All @@ -73,7 +77,7 @@ module.exports = function(selectedFixtures, mongoose){
async.eachSeries(selectedFixtures,
function(item, callback){
// make a call to mongoose to get all the collection names
mongoose.connection.db.collectionNames(function(err,names){
conn.db.collectionNames(function(err,names){
// set our collectionExists flag to false, prove one exists
var collectionExists = false;
var name = '';
Expand All @@ -91,7 +95,7 @@ module.exports = function(selectedFixtures, mongoose){
});

if(collectionExists){
mongoose.connection.db.collection(item.collection, function(err, result){
conn.db.collection(item.collection, function(err, result){
if(err){
callback(err);
}
Expand All @@ -117,14 +121,17 @@ module.exports = function(selectedFixtures, mongoose){
// display interface commands
console.log(parentErr.error);
} else {
console.log('Fixtures removed, closing db');
console.log('Fixtures removed, closing db('+that.hostString+')');
}
mongoose.disconnect(function(err){

conn.close(function(err){
if(err){
console.log('WARNING: mongoose errord while disconnecting');
};
console.log('mongoose - disconnect'.mongoose);
var disconnectMessage = 'mongoose - disconnect ('+that.hostString+')';
console.log(disconnectMessage.mongoose);
}); //< end mongo-disconnect closure

}else{
if(parentErr){
return rootCallback(parentErr);
Expand Down Expand Up @@ -162,13 +169,15 @@ module.exports = function(selectedFixtures, mongoose){
console.log('Reset of Fixtures Complete');
console.log('Fixtures Loaded');
}
mongoose.disconnect(function(err){

conn.close(function(err){
if(err){
console.log('WARNING: mongoose errord while disconnecting');
};
console.log('mongoose - disconnect'.mongoose);
var disconnectMessage = 'mongoose - disconnect ('+that.hostString+')';
console.log(disconnectMessage.mongoose);
}); //< end mongo-disconnect closure

}
);
};
Expand Down
59 changes: 37 additions & 22 deletions lib/MongooseFixtureCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
*/
var npmPackage = require('../package.json');
var _ = require('lodash');
var fs = require('fs');
var argv = require('optimist')
.usage('MongooseFixture is a command line interface to load fixture data into mongodb')
Expand Down Expand Up @@ -269,36 +270,50 @@ if(argv.add === false && argv.reset === false && argv.remove === false){
var mongoose = require('mongoose');
// get database name from config :)
var mongoSettings = fixtureConfig.mongoConnection;
var mongoConnectionString = mongoSettings.host+':'+mongoSettings.port+'/'+mongoSettings.dbname;
var mongoConnectionString = '';
var mongoConnectionSet = [];

mongoose.connection.on('open', function (ref) {
console.log('Connected to mongo server'.mongoose);
// instantiate an instance of mongoose-fixture
// to scope hoist mongoose and fixtureConfig listings
var mongooseFixture = MongooseFixture(fixtureListing, mongoose);
// Going to check if an array of mongoHosts was defiend if so we build a longer
// url of all the instance we need to connect to and execute
if(_.isArray(mongoSettings.servers)){
_.each(mongoSettings.servers, function(setting){
mongoConnectionSet.push('mongodb://'+setting.host+':'+setting.port+'/'+mongoSettings.dbname);
});

// Handle fixture actions
if(argv.add){
mongooseFixture.add();
}
// if no array then execute over the obj-literal to build a connection string
} else {
mongoConnectionSet.push('mongodb://'+mongoSettings.host+':'+mongoSettings.port+'/'+mongoSettings.dbname);
}

if(argv.reset){
mongooseFixture.reset();
}
_.each(mongoConnectionSet, function(connectionString){

if(argv.remove){
// call removal
mongooseFixture.remove();
}
var conn = mongoose.createConnection('mongodb://'+connectionString);

// bind on open listener for each conn
conn.on('open',function(){
var hostString = conn.host+':'+conn.port;
var connectionMessage = 'Established Connection MongoD ('+hostString+')';
console.log(connectionMessage.mongoose);
var mongooseFixture = MongooseFixture(fixtureListing, mongoose, conn);

});

mongoose.connection.on('error', function (err) {
console.log('Error - Could not connect to mongo server!'.error);
console.log(err);
// Handle fixture actions
if(argv.add){
mongooseFixture.add();
}

if(argv.reset){
mongooseFixture.reset();
}

if(argv.remove){
// call removal
mongooseFixture.remove();
}
});

});

mongoose.connect('mongodb://'+mongoConnectionString);



Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "mongoose-fixture",
"preferGlobal": true,
"version": "0.2.3",
"version": "0.3.0",
"author": "Morgan Craft (http://morgancraft.com)",
"description": "Static Fixture Loader with Mongoose Schema",
"description": "Static Fixture Loader with Mongoose ORM",
"main": "./lib/index.js",
"bin": "./bin/mongoose-fixture",
"repository": {
Expand Down
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ Mongoose-Fixture comes with the following features to improve developer workflow
* BoilerPlates, generate Schemas/Fixtures to reduce typing
* API can be used within other processes, a nodejs event-emitter

# Migrating to Release 0.3.0

There were api adjustments moving from version 0.2.x to 0.3.0, specifically within your data-fixtures. In order to support the new 0.3.0 release you need to adjust the method signature in all your data fixtures.

// 0.2.x data fixture method signature
module.exports = function(mongoose, callback){
// function body
};

// new 0.3.0 data fixture method signature
module.exports = function(mongoose, conn, callback){
// function body
};

# Getting Started

Expand Down
43 changes: 39 additions & 4 deletions test/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ colors.setTheme({
var FIXTURE_CONFIG_PRESENCE_FILE = 'mongoose-fixture-config.js';
// used as a mock fixture
var FIXTURE_CONFIG_MOCK_FILE = 'mongoose-fixture-config-mock.js';

var FIXTURE_MULTINODE_CONFIG_MOCK_FILE = 'mongoose-fixture-multinode-config-mock.js';
// SCHEMA/FIXTURE Presence
// These are the ones that get generated/deleted on each test run
var FIXTURE_PRESENCE = 'Products';
Expand All @@ -42,6 +42,8 @@ var SCHEMA_MOCK = 'ProductSchemaMock.js';

// CLI Stubs
var CLI_MOCK = 'mongoose-fixture --configFile='+FIXTURE_CONFIG_MOCK_FILE+' ';
var CLI_MULTI_MOCK = 'mongoose-fixture --configFile='+FIXTURE_MULTINODE_CONFIG_MOCK_FILE+' ';


///////////////////////
// Connect to Mongo
Expand Down Expand Up @@ -307,9 +309,8 @@ test('Test Products-Object-Literal-Mock fixture --fixture=objlit --add'.testDefi
// use the fixture-config-mock to actually generate a fixture file
var cmd = CLI_MOCK+' --fixture="objlit" --add';
process.exec(cmd, function(err, stdout, stdin){

// verify stdout message for fixtures loaded
var txtMatch = stdout.match(/Fixtures Loaded/);
var txtMatch = stdout.match(/Fixtures Loaded on \(localhost:27999\)/);
t.ok(txtMatch, 'Fixture loaded confirmed from stdout'.testOutput);

// check there are now 3 items
Expand All @@ -324,6 +325,7 @@ test('Test Products-Object-Literal-Mock fixture --fixture=objlit --add'.testDefi
});
// do not breakup the follow two test, unless you understand why they are grouped ^^

/* commenting out test so I can make sure there is data in mongo, this will reset it all */
test('Test Products-Mock fixture data using --reset'.testDefinition, function(t){
t.plan(2);

Expand All @@ -339,10 +341,43 @@ test('Test Products-Mock fixture data using --reset'.testDefinition, function(t)
var msg = 'Checking Mongo collection contains '+products.length+' product(s)';
t.ok((products.length === 2), msg.testOutput);
// last test so disconnect mongoose
mongoDisconnect(mongoose);
//mongoDisconnect(mongoose);
t.end();
});

});

});

/*
* This is the test for the multi node instances, comment out if you don't want to run it every time
*/
/* */
test('Test Mulitnode Products-Mock fixture data using --add'.testDefinition, function(t){
t.plan(2);

// use the fixture-config-mock to actually generate a fixture file
var cmd = CLI_MULTI_MOCK+' --fixture="big" --add';
process.exec(cmd, function(err, stdout, stdin){
/*
* This test will work differently than the others since we need to check two mongo instaces
* for data and we don't have the proper scaffolding to drop the dbs and recreate
* so just check to ensure that 'Fixtures Loaded on (host:port) exists for the specified instances
*
*/
// verify stdin message for fixtures loaded
var txtMatchOne = stdout.match(/Fixtures Loaded on \(localhost:27999\)/);
var txtMatchTwo = stdout.match(/Fixtures Loaded on \(localhost:27998\)/);
t.ok(txtMatchOne, 'Fixture loaded confirmed from stdout for localhost:27999'.testOutput);
t.ok(txtMatchTwo, 'Fixture loaded confirmed from stdout for localhost:27998'.testOutput);


// last test and we don't need mongoose to check anything so disconnect now
mongoDisconnect(mongoose);
t.end();

});

});
/* */

2 changes: 1 addition & 1 deletion test/fixtures/ProductsBigMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// expects (err, object)
// object can be an array of data-documents, or a kwarg['dataFixtures']

module.exports = function(mongoose, callback){
module.exports = function(mongoose, conn, callback){

// standard callback error
var error = null;
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/ProductsMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// expects (err, object)
// object can be an array of data-documents, or a kwarg['dataFixtures']

module.exports = function(mongoose, callback){
module.exports = function(mongoose, conn, callback){

// standard callback error
var error = null;
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/ProductsObjectLiteralMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// @callback must be returned
// expects (err, object)
// object can be an array of data-documents, or a kwarg['dataFixtures']
module.exports = function(mongoose, callback){
module.exports = function(mongoose, conn, callback){

// standard callback error
var error = null;
Expand Down
7 changes: 0 additions & 7 deletions test/mongo-conf

This file was deleted.

File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions test/mongod-confs/mongo_test_replica_b_db/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!data/
!.gitignore
2 changes: 2 additions & 0 deletions test/mongod-confs/mongo_test_replica_b_db/data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
3 changes: 3 additions & 0 deletions test/mongod-confs/mongo_test_replica_c_db/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!data/
!.gitignore
2 changes: 2 additions & 0 deletions test/mongod-confs/mongo_test_replica_c_db/data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
Loading

0 comments on commit e3e1667

Please sign in to comment.