From 9e9d3f35aaee8f0f5b809a70512af7f722eb381d Mon Sep 17 00:00:00 2001 From: dave Date: Sun, 30 Mar 2025 20:05:58 +0200 Subject: [PATCH] intermediate commit --- .env.example | 2 ++ .mocharc.js | 5 +++++ index.js | 10 ++++++++-- lib/db.js | 30 ++++++++++++++++++++++++++++++ lib/ws.js | 7 +++++++ package.json | 8 ++++++-- test/db.test.js | 12 ++++++++++++ 7 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 .env.example create mode 100644 .mocharc.js create mode 100644 lib/db.js create mode 100644 lib/ws.js create mode 100644 test/db.test.js diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..545d8e5 --- /dev/null +++ b/.env.example @@ -0,0 +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 diff --git a/.mocharc.js b/.mocharc.js new file mode 100644 index 0000000..16f1c79 --- /dev/null +++ b/.mocharc.js @@ -0,0 +1,5 @@ +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 diff --git a/index.js b/index.js index 828c284..38ccaf1 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,11 @@ -import { WebSocketServer } from 'ws'; -import { MongoClient } from 'mongodb'; +import * as ws from './lib/ws.js'; +import * as db from './lib/db.js'; +let myWs = ws.init({ port: process.env.WEB_SOCKET_PORT }); +let myDb = db.init(process.env.MONGO_CONN_STR); + + +/* const mongo = new MongoClient(process.env.MONGO_CONN_STR); const db = mongo.db(); const collections = {}; @@ -67,3 +72,4 @@ wss.on('connection', function connection(ws) { }); }); +*/ \ No newline at end of file diff --git a/lib/db.js b/lib/db.js new file mode 100644 index 0000000..930e31f --- /dev/null +++ b/lib/db.js @@ -0,0 +1,30 @@ +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; +} + +functionCollection.subsribe = function subsribe (b) { + let collection = db.collection(b); + collection.find({}, {}).toArray().then(r => console.log); +} + +functionCollection.persist = function persist (b) { + console.log('persist'); +} + +functionCollection.publish = function publish (b) { + console.log('publish') +} \ No newline at end of file diff --git a/lib/ws.js b/lib/ws.js new file mode 100644 index 0000000..ef3e493 --- /dev/null +++ b/lib/ws.js @@ -0,0 +1,7 @@ +import { WebSocketServer } from "ws"; + +let wss; + +export function init(cfg) { + wss = new WebSocketServer(cfg); +} \ No newline at end of file diff --git a/package.json b/package.json index f15bcd8..8744188 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,9 @@ "main": "index.js", "type": "module", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "start": "node --env-file=.env index.js" + "test": "mocha", + "start": "node --env-file=.env index.js", + "watch": "node --watch --env-file=.env index.js" }, "author": "", "license": "ISC", @@ -13,5 +14,8 @@ "dependencies": { "mongodb": "^6.13.0", "ws": "^8.18.0" + }, + "devDependencies": { + "mocha": "^11.1.0" } } diff --git a/test/db.test.js b/test/db.test.js new file mode 100644 index 0000000..aeed9f9 --- /dev/null +++ b/test/db.test.js @@ -0,0 +1,12 @@ +import mocha from 'mocha'; +import { subscribe } from '../lib/db.js'; + +const assert = mocha.assert; + +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); + }); + }); +}); \ No newline at end of file