Skip to content

Commit

Permalink
[#172332578] fix documentdb client implementation (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
gunzip authored Apr 19, 2020
1 parent b9463fe commit fde35c4
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 82 deletions.
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
});

0 comments on commit fde35c4

Please sign in to comment.