Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import {
CosmosDBFunctionOptions,
CosmosDBMongoFunctionOptions,
EventGridFunctionOptions,
EventHubFunctionOptions,
FunctionTrigger,
Expand Down Expand Up @@ -131,6 +132,11 @@ export function cosmosDB(name: string, options: CosmosDBFunctionOptions): void {
generic(name, convertToGenericOptions(options, <any>trigger.cosmosDB));
}

export function cosmosDBMongo<T = unknown>(name: string, options: CosmosDBMongoFunctionOptions<T>): void {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
generic(name, convertToGenericOptions(options, <any>trigger.cosmosDBMongo));
}

export function warmup(name: string, options: WarmupFunctionOptions): void {
generic(name, convertToGenericOptions(options, trigger.warmup));
}
Expand Down
9 changes: 9 additions & 0 deletions src/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import {
CosmosDBInput,
CosmosDBInputOptions,
CosmosDBMongoInput,
CosmosDBMongoInputOptions,
FunctionInput,
GenericInputOptions,
MySqlInput,
Expand Down Expand Up @@ -42,6 +44,13 @@ export function cosmosDB(options: CosmosDBInputOptions): CosmosDBInput {
});
}

export function cosmosDBMongo(options: CosmosDBMongoInputOptions): CosmosDBMongoInput {
return addInputBindingName({
...options,
type: 'cosmosDBMongo',
});
}

export function sql(options: SqlInputOptions): SqlInput {
return addInputBindingName({
...options,
Expand Down
9 changes: 9 additions & 0 deletions src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT License.

import {
CosmosDBMongoOutput,
CosmosDBMongoOutputOptions,
CosmosDBOutput,
CosmosDBOutputOptions,
EventGridOutput,
Expand Down Expand Up @@ -94,6 +96,13 @@ export function cosmosDB(options: CosmosDBOutputOptions): CosmosDBOutput {
});
}

export function cosmosDBMongo(options: CosmosDBMongoOutputOptions): CosmosDBMongoOutput {
return addOutputBindingName({
...options,
type: 'cosmosDBMongo',
});
}

export function sql(options: SqlOutputOptions): SqlOutput {
return addOutputBindingName({
...options,
Expand Down
9 changes: 9 additions & 0 deletions src/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT License.

import {
CosmosDBMongoTrigger,
CosmosDBMongoTriggerOptions,
CosmosDBTrigger,
CosmosDBTriggerOptions,
EventGridTrigger,
Expand Down Expand Up @@ -104,6 +106,13 @@ export function cosmosDB(options: CosmosDBTriggerOptions): CosmosDBTrigger {
});
}

export function cosmosDBMongo(options: CosmosDBMongoTriggerOptions): CosmosDBMongoTrigger {
return addTriggerBindingName({
...options,
type: 'cosmosDBMongoTrigger',
});
}

export function warmup(options: WarmupTriggerOptions): WarmupTrigger {
return addTriggerBindingName({
...options,
Expand Down
193 changes: 193 additions & 0 deletions test/cosmosDBMongo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License.

import 'mocha';
import { expect } from 'chai';
import { input, output, trigger } from '../src';
import { toCoreFunctionMetadata } from '../src/converters/toCoreFunctionMetadata';
import { InvocationContext } from '../types';

describe('cosmosDBMongo bindings', () => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const handler = (_doc: unknown, _context: InvocationContext) => {};

const minimalTriggerOptions = {
connectionStringSetting: 'CosmosDBMongo',
databaseName: 'MyDatabase',
collectionName: 'MyCollection',
};

// -------------------------------------------------------------------------
// trigger
// -------------------------------------------------------------------------

describe('trigger.cosmosDBMongo', () => {
it('produces correct type string', () => {
const trig = trigger.cosmosDBMongo(minimalTriggerOptions);
expect(trig.type).to.equal('cosmosDBMongoTrigger');
});

it('copies all trigger options onto the binding object', () => {
const trig = trigger.cosmosDBMongo({
connectionStringSetting: 'CosmosDBMongo',
databaseName: 'db',
collectionName: 'coll',
createIfNotExists: true,
triggerLevel: 'Database',
leaseDatabaseName: 'leaseDb',
leaseCollectionName: 'leases',
leaseConnectionStringSetting: 'LeaseConn',
tenantId: 'tenant-abc',
managedIdentityClientId: 'mi-abc',
leaseTenantId: 'lease-tenant',
leaseManagedIdentityClientId: 'lease-mi',
});
expect(trig.connectionStringSetting).to.equal('CosmosDBMongo');
expect(trig.databaseName).to.equal('db');
expect(trig.collectionName).to.equal('coll');
expect(trig.createIfNotExists).to.equal(true);
expect(trig.triggerLevel).to.equal('Database');
expect(trig.leaseDatabaseName).to.equal('leaseDb');
expect(trig.leaseCollectionName).to.equal('leases');
expect(trig.leaseConnectionStringSetting).to.equal('LeaseConn');
expect(trig.tenantId).to.equal('tenant-abc');
expect(trig.managedIdentityClientId).to.equal('mi-abc');
expect(trig.leaseTenantId).to.equal('lease-tenant');
expect(trig.leaseManagedIdentityClientId).to.equal('lease-mi');
});

it('generates a deterministic binding name with Trigger suffix', () => {
const trig1 = trigger.cosmosDBMongo(minimalTriggerOptions);
const trig2 = trigger.cosmosDBMongo(minimalTriggerOptions);
expect(trig1.name).to.equal(trig2.name);
expect(trig1.name).to.include('cosmosDBMongoTrigger');
});

it('sets direction = in via toCoreFunctionMetadata', () => {
const result = toCoreFunctionMetadata('mongoTrigFunc', {
handler,
trigger: trigger.cosmosDBMongo(minimalTriggerOptions),
});
const bindingValues = Object.values(result.bindings) as Record<string, unknown>[];
const trig = bindingValues.find((b) => b['type'] === 'cosmosDBMongoTrigger');
expect(trig).to.exist;
expect(trig!['direction']).to.equal('in');
});
});

// -------------------------------------------------------------------------
// input
// -------------------------------------------------------------------------

describe('input.cosmosDBMongo', () => {
it('produces correct type string', () => {
const inp = input.cosmosDBMongo({
connectionStringSetting: 'CosmosDBMongo',
databaseName: 'db',
collectionName: 'coll',
});
expect(inp.type).to.equal('cosmosDBMongo');
});

it('copies all input options onto the binding object', () => {
const inp = input.cosmosDBMongo({
connectionStringSetting: 'CosmosDBMongo',
databaseName: 'db',
collectionName: 'coll',
queryString: '{"status": "active"}',
createIfNotExists: false,
tenantId: 'tenant-xyz',
managedIdentityClientId: 'mi-xyz',
});
expect(inp.connectionStringSetting).to.equal('CosmosDBMongo');
expect(inp.databaseName).to.equal('db');
expect(inp.collectionName).to.equal('coll');
expect(inp.queryString).to.equal('{"status": "active"}');
expect(inp.createIfNotExists).to.equal(false);
expect(inp.tenantId).to.equal('tenant-xyz');
expect(inp.managedIdentityClientId).to.equal('mi-xyz');
});

it('generates a deterministic binding name with Input suffix', () => {
const inp = input.cosmosDBMongo({
connectionStringSetting: 'CosmosDBMongo',
databaseName: 'db',
collectionName: 'coll',
});
expect(inp.name).to.include('Input');
});

it('sets direction = in via toCoreFunctionMetadata extra input', () => {
const inp = input.cosmosDBMongo({
connectionStringSetting: 'CosmosDBMongo',
databaseName: 'db',
collectionName: 'coll',
});
const result = toCoreFunctionMetadata('mongoInputFunc', {
handler: () => {},
trigger: trigger.cosmosDBMongo(minimalTriggerOptions),
extraInputs: [inp],
});
const bindingValues = Object.values(result.bindings) as Record<string, unknown>[];
const inputBinding = bindingValues.find((b) => b['type'] === 'cosmosDBMongo' && b['direction'] === 'in');
expect(inputBinding).to.exist;
});
});

// -------------------------------------------------------------------------
// output
// -------------------------------------------------------------------------

describe('output.cosmosDBMongo', () => {
it('produces correct type string', () => {
const out = output.cosmosDBMongo({
connectionStringSetting: 'CosmosDBMongo',
databaseName: 'db',
collectionName: 'coll',
});
expect(out.type).to.equal('cosmosDBMongo');
});

it('copies all output options onto the binding object', () => {
const out = output.cosmosDBMongo({
connectionStringSetting: 'CosmosDBMongo',
databaseName: 'db',
collectionName: 'coll',
createIfNotExists: true,
tenantId: 'tenant-out',
managedIdentityClientId: 'mi-out',
});
expect(out.connectionStringSetting).to.equal('CosmosDBMongo');
expect(out.databaseName).to.equal('db');
expect(out.collectionName).to.equal('coll');
expect(out.createIfNotExists).to.equal(true);
expect(out.tenantId).to.equal('tenant-out');
expect(out.managedIdentityClientId).to.equal('mi-out');
});

it('generates a deterministic binding name with Output suffix', () => {
const out = output.cosmosDBMongo({
connectionStringSetting: 'CosmosDBMongo',
databaseName: 'db',
collectionName: 'coll',
});
expect(out.name).to.include('Output');
});

it('sets direction = out via toCoreFunctionMetadata extra output', () => {
const out = output.cosmosDBMongo({
connectionStringSetting: 'CosmosDBMongo',
databaseName: 'db',
collectionName: 'coll',
});
const result = toCoreFunctionMetadata('mongoOutputFunc', {
handler: () => {},
trigger: trigger.cosmosDBMongo(minimalTriggerOptions),
extraOutputs: [out],
});
const bindingValues = Object.values(result.bindings) as Record<string, unknown>[];
const outputBinding = bindingValues.find((b) => b['type'] === 'cosmosDBMongo' && b['direction'] === 'out');
expect(outputBinding).to.exist;
});
});
});
8 changes: 8 additions & 0 deletions types/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

import { CosmosDBFunctionOptions } from './cosmosDB';
import { CosmosDBMongoFunctionOptions } from './cosmosDBMongo';
import { EventGridEvent, EventGridFunctionOptions } from './eventGrid';
import { EventHubFunctionOptions } from './eventHub';
import { GenericFunctionOptions } from './generic';
Expand Down Expand Up @@ -157,6 +158,13 @@ export function eventGrid<T = EventGridEvent>(name: string, options: EventGridFu
*/
export function cosmosDB<T = unknown>(name: string, options: CosmosDBFunctionOptions<T>): void;

/**
* Registers an Azure Cosmos DB for MongoDB function in your app that will be triggered whenever change stream events occur
* @param name The name of the function. The name must be unique within your app and will mostly be used for your own tracking purposes
* @param options Configuration options describing the inputs, outputs, and handler for this function
*/
export function cosmosDBMongo<T = unknown>(name: string, options: CosmosDBMongoFunctionOptions<T>): void;

/**
* Registers a function in your app that will be triggered when an instance is added to scale a running function app.
* The warmup trigger is only called during scale-out operations, not during restarts or other non-scale startups.
Expand Down
Loading
Loading