I've read in the documentation that upsertGraph() is not atomic and should be used with transaction. Would you say this would be the right way to do it? I failed to find any examples on this.
...
const opts: UpsertOptions = {
relate: true,
unrelate: true,
}
let updatedAd
const trx = await transaction.start(AdModel.knex())
try {
updatedAd = await AdModel.query(trx)
.upsertGraphAndFetch(graph, opts)
.omit(['token'])
.debug()
await trx.commit()
} catch (err) {
console.error(err)
await trx.rollback()
return ctx.throw(...)
}
The graph data I am inserting is pretty simple has only one nested owner relation like this:
{
id: adId,
classifiedCode: 'cllgb',
sectionCode: 'hhh',
categoryCode: 'apa',
title: 'Updated title',
description: 'Updated description',
...
owner: {
id: 23341
email: '[email protected]',
},
...
}
The transaction section is full of examples. All ways work with all methods. I didn't think I needed to add an example for each method.
@koskimas I am new to Objection.js and just wanted to make sure that I am properly using it with upsertGraphAndFetch() method in this particular situation. My apologies for wasting your precious time with this stupid question.
@demisx Try checking out the gitter. It's a bit of a cluster f* at times but you might be able to get more help there, and chat with other users live.
It's very difficult to keep a repository clean and maintain a low amount of issues and dangling PRs, so I agree with koskimas' decisions to default to closing most of them as soon as possible. Please don't take offense to this action as in the long run, it is good for the project as a whole.
If you want my opinion, I stay away from upsert/insert graph mainly because there are so many options and I prefer to be explicit about my operations, even if it means a bit longer code.
But if you must, you may also want to try the alternative syntax:
const opts: UpsertOptions = {
relate: true,
unrelate: true,
}
let updatedAd
try {
updatedAd = await objection.transaction(AdModel.knex(), async () => {
return AdModel.query(trx)
.upsertGraphAndFetch(graph, opts)
.omit(['token'])
.debug()
})
} catch (e) {
// transaction failed!
}
This way commit and rollback are handled automatically.
@heisian Thank you so much Tim for the info and letting me know about Gitter. I will definitely check it out and use it moving forward.
What would also be helpful, if there was a brief message in the GitHub new issue template field explaining what type of issues should be filed here and what should be deferred to Gitter. I'd be more than happy to comply and keep the core team focused on real issues/bugs.
Yes I think it would be better to be explicit about the right places to find help.. I think somewhere out there some text even suggests posting a question in an issue, which I don't agree with, but ultimately that's up to koskimas. Feel free to ping me if you have other Objection questions.
Most helpful comment
@demisx Try checking out the gitter. It's a bit of a cluster f* at times but you might be able to get more help there, and chat with other users live.
It's very difficult to keep a repository clean and maintain a low amount of issues and dangling PRs, so I agree with koskimas' decisions to default to closing most of them as soon as possible. Please don't take offense to this action as in the long run, it is good for the project as a whole.
If you want my opinion, I stay away from upsert/insert graph mainly because there are so many options and I prefer to be explicit about my operations, even if it means a bit longer code.
But if you must, you may also want to try the alternative syntax:
This way
commitandrollbackare handled automatically.