你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
替换触发器
对特定触发器资源执行 PUT 操作会替换整个触发器资源。 所有用户可设置的属性(包括 ID、正文、triggerOperation 和 triggerType)都必须在正文中提交才能执行替换。
请求
方法 | 请求 URI | 说明 |
---|---|---|
PUT | https://{databaseaccount}.documents.azure.com/dbs/{db-id}/colls/{coll-id}/triggers/{trigger-name} |
{databaseaccount} 是在订阅下创建的 Azure Cosmos DB 帐户的名称。 {db-id} 值是替换了触发器的数据库的用户生成的名称/ID,而不是数据库 (删除) 的系统生成的 ID。 {coll-id} 值是替换触发器的集合的名称。 {trigger-name} 值是要替换的触发器的名称。 |
标头
有关所有 Cosmos DB 请求 使用的标头,请参阅常见的 Azure Cosmos DB REST 请求标头。
正文
属性 | 必选 | 类型 | 说明 |
---|---|---|---|
id | 必须 | 字符串 | 它是用来标识该触发器的唯一名称。 ID 不得超过 255 个字符。 |
body | 必须 | 字符串 | 它是触发器的主体。 |
triggerOperation | 必须 | 字符串 | 它是调用触发器的操作类型。 可接受的值为: All、 Insert、 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"
}
响应
标头
有关所有 Cosmos DB 响应 返回的标头,请参阅常见的 Azure Cosmos DB REST 响应标头。
状态代码
下表列出了此操作返回的常见状态代码。 有关状态代码的完整列表,请参阅 HTTP 状态代码。
HTTP 状态代码 | 说明 |
---|---|
200 正常 | 替换操作成功。 |
400 错误的请求 | JSON 正文无效。 检查是否缺少大括号或引号。 |
404 未找到 | 要替换的触发器不再是资源,即触发器已删除。 |
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": 1449689654,
"_self": "dbs\/Sl8fAA==\/colls\/Sl8fALN4sw4=\/triggers\/Sl8fALN4sw4BAAAAAAAAcA==\/",
"_etag": "\"060022e5-0000-0000-0000-566882360000\""
}
示例
PUT https://contosomarketing.documents.azure.com/dbs/volcanodb/colls/volcano1/triggers/PostTrigger-UpdateMetaAll HTTP/1.1
x-ms-session-token: 42
x-ms-date: Wed, 09 Dec 2015 19:34:14 GMT
authorization: type%3dmaster%26ver%3d1.0%26sig%3dZyzGdsA62guUndVJ4UOhLYaGcoszBFqlYOyXYFpuBwI%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=42
Content-Length: 1556
Expect: 100-continue
{
"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 200 Ok
Cache-Control: no-store, no-cache
Pragma: no-cache
Content-Type: application/json
Content-Location: https://contosomarketing.documents.azure.com/dbs/volcanodb/colls/volcano1/triggers/PostTrigger-UpdateMetaAll
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: "060022e5-0000-0000-0000-566882360000"
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: 42
x-ms-session-token: 43
x-ms-current-write-quorum: 3
x-ms-current-replica-set-size: 4
x-ms-request-charge: 12.57
x-ms-serviceversion: version=1.5.57.3
x-ms-activity-id: 614411ac-2abb-4770-a1f9-3533d7339ec6
Set-Cookie: x-ms-session-token=43; Domain=contosomarketing.documents.azure.com; Path=/dbs/volcanodb/colls/volcano1
x-ms-gatewayversion: version=1.5.57.3
Date: Wed, 09 Dec 2015 19:34:15 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": 1449689654,
"_self": "dbs\/Sl8fAA==\/colls\/Sl8fALN4sw4=\/triggers\/Sl8fALN4sw4BAAAAAAAAcA==\/",
"_etag": "\"060022e5-0000-0000-0000-566882360000\""
}