mongo-unit
Advanced tools
Comparing version 1.1.0 to 1.2.0
75
index.js
'use strict'; | ||
//process.env.DEBUG= '*' | ||
const Debug = require('debug') | ||
const portfinder = require('portfinder') | ||
const {MongodHelper} = require('mongodb-prebuilt') | ||
const client = require('mongodb').MongoClient | ||
const fs = require('fs') | ||
const ps = require('ps-node'); | ||
const ps = require('ps-node') | ||
const debug = Debug('mongo-unit') | ||
@@ -14,8 +14,4 @@ const dataFolder = '/.mongo-unit' | ||
dbName:'test', | ||
auto_shutdown: true, | ||
args: { | ||
storageEngine: "ephemeralForTest", | ||
dbpath: defaultTempDir, | ||
port: 27017 | ||
} | ||
dbpath: defaultTempDir, | ||
port: 27017, | ||
} | ||
@@ -25,19 +21,27 @@ | ||
function runMogo(opts, port){ | ||
const MongodHelper = require('mongodb-prebuilt').MongodHelper | ||
opts.port = port | ||
const mongodHelper = new MongodHelper(['--port', port, '--dbpath', opts.dbpath,'--storageEngine', 'ephemeralForTest']); | ||
return mongodHelper.run() | ||
.then(()=>{ | ||
dbUrl = 'mongodb://localhost:' + port+'/'+opts.dbName | ||
debug(`mongo is started on ${dbUrl}`) | ||
return dbUrl | ||
}) | ||
} | ||
function start(opts) { | ||
const mongo_opts = Object.assign(defaultMongoOpts, (opts || {})) | ||
Debug.enable('mongo-unit') | ||
if(mongo_opts.verbose){ | ||
Debug.enable('*') | ||
} | ||
if (dbUrl) { | ||
return Promise.resolve(dbUrl) | ||
} else { | ||
const mongo_opts = Object.assign(defaultMongoOpts, (opts || {})) | ||
makeSureTempDirExist(mongo_opts.args.dbpath) | ||
return makeSureOtherMongoProcessesKilled(mongo_opts.args.dbpath) | ||
.then(()=>getFreePort(mongo_opts.args.port)) | ||
.then(port => { | ||
mongo_opts.args.port = port | ||
const mongodHelper = new MongodHelper(['--port', port, '--dbpath', mongo_opts.args.dbpath,'--storageEngine', 'ephemeralForTest']); | ||
return mongodHelper.run() | ||
.then(()=>{ | ||
dbUrl = 'mongodb://localhost:' + port+'/'+mongo_opts.dbName | ||
return dbUrl | ||
}) | ||
}) | ||
makeSureTempDirExist(mongo_opts.dbpath) | ||
return makeSureOtherMongoProcessesKilled(mongo_opts.dbpath) | ||
.then(()=>getFreePort(mongo_opts.port)) | ||
.then(port => runMogo(mongo_opts, port)) | ||
} | ||
@@ -65,3 +69,2 @@ } | ||
function clean(data) { | ||
@@ -87,3 +90,3 @@ return client.connect(getUrl()) | ||
if (err) { | ||
console.log('cannot get free port', err) | ||
debug(`cannot get free port: ${err}`) | ||
reject(err) | ||
@@ -130,2 +133,22 @@ } else { | ||
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) | ||
}) | ||
} | ||
function dropDb(url) { | ||
return client.connect(url) | ||
.then(db => db.collections()) | ||
.then(collections => { | ||
const requests = collections.map(col => col.drop()) | ||
return Promise.all(requests) | ||
}) | ||
} | ||
module.exports = { | ||
@@ -136,3 +159,5 @@ start, | ||
clean, | ||
drop | ||
drop, | ||
initDb, | ||
dropDb, | ||
} |
{ | ||
"name": "mongo-unit", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "mongo db for unit tests", | ||
@@ -33,2 +33,3 @@ "main": "index.js", | ||
"dependencies": { | ||
"debug": "^3.0.1", | ||
"mongodb": "~2.2.31", | ||
@@ -41,2 +42,3 @@ "mongodb-prebuilt": "~6.3.3", | ||
"chai": "^3.5.0", | ||
"co": "^4.6.0", | ||
"mocha": "^3.2.0", | ||
@@ -43,0 +45,0 @@ "mongoose": "^4.7.6" |
@@ -24,5 +24,8 @@ # mongo-unit | ||
It starts mongod on one of available port and returns Promise with URL to connect to this db | ||
`opts` i optional params, you can specify your own command line params for mongod | ||
`opts` is optional params, you can specify some command line params for mongod | ||
(more about it in documentation for [mongodb-prebuilt](https://github.com/winfinit/mongodb-prebuilt)) | ||
`opts.dbName` - specify name of test db, it will be `test` by default | ||
`opts.port` - preferable mongo db port, default: `27017` | ||
`opts.dbName` - name of test db, default: `test` | ||
`opts.dbpath` - db path, default: `<node_modules/mongo-unit>\.mongo-unit` | ||
`opts.verbose` - enable debug informaton for mongodb-prebuilt, default: `false` | ||
@@ -29,0 +32,0 @@ ### `getUrl()` |
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
358433
67
141
138
5
4
55
+ Addeddebug@^3.0.1