From f04d4332b04f724b06e680f1d8e6047d9a9913fd Mon Sep 17 00:00:00 2001 From: hanoak20 Date: Thu, 18 Jan 2024 23:06:49 +0530 Subject: [PATCH 1/3] chore: updated DB schemas & mongoose connection config. --- src/config/index.ts | 2 ++ src/constants/index.ts | 2 ++ src/database.ts | 4 ++- src/models/authenticationLog.ts | 28 ++++++----------- src/models/migration.ts | 56 +++++++++++++++------------------ 5 files changed, 42 insertions(+), 50 deletions(-) diff --git a/src/config/index.ts b/src/config/index.ts index cf405bcd2..93b1575f7 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -11,6 +11,7 @@ export type ConfigType = { APP_TOKEN_EXP: string; APP_TOKEN_KEY: string; PORT: string; + APP_ENV: string; MONGODB_URI: string; CS_API: { US: string; @@ -23,6 +24,7 @@ export type ConfigType = { export const config: ConfigType = { APP_TOKEN_EXP: "1d", PORT: process.env.PORT!, + APP_ENV: process.env.NODE_ENV!, APP_TOKEN_KEY: process.env.APP_TOKEN_KEY!, MONGODB_URI: process.env.MONGODB_URI!, ...(process.env.NODE_ENV === "production" ? prodConfig : devConfig), diff --git a/src/constants/index.ts b/src/constants/index.ts index 66d27df41..9a936cd2a 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -11,6 +11,7 @@ export type HttpErrorCodes = { SERVER_ERROR: number; }; export type ConstantType = { + CS_REGIONS: Array; AXIOS_TIMEOUT: number; HTTP_CODES: HttpErrorCodes; HTTP_TEXTS: HttpErrorTexts; @@ -35,6 +36,7 @@ export type HttpResponseHeaders = { }; export const constants: ConstantType = { + CS_REGIONS: ["US", "EU", "AZURE_NA", "AZURE_EU"], AXIOS_TIMEOUT: 60 * 1000, HTTP_CODES: { OK: 200, diff --git a/src/database.ts b/src/database.ts index 1c8bc8294..13fb48f56 100644 --- a/src/database.ts +++ b/src/database.ts @@ -8,7 +8,9 @@ import AuditLogModel from "./models/auditLog"; const connectToDatabase = async () => { try { - await mongoose.connect(config.MONGODB_URI); + await mongoose.connect(config.MONGODB_URI, { + ...(config.APP_ENV === "production" ? { autoIndex: false } : {}), + }); logger.info("Connected to MongoDB"); diff --git a/src/models/authenticationLog.ts b/src/models/authenticationLog.ts index 638b56741..59a50b910 100644 --- a/src/models/authenticationLog.ts +++ b/src/models/authenticationLog.ts @@ -1,32 +1,22 @@ // src/models/Authentication.ts import { Schema, model, Document } from "mongoose"; - -// Disabling this error until API's being implemented -// eslint-disable-next-line @typescript-eslint/no-unused-vars -interface Authentication { - user_id: string; - region: string; - authtoken: string; - created_at: string; - modified_at: string; -} +import { constants } from "../constants"; interface AuthenticationDocument extends Document { user_id: string; region: string; authtoken: string; - created_at: string; - modified_at: string; } -const authenticationSchema = new Schema({ - user_id: { type: String, required: true }, - region: { type: String, required: true }, - authtoken: { type: String, required: true }, - created_at: { type: String, required: true }, - modified_at: { type: String, required: true }, -}); +const authenticationSchema = new Schema( + { + user_id: { type: String, required: true }, + region: { type: String, required: true, enum: constants.CS_REGIONS }, + authtoken: { type: String, required: true }, + }, + { timestamps: { createdAt: "created_at", updatedAt: "updated_at" } } +); const AuthenticationModel = model( "Authentication", diff --git a/src/models/migration.ts b/src/models/migration.ts index 09a797488..611fe75c0 100644 --- a/src/models/migration.ts +++ b/src/models/migration.ts @@ -1,4 +1,5 @@ import { Schema, model, Document } from "mongoose"; +import { constants } from "../constants"; interface LegacyCMS { cms: string; @@ -19,7 +20,6 @@ interface Modules { interface Migration { name: string; description: string; - modified_at: string; modules: Modules; } @@ -28,51 +28,47 @@ interface ExecutionLog { } interface MigrationDocument extends Document { - id: string; region: string; org_id: string; owner: string; created_by: string; name: string; description: string; - created_at: string; - modified_at: string; status: boolean; migration: Migration; execution_log: ExecutionLog; } -const migrationSchema = new Schema({ - id: { type: String, required: true }, - region: { type: String, required: true }, - org_id: { type: String, required: true }, - owner: { type: String, required: true }, - created_by: { type: String, required: true }, - name: { type: String, required: true }, - description: { type: String, required: true }, - created_at: { type: String, required: true }, - modified_at: { type: String, required: true }, - status: { type: Boolean, required: true }, - migration: { +const migrationSchema = new Schema( + { + region: { type: String, required: true, enum: constants.CS_REGIONS }, + org_id: { type: String, required: true }, + owner: { type: String, required: true }, + created_by: { type: String, required: true }, name: { type: String, required: true }, description: { type: String, required: true }, - modified_at: { type: String, required: true }, - modules: { - legacy_cms: { - cms: { type: String, required: true }, - file_format: { type: String, required: true }, - import_data: { type: String, required: true }, - }, - destination_cms: { - stack_id: { type: String, required: true }, - org_id: { type: String, required: true }, + status: { type: Boolean, required: true, default: true }, + migration: { + name: { type: String, required: true }, + description: { type: String, required: true }, + modules: { + legacy_cms: { + cms: { type: String, required: true }, + file_format: { type: String, required: true }, + import_data: { type: String, required: true }, + }, + destination_cms: { + stack_id: { type: String, required: true }, + org_id: { type: String, required: true }, + }, }, }, + execution_log: { + log_url: { type: String, required: true }, + }, }, - execution_log: { - log_url: { type: String, required: true }, - }, -}); + { timestamps: { createdAt: "created_at", updatedAt: "updated_at" } } +); const MigrationModel = model("Migration", migrationSchema); From f8256955ceb01a7093c73cceddd3447cfdc8807c Mon Sep 17 00:00:00 2001 From: hanoak20 Date: Thu, 18 Jan 2024 23:56:57 +0530 Subject: [PATCH 2/3] chore: updated the names of models & collections to appropriate names. --- src/database.ts | 6 +++--- src/models/{authenticationLog.ts => authentication.ts} | 0 src/models/{migration.ts => project.ts} | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) rename src/models/{authenticationLog.ts => authentication.ts} (100%) rename src/models/{migration.ts => project.ts} (88%) diff --git a/src/database.ts b/src/database.ts index 13fb48f56..a30d79976 100644 --- a/src/database.ts +++ b/src/database.ts @@ -2,8 +2,8 @@ import mongoose from "mongoose"; import { config } from "./config"; import logger from "./utils/logger"; -import MigrationModel from "./models/migration"; -import AuthenticationModel from "./models/authenticationLog"; +import ProjectModel from "./models/project"; +import AuthenticationModel from "./models/authentication"; import AuditLogModel from "./models/auditLog"; const connectToDatabase = async () => { @@ -15,7 +15,7 @@ const connectToDatabase = async () => { logger.info("Connected to MongoDB"); // Create the collection's if it doesn't exist - await MigrationModel.init(); + await ProjectModel.init(); await AuthenticationModel.init(); await AuditLogModel.init(); } catch (error) { diff --git a/src/models/authenticationLog.ts b/src/models/authentication.ts similarity index 100% rename from src/models/authenticationLog.ts rename to src/models/authentication.ts diff --git a/src/models/migration.ts b/src/models/project.ts similarity index 88% rename from src/models/migration.ts rename to src/models/project.ts index 611fe75c0..46705205a 100644 --- a/src/models/migration.ts +++ b/src/models/project.ts @@ -27,7 +27,7 @@ interface ExecutionLog { log_url: string; } -interface MigrationDocument extends Document { +interface ProjectDocument extends Document { region: string; org_id: string; owner: string; @@ -39,7 +39,7 @@ interface MigrationDocument extends Document { execution_log: ExecutionLog; } -const migrationSchema = new Schema( +const projectSchema = new Schema( { region: { type: String, required: true, enum: constants.CS_REGIONS }, org_id: { type: String, required: true }, @@ -70,6 +70,6 @@ const migrationSchema = new Schema( { timestamps: { createdAt: "created_at", updatedAt: "updated_at" } } ); -const MigrationModel = model("Migration", migrationSchema); +const ProjectModel = model("Project", projectSchema); -export default MigrationModel; +export default ProjectModel; From b3ca53aa51726de33e6a53b4561e9f95ea6c0758 Mon Sep 17 00:00:00 2001 From: hanoak20 Date: Fri, 19 Jan 2024 12:26:18 +0530 Subject: [PATCH 3/3] chore: updated the project's schema. --- src/models/project.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/models/project.ts b/src/models/project.ts index 46705205a..60738430e 100644 --- a/src/models/project.ts +++ b/src/models/project.ts @@ -47,24 +47,24 @@ const projectSchema = new Schema( created_by: { type: String, required: true }, name: { type: String, required: true }, description: { type: String, required: true }, - status: { type: Boolean, required: true, default: true }, + status: { type: Boolean, default: true }, migration: { - name: { type: String, required: true }, - description: { type: String, required: true }, + name: { type: String }, + description: { type: String }, modules: { legacy_cms: { - cms: { type: String, required: true }, - file_format: { type: String, required: true }, - import_data: { type: String, required: true }, + cms: { type: String }, + file_format: { type: String }, + import_data: { type: String }, }, destination_cms: { - stack_id: { type: String, required: true }, - org_id: { type: String, required: true }, + stack_id: { type: String }, + org_id: { type: String }, }, }, }, execution_log: { - log_url: { type: String, required: true }, + log_url: { type: String }, }, }, { timestamps: { createdAt: "created_at", updatedAt: "updated_at" } }