-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
54 lines (43 loc) · 1.61 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// import database from './database.js';
import { MongoClient, ObjectId } from 'mongodb';
import config from './config.js';
const users = ['Herminia', 'Bertoldo', 'Aniceto','Godofredo'];
const transaction = async () => {
const client = new MongoClient(config.url, {useNewUrlParser: true});
await client.connect();
const session = await client.startSession();
try {
// await session.startTransaction();
await session.withTransaction(async () => {
for(const user of users) {
const objectId = new ObjectId();
const db = client.db('running');
console.log(session);
// await db.createCollection('usuarios', { session });
await db.collection('usuarios').insertOne({
_id: objectId,
nombre: user
}, { session });
await db.collection('sesiones').findOneAndUpdate(
{ nombre: user },
[{ $set: { usuarioId: objectId } },
{ $unset: "nombre" }],
{ session }
);
await db.collection('gustos').findOneAndUpdate(
{ nombre: user },
[{ $set: { usuarioId: objectId } },
{ $unset: "nombre" }],
{ session }
)
// await session.commitTransaction();
}
})
}
finally {
await session.endSession();
await client.close();
}
}
transaction().then(_ => console.log("ok"))
.catch(console.error);