2023-05-11 11:50:53 +00:00
|
|
|
const { ObjectId } = require('mongodb')
|
|
|
|
const { waitForDb, db } = require('../../../app/src/infrastructure/mongodb')
|
|
|
|
const { getMongoClient } = require('../../../app/src/infrastructure/Mongoose')
|
2021-07-14 10:55:08 +00:00
|
|
|
|
2023-05-11 11:50:53 +00:00
|
|
|
async function main() {
|
|
|
|
try {
|
|
|
|
await waitForDb()
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Cannot connect to mongodb')
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await testTransactions()
|
|
|
|
} catch (err) {
|
|
|
|
console.error("Mongo instance doesn't support transactions")
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function testTransactions() {
|
|
|
|
const mongoClient = await getMongoClient()
|
|
|
|
const session = mongoClient.startSession()
|
|
|
|
try {
|
|
|
|
await session.withTransaction(async () => {
|
|
|
|
await db.users.findOne({ _id: ObjectId() }, { session })
|
|
|
|
})
|
|
|
|
} finally {
|
|
|
|
await session.endSession()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main()
|
2021-07-14 10:55:08 +00:00
|
|
|
.then(() => {
|
|
|
|
console.error('Mongodb is up.')
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err)
|
|
|
|
process.exit(1)
|
|
|
|
})
|