Skip to content

Commit

Permalink
Extension improvements (#37)
Browse files Browse the repository at this point in the history
* extension-improvements override addFunctions method of NetlifyExtension class

* extension-improvements add empty default for appName

* extension-improvements add slack auth token to dbconfig

* extension-improvements updating configurations, types
  • Loading branch information
anabellabuckvar authored Nov 15, 2024
1 parent 7951c42 commit 78ae0c0
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 158 deletions.
21 changes: 8 additions & 13 deletions libs/util/src/assertDbEnvVars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,29 @@ export type Environments = 'dev' | 'stg' | 'dotcomstg' | 'prd' | 'dotcomprd';

export type CollectionName = 'repos_branches' | 'docsets' | 'documents';

export type S3UploadParams = {
bucket: string;
prefix: string;
fileName: string;
manifest: string;
};

export type DbConfig = {
export type StaticEnvVars = {
ATLAS_CLUSTER0_URI: string;
ATLAS_SEARCH_URI: string;
AWS_S3_ACCESS_KEY_ID: string;
AWS_S3_SECRET_ACCESS_KEY: string;
DOCSETS_COLLECTION: CollectionName;
DOCUMENTS_COLLECTION: CollectionName;
DOCUMENTS_COLLECTION: string;
REPOS_BRANCHES_COLLECTION: CollectionName;
SLACK_AUTH_TOKEN: string;
SLACK_SIGNING_SECRET: string;
};

const assertEnvVars = (vars: DbConfig) => {
const assertEnvVars = (vars: StaticEnvVars) => {
const missingVars = Object.entries(vars)
.filter(([, value]) => !value)
.map(([key]) => `- ${key}`)
.join('\n');
if (missingVars)
throw new Error(`Missing env var(s) ${JSON.stringify(missingVars)}`);
if (missingVars)
throw new Error(`Missing env var(s) ${JSON.stringify(missingVars)}`);
return vars;
};

export const getDbConfig = (): DbConfig => {
export const getDbConfig = (): StaticEnvVars => {
const environmentVariables = assertEnvVars({
ATLAS_CLUSTER0_URI: `mongodb+srv://${process.env.MONGO_ATLAS_USERNAME}:${process.env.MONGO_ATLAS_PASSWORD}@${process.env.MONGO_ATLAS_CLUSTER0_HOST}/?retryWrites=true&w=majority`,
ATLAS_SEARCH_URI: `mongodb+srv://${process.env.MONGO_ATLAS_USERNAME}:${process.env.MONGO_ATLAS_PASSWORD}@${process.env.MONGO_ATLAS_SEARCH_HOST}/?retryWrites=true&w=majority`,
Expand All @@ -44,6 +38,7 @@ export const getDbConfig = (): DbConfig => {
(process.env.REPOS_BRANCHES_COLLECTION as CollectionName) ??
'repos_branches',
SLACK_SIGNING_SECRET: process.env.SLACK_SIGNING_SECRET as string,
SLACK_AUTH_TOKEN: process.env.SLACK_AUTH_TOKEN as string,
});

return environmentVariables;
Expand Down
76 changes: 0 additions & 76 deletions libs/util/src/databaseConnection/atlasClusterConnector.ts

This file was deleted.

19 changes: 19 additions & 0 deletions libs/util/src/databaseConnection/clusterConnector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as mongodb from 'mongodb';

export const teardown = async (client: mongodb.MongoClient): Promise<void> => {
await client.close();
};

// Handles memoization of db object, and initial connection logic if needs to be initialized
export const dbClient = async ({
uri,
appName,
}: { uri: string; appName: string }): Promise<mongodb.MongoClient> => {
const client = new mongodb.MongoClient(uri, { appName });
try {
await client.connect();
return client;
} catch (error) {
throw new Error(`Error at client connection: ${error} `);
}
};
27 changes: 9 additions & 18 deletions libs/util/src/databaseConnection/clusterZeroConnector.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
import * as mongodb from 'mongodb';
import type * as mongodb from 'mongodb';
import { teardown, dbClient } from './clusterConnector';

let clusterZeroClient: mongodb.MongoClient;

export const teardown = async (client: mongodb.MongoClient): Promise<void> => {
await client.close();
};

// Handles memoization of db object, and initial connection logic if needs to be initialized
const dbClient = async (uri: string): Promise<mongodb.MongoClient> => {
const client = new mongodb.MongoClient(uri);
try {
await client.connect();
return client;
} catch (error) {
throw new Error(`Error at client connection: ${error} `);
}
};

export const getPoolDb = async ({
clusterZeroURI,
databaseName,
}: { clusterZeroURI: string; databaseName: string }): Promise<mongodb.Db> => {
appName,
}: {
clusterZeroURI: string;
databaseName: string;
appName: string;
}): Promise<mongodb.Db> => {
if (!clusterZeroClient) {
console.info('Creating new instance of Cluster Zero client');
clusterZeroClient = await dbClient(clusterZeroURI);
clusterZeroClient = await dbClient({ uri: clusterZeroURI, appName });
}
return clusterZeroClient.db(databaseName);
};
Expand Down
12 changes: 6 additions & 6 deletions libs/util/src/databaseConnection/fetchDocsetsData.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type * as mongodb from 'mongodb';
import { getPoolDb } from './atlasClusterConnector';
import { getPoolDb } from './clusterZeroConnector';
import type { DocsetsDocument } from './types';

export const getDocsetsCollection = async ({
URI,
clusterZeroURI,
databaseName,
collectionName,
extensionName,
}: {
URI: string;
clusterZeroURI: string;
databaseName: string;
collectionName: string;
extensionName: string;
extensionName?: string;
}): Promise<mongodb.Collection<DocsetsDocument>> => {
const dbSession = await getPoolDb({
URI,
clusterZeroURI,
databaseName,
appName: extensionName,
appName: extensionName ?? '',
});
return dbSession.collection<DocsetsDocument>(collectionName);
};
12 changes: 6 additions & 6 deletions libs/util/src/databaseConnection/fetchReposBranchesData.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type * as mongodb from 'mongodb';
import { getPoolDb } from './atlasClusterConnector';
import { getPoolDb } from './clusterZeroConnector';
import type { ReposBranchesDocument } from './types';

export const getReposBranchesCollection = async ({
URI,
clusterZeroURI,
databaseName,
collectionName,
extensionName,
}: {
URI: string;
clusterZeroURI: string;
databaseName: string;
collectionName: string;
extensionName: string;
extensionName?: string;
}): Promise<mongodb.Collection<ReposBranchesDocument>> => {
const dbSession = await getPoolDb({
URI,
clusterZeroURI,
databaseName,
appName: extensionName,
appName: extensionName ?? '',
});
return dbSession.collection<ReposBranchesDocument>(collectionName);
};
22 changes: 7 additions & 15 deletions libs/util/src/databaseConnection/fetchSearchData.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
import type * as mongodb from 'mongodb';
import { getSearchDb } from './atlasClusterConnector';

export interface SearchDocument {
url: string;
slug: string;
lastModified: Date;
manifestRevisionId: string;
searchProperty: Array<string>;
includeInGlobalSearch: boolean;
}
import { getSearchDb } from './searchClusterConnector';
import type { SearchDocument } from './types';

export const getDocumentsCollection = async ({
URI,
searchURI,
databaseName,
collectionName,
extensionName,
}: {
URI: string;
searchURI: string;
databaseName: string;
collectionName: string;
extensionName: string;
extensionName?: string;
}): Promise<mongodb.Collection<SearchDocument>> => {
const dbSession = await getSearchDb({
URI,
searchURI,
databaseName,
appName: extensionName,
appName: extensionName ?? '',
});
return dbSession.collection<SearchDocument>(collectionName);
};
27 changes: 27 additions & 0 deletions libs/util/src/databaseConnection/searchClusterConnector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type * as mongodb from 'mongodb';
import { teardown, dbClient } from './clusterConnector';

let searchClusterClient: mongodb.MongoClient;

export const getSearchDb = async ({
searchURI,
databaseName,
appName,
}: {
searchURI: string;
databaseName: string;
appName: string;
}): Promise<mongodb.Db> => {
if (!searchClusterClient) {
console.info('Creating new instance of Cluster Zero client');
searchClusterClient = await dbClient({ uri: searchURI, appName });
}
return searchClusterClient.db(databaseName);
};

export const closeSearchDb = async () => {
if (searchClusterClient) await teardown(searchClusterClient);
else {
console.info('No client connection open to Search Cluster client');
}
};
51 changes: 33 additions & 18 deletions libs/util/src/databaseConnection/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type PoolDbName = 'pool' | 'pool_test';
export type Environments = 'dev' | 'stg' | 'dotcomstg' | 'prd' | 'dotcomprd';

export interface EnvironmentConfig {
dev?: string;
Expand All @@ -14,12 +14,19 @@ export interface DocsetsDocument {
url: EnvironmentConfig;
prefix: EnvironmentConfig;
}
export type CollectionName = 'repos_branches' | 'docsets' | 'documents';

export type CollectionConnectionInfo = {
export type clusterZeroConnectionInfo = {
clusterZeroURI: string;
databaseName: PoolDbName;
collectionName: CollectionName;
databaseName: ClusterZeroDBName;
collectionName: string;
extensionName: string;
};

export type SearchClusterConnectionInfo = {
searchURI: string;
databaseName: SearchDBName;
collectionName: string;
extensionName: string;
};

export interface BranchEntry {
Expand All @@ -42,22 +49,30 @@ export interface ReposBranchesDocument {
internalOnly: boolean;
}

export type Environments = 'dev' | 'stg' | 'dotcomstg' | 'prd' | 'dotcomprd';

export type S3UploadParams = {
bucket: string;
prefix: string;
fileName: string;
manifest: string;
obj: string;
};

export type DbConfig = {
ATLAS_CLUSTER0_URI: string;
ATLAS_SEARCH_URI: string;
AWS_S3_ACCESS_KEY_ID: string;
AWS_S3_SECRET_ACCESS_KEY: string;
DOCSETS_COLLECTION: CollectionName;
DOCUMENTS_COLLECTION: CollectionName;
REPOS_BRANCHES_COLLECTION: CollectionName;
SLACK_SIGNING_SECRET: string;
};
export interface SearchDocument {
url: string;
slug: string;
lastModified: Date;
manifestRevisionId: string;
searchProperty: Array<string>;
includeInGlobalSearch: boolean;
}

export type SearchDBName = 'search' | 'search-test' | 'search-stage';

export type PoolDBName = 'pool' | 'pool_test';

export type SnootyDBName =
| 'test'
| 'snooty_dev'
| 'snooty_dotcomstg'
| 'snooty_dotcomprd';

export type ClusterZeroDBName = PoolDBName | 'docs_metadata' | SnootyDBName;
Loading

0 comments on commit 78ae0c0

Please sign in to comment.