UNPKG

mongoose-management

Version:
79 lines (78 loc) 2.9 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const abstractColumn_1 = __importDefault(require("./abstractColumn")); const column_1 = __importDefault(require("./column")); const index_1 = __importDefault(require("./index")); const sort_1 = require("../helper/sort"); exports.specialColumns = [ ['createdAt', 'date'], ['updatedAt', 'date'], ['_id', 'objectId'], ]; class CollectionDataset extends abstractColumn_1.default { constructor(collection, parent) { super(parent); const specials = exports.specialColumns.map(([name, type]) => new column_1.default({ name, type }, this, this, true)); this.indexes = collection.indexes.map((i) => new index_1.default(i, this)); this.columns = collection.columns.map((c) => new column_1.default(c, this, this)).concat(specials); this.name = collection.name; this.sortColumns(); } setReference() { this.columns.forEach((column) => column.setReference()); this.indexes.forEach((index) => index.setReference()); } getName() { return this.name; } setName(name) { this.name = name; this.parent.sortCollections(); } getIndexes() { return this.indexes; } getIndex(name) { const indexes = this.indexes.filter((i) => i.getName() === name); return indexes.length === 1 ? indexes[0] : undefined; } addIndex(index) { this.indexes.push(index); this.sortIndexes(); index.setReference(); return index; } removeIndex(index) { this.indexes = this.indexes.filter((i) => i !== index); } sortIndexes() { this.indexes.sort(sort_1.sortByName); } remove() { this.parent.removeCollection(this); } getPopulates(withOwnPopulates = true) { return this.getParent() .getCollections() .reduce((prev, curr) => prev.concat(curr.flatColumns()), []) .filter((column) => { const populate = column.getPopulate(); const collection = populate instanceof column_1.default ? populate.getCollection() : populate; return collection === this; }) .filter((column) => withOwnPopulates || column.getCollection() !== this) .filter((value, index, self) => self.indexOf(value) === index); } getObject() { const specialNames = exports.specialColumns.map((d) => d[0]); return { name: this.name, columns: this.columns.filter((c) => specialNames.indexOf(c.getName()) === -1).map((c) => c.getObject()), indexes: this.indexes.map((i) => i.getObject()), }; } } exports.default = CollectionDataset;