diff --git a/CreateMessage/index.ts b/CreateMessage/index.ts index 0f38214c..5cc939c2 100644 --- a/CreateMessage/index.ts +++ b/CreateMessage/index.ts @@ -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 @@ -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"); @@ -47,8 +45,6 @@ const servicesCollectionUrl = documentDbUtils.getCollectionUri( SERVICE_COLLECTION_NAME ); -const documentClient = getDocumentClient(cosmosDbUri, cosmosDbKey); - const messageModel = new MessageModel( documentClient, messagesCollectionUrl, diff --git a/CreateNotificationActivity/index.ts b/CreateNotificationActivity/index.ts index 8c78eb22..8a48a01b 100644 --- a/CreateNotificationActivity/index.ts +++ b/CreateNotificationActivity/index.ts @@ -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( @@ -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 diff --git a/EmailNotificationActivity/index.ts b/EmailNotificationActivity/index.ts index 0c89c7b4..c3f9737f 100644 --- a/EmailNotificationActivity/index.ts +++ b/EmailNotificationActivity/index.ts @@ -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 diff --git a/GetLimitedProfile/index.ts b/GetLimitedProfile/index.ts index 253e071d..d92280d5 100644 --- a/GetLimitedProfile/index.ts +++ b/GetLimitedProfile/index.ts @@ -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 @@ -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( diff --git a/GetLimitedProfileByPOST/index.ts b/GetLimitedProfileByPOST/index.ts index a111f57b..8cb8546c 100644 --- a/GetLimitedProfileByPOST/index.ts +++ b/GetLimitedProfileByPOST/index.ts @@ -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( diff --git a/GetMessage/index.ts b/GetMessage/index.ts index 0d54c7fb..7b5e39d4 100644 --- a/GetMessage/index.ts +++ b/GetMessage/index.ts @@ -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 @@ -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"); @@ -69,8 +67,6 @@ const notificationsStatusCollectionUrl = documentDbUtils.getCollectionUri( NOTIFICATION_STATUS_COLLECTION_NAME ); -const documentClient = getDocumentClient(cosmosDbUri, cosmosDbKey); - const messageModel = new MessageModel( documentClient, messagesCollectionUrl, diff --git a/GetSubscriptionsFeed/index.ts b/GetSubscriptionsFeed/index.ts index d250d361..d8383703 100644 --- a/GetSubscriptionsFeed/index.ts +++ b/GetSubscriptionsFeed/index.ts @@ -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 @@ -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); @@ -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"); diff --git a/MessageStatusUpdaterActivity/index.ts b/MessageStatusUpdaterActivity/index.ts index bf15b86b..56afbb4b 100644 --- a/MessageStatusUpdaterActivity/index.ts +++ b/MessageStatusUpdaterActivity/index.ts @@ -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 diff --git a/NotificationStatusUpdaterActivity/index.ts b/NotificationStatusUpdaterActivity/index.ts index 35f6e1d6..13fd9be5 100644 --- a/NotificationStatusUpdaterActivity/index.ts +++ b/NotificationStatusUpdaterActivity/index.ts @@ -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 diff --git a/StoreMessageContentActivity/index.ts b/StoreMessageContentActivity/index.ts index b640928b..a0efc919 100644 --- a/StoreMessageContentActivity/index.ts +++ b/StoreMessageContentActivity/index.ts @@ -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 diff --git a/WebhookNotificationActivity/index.ts b/WebhookNotificationActivity/index.ts index 2c93eb0f..5a0268a4 100644 --- a/WebhookNotificationActivity/index.ts +++ b/WebhookNotificationActivity/index.ts @@ -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 diff --git a/utils/cosmosdb.ts b/utils/cosmosdb.ts index cac229e3..2b926eb7 100644 --- a/utils/cosmosdb.ts +++ b/utils/cosmosdb.ts @@ -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 +});