你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
创建触发器
该 Create Trigger
操作在集合中创建新的触发器。
请求
方法 | 请求 URI | 说明 |
---|---|---|
POST | https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/triggers |
请注意,{databaseaccount} 是在订阅下创建的 Azure Cosmos DB 帐户的名称。 {db-id} 值是创建触发器的数据库的用户生成的名称/ID,而不是数据库 (删除) 的系统生成的 ID。 {coll-id} 值是创建触发器的集合的名称。 |
标头
有关所有 Azure Cosmos DB 请求 使用的标头,请参阅常见的 Azure Cosmos DB REST 请求标头。
正文
属性 | 必选 | 类型 | 说明 |
---|---|---|---|
id | 必须 | 字符串 | 它是用来标识该触发器的唯一名称。 ID 不得超过 255 个字符。 |
body | 必须 | 字符串 | 它是触发器的主体。 |
triggerOperation | 必须 | 字符串 | 它是调用触发器的操作类型。 可接受的值为: All、 Create、 Replace 和 Delete。 |
triggerType | 必须 | 字符串 | 此值指定触发触发器的时间。 可接受的值为: Pre 和 Post。 前 触发器在操作之前触发,而 Post 触发器在操作之后触发。 |
{
"body": "function updateMetadata() {\r\n var context = getContext();\r\n var collection = context.getCollection();\r\n var response = context.getResponse();\r\n var createdDocument = response.getBody();\r\n\r\n // query for metadata document\r\n var filterQuery = 'SELECT * FROM root r WHERE r.id = \"_metadata\"';\r\n var accept = collection.queryDocuments(collection.getSelfLink(), filterQuery,\r\n updateMetadataCallback);\r\n if(!accept) throw \"Unable to update metadata, abort\";\r\n\r\n function updateMetadataCallback(err, documents, responseOptions) {\r\n if(err) throw new Error(\"Error\" + err.message);\r\n if(documents.length != 1) throw 'Unable to find metadata document';\r\n var metadataDocument = documents[0];\r\n\r\n // update metadata\r\n metadataDocument.createdDocuments += 1;\r\n metadataDocument.createdNames += \" \" + createdDocument.id;\r\n var accept = collection.replaceDocument(metadataDocument._self,\r\n metadataDocument, function(err, docReplaced) {\r\n if(err) throw \"Unable to update metadata, abort\";\r\n });\r\n if(!accept) throw \"Unable to update metadata, abort\";\r\n return; \r\n }",
"id": "PostTrigger-UpdateMetaAll",
"triggerOperation": "All",
"triggerType": "Post"
}
响应
标头
有关所有 Azure Cosmos DB 响应 返回的标头,请参阅常见的 Azure Cosmos DB REST 响应标头。
状态代码
下表列出了此操作返回的常见状态代码。 有关状态代码的完整列表,请参阅 HTTP 状态代码。
HTTP 状态代码 | 说明 |
---|---|
201 Created | 操作成功。 |
400 错误的请求 | JSON 正文无效。 检查是否缺少大括号或引号。 |
409 冲突 | 为新触发器提供的 ID 已被现有触发器占用。 |
正文
属性 | 说明 |
---|---|
_摆脱 | 它是系统生成的属性。 资源 ID (_rid) 是一个唯一标识符,也是按资源模型上的资源堆栈分层的标识符。 它可供内部用于放置和导航触发器资源。 |
_ts | 它是系统生成的属性。 它指定资源的上次更新时间戳。 高值是一个时间戳。 |
_自我 | 它是系统生成的属性。 它是资源的唯一可寻址 URI。 |
_Etag | 它是一个系统生成的属性,指定乐观并发控制所需的资源 etag。 |
{
"body": "function updateMetadata() {\r\n var context = getContext();\r\n var collection = context.getCollection();\r\n var response = context.getResponse();\r\n var createdDocument = response.getBody();\r\n\r\n // query for metadata document\r\n var filterQuery = 'SELECT * FROM root r WHERE r.id = \"_metadata\"';\r\n var accept = collection.queryDocuments(collection.getSelfLink(), filterQuery,\r\n updateMetadataCallback);\r\n if(!accept) throw \"Unable to update metadata, abort\";\r\n\r\n function updateMetadataCallback(err, documents, responseOptions) {\r\n if(err) throw new Error(\"Error\" + err.message);\r\n if(documents.length != 1) throw 'Unable to find metadata document';\r\n var metadataDocument = documents[0];\r\n\r\n // update metadata\r\n metadataDocument.createdDocuments += 1;\r\n metadataDocument.createdNames += \" \" + createdDocument.id;\r\n var accept = collection.replaceDocument(metadataDocument._self,\r\n metadataDocument, function(err, docReplaced) {\r\n if(err) throw \"Unable to update metadata, abort\";\r\n });\r\n if(!accept) throw \"Unable to update metadata, abort\";\r\n return; \r\n }",
"id": "PostTrigger-UpdateMetaAll",
"triggerOperation": "All",
"triggerType": "Post",
"_rid": "Sl8fALN4sw4BAAAAAAAAcA==",
"_ts": 1449689248,
"_self": "dbs\/Sl8fAA==\/colls\/Sl8fALN4sw4=\/triggers\/Sl8fALN4sw4BAAAAAAAAcA==\/",
"_etag": "\"0600fbe4-0000-0000-0000-566880a00000\""
}
示例
POST https://contosomarketing.documents.azure.com/dbs/volcanodb/colls/volcano1/triggers HTTP/1.1
x-ms-session-token: 41
x-ms-date: Wed, 09 Dec 2015 19:27:28 GMT
authorization: type%3dmaster%26ver%3d1.0%26sig%3dHmx5WSnyLdbJnDB1Ixme8uGbUSl%2f96HkCgSsSnsSq%2fI%3d
Cache-Control: no-cache
User-Agent: contoso/1.0
x-ms-version: 2015-08-06
Accept: application/json
Host: contosomarketing.documents.azure.com
Cookie: x-ms-session-token=41
Content-Length: 1380
Expect: 100-continue
Connection: Keep-Alive
{
"body": "function updateMetadata() {\r\n var context = getContext();\r\n var collection = context.getCollection();\r\n var response = context.getResponse();\r\n var createdDocument = response.getBody();\r\n\r\n // query for metadata document\r\n var filterQuery = 'SELECT * FROM root r WHERE r.id = \"_metadata\"';\r\n var accept = collection.queryDocuments(collection.getSelfLink(), filterQuery,\r\n updateMetadataCallback);\r\n if(!accept) throw \"Unable to update metadata, abort\";\r\n\r\n function updateMetadataCallback(err, documents, responseOptions) {\r\n if(err) throw new Error(\"Error\" + err.message);\r\n if(documents.length != 1) throw 'Unable to find metadata document';\r\n var metadataDocument = documents[0];\r\n\r\n // update metadata\r\n metadataDocument.createdDocuments += 1;\r\n metadataDocument.createdNames += \" \" + createdDocument.id;\r\n var accept = collection.replaceDocument(metadataDocument._self,\r\n metadataDocument, function(err, docReplaced) {\r\n if(err) throw \"Unable to update metadata, abort\";\r\n });\r\n if(!accept) throw \"Unable to update metadata, abort\";\r\n return; \r\n }",
"id": "PostTrigger-UpdateMetaAll",
"triggerOperation": "All",
"triggerType": "Post"
}
HTTP/1.1 201 Created
Cache-Control: no-store, no-cache
Pragma: no-cache
Content-Type: application/json
Server: Microsoft-HTTPAPI/2.0
Strict-Transport-Security: max-age=31536000
x-ms-last-state-change-utc: Sun, 29 Nov 2015 19:20:18.154 GMT
etag: "0600fbe4-0000-0000-0000-566880a00000"
x-ms-resource-quota: triggers=25;
x-ms-resource-usage: triggers=1;
x-ms-schemaversion: 1.1
x-ms-alt-content-path: dbs/volcanodb/colls/volcano1
x-ms-content-path: Sl8fALN4sw4=
x-ms-quorum-acked-lsn: 41
x-ms-session-token: 42
x-ms-current-write-quorum: 3
x-ms-current-replica-set-size: 4
x-ms-request-charge: 6.29
x-ms-serviceversion: version=1.5.57.3
x-ms-activity-id: b407dfec-b6ec-4aa5-9833-eadad231fcd4
x-ms-gatewayversion: version=1.5.57.3
Date: Wed, 09 Dec 2015 19:27:29 GMT
Content-Length: 1565
{
"body": "function updateMetadata() {\r\n var context = getContext();\r\n var collection = context.getCollection();\r\n var response = context.getResponse();\r\n var createdDocument = response.getBody();\r\n\r\n // query for metadata document\r\n var filterQuery = 'SELECT * FROM root r WHERE r.id = \"_metadata\"';\r\n var accept = collection.queryDocuments(collection.getSelfLink(), filterQuery,\r\n updateMetadataCallback);\r\n if(!accept) throw \"Unable to update metadata, abort\";\r\n\r\n function updateMetadataCallback(err, documents, responseOptions) {\r\n if(err) throw new Error(\"Error\" + err.message);\r\n if(documents.length != 1) throw 'Unable to find metadata document';\r\n var metadataDocument = documents[0];\r\n\r\n // update metadata\r\n metadataDocument.createdDocuments += 1;\r\n metadataDocument.createdNames += \" \" + createdDocument.id;\r\n var accept = collection.replaceDocument(metadataDocument._self,\r\n metadataDocument, function(err, docReplaced) {\r\n if(err) throw \"Unable to update metadata, abort\";\r\n });\r\n if(!accept) throw \"Unable to update metadata, abort\";\r\n return; \r\n }",
"id": "PostTrigger-UpdateMetaAll",
"triggerOperation": "All",
"triggerType": "Post",
"_rid": "Sl8fALN4sw4BAAAAAAAAcA==",
"_ts": 1449689248,
"_self": "dbs\/Sl8fAA==\/colls\/Sl8fALN4sw4=\/triggers\/Sl8fALN4sw4BAAAAAAAAcA==\/",
"_etag": "\"0600fbe4-0000-0000-0000-566880a00000\""
}