Skip to content
Closed
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
1 change: 1 addition & 0 deletions dist/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* MIT Licensed
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.config = void 0;
exports.config = {
contentStore: {
collection: {
Expand Down
12 changes: 7 additions & 5 deletions dist/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,34 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.connect = void 0;
const debug_1 = __importDefault(require("debug"));
const lodash_1 = require("lodash");
const mongodb_1 = require("mongodb");
const validations_1 = require("./util/validations");
const debug = debug_1.default('connection');
const debug = (0, debug_1.default)('connection');
let indexes = {
_content_type_uid: 1,
uid: 1,
};
const instance = {};
exports.connect = (config) => {
const connect = (config) => {
return new Promise((resolve, reject) => {
try {
if (Object.keys(instance).length !== 0) {
return resolve(instance);
}
const mongoConfig = config.contentStore;
validations_1.validateMongodbConfig(mongoConfig);
(0, validations_1.validateMongodbConfig)(mongoConfig);
const connectionUri = mongoConfig.url || mongoConfig.uri;
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));
if (mongoConfig.indexes && lodash_1.isPlainObject(mongoConfig.indexes) && !(lodash_1.isEmpty(mongoConfig.indexes))) {
indexes = lodash_1.merge(indexes, mongoConfig.indexes);
if (mongoConfig.indexes && (0, lodash_1.isPlainObject)(mongoConfig.indexes) && !((0, lodash_1.isEmpty)(mongoConfig.indexes))) {
indexes = (0, lodash_1.merge)(indexes, mongoConfig.indexes);
}
const client = new mongodb_1.MongoClient(connectionUri, options);
return client.connect().then(() => {
Expand All @@ -50,3 +51,4 @@ exports.connect = (config) => {
}
});
};
exports.connect = connect;
28 changes: 17 additions & 11 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,43 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.start = exports.getMongoClient = exports.getConfig = exports.setConfig = exports.setAssetConnector = void 0;
const debug_1 = __importDefault(require("debug"));
const lodash_1 = require("lodash");
const config_1 = require("./config");
const connection_1 = require("./connection");
const mongodb_1 = require("./mongodb");
const index_1 = require("./util/index");
const validations_1 = require("./util/validations");
const debug = debug_1.default('registration');
const debug = (0, debug_1.default)('registration');
let appConfig = {};
let assetConnectorInstance;
let mongoClient;
exports.setAssetConnector = (instance) => {
const setAssetConnector = (instance) => {
assetConnectorInstance = instance;
};
exports.setConfig = (config) => {
exports.setAssetConnector = setAssetConnector;
const setConfig = (config) => {
appConfig = config;
};
exports.getConfig = () => {
exports.setConfig = setConfig;
const getConfig = () => {
return appConfig;
};
exports.getMongoClient = () => {
exports.getConfig = getConfig;
const getMongoClient = () => {
return mongoClient;
};
exports.start = (connector, config) => {
exports.getMongoClient = getMongoClient;
const start = (connector, config) => {
return new Promise((resolve, reject) => {
try {
appConfig = lodash_1.merge(config_1.config, appConfig, config);
validations_1.validateConfig(appConfig);
appConfig = index_1.sanitizeConfig(appConfig);
appConfig = (0, lodash_1.merge)(config_1.config, appConfig, config);
(0, validations_1.validateConfig)(appConfig);
appConfig = (0, index_1.sanitizeConfig)(appConfig);
assetConnectorInstance = connector || assetConnectorInstance;
validations_1.validateAssetConnectorInstance(assetConnectorInstance);
return connection_1.connect(appConfig)
(0, validations_1.validateAssetConnectorInstance)(assetConnectorInstance);
return (0, connection_1.connect)(appConfig)
.then((mongo) => {
mongoClient = new mongodb_1.Mongodb(mongo, assetConnectorInstance, appConfig.contentStore);
debug('Mongo connector instance created successfully!');
Expand All @@ -52,3 +57,4 @@ exports.start = (connector, config) => {
}
});
};
exports.start = start;
81 changes: 39 additions & 42 deletions dist/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
* MIT Licensed
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Mongodb = void 0;
const debug_1 = __importDefault(require("debug"));
const lodash_1 = require("lodash");
const index_1 = require("./util/index");
const validations_1 = require("./util/validations");
const debug = debug_1.default('mongodb-core');
const debug = (0, debug_1.default)('mongodb-core');
let mongo = null;
class Mongodb {
constructor(mongodb, assetStore, config) {
Expand Down Expand Up @@ -49,9 +51,9 @@ class Mongodb {
debug(`Asset publish called ${JSON.stringify(data)}`);
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
try {
let assetJSON = lodash_1.cloneDeep(data);
validations_1.validateAssetPublish(assetJSON);
assetJSON = index_1.filterAssetKeys(assetJSON);
let assetJSON = (0, lodash_1.cloneDeep)(data);
(0, validations_1.validateAssetPublish)(assetJSON);
assetJSON = (0, index_1.filterAssetKeys)(assetJSON);
if (assetJSON.hasOwnProperty('_version')) {
yield this.unpublish(data);
}
Expand All @@ -66,7 +68,7 @@ class Mongodb {
else {
query._version = asset._version;
}
const result = this.db.collection(index_1.getCollectionName(asset))
const result = this.db.collection((0, index_1.getCollectionName)(asset))
.updateOne(query, {
$set: assetJSON,
}, {
Expand All @@ -84,11 +86,11 @@ class Mongodb {
debug(`Entry publish called ${JSON.stringify(contentType)}`);
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
try {
let contentTypeJSON = lodash_1.cloneDeep(contentType);
validations_1.validateContentTypeUpdate(contentTypeJSON);
contentTypeJSON = index_1.filterContentTypeKeys(contentTypeJSON);
let contentTypeJSON = (0, lodash_1.cloneDeep)(contentType);
(0, validations_1.validateContentTypeUpdate)(contentTypeJSON);
contentTypeJSON = (0, index_1.filterContentTypeKeys)(contentTypeJSON);
const contentTypeUpdateResult = yield this.db
.collection(index_1.getCollectionName(contentTypeJSON))
.collection((0, index_1.getCollectionName)(contentTypeJSON))
.updateOne({
_content_type_uid: contentTypeJSON._content_type_uid,
uid: contentTypeJSON.uid,
Expand All @@ -109,10 +111,10 @@ class Mongodb {
debug(`Entry publish called ${JSON.stringify(entry)}`);
return new Promise((resolve, reject) => {
try {
let entryJSON = lodash_1.cloneDeep(entry);
validations_1.validateEntryPublish(entryJSON);
entryJSON = index_1.filterEntryKeys(entryJSON);
return this.db.collection(index_1.getCollectionName(entryJSON))
let entryJSON = (0, lodash_1.cloneDeep)(entry);
(0, validations_1.validateEntryPublish)(entryJSON);
entryJSON = (0, index_1.filterEntryKeys)(entryJSON);
return this.db.collection((0, index_1.getCollectionName)(entryJSON))
.updateOne({
_content_type_uid: entryJSON._content_type_uid,
locale: entryJSON.locale,
Expand Down Expand Up @@ -146,32 +148,27 @@ class Mongodb {
}
delete(data) {
return new Promise((resolve, reject) => {
try {
if (data._content_type_uid === '_assets') {
return this.deleteAsset(data)
.then(resolve)
.catch(reject);
}
else if (data._content_type_uid === '_content_types') {
return this.deleteContentType(data)
.then(resolve)
.catch(reject);
}
return this.deleteEntry(data)
if (data._content_type_uid === '_assets') {
return this.deleteAsset(data)
.then(resolve)
.catch(reject);
}
catch (error) {
return reject(error);
else if (data._content_type_uid === '_content_types') {
return this.deleteContentType(data)
.then(resolve)
.catch(reject);
}
return this.deleteEntry(data)
.then(resolve)
.catch(reject);
});
}
unpublishEntry(entry) {
debug(`Delete entry called ${JSON.stringify(entry)}`);
return new Promise((resolve, reject) => {
try {
validations_1.validateEntryRemove(entry);
return this.db.collection(index_1.getCollectionName(entry))
(0, validations_1.validateEntryRemove)(entry);
return this.db.collection((0, index_1.getCollectionName)(entry))
.deleteOne({
_content_type_uid: entry._content_type_uid,
locale: entry.locale,
Expand All @@ -192,8 +189,8 @@ class Mongodb {
debug(`Delete entry called ${JSON.stringify(entry)}`);
return new Promise((resolve, reject) => {
try {
validations_1.validateEntryRemove(entry);
return this.db.collection(index_1.getCollectionName(entry))
(0, validations_1.validateEntryRemove)(entry);
return this.db.collection((0, index_1.getCollectionName)(entry))
.deleteMany({
_content_type_uid: entry._content_type_uid,
uid: entry.uid,
Expand All @@ -213,8 +210,8 @@ class Mongodb {
debug(`Unpublish asset called ${JSON.stringify(asset)}`);
return new Promise((resolve, reject) => {
try {
validations_1.validateAssetUnpublish(asset);
return this.db.collection(index_1.getCollectionName(asset))
(0, validations_1.validateAssetUnpublish)(asset);
return this.db.collection((0, index_1.getCollectionName)(asset))
.findOneAndDelete({
_content_type_uid: asset._content_type_uid,
_version: {
Expand All @@ -228,7 +225,7 @@ class Mongodb {
if (result.value === null) {
return resolve(asset);
}
return this.db.collection(index_1.getCollectionName(asset))
return this.db.collection((0, index_1.getCollectionName)(asset))
.find({
_content_type_uid: asset._content_type_uid,
download_id: {
Expand Down Expand Up @@ -260,8 +257,8 @@ class Mongodb {
debug(`Delete asset called ${JSON.stringify(asset)}`);
return new Promise((resolve, reject) => {
try {
validations_1.validateAssetDelete(asset);
return this.db.collection(index_1.getCollectionName(asset))
(0, validations_1.validateAssetDelete)(asset);
return this.db.collection((0, index_1.getCollectionName)(asset))
.find({
_content_type_uid: '_assets',
locale: asset.locale,
Expand All @@ -273,7 +270,7 @@ class Mongodb {
debug('Asset did not exist!');
return resolve(asset);
}
return this.db.collection(index_1.getCollectionName(asset))
return this.db.collection((0, index_1.getCollectionName)(asset))
.deleteMany({
_content_type_uid: '_assets',
locale: asset.locale,
Expand All @@ -298,14 +295,14 @@ class Mongodb {
debug(`Delete content type called ${JSON.stringify(contentType)}`);
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
try {
validations_1.validateContentTypeDelete(contentType);
(0, validations_1.validateContentTypeDelete)(contentType);
const collectionsResult = yield this.db
.listCollections({}, { nameOnly: true })
.toArray();
if (collectionsResult.length === 0) {
return resolve(contentType);
}
const collections = index_1.getLocalesFromCollections(collectionsResult);
const collections = (0, index_1.getLocalesFromCollections)(collectionsResult);
const promisifiedBucket = [];
collections.forEach((collection) => {
promisifiedBucket.push(this.deleteCT(contentType.uid, collection));
Expand All @@ -321,7 +318,7 @@ class Mongodb {
deleteCT(uid, collection) {
return new Promise((resolve, reject) => {
try {
return this.db.collection(index_1.getCollectionName({ _content_type_uid: uid, locale: collection.locale }))
return this.db.collection((0, index_1.getCollectionName)({ _content_type_uid: uid, locale: collection.locale }))
.deleteMany({
_content_type_uid: uid,
})
Expand All @@ -334,7 +331,7 @@ class Mongodb {
})
.then((contentTypeDeleteResult) => {
debug(`Content type delete result ${JSON.stringify(contentTypeDeleteResult)}`);
return resolve();
return resolve('');
});
})
.catch(reject);
Expand Down
2 changes: 1 addition & 1 deletion dist/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const calculateKillDuration = () => {
return killDuration;
};
const abort = () => {
const mongoClient = _1.getMongoClient();
const mongoClient = (0, _1.getMongoClient)();
mongoClient.client.close();
process.abort();
};
Expand Down
Loading