Hello everyone,
I have a problem on my Atlas cluster with partition sync.
in a cloud function with realm authentication I need to create a partition (string), add the partition to the “writePartitions” array of the user’s custom data and, with the same operation, create an object that has the newly created partition.
I would like to put everything in a transaction to avoid that, if the insert fails, the user will end up with an “orphan” partition in his “writePartitions” list.
with the code I created, the function returns a 403 error as if the user did not have permission to insert an object with the specified partition but, theoretically, inserting the partition into the “writePartitions” array happens BEFORE inserting of the object.
I don’t understand where the problem is.
exports = async function(site){
let user = context.user;
const _id = new BSON.ObjectId();
const _partition = "site_".concat(_id.toString());
const userId = user.id;
site._id = _id;
site._partition = _partition;
site.creationUserId = userId;
const client = context.services.get("mongodb-atlas");
const session = client.startSession();
// mettere il db name come value
let profileCollection = client.db("accounts").collection("profile");
let siteCollection = client.db("sias").collection("site");
let result = {};
const transactionOptions = {
readPreference: 'primary',
readConcern: { level: 'local' },
writeConcern: { w: 'majority' }
};
try {
await session.withTransaction(
async () => {
// Important:: You must pass the session to the operations
await profileCollection.findOneAndUpdate({},
{
$push: { "writePartitions": _partition }
},
{ session }
);
result = await siteCollection.insertOne(site, { session });
}, transactionOptions
);
} catch (e) {
await session.abortTransaction();
throw e;
} finally {
await session.endSession();
}
return result;
};