Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#172332578] fix documentdb client implementation #50

Merged
merged 1 commit into from
Apr 19, 2020
Merged
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: 1 addition & 5 deletions CreateMessage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
initAppInsights,
withAppInsightsContext
} from "io-functions-commons/dist/src/utils/application_insights";
import { getDocumentClient } from "../utils/cosmosdb";
import { documentClient } from "../utils/cosmosdb";
import { CreateMessage } from "./handler";

// Setup Express
Expand All @@ -32,8 +32,6 @@ secureExpressApp(app);
// Set up CORS (free access to the API from browser clients)
app.use(cors());

const cosmosDbUri = getRequiredStringEnv("COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const messageContainerName = getRequiredStringEnv("MESSAGE_CONTAINER_NAME");

Expand All @@ -47,8 +45,6 @@ const servicesCollectionUrl = documentDbUtils.getCollectionUri(
SERVICE_COLLECTION_NAME
);

const documentClient = getDocumentClient(cosmosDbUri, cosmosDbKey);

const messageModel = new MessageModel(
documentClient,
messagesCollectionUrl,
Expand Down
9 changes: 1 addition & 8 deletions CreateNotificationActivity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import * as documentDbUtils from "io-functions-commons/dist/src/utils/documentdb";
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";

import { getDocumentClient } from "../utils/cosmosdb";
import { documentClient } from "../utils/cosmosdb";
import { getCreateNotificationActivityHandler } from "./handler";

const sandboxFiscalCode = FiscalCode.decode(
Expand All @@ -36,17 +36,10 @@ const sandboxFiscalCode = FiscalCode.decode(
});

// Setup DocumentDB
const cosmosDbUri = getRequiredStringEnv("COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");

const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);

// We create the db client, services and models here
// as if any error occurs during the construction of these objects
// that would be unrecoverable anyway and we neither may trig a retry
const documentClient = getDocumentClient(cosmosDbUri, cosmosDbKey);

const notificationsCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
NOTIFICATION_COLLECTION_NAME
Expand Down
9 changes: 1 addition & 8 deletions EmailNotificationActivity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,14 @@ import * as documentDbUtils from "io-functions-commons/dist/src/utils/documentdb
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { MailUpTransport } from "io-functions-commons/dist/src/utils/mailup";

import { getDocumentClient } from "../utils/cosmosdb";
import { documentClient } from "../utils/cosmosdb";
import { getEmailNotificationActivityHandler } from "./handler";

// Setup DocumentDB
const cosmosDbUri = getRequiredStringEnv("COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");

const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);

// We create the db client, services and models here
// as if any error occurs during the construction of these objects
// that would be unrecoverable anyway and we neither may trig a retry
const documentClient = getDocumentClient(cosmosDbUri, cosmosDbKey);

const notificationsCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
NOTIFICATION_COLLECTION_NAME
Expand Down
6 changes: 1 addition & 5 deletions GetLimitedProfile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { secureExpressApp } from "io-functions-commons/dist/src/utils/express";
import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/context_middleware";
import createAzureFunctionHandler from "io-functions-express/dist/src/createAzureFunctionsHandler";

import { getDocumentClient } from "../utils/cosmosdb";
import { documentClient } from "../utils/cosmosdb";
import { GetLimitedProfile } from "./handler";

// Setup Express
Expand All @@ -25,12 +25,8 @@ secureExpressApp(app);
// Set up CORS (free access to the API from browser clients)
app.use(cors());

const cosmosDbUri = getRequiredStringEnv("COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");

const documentClient = getDocumentClient(cosmosDbUri, cosmosDbKey);

const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);

const servicesCollectionUrl = documentDbUtils.getCollectionUri(
Expand Down
6 changes: 1 addition & 5 deletions GetLimitedProfileByPOST/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,11 @@ import { secureExpressApp } from "io-functions-commons/dist/src/utils/express";
import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/context_middleware";
import createAzureFunctionHandler from "io-functions-express/dist/src/createAzureFunctionsHandler";

import { getDocumentClient } from "../utils/cosmosdb";
import { documentClient } from "../utils/cosmosdb";
import { GetLimitedProfileByPOST } from "./handler";

const cosmosDbUri = getRequiredStringEnv("COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");

const documentClient = getDocumentClient(cosmosDbUri, cosmosDbKey);

const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);

const servicesCollectionUrl = documentDbUtils.getCollectionUri(
Expand Down
6 changes: 1 addition & 5 deletions GetMessage/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
NotificationStatusModel
} from "io-functions-commons/dist/src/models/notification_status";

import { getDocumentClient } from "../utils/cosmosdb";
import { documentClient } from "../utils/cosmosdb";
import { GetMessage } from "./handler";

// Setup Express
Expand All @@ -42,8 +42,6 @@ secureExpressApp(app);
// Set up CORS (free access to the API from browser clients)
app.use(cors());

const cosmosDbUri = getRequiredStringEnv("COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");
const messageContainerName = getRequiredStringEnv("MESSAGE_CONTAINER_NAME");

Expand All @@ -69,8 +67,6 @@ const notificationsStatusCollectionUrl = documentDbUtils.getCollectionUri(
NOTIFICATION_STATUS_COLLECTION_NAME
);

const documentClient = getDocumentClient(cosmosDbUri, cosmosDbKey);

const messageModel = new MessageModel(
documentClient,
messagesCollectionUrl,
Expand Down
6 changes: 1 addition & 5 deletions GetSubscriptionsFeed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { setAppContext } from "io-functions-commons/dist/src/utils/middlewares/c

import createAzureFunctionHandler from "io-functions-express/dist/src/createAzureFunctionsHandler";

import { getDocumentClient } from "../utils/cosmosdb";
import { documentClient } from "../utils/cosmosdb";
import { GetSubscriptionsFeed } from "./handler";

// Setup Express
Expand All @@ -25,8 +25,6 @@ secureExpressApp(app);
// Set up CORS (free access to the API from browser clients)
app.use(cors());

const cosmosDbUri = getRequiredStringEnv("COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");

const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);
Expand All @@ -36,8 +34,6 @@ const servicesCollectionUrl = documentDbUtils.getCollectionUri(
SERVICE_COLLECTION_NAME
);

const documentClient = getDocumentClient(cosmosDbUri, cosmosDbKey);

const serviceModel = new ServiceModel(documentClient, servicesCollectionUrl);

const storageConnectionString = getRequiredStringEnv("QueueStorageConnection");
Expand Down
6 changes: 1 addition & 5 deletions MessageStatusUpdaterActivity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@ import {
import * as documentDbUtils from "io-functions-commons/dist/src/utils/documentdb";
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";

import { getDocumentClient } from "../utils/cosmosdb";
import { documentClient } from "../utils/cosmosdb";
import { getMessageStatusUpdaterActivityHandler } from "./handler";

const cosmosDbUri = getRequiredStringEnv("COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");

const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);

const documentClient = getDocumentClient(cosmosDbUri, cosmosDbKey);

const messagesStatusCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
MESSAGE_STATUS_COLLECTION_NAME
Expand Down
9 changes: 1 addition & 8 deletions NotificationStatusUpdaterActivity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,15 @@ import {
} from "io-functions-commons/dist/src/models/notification_status";
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";

import { getDocumentClient } from "../utils/cosmosdb";
import { documentClient } from "../utils/cosmosdb";
import { getNotificationStatusUpdaterActivityHandler } from "./handler";

// Setup DocumentDB

const cosmosDbUri = getRequiredStringEnv("COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");

const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);

// We create the db client, services and models here
// as if any error occurs during the construction of these objects
// that would be unrecoverable anyway and we neither may trig a retry
const documentClient = getDocumentClient(cosmosDbUri, cosmosDbKey);

const notificationStatusCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
NOTIFICATION_STATUS_COLLECTION_NAME
Expand Down
9 changes: 1 addition & 8 deletions StoreMessageContentActivity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,14 @@ import {
} from "io-functions-commons/dist/src/models/profile";
import * as documentDbUtils from "io-functions-commons/dist/src/utils/documentdb";
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";
import { getDocumentClient } from "../utils/cosmosdb";
import { documentClient } from "../utils/cosmosdb";
import { getStoreMessageContentActivityHandler } from "./handler";

// Setup DocumentDB
const cosmosDbUri = getRequiredStringEnv("COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");

const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);

// We create the db client, services and models here
// as if any error occurs during the construction of these objects
// that would be unrecoverable anyway and we neither may trig a retry
const documentClient = getDocumentClient(cosmosDbUri, cosmosDbKey);

const profilesCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
PROFILE_COLLECTION_NAME
Expand Down
9 changes: 1 addition & 8 deletions WebhookNotificationActivity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,15 @@ import {
} from "italia-ts-commons/lib/fetch";
import { Millisecond } from "italia-ts-commons/lib/units";

import { getDocumentClient } from "../utils/cosmosdb";
import { documentClient } from "../utils/cosmosdb";
import { getNotifyClient } from "./client";
import { getWebhookNotificationActivityHandler } from "./handler";

// Setup DocumentDB
const cosmosDbUri = getRequiredStringEnv("COSMOSDB_URI");
const cosmosDbKey = getRequiredStringEnv("COSMOSDB_KEY");
const cosmosDbName = getRequiredStringEnv("COSMOSDB_NAME");

const documentDbDatabaseUrl = documentDbUtils.getDatabaseUri(cosmosDbName);

// We create the db client, services and models here
// as if any error occurs during the construction of these objects
// that would be unrecoverable anyway and we neither may trig a retry
const documentClient = getDocumentClient(cosmosDbUri, cosmosDbKey);

const notificationsCollectionUrl = documentDbUtils.getCollectionUri(
documentDbDatabaseUrl,
NOTIFICATION_COLLECTION_NAME
Expand Down
18 changes: 6 additions & 12 deletions utils/cosmosdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@
* Use a singleton CosmosDB client across functions.
*/
import { DocumentClient as DocumentDBClient } from "documentdb";
import { getRequiredStringEnv } from "io-functions-commons/dist/src/utils/env";

// tslint:disable-next-line: no-let
let instance: DocumentDBClient;
const cosmosDbUri = getRequiredStringEnv("COSMOSDB_URI");
const masterKey = getRequiredStringEnv("COSMOSDB_KEY");

export function getDocumentClient(
cosmosDbUri: string,
cosmosDbKey: string
): DocumentDBClient {
return instance
? instance
: (instance = new DocumentDBClient(cosmosDbUri, {
masterKey: cosmosDbKey
}));
}
export const documentClient = new DocumentDBClient(cosmosDbUri, {
masterKey
});