mongoose-management
Version:
Mongoose schemas management tool
80 lines (79 loc) • 3.77 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"));
exports.call = (prompts, collection, answersMain, answersSubType, column) => __awaiter(this, void 0, void 0, function* () {
if (answersMain.type !== 'objectId' &&
(answersMain.type !== 'arrayType' || answersSubType[answersSubType.length - 1] !== 'objectId')) {
return {};
}
const questions = exports.getQuestions(collection, column);
const answersPopulate = yield prompts.call(questions);
return answersPopulate;
});
exports.getQuestions = (collection, column) => {
const populate = column && column.getPopulate();
const collections = collection.getParent().getCollections();
const selectedCollection = populate
? collections.indexOf(populate instanceof column_1.default ? populate.getCollection() : populate)
: -1;
const choices = [{ name: '- Without reference -', short: chalk_1.default.red('Without reference'), value: undefined }];
return [
{
type: 'list',
name: 'collection',
message: 'Choose a reference collection',
choices: [...choices, ...collections.map((c) => ({ name: c.getName(), short: c.getName(), value: c }))],
default: populate ? selectedCollection + choices.length : undefined,
},
{
type: 'list',
name: 'column',
message: 'Choose a column with nested schemas or the collection',
choices: exports.choicesColumn,
default: exports.defaultColumn(populate),
when: exports.whenColumn,
},
];
};
exports.evaluation = (answers) => {
return (column) => {
if (answers.collection && answers.column) {
column.setPopulate(answers.column);
}
else {
column.setPopulate(answers.collection);
}
return column;
};
};
exports.getCollectionWithNestedSchemas = (collection) => collection.flatColumns().filter((c) => ['object', 'array'].indexOf(c.get('type')) >= 0);
exports.whenColumn = ({ collection }) => collection !== undefined && exports.getCollectionWithNestedSchemas(collection).length > 0;
exports.choicesColumn = ({ collection }) => {
if (!collection) {
throw new Error('No collection has been selected');
}
const columnChoices = exports.getCollectionWithNestedSchemas(collection).map((c) => ({
name: c.getFullname(false, false),
short: c.getFullname(false, false),
value: c,
}));
return [{ name: '_id', short: `${collection.getName()}._id`, value: undefined }, ...columnChoices];
};
exports.defaultColumn = (prevPopulate) => ({ collection }) => {
if (prevPopulate instanceof column_1.default && prevPopulate.getCollection() === collection) {
return exports.getCollectionWithNestedSchemas(collection).indexOf(prevPopulate) + 1;
}
return -1;
};