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