intermediate commit
This commit is contained in:
parent
25485dde69
commit
9e9d3f35aa
2
.env.example
Normal file
2
.env.example
Normal 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
5
.mocharc.js
Normal 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];
|
||||||
10
index.js
10
index.js
@ -1,6 +1,11 @@
|
|||||||
import { WebSocketServer } from 'ws';
|
import * as ws from './lib/ws.js';
|
||||||
import { MongoClient } from 'mongodb';
|
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 mongo = new MongoClient(process.env.MONGO_CONN_STR);
|
||||||
const db = mongo.db();
|
const db = mongo.db();
|
||||||
const collections = {};
|
const collections = {};
|
||||||
@ -67,3 +72,4 @@ wss.on('connection', function connection(ws) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
30
lib/db.js
Normal file
30
lib/db.js
Normal 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
7
lib/ws.js
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { WebSocketServer } from "ws";
|
||||||
|
|
||||||
|
let wss;
|
||||||
|
|
||||||
|
export function init(cfg) {
|
||||||
|
wss = new WebSocketServer(cfg);
|
||||||
|
}
|
||||||
@ -4,8 +4,9 @@
|
|||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "mocha",
|
||||||
"start": "node --env-file=.env index.js"
|
"start": "node --env-file=.env index.js",
|
||||||
|
"watch": "node --watch --env-file=.env index.js"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
@ -13,5 +14,8 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"mongodb": "^6.13.0",
|
"mongodb": "^6.13.0",
|
||||||
"ws": "^8.18.0"
|
"ws": "^8.18.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"mocha": "^11.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
12
test/db.test.js
Normal file
12
test/db.test.js
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Loading…
x
Reference in New Issue
Block a user