transactions-mongoose
Advanced tools
Comparing version 1.2.0 to 1.2.1
@@ -15,2 +15,4 @@ /* | ||
const personHelper = require('./personHelper'); | ||
const {describe, it} = require("node:test"); | ||
const {strict: assert} = require("node:assert"); | ||
@@ -26,3 +28,3 @@ | ||
const persons = await personHelper.createNewPersons() | ||
const persons = await personHelper.createNewPersons(true) | ||
@@ -33,3 +35,2 @@ let personSancho = await Person.findById(persons.Sancho._id); // test Sancho exists? | ||
const transactionData = transaction.execute(async () => { | ||
@@ -62,7 +63,22 @@ transaction.add(personSancho).update({ | ||
await transaction.commit(); | ||
console.log('transaction result', transactionData.result.result); | ||
let Janna = await Person.findById(persons.Janna._id); | ||
describe('Transaction Execute', () => { | ||
it('Execute return: TransactionData', () => { | ||
assert.strictEqual(transactionData.constructor.name, 'TransactionData'); | ||
}) | ||
it('TransactionData result is TransactionData', () => { | ||
assert.strictEqual(transactionData.result.constructor.name, 'TransactionData'); | ||
}) | ||
it('Person Janna has avatar', () => { | ||
assert.strictEqual(Janna.avatar && Janna.avatar.includes('base64') > -1, true); | ||
}) | ||
it('commits', () => { | ||
assert.strictEqual(transaction.commits.length, 3); | ||
}) | ||
}) | ||
await mongoose.disconnect(); | ||
await mongod.stop(); | ||
})(); |
@@ -13,4 +13,8 @@ /* | ||
const createNewPersons = async () => { | ||
const transaction = new Transaction().setSendbox(true); | ||
/** | ||
* @param {boolean} sandbox | ||
* @return {Promise<{Sancho, Hulio, Janna}>} | ||
*/ | ||
const createNewPersons = async (sandbox = true) => { | ||
const transaction = new Transaction().setSendbox(sandbox); | ||
@@ -52,5 +56,20 @@ // variant #1 | ||
const get = async (...args) => { | ||
return { | ||
status: 200, | ||
blob: async () => { | ||
return { | ||
type: 'awesome', arrayBuffer: async (c = 30 * 30) => { | ||
const r = Array(c); | ||
for (let i = 0; i < c; ++i) r[i] = Math.floor(256 * Math.random()); | ||
return r; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
// node 18+ | ||
const getAvatar = async (id) => { | ||
const response = await fetch('https://i.pravatar.cc/50?u=' + id); | ||
const response = await get('https://i.pravatar.cc/50?u=' + id); | ||
const blob = await response.blob() | ||
@@ -57,0 +76,0 @@ return "data:" + blob.type + ';base64,' + Buffer.from(await blob.arrayBuffer()).toString('base64'); |
@@ -15,2 +15,4 @@ /* | ||
const personHelper = require('./personHelper'); | ||
const {describe, it} = require("node:test"); | ||
const {strict: assert} = require("node:assert"); | ||
@@ -25,5 +27,19 @@ | ||
const transaction = new Transaction().setSendbox(true); | ||
const transaction = new Transaction().setSendbox(false); | ||
const persons = await personHelper.createNewPersons() | ||
const persons = await personHelper.createNewPersons(false) | ||
describe('Transaction Session work', () => { | ||
it('Replica Set', () => { | ||
assert.strictEqual(transaction.isReplicaSet, true); | ||
}) | ||
it(persons.Sancho.firstname + ' created', () => { | ||
assert.strictEqual(transaction.isDocument(persons.Sancho), true); | ||
}) | ||
it(persons.Janna.firstname + ' created', () => { | ||
assert.strictEqual(transaction.isDocument(persons.Sancho), true); | ||
}) | ||
it(persons.Hulio.firstname + ' created', () => { | ||
assert.strictEqual(transaction.isDocument(persons.Sancho), true); | ||
}) | ||
}) | ||
@@ -36,5 +52,17 @@ let personSancho = await Person.findById(persons.Sancho._id); // test Sancho exists? | ||
// let's check that nothing has changed? | ||
console.log('Sancho age is 22 -', await Person.findById(persons.Sancho._id).select('-_id age')) | ||
console.log('Janna age is 21 -', await Person.findById(persons.Janna._id).select('-_id age')) | ||
console.log('Hulio age is 35 -', await Person.findById(persons.Hulio._id).select('-_id age'), '\n\n') | ||
const Sancho = await Person.findById(persons.Sancho._id).select('-_id age') | ||
const Janna = await Person.findById(persons.Janna._id).select('-_id age') | ||
const Hulio = await Person.findById(persons.Hulio._id).select('-_id age') | ||
describe('Transaction Session fail', () => { | ||
it('Sancho age is 22', () => { | ||
assert.strictEqual(Sancho.age, 22); | ||
}); | ||
it('Janna age is 21', () => { | ||
assert.strictEqual(Janna.age, 21); | ||
}); | ||
it('Hulio age is 35', () => { | ||
assert.strictEqual(Hulio.age, 35); | ||
}) | ||
}) | ||
} | ||
@@ -62,3 +90,3 @@ | ||
} catch (e) { | ||
console.log(e, '\nPassed!!') | ||
// console.log(e, '\nPassed!!') | ||
} | ||
@@ -100,3 +128,3 @@ await showPersonsAge(); | ||
} catch (e) { | ||
console.log(e, '\nPassed!!') | ||
// console.log(e, '\nPassed!!') | ||
} | ||
@@ -138,3 +166,3 @@ await showPersonsAge(); | ||
} catch (e) { | ||
console.log(e, '\nPassed!!') | ||
// console.log(e, '\nPassed!!') | ||
} | ||
@@ -175,11 +203,15 @@ await showPersonsAge() | ||
console.log('List of all Persons:') | ||
for await (let v of (await Person.find({}, { | ||
firstname: 1, | ||
age: 1 | ||
}).cursor())) console.log('\t', v.name, 'age is', v.age) | ||
const list100 = await Person.find({age: 100}, {firstname: 1, age: 1}) | ||
const pedro = await Person.findOne({firstname: 'Pedro'}) | ||
describe('Transaction Session - Replica Set', () => { | ||
it('Reload document with/without session, save with/without', () => { | ||
assert.strictEqual(list100.filter(it => it.age === 100).length, 3); | ||
}) | ||
it('new Pedro document stored', () => { | ||
assert.strictEqual(pedro.age, 33); | ||
}) | ||
}) | ||
await mongoose.disconnect(); | ||
await mongod.stop(); | ||
})(); |
@@ -24,3 +24,3 @@ /* | ||
const persons = await personHelper.createNewPersons() | ||
const persons = await personHelper.createNewPersons(true) | ||
@@ -27,0 +27,0 @@ let personSancho = await Person.findById(persons.Sancho._id); // test Sancho exists? |
{ | ||
"name": "transactions-mongoose", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "Transactions for mongoose", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -34,2 +34,3 @@ # 🇺🇦 Transactions for mongoose | ||
const transaction = new Transaction(); | ||
// or with debug log | ||
@@ -176,3 +177,3 @@ const transaction = new Transaction().setSendbox(true); | ||
// there must be a return result - and it must be a mongo document | ||
// return result - The result can be whatever you want | ||
return personJanna | ||
@@ -179,0 +180,0 @@ }); |
@@ -138,3 +138,4 @@ /* | ||
*/ | ||
isDocument = (v) => !!v && v.constructor && v.constructor.name === 'model' && !!v.constructor.modelName && !!v._doc; | ||
isDocument = (v) => !!v && v.constructor && v.constructor.name === 'model' && | ||
!!Namespace.modelNames().includes(v.constructor.modelName) && !!v._doc; | ||
/** | ||
@@ -144,3 +145,3 @@ * @param {object|HydratedDocument} v | ||
*/ | ||
documentModel = (v) => this.isDocument(v) && v.db ? (v.db.models[Object.keys(v.db.models)[0]]) : null; | ||
documentModel = (v) => this.isDocument(v) ? Namespace.model(v.constructor.modelName) : null; | ||
/** | ||
@@ -147,0 +148,0 @@ * @param {object} v |
Sorry, the diff of this file is not supported yet
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
69292
1183
188
0