transactions-mongoose
Advanced tools
Comparing version 1.1.11 to 1.1.12
@@ -11,3 +11,3 @@ /* | ||
const Person = require("./models/Person"); | ||
const {MongoMemoryServer} = require('mongodb-memory-server'); | ||
const {MongoMemoryReplSet} = require('mongodb-memory-server'); | ||
const mongoose = require("mongoose"); | ||
@@ -19,3 +19,4 @@ const {Transaction} = require("../src/index"); | ||
(async () => { | ||
const mongod = await MongoMemoryServer.create(); | ||
const mongod = await MongoMemoryReplSet.create({replSet: {count: 4}}); | ||
// const mongod = await MongoMemoryServer.create(); // <-- wrong way for transactions with session | ||
const uri = mongod.getUri(); | ||
@@ -33,18 +34,21 @@ console.log('mongo uri:', uri); | ||
const showPersonsAge = async () => { | ||
// let's check that nothing has changed? | ||
console.log('Sancho age is 22 -', await Person.findById(persons.Sancho._id).select('age')) | ||
console.log('Janna age is 21 -', await Person.findById(persons.Janna._id).select('age')) | ||
console.log('Hulio age is 35 -', await Person.findById(persons.Hulio._id).select('age'), '\n\n') | ||
} | ||
// Error in code | ||
try { | ||
transaction.session(async (session) => { | ||
personJanna.age++; | ||
personJanna.updatedAt = Date.now() | ||
await personJanna.save() | ||
personSancho.age = 100; // <-- let's try to make it more mature | ||
await personSancho.save({session}) | ||
personHulio.age++; | ||
personHulio.updatedAt = Date.now() | ||
await personHulio.save() | ||
personJanna.age = 100; // <-- let's try to make it more mature too | ||
await personJanna.save({session}) | ||
transaction.add(Person, personSancho).update({ | ||
updatedAt: Date.now(), | ||
__v: ++personSancho.__v | ||
}); | ||
personHulio.age = 100; // <-- let's try to make it more mature too | ||
await personHulio.save({session}) | ||
@@ -61,5 +65,36 @@ throw new Error('Test an error - or remark me') // No changes will be saved | ||
} | ||
await showPersonsAge(); | ||
// Error in Person age - validate error | ||
try { | ||
transaction.session(async (session) => { | ||
let personSancho = await Person.findById(persons.Sancho._id).session(session); // test Sancho exists? | ||
let personJanna = await Person.findById(persons.Janna._id).session(session); | ||
let personHulio = await Person.findById(persons.Hulio._id).session(session); | ||
personSancho.age = 100; // <-- let's try to make it more mature | ||
await personSancho.save({session}) | ||
personJanna.age = 100; // <-- let's try to make it more mature too | ||
await personJanna.save({session}) // try test without {session} | ||
personHulio.age = 300; // <-- Error validate | ||
personHulio.status = '???'; // <-- Error validate | ||
await personHulio.save({session}) | ||
// there must be a return result - and it must be a mongo document | ||
return personJanna | ||
}); | ||
await transaction.commit(); | ||
} catch (e) { | ||
console.log(e, '\nPassed!!') | ||
} | ||
await showPersonsAge(); | ||
await mongoose.disconnect(); | ||
await mongod.stop(); | ||
})(); |
{ | ||
"name": "transactions-mongoose", | ||
"version": "1.1.11", | ||
"version": "1.1.12", | ||
"description": "Transactions for mongoose", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -154,2 +154,3 @@ # 🇺🇦 Transactions for mongoose | ||
To use with Mondo Replica set | ||
```javascript | ||
@@ -160,18 +161,16 @@ const {Transaction} = require("transactions-mongoose"); | ||
transaction.session(async (session) => { | ||
let personJanna = await Person.findById('...Janna id'); | ||
personJanna.age++; | ||
personJanna.updatedAt = Date.now() | ||
await personJanna.save() | ||
let personHulio = await Person.findById('...Hulio id'); | ||
personHulio.age++; | ||
personHulio.updatedAt = Date.now() | ||
await personHulio.save() | ||
let personSancho = await Person.findById('...Sancho id').session(session); | ||
let personJanna = await Person.findById('...Janna id').session(session); | ||
let personHulio = await Person.findById('...Hulio id').session(session); | ||
let personSancho = await Person.findById('...Sancho id'); | ||
transaction.add(Person, personSancho).update({ | ||
updatedAt: Date.now(), | ||
__v: ++personSancho.__v | ||
}); | ||
personSancho.age = 100; // <-- let's try to make it more mature | ||
await personSancho.save({session}) | ||
personJanna.age = 100; // <-- let's try to make it more mature too | ||
await personJanna.save({session}) | ||
personHulio.age = 100; // <-- let's try to make it more mature too | ||
await personHulio.save({session}) | ||
throw new Error('Test an error - or remark me') // No changes will be saved | ||
@@ -178,0 +177,0 @@ |
@@ -512,5 +512,5 @@ /* | ||
}).then((doc) => session.commitTransaction()).then((doc) => session.endSession()) | ||
.catch(e => { | ||
session.abortTransaction() | ||
session.endSession() | ||
.catch(async (e) => { | ||
await session.abortTransaction() | ||
await session.endSession() | ||
throw e | ||
@@ -517,0 +517,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
57882
945
188