Created
March 23, 2015 20:09
-
-
Save rkamradt/8bec4479d6070c0d29b9 to your computer and use it in GitHub Desktop.
loading mongo for tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var MongoClient = require('mongodb').MongoClient; | |
var test = JSON.parse(fs.readFileSync('test/test-data.json')); | |
// Connection URL | |
var url = 'mongodb://localhost:27017/myproject'; | |
module.exports = { | |
'load': function(done) { | |
MongoClient.connect(url, function(err, db) { | |
if(err) return done(err); | |
var collection = db.collection('documents'); | |
collection.drop(function(err, result) { | |
if(err) console.log('drop: ' + err); | |
collection = db.collection('documents'); | |
var tarray = test.data; | |
collection.insert(tarray, function(err, result) { | |
if(err) return done(err); | |
db.close(); | |
done(); | |
}); | |
}); | |
}); | |
}, | |
'getAll': function(done) { | |
MongoClient.connect(url, function(err, db) { | |
if(err) return done(err); | |
var collection = db.collection('documents'); | |
collection.find({}).toArray(function(err, docs) { | |
if(err) return done(err); | |
done('', docs); | |
}); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment