Skip to content

New CosmosDbTrigger

Daniel Scott-Raynsford edited this page Dec 23, 2018 · 4 revisions

external help file: CosmosDB-help.xml Module Name: CosmosDB online version: schema: 2.0.0

New-CosmosDbTrigger

SYNOPSIS

Create a new trigger for a collection in a Cosmos DB database.

SYNTAX

Context (Default)

New-CosmosDbTrigger -Context <Context> [-KeyType <String>] [-Key <SecureString>] [-Database <String>]
 -CollectionId <String> -Id <String> -TriggerBody <String> -TriggerOperation <String> -TriggerType <String>
 [<CommonParameters>]

Account

New-CosmosDbTrigger -Account <String> [-KeyType <String>] [-Key <SecureString>] [-Database <String>]
 -CollectionId <String> -Id <String> -TriggerBody <String> -TriggerOperation <String> -TriggerType <String>
 [<CommonParameters>]

DESCRIPTION

This cmdlet will create a trigger for a collection in a Cosmos DB.

EXAMPLES

Example 1

PS C:\> $body = @'
function updateMetadata() {
    var context = getContext();
    var collection = context.getCollection();
    var response = context.getResponse();
    var createdDocument = response.getBody();

    // query for metadata document
    var filterQuery = 'SELECT * FROM root r WHERE r.id = "_metadata"';
    var accept = collection.queryDocuments(collection.getSelfLink(), filterQuery, updateMetadataCallback);
    if(!accept) throw "Unable to update metadata, abort";

    function updateMetadataCallback(err, documents, responseOptions) {
        if(err) throw new Error("Error" + err.message);

        if(documents.length != 1) throw 'Unable to find metadata document';
        var metadataDocument = documents[0];

        // update metadata
        metadataDocument.createdDocuments += 1;
        metadataDocument.createdNames += " " + createdDocument.id;

        var accept = collection.replaceDocument(metadataDocument._self, metadataDocument, function(err, docReplaced) {
            if(err) throw "Unable to update metadata, abort";
        });

        if(!accept) throw "Unable to update metadata, abort";
        return;
    }
}
'@
PS C:\> New-CosmosDbTrigger -Context $cosmosDbContext -CollectionId 'MyNewCollection' -Id 'MyTrigger' -TriggerBody $body -TriggerOperation All -TriggerType Post

Create a new trigger called 'MyTrigger' in the collection 'MyNewCollection' that executes on all Post operations.

PARAMETERS

-Account

The account name of the Cosmos DB to access.

Type: String
Parameter Sets: Account
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-CollectionId

This is the Id of the collection to create the trigger for.

Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Context

This is an object containing the context information of the Cosmos DB database that will be deleted. It should be created by `New-CosmosDbContext`.

Type: Context
Parameter Sets: Context
Aliases: Connection

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Database

The name of the database to access in the Cosmos DB account.

Type: String
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Id

This is the Id of the trigger to create.

Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Key

The key to be used to access this Cosmos DB.

Type: SecureString
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-KeyType

The type of key that will be used to access ths Cosmos DB.

Type: String
Parameter Sets: (All)
Aliases:
Accepted values: master, resource

Required: False
Position: Named
Default value: Master
Accept pipeline input: False
Accept wildcard characters: False

-TriggerBody

This is the body of the trigger.

Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-TriggerOperation

This is the type of operation that will invoke the trigger.

Type: String
Parameter Sets: (All)
Aliases:
Accepted values: All, Create, Replace, Delete

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-TriggerType

This specifies when the trigger will be fired.

Type: String
Parameter Sets: (All)
Aliases:
Accepted values: Pre, Post

Required: True
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).

INPUTS

OUTPUTS

System.Object

NOTES

RELATED LINKS

CosmosDB Cmdlets

Clone this wiki locally