Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/datasync-content-store-mongodb",
"author": "Contentstack Ecosystem <[email protected]>",
"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": {
Expand Down
11 changes: 6 additions & 5 deletions src/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
})
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
})
Expand Down
55 changes: 20 additions & 35 deletions src/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getCollectionName,
getLocalesFromCollections,
} from "./util/index";
import { MESSAGES } from "./util/messages";
import {
validateAssetDelete,
validateAssetPublish,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
})
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
})
Expand All @@ -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 {
Expand All @@ -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);
})
Expand All @@ -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 {
Expand All @@ -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);
}
Expand All @@ -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);
});
Expand All @@ -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 {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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);
});
Expand Down
5 changes: 3 additions & 2 deletions src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down
2 changes: 2 additions & 0 deletions src/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import { getConfig } from '../index'

export { MESSAGES } from './messages'

interface IContentStore {
collectionName?: string,
collection?: {
Expand Down
69 changes: 69 additions & 0 deletions src/util/messages.ts
Original file line number Diff line number Diff line change
@@ -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;

2 changes: 1 addition & 1 deletion typings/util/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* MIT Licensed
*
*/
export { MESSAGES } from './messages';
interface IContentStore {
collectionName?: string;
collection?: {
Expand Down Expand Up @@ -31,4 +32,3 @@ export declare const getLocalesFromCollections: (collections: {
name: string;
locale: string;
}[];
export {};
Loading
Loading