Skip to content

Commit 253374d

Browse files
committed
Add Cosmos DB extension v4 types
And some missing properties for extension v3 types
1 parent 45f0698 commit 253374d

File tree

5 files changed

+253
-48
lines changed

5 files changed

+253
-48
lines changed

src/app.ts

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import {
55
CosmosDBFunctionOptions,
6+
CosmosDBTrigger,
67
EventGridFunctionOptions,
78
EventHubFunctionOptions,
89
FunctionOptions,
@@ -18,8 +19,8 @@ import {
1819
} from '@azure/functions';
1920
import * as coreTypes from '@azure/functions-core';
2021
import { CoreInvocationContext, FunctionCallback } from '@azure/functions-core';
21-
import { returnBindingKey, version } from './constants';
2222
import { InvocationModel } from './InvocationModel';
23+
import { returnBindingKey, version } from './constants';
2324
import * as output from './output';
2425
import * as trigger from './trigger';
2526
import { isTrigger } from './utils/isTrigger';
@@ -174,21 +175,58 @@ export function eventGrid(name: string, options: EventGridFunctionOptions): void
174175
}
175176

176177
export function cosmosDB(name: string, options: CosmosDBFunctionOptions): void {
177-
generic(name, {
178-
trigger: trigger.cosmosDB({
178+
let cosmosTrigger: CosmosDBTrigger;
179+
if ('connectionStringSetting' in options) {
180+
cosmosTrigger = trigger.cosmosDB({
181+
checkpointDocumentCount: options.checkpointDocumentCount,
182+
checkpointInterval: options.checkpointInterval,
179183
collectionName: options.collectionName,
180184
connectionStringSetting: options.connectionStringSetting,
181185
createLeaseCollectionIfNotExists: options.createLeaseCollectionIfNotExists,
182186
databaseName: options.databaseName,
187+
feedPollDelay: options.feedPollDelay,
183188
id: options.id,
189+
leaseAcquireInterval: options.leaseAcquireInterval,
184190
leaseCollectionName: options.leaseCollectionName,
185191
leaseCollectionPrefix: options.leaseCollectionPrefix,
186192
leaseCollectionThroughput: options.leaseCollectionThroughput,
187193
leaseConnectionStringSetting: options.leaseConnectionStringSetting,
188194
leaseDatabaseName: options.leaseDatabaseName,
195+
leaseExpirationInterval: options.leaseExpirationInterval,
196+
leaseRenewInterval: options.leaseRenewInterval,
197+
maxItemsPerInvocation: options.maxItemsPerInvocation,
189198
partitionKey: options.partitionKey,
199+
preferredLocations: options.preferredLocations,
190200
sqlQuery: options.sqlQuery,
191-
}),
201+
startFromBeginning: options.startFromBeginning,
202+
useMultipleWriteLocations: options.useMultipleWriteLocations,
203+
});
204+
} else {
205+
cosmosTrigger = trigger.cosmosDB({
206+
connection: options.connection,
207+
containerName: options.containerName,
208+
createLeaseContainerIfNotExists: options.createLeaseContainerIfNotExists,
209+
databaseName: options.databaseName,
210+
feedPollDelay: options.feedPollDelay,
211+
id: options.id,
212+
leaseAcquireInterval: options.leaseAcquireInterval,
213+
leaseConnection: options.leaseConnection,
214+
leaseContainerName: options.leaseContainerName,
215+
leaseContainerPrefix: options.leaseContainerPrefix,
216+
leaseDatabaseName: options.leaseDatabaseName,
217+
leaseExpirationInterval: options.leaseExpirationInterval,
218+
leaseRenewInterval: options.leaseRenewInterval,
219+
leasesContainerThroughput: options.leasesContainerThroughput,
220+
maxItemsPerInvocation: options.maxItemsPerInvocation,
221+
partitionKey: options.partitionKey,
222+
preferredLocations: options.preferredLocations,
223+
sqlQuery: options.sqlQuery,
224+
startFromBeginning: options.startFromBeginning,
225+
startFromTime: options.startFromTime,
226+
});
227+
}
228+
generic(name, {
229+
trigger: cosmosTrigger,
192230
...options,
193231
});
194232
}

types/cosmosDB.d.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import {
5+
CosmosDBv3FunctionOptions,
6+
CosmosDBv3Handler,
7+
CosmosDBv3Input,
8+
CosmosDBv3InputOptions,
9+
CosmosDBv3Output,
10+
CosmosDBv3OutputOptions,
11+
CosmosDBv3Trigger,
12+
CosmosDBv3TriggerOptions,
13+
} from './cosmosDB.v3';
14+
import {
15+
CosmosDBv4FunctionOptions,
16+
CosmosDBv4Handler,
17+
CosmosDBv4Input,
18+
CosmosDBv4InputOptions,
19+
CosmosDBv4Output,
20+
CosmosDBv4OutputOptions,
21+
CosmosDBv4Trigger,
22+
CosmosDBv4TriggerOptions,
23+
} from './cosmosDB.v4';
24+
25+
export type CosmosDBHandler = CosmosDBv3Handler | CosmosDBv4Handler;
26+
27+
export type CosmosDBFunctionOptions = CosmosDBv3FunctionOptions | CosmosDBv4FunctionOptions;
28+
29+
export type CosmosDBInputOptions = CosmosDBv3InputOptions | CosmosDBv4InputOptions;
30+
export type CosmosDBInput = CosmosDBv3Input | CosmosDBv4Input;
31+
32+
export type CosmosDBTriggerOptions = CosmosDBv3TriggerOptions | CosmosDBv4TriggerOptions;
33+
export type CosmosDBTrigger = CosmosDBv3Trigger | CosmosDBv4Trigger;
34+
35+
export type CosmosDBOutputOptions = CosmosDBv3OutputOptions | CosmosDBv4OutputOptions;
36+
export type CosmosDBOutput = CosmosDBv3Output | CosmosDBv4Output;

types/cosmosDB.v3.d.ts

Lines changed: 81 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
import { FunctionInput, FunctionOptions, FunctionOutput, FunctionResult, FunctionTrigger } from './index';
55
import { InvocationContext } from './InvocationContext';
66

7-
export type CosmosDBHandler = (documents: unknown[], context: InvocationContext) => FunctionResult;
7+
export type CosmosDBv3Handler = (documents: unknown[], context: InvocationContext) => FunctionResult;
88

9-
export interface CosmosDBFunctionOptions extends CosmosDBTriggerOptions, Partial<FunctionOptions> {
10-
handler: CosmosDBHandler;
9+
export interface CosmosDBv3FunctionOptions extends CosmosDBv3TriggerOptions, Partial<FunctionOptions> {
10+
handler: CosmosDBv3Handler;
1111

12-
trigger?: CosmosDBTrigger;
12+
trigger?: CosmosDBv3Trigger;
1313
}
1414

15-
export interface CosmosDBInputOptions {
15+
export interface CosmosDBv3InputOptions {
1616
/**
1717
* An app setting (or environment variable) with the Cosmos DB connection string
1818
*/
@@ -45,10 +45,16 @@ export interface CosmosDBInputOptions {
4545
* Don't set both the id and sqlQuery properties. If you don't set either one, the entire collection is retrieved.
4646
*/
4747
sqlQuery?: string;
48+
49+
/**
50+
* Defines preferred locations (regions) for geo-replicated database accounts in the Azure Cosmos DB service.
51+
* Values should be comma-separated. For example, East US,South Central US,North Europe
52+
*/
53+
preferredLocations?: string;
4854
}
49-
export type CosmosDBInput = FunctionInput & CosmosDBInputOptions;
55+
export type CosmosDBv3Input = FunctionInput & CosmosDBv3InputOptions;
5056

51-
export interface CosmosDBTriggerOptions extends CosmosDBInputOptions {
57+
export interface CosmosDBv3TriggerOptions extends CosmosDBv3InputOptions {
5258
/**
5359
* The name of an app setting that contains the connection string to the service which holds the lease collection.
5460
* If not set it will connect to the service defined by `connectionStringSetting`
@@ -80,10 +86,64 @@ export interface CosmosDBTriggerOptions extends CosmosDBInputOptions {
8086
* Using a prefix allows two separate Azure Functions to share the same Lease collection by using different prefixes.
8187
*/
8288
leaseCollectionPrefix?: string;
89+
90+
/**
91+
* The time (in milliseconds) for the delay between polling a partition for new changes on the feed, after all current changes are drained.
92+
* Default is 5,000 milliseconds, or 5 seconds.
93+
*/
94+
feedPollDelay?: number;
95+
96+
/**
97+
* When set, it defines, in milliseconds, the interval to kick off a task to compute if partitions are distributed evenly among known host instances.
98+
* Default is 13000 (13 seconds).
99+
*/
100+
leaseAcquireInterval?: number;
101+
102+
/**
103+
* When set, it defines, in milliseconds, the interval for which the lease is taken on a lease representing a partition.
104+
* If the lease is not renewed within this interval, it will cause it to expire and ownership of the partition will move to another instance.
105+
* Default is 60000 (60 seconds).
106+
*/
107+
leaseExpirationInterval?: number;
108+
109+
/**
110+
* When set, it defines, in milliseconds, the renew interval for all leases for partitions currently held by an instance.
111+
* Default is 17000 (17 seconds).
112+
*/
113+
leaseRenewInterval?: number;
114+
115+
/**
116+
* When set, it defines, in milliseconds, the interval between lease checkpoints. Default is always after each Function call.
117+
*/
118+
checkpointInterval?: number;
119+
120+
/**
121+
* Customizes the amount of documents between lease checkpoints. Default is after every function call.
122+
*/
123+
checkpointDocumentCount?: number;
124+
125+
/**
126+
* When set, this property sets the maximum number of items received per Function call.
127+
* If operations in the monitored container are performed through stored procedures, transaction scope is preserved when reading items from the change feed.
128+
* As a result, the number of items received could be higher than the specified value so that the items changed by the same transaction are returned as part of one atomic batch.
129+
*/
130+
maxItemsPerInvocation?: number;
131+
132+
/**
133+
* This option tells the Trigger to read changes from the beginning of the container's change history instead of starting at the current time.
134+
* Reading from the beginning only works the first time the trigger starts, as in subsequent runs, the checkpoints are already stored.
135+
* Setting this option to true when there are leases already created has no effect.
136+
*/
137+
startFromBeginning?: boolean;
138+
139+
/**
140+
* Enables multi-region accounts for writing to the leases collection.
141+
*/
142+
useMultipleWriteLocations?: boolean;
83143
}
84-
export type CosmosDBTrigger = FunctionTrigger & CosmosDBTriggerOptions;
144+
export type CosmosDBv3Trigger = FunctionTrigger & CosmosDBv3TriggerOptions;
85145

86-
export interface CosmosDBOutputOptions {
146+
export interface CosmosDBv3OutputOptions {
87147
/**
88148
* An app setting (or environment variable) with the Cosmos DB connection string
89149
*/
@@ -114,5 +174,16 @@ export interface CosmosDBOutputOptions {
114174
* When createIfNotExists is true, it defines the [throughput](https://docs.microsoft.com/azure/cosmos-db/set-throughput) of the created collection
115175
*/
116176
collectionThroughput?: number;
177+
178+
/**
179+
* Defines preferred locations (regions) for geo-replicated database accounts in the Azure Cosmos DB service.
180+
* Values should be comma-separated. For example, East US,South Central US,North Europe
181+
*/
182+
preferredLocations?: string;
183+
184+
/**
185+
* When set to true along with preferredLocations, supports multi-region writes in the Azure Cosmos DB service.
186+
*/
187+
useMultipleWriteLocations?: boolean;
117188
}
118-
export type CosmosDBOutput = FunctionOutput & CosmosDBOutputOptions;
189+
export type CosmosDBv3Output = FunctionOutput & CosmosDBv3OutputOptions;

0 commit comments

Comments
 (0)