Skip to content
This repository was archived by the owner on Mar 10, 2024. It is now read-only.

Commit 000f51e

Browse files
author
bhasher
committed
Merge remote-tracking branch 'origin/dev' into dev
2 parents 876275a + 00915ca commit 000f51e

File tree

10 files changed

+195
-170
lines changed

10 files changed

+195
-170
lines changed

package-lock.json

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"express-minify": "^1.0.0",
2424
"express-prom-bundle": "^6.2.0",
2525
"less": "^3.12.2",
26+
"mongodb": "^3.6.3",
2627
"morgan": "^1.10.0",
2728
"mysql": "^2.18.1",
2829
"nunjucks": "^3.2.2",

src/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ if (config.PRODUCTION) {
3535
else {
3636
app.use(logger('dev'));
3737
}
38-
app.use(minify({ minifyJS: false }));
38+
app.use(minify({ jsMatch: false }));
3939

4040
// Prometheus middleware
4141
if(config.METRICS){

src/db/DB.js

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/db/MongoDB.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
const { MongoClient, ObjectID } = require("mongodb");
2+
const configs = require('../config/config');
3+
const utils = require('../utils');
4+
5+
const baseCode = [
6+
{uuid: utils.uuid(Math.random().toString(), 10), content: 'def main(text: str) -> None:'},
7+
{uuid: utils.uuid(Math.random().toString(), 10), content: ' print(text)'},
8+
{uuid: utils.uuid(Math.random().toString(), 10), content: ''},
9+
{uuid: utils.uuid(Math.random().toString(), 10), content: 'if __name__ == \'__main__\':'},
10+
{uuid: utils.uuid(Math.random().toString(), 10), content: ' main(\'Hello World !\')'}
11+
];
12+
13+
14+
class MongoDB {
15+
// TODO handle error
16+
// TODO change using BULK write
17+
constructor (username, password, host, database, port) {
18+
this.client = new MongoClient(`mongodb://127.0.0.1:36175/51a8b34f-d253-4cc3-8ed6-96d0ce9c03d5?`);
19+
}
20+
21+
async connect () {
22+
this.db = await this.client.connect();
23+
this.codeWe = await this.db.db('codewe');
24+
this.documentsCollection = await this.codeWe.collection('codewe');
25+
}
26+
27+
async createDocument () {
28+
let doc = {
29+
content: baseCode,
30+
creationDate: Date.now(),
31+
lastViewedDate: Date.now(),
32+
customDocumentName: '',
33+
documentOwner: '',
34+
editors: [],
35+
linkEdit: '',
36+
linkView: '',
37+
language: '',
38+
tab: 4
39+
};
40+
// TODO check if inseted etc
41+
let results = (await this.documentsCollection.insertOne(doc));
42+
return results.insertedId.toString();
43+
44+
}
45+
46+
async getDocument (id) {
47+
return await this.documentsCollection.findOne({_id: ObjectID(id)});
48+
}
49+
50+
async setLine (documentId, uuid, content) {
51+
await this.documentsCollection.updateOne({_id: ObjectID(documentId), 'content.uuid': uuid}, {$set: {'content.$.content': content}});
52+
}
53+
54+
async newLine (documentId, previousUuid, uuid, content) {
55+
// Insert a line at the right place
56+
//TODO is it possible in one operation
57+
// TODO find index
58+
let index = -1
59+
console.log(index)
60+
this.documentsCollection.updateOne({_id: ObjectID(documentId)}, {
61+
$push: {
62+
content: {
63+
$each : [{uuid: uuid, content: content}],
64+
$position : index
65+
}
66+
}
67+
});
68+
}
69+
70+
async deleteLine (documentId, uuid) {
71+
// Delete line at the right place
72+
await this.documentsCollection.updateOne({_id: ObjectID(documentId)}, {$pull: {content: {uuid: uuid}}});
73+
74+
}
75+
76+
async applyRequests (documentId, requests) {
77+
// TODO use bulk write instead of this slow methods
78+
for (let request of requests) {
79+
let requestType = request.type;
80+
let data = request.data;
81+
switch (requestType) {
82+
case 'set-line':
83+
await this.setLine(documentId, data.id, data.content);
84+
break;
85+
case 'new-line':
86+
await this.newLine(documentId, data.previous, data.id, data.content);
87+
break;
88+
case 'delete-line':
89+
await this.deleteLine(documentId, data.id);
90+
break;
91+
}
92+
};
93+
}
94+
}
95+
96+
function getDB () {
97+
db = new MongoDB(configs.DB_USERNAME, configs.DB_PASSWORD, configs.DB_HOST, configs.DB_DATABASE, configs.DB_PORT);
98+
db.connect();
99+
return db
100+
}
101+
102+
module.exports = getDB();

0 commit comments

Comments
 (0)