diff --git a/package-lock.json b/package-lock.json index bd8b1e1..d7e5401 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@contentstack/datasync-content-store-mongodb", - "version": "1.0.9", + "version": "1.0.10", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@contentstack/datasync-content-store-mongodb", - "version": "1.0.9", + "version": "1.0.10", "license": "MIT", "dependencies": { "debug": "^4.4.0", diff --git a/package.json b/package.json index 42fd3bb..0e89f31 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@contentstack/datasync-content-store-mongodb", "author": "Contentstack Ecosystem ", - "version": "1.0.9", + "version": "1.0.10", "description": "Contentstack datasync's mongodb library. Helps to store contents in mongodb", "main": "dist/index.js", "dependencies": { diff --git a/src/connection.ts b/src/connection.ts index 4a822b1..80fe477 100644 --- a/src/connection.ts +++ b/src/connection.ts @@ -8,6 +8,7 @@ import Debug from 'debug' import { isEmpty, isPlainObject, merge } from 'lodash' import { Db, MongoClient } from 'mongodb' import { validateMongodbConfig } from './util/validations' +import { MESSAGES } from './util/messages' const debug = Debug('connection') interface IMongo { @@ -42,10 +43,10 @@ export const connect = (config) => { const dbName = mongoConfig.dbName const options = mongoConfig.options - debug('connection url', connectionUri) - debug('db name', dbName) - debug('collection names', mongoConfig.collection) - debug('db options', JSON.stringify(options)) + debug(MESSAGES.CONNECTION.URL, connectionUri) + debug(MESSAGES.CONNECTION.DB_NAME, dbName) + debug(MESSAGES.CONNECTION.COLLECTION_NAMES, mongoConfig.collection) + debug(MESSAGES.CONNECTION.DB_OPTIONS, JSON.stringify(options)) if (mongoConfig.indexes && isPlainObject(mongoConfig.indexes) && !(isEmpty(mongoConfig.indexes))) { indexes = merge(indexes, mongoConfig.indexes) @@ -56,7 +57,7 @@ export const connect = (config) => { return client.connect().then(() => { instance.db = client.db(dbName) instance.client = client - console.info(`Mongodb connection to ${connectionUri} established successfully!`) + console.info(MESSAGES.CONNECTION.SUCCESS(connectionUri)) return resolve(instance) }) diff --git a/src/index.ts b/src/index.ts index b7afff8..9d8883b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,6 +10,7 @@ import { config as internalConfig } from './config' import { connect } from './connection' import { Mongodb } from './mongodb' import { sanitizeConfig } from './util/index' +import { MESSAGES } from './util/messages' import { validateAssetConnectorInstance, @@ -103,7 +104,7 @@ export const start = (connector: IAssetConnector, config?: IConfig) => { return connect(appConfig) .then((mongo) => { mongoClient = new Mongodb(mongo, assetConnectorInstance, appConfig.contentStore, appConfig) - debug('Mongo connector instance created successfully!') + debug(MESSAGES.CONNECTOR.INSTANCE_CREATED) return resolve(mongoClient) }) diff --git a/src/mongodb.ts b/src/mongodb.ts index 2b30ab7..e1aba5e 100644 --- a/src/mongodb.ts +++ b/src/mongodb.ts @@ -13,6 +13,7 @@ import { getCollectionName, getLocalesFromCollections, } from "./util/index"; +import { MESSAGES } from "./util/messages"; import { validateAssetDelete, validateAssetPublish, @@ -93,7 +94,7 @@ export class Mongodb { * @returns {Promise} Returns a promise */ public publishAsset(data) { - debug(`Asset publish called ${JSON.stringify(data)}`); + debug(MESSAGES.ASSET.PUBLISH_INITIATED(data)); return new Promise(async (resolve, reject) => { try { @@ -127,7 +128,7 @@ export class Mongodb { upsert: true, } ); - debug(`Asset publish result ${JSON.stringify(result)}`); + debug(MESSAGES.ASSET.PUBLISH_RESULT(result)); return resolve(data); } catch (error) { @@ -142,7 +143,7 @@ export class Mongodb { * @returns {Promise} Returns a promise */ public updateContentType(contentType) { - debug(`Entry publish called ${JSON.stringify(contentType)}`); + debug(MESSAGES.ENTRY.PUBLISH_INITIATED(contentType)); return new Promise(async (resolve, reject) => { try { @@ -164,11 +165,7 @@ export class Mongodb { upsert: true, } ); - debug( - `Content type update result ${JSON.stringify( - contentTypeUpdateResult - )}` - ); + debug(MESSAGES.CONTENT_TYPE.UPDATE_RESULT(contentTypeUpdateResult)); return resolve(contentType); } catch (error) { @@ -183,7 +180,7 @@ export class Mongodb { * @returns {Promise} Returns a promise */ public publishEntry(entry) { - debug(`Entry publish called ${JSON.stringify(entry)}`); + debug(MESSAGES.ENTRY.PUBLISH_INITIATED(entry)); return new Promise((resolve, reject) => { try { @@ -220,7 +217,7 @@ export class Mongodb { } ) .then((entryPublishResult) => { - debug(`Entry publish result ${JSON.stringify(entryPublishResult)}`); + debug(MESSAGES.ENTRY.PUBLISH_RESULT(entryPublishResult)); return resolve(entry); }) @@ -270,7 +267,7 @@ export class Mongodb { * @returns {Promise} Returns a promise */ private unpublishEntry(entry) { - debug(`Delete entry called ${JSON.stringify(entry)}`); + debug(MESSAGES.ENTRY.DELETE_INITIATED(entry)); return new Promise((resolve, reject) => { try { @@ -284,7 +281,7 @@ export class Mongodb { uid: entry.uid, }) .then((result) => { - debug(`Delete entry result ${JSON.stringify(result)}`); + debug(MESSAGES.ENTRY.DELETE_RESULT(result)); return resolve(entry); }) @@ -301,7 +298,7 @@ export class Mongodb { * @returns {Promise} Returns a promise */ private deleteEntry(entry) { - debug(`Delete entry called ${JSON.stringify(entry)}`); + debug(MESSAGES.ENTRY.DELETE_INITIATED(entry)); return new Promise((resolve, reject) => { try { @@ -314,7 +311,7 @@ export class Mongodb { uid: entry.uid, }) .then((result) => { - debug(`Delete entry result ${JSON.stringify(result)}`); + debug(MESSAGES.ENTRY.DELETE_RESULT(result)); return resolve(entry); }) @@ -331,7 +328,7 @@ export class Mongodb { * @returns {Promise} Returns a promise */ private unpublishAsset(asset) { - debug(`Unpublish asset called ${JSON.stringify(asset)}`); + debug(MESSAGES.ASSET.UNPUBLISH_INITIATED(asset)); return new Promise((resolve, reject) => { try { @@ -348,7 +345,7 @@ export class Mongodb { uid: asset.uid, }) .then((result) => { - debug(`Asset unpublish status: ${JSON.stringify(result)}`); + debug(MESSAGES.ASSET.UNPUBLISH_RESULT(result)); if (!result?.value) { return resolve(asset); } @@ -367,19 +364,13 @@ export class Mongodb { .toArray() .then((assets) => { if (assets.length === 0) { - debug( - `Only published object of ${JSON.stringify( - asset - )} was present` - ); + debug(MESSAGES.ASSET.ONLY_PUBLISHED_PRESENT(asset)); return this.assetStore .unpublish(result.value) .then(() => resolve(asset)); } - debug( - "Asset existed in pubilshed and RTE/Markdown form. Removed published asset object." - ); + debug(MESSAGES.ASSET.EXISTED_IN_MULTIPLE_FORMS); return resolve(asset); }); @@ -397,7 +388,7 @@ export class Mongodb { * @returns {Promise} Returns a promise */ private deleteAsset(asset) { - debug(`Delete asset called ${JSON.stringify(asset)}`); + debug(MESSAGES.ASSET.DELETE_INITIATED(asset)); return new Promise((resolve, reject) => { try { @@ -413,7 +404,7 @@ export class Mongodb { .toArray() .then((result) => { if (result.length === 0) { - debug("Asset did not exist!"); + debug(MESSAGES.ASSET.DOES_NOT_EXIST); return resolve(asset); } @@ -445,7 +436,7 @@ export class Mongodb { * @returns {Promise} Returns a promise */ private deleteContentType(contentType) { - debug(`Delete content type called ${JSON.stringify(contentType)}`); + debug(MESSAGES.CONTENT_TYPE.DELETE_INITIATED(contentType)); return new Promise(async (resolve, reject) => { try { @@ -485,9 +476,7 @@ export class Mongodb { _content_type_uid: uid, }) .then((entriesDeleteResult) => { - debug( - `Delete entries result ${JSON.stringify(entriesDeleteResult)}` - ); + debug(MESSAGES.CONTENT_TYPE.DELETE_ENTRIES_RESULT(entriesDeleteResult)); return this.db .collection(collection.name) @@ -496,11 +485,7 @@ export class Mongodb { uid, }) .then((contentTypeDeleteResult) => { - debug( - `Content type delete result ${JSON.stringify( - contentTypeDeleteResult - )}` - ); + debug(MESSAGES.CONTENT_TYPE.DELETE_RESULT(contentTypeDeleteResult)); return resolve(0); }); diff --git a/src/process.ts b/src/process.ts index f3e26ec..e24f9f5 100644 --- a/src/process.ts +++ b/src/process.ts @@ -10,6 +10,7 @@ */ import { getMongoClient } from './' +import { MESSAGES } from './util/messages' /** * @description Handles process exit. Stops the current application and manages a graceful shutdown @@ -18,7 +19,7 @@ import { getMongoClient } from './' const handleExit = (signal) => { const killDuration = (process.env.KILLDURATION) ? calculateKillDuration() : 15000 // tslint:disable-next-line: no-console - console.info(`Received ${signal}. This will shut down the process in ${killDuration}ms..`) + console.info(MESSAGES.PROCESS.SHUTDOWN(signal, killDuration)) setInterval(abort, killDuration) } @@ -30,7 +31,7 @@ const handleExit = (signal) => { * @param {Object} error - Unhandled error object */ const unhandledErrors = (error) => { - console.error('Unhandled exception caught. Locking down process for 10s to recover..') + console.error(MESSAGES.PROCESS.UNHANDLED_ERROR) console.error(error) } diff --git a/src/util/index.ts b/src/util/index.ts index 8f584a6..27074b7 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -7,6 +7,8 @@ import { getConfig } from '../index' +export { MESSAGES } from './messages' + interface IContentStore { collectionName?: string, collection?: { diff --git a/src/util/messages.ts b/src/util/messages.ts new file mode 100644 index 0000000..02e84a4 --- /dev/null +++ b/src/util/messages.ts @@ -0,0 +1,69 @@ +/*! +* Contentstack Mongodb Content Connector +* Copyright (c) 2019 Contentstack LLC +* MIT Licensed +*/ + +/** + * Centralized messages for logging and error handling + * This file contains all log messages, error messages, and console outputs + * used throughout the application for consistency and maintainability. + */ + +export const MESSAGES = { + // Connection messages + CONNECTION: { + URL: 'Connection URL', + DB_NAME: 'Database name', + COLLECTION_NAMES: 'Collection names', + DB_OPTIONS: 'Database options', + SUCCESS: (connectionUri: string) => `MongoDB connection to ${connectionUri} established successfully.`, + }, + + // MongoDB connector messages + CONNECTOR: { + INSTANCE_CREATED: 'Mongo connector instance created successfully.', + }, + + // Asset operation messages + ASSET: { + PUBLISH_INITIATED: (data: any) => `Asset publish initiated with: ${JSON.stringify(data)}`, + PUBLISH_RESULT: (result: any) => `Asset publish result: ${JSON.stringify(result)}`, + UNPUBLISH_INITIATED: (asset: any) => `Asset unpublish initiated for: ${JSON.stringify(asset)}`, + UNPUBLISH_RESULT: (result: any) => `Asset unpublish result: ${JSON.stringify(result)}`, + DELETE_INITIATED: (asset: any) => `Asset delete initiated for: ${JSON.stringify(asset)}`, + DOES_NOT_EXIST: 'Asset does not exist.', + ONLY_PUBLISHED_PRESENT: (asset: any) => `Only published object of ${JSON.stringify(asset)} was present`, + EXISTED_IN_MULTIPLE_FORMS: 'Asset existed in pubilshed and RTE/Markdown form. Removed published asset object.', + }, + + // Entry operation messages + ENTRY: { + PUBLISH_INITIATED: (entry: any) => `Entry publish initiated with: ${JSON.stringify(entry)}`, + PUBLISH_RESULT: (result: any) => `Entry publish result: ${JSON.stringify(result)}`, + DELETE_INITIATED: (entry: any) => `Entry delete initiated for: ${JSON.stringify(entry)}`, + DELETE_RESULT: (result: any) => `Entry delete result: ${JSON.stringify(result)}`, + }, + + // Content type operation messages + CONTENT_TYPE: { + UPDATE_RESULT: (result: any) => `Content type update result ${JSON.stringify(result)}`, + DELETE_INITIATED: (contentType: any) => `Content type delete initiated for: ${JSON.stringify(contentType)}`, + DELETE_RESULT: (result: any) => `Content type delete result ${JSON.stringify(result)}`, + DELETE_ENTRIES_RESULT: (result: any) => `Delete entries result ${JSON.stringify(result)}`, + }, + + // Process management messages + PROCESS: { + SHUTDOWN: (signal: string, killDuration: number) => `Received ${signal}. Shutting down the process in ${killDuration} ms.`, + UNHANDLED_ERROR: 'An unexpected error occurred. Locking the process for 10 seconds to recover.', + }, + + // Logger messages + LOGGER: { + MESSAGE_MISSING: 'Message missing or could not be resolved.', + }, +}; + +export default MESSAGES; + diff --git a/typings/util/index.d.ts b/typings/util/index.d.ts index 5562c39..a30ac12 100644 --- a/typings/util/index.d.ts +++ b/typings/util/index.d.ts @@ -4,6 +4,7 @@ * MIT Licensed * */ +export { MESSAGES } from './messages'; interface IContentStore { collectionName?: string; collection?: { @@ -31,4 +32,3 @@ export declare const getLocalesFromCollections: (collections: { name: string; locale: string; }[]; -export {}; diff --git a/typings/util/messages.d.ts b/typings/util/messages.d.ts new file mode 100644 index 0000000..45e96c1 --- /dev/null +++ b/typings/util/messages.d.ts @@ -0,0 +1,47 @@ +/*! +* Contentstack Mongodb Content Connector +* Copyright (c) 2019 Contentstack LLC +* MIT Licensed +*/ +export declare const MESSAGES: { + CONNECTION: { + URL: string; + DB_NAME: string; + COLLECTION_NAMES: string; + DB_OPTIONS: string; + SUCCESS: (connectionUri: string) => string; + }; + CONNECTOR: { + INSTANCE_CREATED: string; + }; + ASSET: { + PUBLISH_INITIATED: (data: any) => string; + PUBLISH_RESULT: (result: any) => string; + UNPUBLISH_INITIATED: (asset: any) => string; + UNPUBLISH_RESULT: (result: any) => string; + DELETE_INITIATED: (asset: any) => string; + DOES_NOT_EXIST: string; + ONLY_PUBLISHED_PRESENT: (asset: any) => string; + EXISTED_IN_MULTIPLE_FORMS: string; + }; + ENTRY: { + PUBLISH_INITIATED: (entry: any) => string; + PUBLISH_RESULT: (result: any) => string; + DELETE_INITIATED: (entry: any) => string; + DELETE_RESULT: (result: any) => string; + }; + CONTENT_TYPE: { + UPDATE_RESULT: (result: any) => string; + DELETE_INITIATED: (contentType: any) => string; + DELETE_RESULT: (result: any) => string; + DELETE_ENTRIES_RESULT: (result: any) => string; + }; + PROCESS: { + SHUTDOWN: (signal: string, killDuration: number) => string; + UNHANDLED_ERROR: string; + }; + LOGGER: { + MESSAGE_MISSING: string; + }; +}; +export default MESSAGES;