mongoose-management
Version:
Mongoose schemas management tool
98 lines (97 loc) • 4.22 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
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); }
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 });
const chalk_1 = __importDefault(require("chalk"));
const column_1 = __importDefault(require("../dataset/column"));
const index_1 = __importDefault(require("../dataset/index"));
const errors_1 = require("../errors");
const collection_1 = __importDefault(require("../menu/collection"));
const collection_2 = __importDefault(require("../prompts/collection"));
const column_2 = __importDefault(require("../prompts/column"));
const index_2 = __importDefault(require("../prompts/index"));
const abstract_1 = __importDefault(require("./abstract"));
const column_3 = __importDefault(require("./column"));
const index_3 = __importDefault(require("./index"));
class CollectionLevel extends abstract_1.default {
constructor(dataset, options) {
super(dataset, new collection_1.default(options.prompts), options);
this.promptEdit = collection_2.default;
this.promptCreate = () => {
throw new Error('Creating action is not done in the abstract class');
};
}
create(action) {
return __awaiter(this, void 0, void 0, function* () {
let dataset;
switch (action) {
case 'createIndex':
yield index_2.default(this.prompts, this.dataset);
break;
case 'createColumn':
dataset = yield column_2.default(this.prompts, this.dataset);
if (dataset.get('type') !== 'array' && dataset.get('type') !== 'object') {
dataset = undefined;
}
break;
default:
throw new Error('Unknown action');
}
return dataset;
});
}
remove(dataset) {
const _super = Object.create(null, {
remove: { get: () => super.remove }
});
return __awaiter(this, void 0, void 0, function* () {
const populates = dataset
.getPopulates(false)
.map((c) => `- ${chalk_1.default.bold(c.getCollection().getName())}.${c.getFullname(false, false)}`);
if (populates.length > 0) {
throw new Error([
'This collection is referenced by other column(s):',
'',
...populates,
'',
'The references must first be deleted',
].join('\n'));
}
const result = yield _super.remove.call(this, dataset);
return result;
});
}
show(dataset) {
return __awaiter(this, void 0, void 0, function* () {
try {
if (dataset instanceof column_1.default) {
const level = new column_3.default(dataset, this.options);
yield level.exec();
return;
}
if (dataset instanceof index_1.default) {
const level = new index_3.default(dataset, this.options);
yield level.exec();
return;
}
}
catch (err) {
if (err instanceof errors_1.BackToCollectionError) {
return;
}
throw err;
}
throw new Error('Unknown dataset instance');
});
}
}
exports.default = CollectionLevel;