diff --git a/.env.example b/.env.example index 545d8e5..c80e4b5 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,2 @@ # connection string for mongodb in the form of "mongodb://user:password@host:port/db" -MONGO_CONN_STR=mongodb://timo:bert@127.0.0.1:27017/timobert +MONGO_CONN_STR=mongodb://timo:bert@127.0.0.1:27017/testSuite diff --git a/.mocharc.js b/.mocharc.js index 16f1c79..90803de 100644 --- a/.mocharc.js +++ b/.mocharc.js @@ -1,5 +1,6 @@ import { readFileSync } from "fs"; -const envFile = readFileSync('./.env', { encoding: 'utf8' }); - -process.env.MONGO_CONN_STR = envFile.split('MONGO_CONN_STR=')[1].split('\n')[0]; \ No newline at end of file +try { + const envFile = readFileSync('./.env', { encoding: 'utf8' }); + process.env.MONGO_TEST_CONN_STR = envFile.split('MONGO_TEST_CONN_STR=')[1].split('\n')[0]; +} catch (e) {} diff --git a/lib/db.js b/lib/db.js index 930e31f..6a86375 100644 --- a/lib/db.js +++ b/lib/db.js @@ -1,30 +1,50 @@ import { MongoClient } from 'mongodb'; -/* -console.log('connecting to ' + process.env.MONGO_CONN_STR); -const -*/ + let mongo; let db; let functionCollection = {}; export function init(connectionString) { - console.log(connectionString); - mongo = new MongoClient(connectionString); - mongo.connect().catch(e => {throw e}); - db = mongo.db(); - - return functionCollection; + try { + mongo = new MongoClient(connectionString); + db = mongo.db(); + return mongo.connect().then(() => Promise.resolve(functionCollection)); + } catch (e) { + return Promise.reject(e); + } } -functionCollection.subsribe = function subsribe (b) { - let collection = db.collection(b); - collection.find({}, {}).toArray().then(r => console.log); +functionCollection.subscribe = function subscribe (collectionName, filter) { + let collection = db.collection(collectionName); + return collection + .find(filter, {}) + .toArray(); } -functionCollection.persist = function persist (b) { - console.log('persist'); +functionCollection.persist = function persist (collectionName, filter, fieldName, value) { + let collection = db.collection(collectionName); + let newValue = { $set: {} }; + newValue.$set[fieldName + ".modifiedAt"] = Date.now(); + newValue.$set[fieldName + ".modifiedBy"] = "anonymous"; + newValue.$set[fieldName + ".transientValue"] = value; + + return collection.updateMany(filter, newValue, { upsert: true }); } functionCollection.publish = function publish (b) { console.log('publish') +} + +functionCollection.close = function close () { + return mongo.close(); +} + +functionCollection.purge = function purge (collectionNames) { + let promises = []; + + for (let i = 0; i < collectionNames.length; i++) { + const collection = db.collection(collectionNames[i]); + promises.push(collection.drop()); + } + return Promise.all(promises); } \ No newline at end of file diff --git a/package.json b/package.json index 8744188..56f6776 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "type": "module", "scripts": { "test": "mocha", + "testWatch": "mocha -w", "start": "node --env-file=.env index.js", "watch": "node --watch --env-file=.env index.js" }, diff --git a/test/db.test.js b/test/db.test.js index aeed9f9..27bd3c4 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -1,12 +1,53 @@ -import mocha from 'mocha'; -import { subscribe } from '../lib/db.js'; +import { before } from 'mocha'; +import * as db from '../lib/db.js'; +import * as assert from 'assert/strict'; -const assert = mocha.assert; +const rng = Math.random(); describe('lib/db', function () { - describe('#indexOf()', function () { - it('should return -1 when the value is not present', function () { - assert.equal([1, 2, 3].indexOf(4), -1); + let myDb; + let dbFuncs; + + describe('#init()', function () { + it('should fail to connect given invalid connection string', function () { + return db.init('not a connection string').then(() => Promise.reject()).catch(() => Promise.resolve()); + }); + it('should successfully connect to mongodb', function () { + return myDb = db.init(process.env.MONGO_TEST_CONN_STR); + }); + }); + describe('#close()', function () { + it('should close the connection', function () { + return myDb.then(db => db.close()); + }); + }); + + describe('with a established connection ->', function () { + before(function () { + myDb = db.init(process.env.MONGO_TEST_CONN_STR) + .then(funcs => dbFuncs = funcs); + }); + + after(function () { + dbFuncs.purge(['readWriteTest']) + .then(() => myDb.then(db => db.close())); + }); + + describe('#persist()', function () { + it('should be able to write to mongodb', function () { + return dbFuncs.persist('readWriteTest', {}, 'rng', rng); + }); + }); + + describe('#subscribe()', function () { + it('should be able to read from mongodb', function () { + return dbFuncs + .subscribe('readWriteTest', {}) + .then(v => { + assert.equal(v.length, 1); + assert.equal(v[0].rng.transientValue, rng); + }); + }); }); }); }); \ No newline at end of file