mongo-unit
Advanced tools
Comparing version 1.4.5 to 1.5.0
156
index.js
@@ -1,5 +0,5 @@ | ||
'use strict'; | ||
'use strict' | ||
const Debug = require('debug') | ||
const portfinder = require('portfinder') | ||
const client = require('mongodb').MongoClient | ||
const MongoClient = require('mongodb').MongoClient | ||
const fs = require('fs') | ||
@@ -19,3 +19,5 @@ const ps = require('ps-node') | ||
var mongodHelper | ||
var dbUrl | ||
var dbUrl = null | ||
var client | ||
var dbName | ||
@@ -25,19 +27,21 @@ function runMogo(opts, port) { | ||
opts.port = port | ||
mongodHelper = new MongodHelper([ | ||
'--port', port, | ||
'--dbpath', opts.dbpath, | ||
'--storageEngine', 'ephemeralForTest' | ||
], { | ||
mongodHelper = new MongodHelper(['--port', port, '--dbpath', opts.dbpath, '--storageEngine', 'ephemeralForTest'], { | ||
version: opts.version, | ||
}); | ||
return mongodHelper.run() | ||
}) | ||
return mongodHelper | ||
.run() | ||
.then(() => { | ||
dbUrl = 'mongodb://localhost:' + port + '/' + opts.dbName | ||
dbName = opts.dbName | ||
dbUrl = 'mongodb://localhost:' + port + '/' + dbName | ||
debug(`mongo is started on ${dbUrl}`) | ||
return dbUrl | ||
}) | ||
.then(url => MongoClient.connect(url)) | ||
.then(dbClient => { | ||
client = dbClient | ||
}) | ||
} | ||
function start(opts) { | ||
const mongo_opts = Object.assign(defaultMongoOpts, (opts || {})) | ||
const mongo_opts = Object.assign(defaultMongoOpts, opts || {}) | ||
if (mongo_opts.verbose) { | ||
@@ -61,5 +65,7 @@ Debug.enable('mongo-unit') | ||
function stop() { | ||
mongodHelper && mongodHelper.mongoBin.childProcess.kill() | ||
dbUrl = null; | ||
return delay(100) //this is small delay to make sure kill signal is sent | ||
return client.close(true).then(() => { | ||
mongodHelper && mongodHelper.mongoBin.childProcess.kill() | ||
dbUrl = null | ||
return delay(100) //this is small delay to make sure kill signal is sent | ||
}) | ||
} | ||
@@ -76,26 +82,21 @@ | ||
function load(data) { | ||
return client.connect(getUrl()) | ||
.then(db => { | ||
const queries = Object.keys(data).map(col => { | ||
const collection = db.collection(col) | ||
return collection.insert(data[col]) | ||
}) | ||
return Promise.all(queries).then(() => db.close()) | ||
}) | ||
const db = client.db(dbName) | ||
const queries = Object.keys(data).map(col => { | ||
const collection = db.collection(col) | ||
return collection.insertMany(data[col]) | ||
}) | ||
return Promise.all(queries) | ||
} | ||
function clean(data) { | ||
return client.connect(getUrl()) | ||
.then(db => { | ||
const queries = Object.keys(data).map(col => { | ||
const collection = db.collection(col) | ||
return collection.drop() | ||
}) | ||
return Promise.all(queries).then(() => db.close()) | ||
}) | ||
const db = client.db(dbName) | ||
const queries = Object.keys(data).map(col => { | ||
const collection = db.collection(col) | ||
return collection.drop() | ||
}) | ||
return Promise.all(queries) | ||
} | ||
function drop() { | ||
return client.connect(getUrl()) | ||
.then(db => db.dropDatabase().then(() => db.close())) | ||
return client.db(dbName).dropDatabase() | ||
} | ||
@@ -105,10 +106,12 @@ | ||
portfinder.basePort = possiblePort | ||
return new Promise((resolve, reject) => portfinder.getPort((err, port) => { | ||
if (err) { | ||
debug(`cannot get free port: ${err}`) | ||
reject(err) | ||
} else { | ||
resolve(port) | ||
} | ||
})) | ||
return new Promise((resolve, reject) => | ||
portfinder.getPort((err, port) => { | ||
if (err) { | ||
debug(`cannot get free port: ${err}`) | ||
reject(err) | ||
} else { | ||
resolve(port) | ||
} | ||
}) | ||
) | ||
} | ||
@@ -118,7 +121,7 @@ | ||
try { | ||
fs.mkdirSync(dir); | ||
fs.mkdirSync(dir) | ||
} catch (e) { | ||
if (e.code !== "EEXIST") { | ||
if (e.code !== 'EEXIST') { | ||
console.log('cannot create db folder', dir, e) | ||
throw e; | ||
throw e | ||
} | ||
@@ -130,20 +133,23 @@ } | ||
return new Promise((resolve, reject) => { | ||
ps.lookup({ | ||
psargs: ['-A'], | ||
command: 'mongod', | ||
arguments: dataFolder | ||
}, (err, resultList) => { | ||
if (err) { | ||
console.log('ps-node error', err) | ||
return reject(err) | ||
ps.lookup( | ||
{ | ||
psargs: ['-A'], | ||
command: 'mongod', | ||
arguments: dataFolder, | ||
}, | ||
(err, resultList) => { | ||
if (err) { | ||
console.log('ps-node error', err) | ||
return reject(err) | ||
} | ||
resultList.forEach(process => { | ||
if (process) { | ||
console.log('KILL PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments) | ||
ps.kill(process.pid) | ||
} | ||
}) | ||
return resolve() | ||
} | ||
resultList.forEach(process => { | ||
if (process) { | ||
console.log('KILL PID: %s, COMMAND: %s, ARGUMENTS: %s', process.pid, process.command, process.arguments) | ||
ps.kill(process.pid) | ||
} | ||
}); | ||
return resolve() | ||
}) | ||
) | ||
}) | ||
@@ -153,22 +159,16 @@ } | ||
function initDb(url, data) { | ||
return client.connect(url) | ||
.then(db => { | ||
const requests = Object.keys(data).map(col => { | ||
const collection = db.collection(col) | ||
return collection.insert(data[col]) | ||
}) | ||
return Promise.all(requests).then(() => db.close()) | ||
}) | ||
const db = client.db(dbName) | ||
const requests = Object.keys(data).map(col => { | ||
const collection = db.collection(col) | ||
return collection.insertMany(data[col]) | ||
}) | ||
return Promise.all(requests) | ||
} | ||
function dropDb(url) { | ||
return client.connect(url) | ||
.then(db => { | ||
return db.collections() | ||
.then(collections => { | ||
const requests = collections.map(col => col.drop()) | ||
return Promise.all(requests) | ||
}) | ||
.then(() => db.close()) | ||
}) | ||
const db = client.db(dbName) | ||
return db.collections().then(collections => { | ||
const requests = collections.map(col => col.drop()) | ||
return Promise.all(requests) | ||
}) | ||
} | ||
@@ -175,0 +175,0 @@ |
{ | ||
"name": "mongo-unit", | ||
"version": "1.4.5", | ||
"version": "1.5.0", | ||
"description": "mongo db for unit tests", | ||
@@ -9,3 +9,3 @@ "main": "index.js", | ||
"test": "mocha test.js", | ||
"test-basic": "mocha ./examples/basic.js", | ||
"test-example": "mocha ./examples/test.it.js --delay", | ||
"release": "git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" | ||
@@ -36,4 +36,4 @@ }, | ||
"debug": "^3.0.1", | ||
"mongodb": "~2.2.31", | ||
"mongodb-prebuilt": "~6.4.0", | ||
"mongodb": "^3.3.3", | ||
"mongodb-prebuilt": "~6.5.0", | ||
"portfinder": "^1.0.10", | ||
@@ -46,4 +46,4 @@ "ps-node": "^0.1.4" | ||
"mocha": "^3.2.0", | ||
"mongoose": "^4.7.6" | ||
"mongoose": "^5.7.7" | ||
} | ||
} |
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
153159
30
23
+ Addedbl@2.2.1(transitive)
+ Addedbson@1.1.6(transitive)
+ Addeddenque@1.5.1(transitive)
+ Addedmemory-pager@1.5.0(transitive)
+ Addedmongodb@3.7.4(transitive)
+ Addedmongodb-prebuilt@6.5.0(transitive)
+ Addedoptional-require@1.1.8(transitive)
+ Addedrequire-at@1.0.6(transitive)
+ Addedsaslprep@1.0.3(transitive)
+ Addedsparse-bitfield@3.0.3(transitive)
- Removedbson@1.0.9(transitive)
- Removedbuffer-shims@1.0.0(transitive)
- Removedes6-promise@3.2.1(transitive)
- Removedmongodb@2.2.36(transitive)
- Removedmongodb-core@2.1.20(transitive)
- Removedmongodb-prebuilt@6.4.0(transitive)
- Removedprocess-nextick-args@1.0.7(transitive)
- Removedreadable-stream@2.2.7(transitive)
- Removedrequire_optional@1.0.1(transitive)
- Removedresolve-from@2.0.0(transitive)
- Removedstring_decoder@1.0.3(transitive)
Updatedmongodb@^3.3.3
Updatedmongodb-prebuilt@~6.5.0