Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mongo-unit

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongo-unit - npm Package Compare versions

Comparing version 1.4.5 to 1.5.0

.mongo-unit/diagnostic.data/metrics.2019-11-30T18-38-09Z-00000

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"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc