|
| 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 | + constructor (username, password, host, database, port) { |
| 16 | + let url = `mongodb://${username}:${password}@${host}:${port}/?retryWrites=true&w=majority`; |
| 17 | + this.client = new MongoClient(url); |
| 18 | + } |
| 19 | + |
| 20 | + async connect () { |
| 21 | + try { |
| 22 | + this.db = await this.client.connect(); |
| 23 | + this.codeWe = await this.db.db('codewe'); |
| 24 | + this.documentsCollection = await this.codeWe.collection('codewe'); |
| 25 | + } catch (err) { |
| 26 | + if (configs.DEBUG) { |
| 27 | + console.error('Error with db connection'); |
| 28 | + } |
| 29 | + throw new Error(err); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + async createDocument () { |
| 34 | + let doc = { |
| 35 | + content: baseCode, |
| 36 | + creationDate: Date.now(), |
| 37 | + lastViewedDate: Date.now(), |
| 38 | + customDocumentName: '', |
| 39 | + documentOwner: '', |
| 40 | + editors: [], |
| 41 | + linkEdit: '', |
| 42 | + linkView: '', |
| 43 | + language: '', |
| 44 | + tab: 4 |
| 45 | + }; |
| 46 | + try { |
| 47 | + let results = (await this.documentsCollection.insertOne(doc)); |
| 48 | + const documentLink = utils.uuid(results.insertedId.toString()); |
| 49 | + this.documentsCollection.updateOne({_id: results.insertedId}, {$set: {documentLink: documentLink}}) |
| 50 | + return documentLink; |
| 51 | + } catch (err) { |
| 52 | + if (configs.DEBUG) { |
| 53 | + console.error('Error when creating a new document'); |
| 54 | + } |
| 55 | + throw new Error(err); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + async getDocument (documentLink) { |
| 61 | + try { |
| 62 | + return await this.documentsCollection.findOne({documentLink: documentLink}); |
| 63 | + } catch (err) { |
| 64 | + if (configs.DEBUG) { |
| 65 | + console.error('Error when fetching document'); |
| 66 | + } |
| 67 | + throw new Error(err); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + async setLine (documentLink, uuid, content) { |
| 72 | + try { |
| 73 | + await this.documentsCollection.updateOne({documentLink: documentLink, 'content.uuid': uuid}, {$set: {'content.$.content': content}}); |
| 74 | + } catch (err) { |
| 75 | + if (configs.DEBUG) { |
| 76 | + console.error('Error when changing line content'); |
| 77 | + } |
| 78 | + throw new Error(err); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + async newLine (documentLink, previousUuid, uuid, content) { |
| 83 | + // Insert a line at the right place |
| 84 | + //TODO is it possible in one operation ? |
| 85 | + // TODO is it possible to implement with bulk? |
| 86 | + try { |
| 87 | + let doc = await this.documentsCollection.findOne({documentLink: documentLink}); |
| 88 | + let index = doc.content.findIndex(line => { |
| 89 | + return line.uuid == previousUuid; |
| 90 | + }); |
| 91 | + this.documentsCollection.updateOne({documentLink: documentLink}, { |
| 92 | + $push: { |
| 93 | + content: { |
| 94 | + $each : [{uuid: uuid, content: content}], |
| 95 | + $position : index + 1 |
| 96 | + } |
| 97 | + } |
| 98 | + }); |
| 99 | + } catch (err) { |
| 100 | + if (configs.DEBUG) { |
| 101 | + console.error('Error when adding a new line to document'); |
| 102 | + } |
| 103 | + throw new Error(err); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + async deleteLine (documentLink, uuid) { |
| 108 | + try { |
| 109 | + // Delete line at the right place |
| 110 | + await this.documentsCollection.updateOne({documentLink: documentLink}, {$pull: {content: {uuid: uuid}}}); |
| 111 | + } catch (err) { |
| 112 | + if (configs.DEBUG) { |
| 113 | + console.error('Error when deleting a line in document'); |
| 114 | + } |
| 115 | + throw new Error(err); |
| 116 | + } |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + async applyRequests (documentLink, requests) { |
| 121 | + // TODO look to use bulk write |
| 122 | + try { |
| 123 | + for (let request of requests) { |
| 124 | + let requestType = request.type; |
| 125 | + let data = request.data; |
| 126 | + switch (requestType) { |
| 127 | + case 'set-line': |
| 128 | + await this.setLine(documentLink, data.id, data.content); |
| 129 | + break; |
| 130 | + case 'new-line': |
| 131 | + await this.newLine(documentLink, data.previous, data.id, data.content); |
| 132 | + break; |
| 133 | + case 'delete-line': |
| 134 | + await this.deleteLine(documentLink, data.id); |
| 135 | + break; |
| 136 | + } |
| 137 | + } |
| 138 | + } catch (err) { |
| 139 | + if (configs.DEBUG) { |
| 140 | + console.error('Error when applying requests'); |
| 141 | + } |
| 142 | + throw new Error(err); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +function getDB () { |
| 148 | + db = new MongoDB( |
| 149 | + configs.DB_CONFIG.DB_USERNAME, |
| 150 | + configs.DB_CONFIG.DB_PASSWORD, |
| 151 | + configs.DB_CONFIG.DB_HOST, |
| 152 | + configs.DB_CONFIG.DB_DATABASE, |
| 153 | + configs.DB_CONFIG.DB_PORT |
| 154 | + ); |
| 155 | + db.connect(); |
| 156 | + return db; |
| 157 | +} |
| 158 | + |
| 159 | +module.exports = getDB(); |
0 commit comments