How can we improve Azure Cosmos DB?

Allow in a pre trigger to insert a document on a different partition key

In creating a "delete" audit scenario where i need to capture a new document being deleted in a pre delete trigger, why as long as i am in the same collection are we being limited to the same PartitionKey? This now requires a secondary filter to be applied to all queries to filter out the "Delete" audit records. Where adding a custom "delete" partitionKey would solve all issues.

error received:

{
"errorCode": 500,
"errorMessage": "Message: {\"Errors\":[\"Encountered exception while executing function. Exception = Error: Error: {\\\"Errors\\\":[\\\"Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted.\\\"]}\\r\\nStack trace: Error: Error: {\\\"Errors\\\":[\\\"Requests originating from scripts cannot reference partition keys other than the one for which client request was submitted.\\\"]}\\n at Anonymous function (deleteAuditTrigger.js:27:24)\\n at Anonymous function (deleteAuditTrigger.js:679:29)\"]}\r\nActivityId: ea11c7ae-aa4e-4b66-b7c8-e47201c2da50, Request URI: /apps/DocDbApp/services/DocDbServer10/partitions/a4cb4956-38c8-11e6-8106-8cdcd42c33be/replicas/1p/, RequestStats: \r\nRequestStartTime: 2019-04-29T18:06:43.4281968Z, Number of regions attempted: 1\r\n, SDK: Microsoft.Azure.Documents.Common/2.1.0.0, Windows/6.3.9600 documentdb-netcore-sdk/2.1.3",
"errorMessageLocaleId": "unknown_error",
"details": null
}

Trigger Code:

var context = getContext();
var request = context.getRequest();
var collection = getContext().getCollection();
// document to be deleted in the current operation
var documentToDelete = request.getBody();
documentToDelete["_dbEventType"] = "Delete";
// update the document that will be deleted
request.setBody(documentToDelete);

var currentDocQuery = 'SELECT * From c Where c.id = "' + documentToDelete.id +'"';
collection.queryDocuments(collection.getSelfLink(), currentDocQuery, resultCallback);
function resultCallback(err, documents, responseOptions) {
if (err) throw new Error("Error" + err.message);
if (documents.length !== 1) throw 'Unable to find document';

var docCopy = documents[0];
var auditDoc =
{
"id": "Deleted_" + documentToDelete.id,
"ttl": 5184000, //60 days
"PartitionKey": "Deleted_" + documentToDelete.PartitionKey,
"Original": docCopy

};
collection.createDocument(collection.getSelfLink(), auditDoc, function (err, docCreated) {
if (err) { throw new Error('Error: ' + err.message); }
});

}

3 votes
Vote
Sign in
(thinking…)
Sign in with: oidc
Signed in as (Sign out)
You have left! (?) (thinking…)
Dan Turco shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

0 comments

Sign in
(thinking…)
Sign in with: oidc
Signed in as (Sign out)
Submitting...

Feedback and Knowledge Base