cosmosdb-server
cosmosdb-server copied to clipboard
Error when a trigger is created
Hello,
I'm trying to create a trigger, but is returning a bad request.
Here it is my code
from pydocumentdb import document_client
DB_DATABASE = 'test_database2'
DB_COLLECTION = 'my_collection'
client = document_client.DocumentClient(
'https://localhost:3000/', {'masterKey': ''})
print("deleting database")
try:
db = next((data for data in client.ReadDatabases(
) if data['id'] == DB_DATABASE))
client.DeleteDatabase(db['_self'])
except:
pass
print("creating database")
db = client.CreateDatabase({'id': DB_DATABASE})
print("creating collection")
collection = client.CreateCollection(db['_self'], {'id': DB_COLLECTION})
path = 'dbs/{}/colls/{}'.format(DB_DATABASE, DB_COLLECTION)
# Create trigger
trigger_definition = {
'id': '_type',
'serverScript': """
// Ensures that documents have a _type field.
function validateNameExists() {
var collection = getContext().getCollection();
var request = getContext().getRequest();
var docToCreate = request.getBody();
// Reject documents that do not have a name property by throwing an exception.
if (!docToCreate._type) {
throw new Error('Document must include a "_type" property.');
}
}
""",
'triggerType': 'Pre',
'triggerOperation': 'All'
}
try:
client.CreateTrigger(collection['_self'], trigger_definition)
except Exception as e:
print(e)
The request is created for
'https://localhost:3000//dbs/wgLLAG--C30lC30lC30lD0--/colls/V2u_QG--C30lC30lC30lC0--/triggers/'
With this header
<class 'dict'>: {'Cache-Control': 'no-cache', 'x-ms-version': '2016-07-11', 'User-Agent': 'Windows/10 Python/3.6.8 documentdb-python-sdk/2.0.0', 'x-ms-consistency-level': 'Session', 'x-ms-session-token': '', 'Content-Type': 'application/json', 'Accept': 'application/json'}
And with this body
<class 'dict'>: {'id': '_type', 'triggerType': 'Pre', 'triggerOperation': 'All', 'body': '\n // Ensures that documents have a _type field.\n function validateNameExists() {\n var collection = getContext().getCollection();\n var request = getContext().getRequest();\n var docToCreate = request.getBody();\n\n // Reject documents that do not have a name property by throwing an exception.\n if (!docToCreate._type) {\n throw new Error(\'Document must include a "_type" property.\');\n }\n }\n '}```
Unfortunately, trigger is not supported yet.