intermediate commit

This commit is contained in:
dave 2025-03-30 20:05:58 +02:00
parent 25485dde69
commit 9e9d3f35aa
7 changed files with 70 additions and 4 deletions

2
.env.example Normal file
View File

@ -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

5
.mocharc.js Normal file
View File

@ -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];

View File

@ -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) {
});
});
*/

30
lib/db.js Normal file
View File

@ -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')
}

7
lib/ws.js Normal file
View File

@ -0,0 +1,7 @@
import { WebSocketServer } from "ws";
let wss;
export function init(cfg) {
wss = new WebSocketServer(cfg);
}

View File

@ -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"
}
}

12
test/db.test.js Normal file
View File

@ -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);
});
});
});