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

Commit 2d93ea7

Browse files
new line write in db
1 parent 5fb46c2 commit 2d93ea7

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/db/MongoDB.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class MongoDB {
1515
// TODO handle error
1616
// TODO change using BULK write
1717
constructor (username, password, host, database, port) {
18-
this.client = new MongoClient(`mongodb://${username}:${password}@${host}:${port}/?retryWrites=true&w=majority`);
18+
const url = `mongodb://${username}:${password}@${host}:${port}/?retryWrites=true&w=majority`;
19+
this.client = new MongoClient(url);
1920
}
2021

2122
async connect () {
@@ -53,15 +54,17 @@ class MongoDB {
5354

5455
async newLine (documentId, previousUuid, uuid, content) {
5556
// 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)
57+
//TODO is it possible in one operation ?
58+
// TODO is it possible to implement with bulk?
59+
let doc = await this.documentsCollection.findOne({_id: ObjectID(documentId)});
60+
let index = doc.content.findIndex(line => {
61+
return line.uuid == previousUuid;
62+
});
6063
this.documentsCollection.updateOne({_id: ObjectID(documentId)}, {
6164
$push: {
6265
content: {
6366
$each : [{uuid: uuid, content: content}],
64-
$position : index
67+
$position : index + 1
6568
}
6669
}
6770
});

0 commit comments

Comments
 (0)