mongoose-management
Version:
Mongoose schemas management tool
68 lines (67 loc) • 2.09 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const abstract_1 = __importDefault(require("./abstract"));
class IndexDataset extends abstract_1.default {
constructor(index, collection) {
super(collection);
this.index = index;
this.columns = [];
this.name = index.name;
this.properties = index.properties;
this.readonly = index.readonly === true;
}
setReference() {
this.columns = Object.entries(this.index.columns)
.map(([name, value]) => [this.getCollection().getColumn(name, true), value])
.filter(([column]) => column !== undefined);
}
getName() {
return this.name;
}
setName(name) {
this.name = name;
this.getCollection().sortIndexes();
}
getColumns() {
return this.columns;
}
getColumnsNormalize() {
return this.columns.reduce((prev, [column, value]) => (Object.assign({}, prev, { [column.getFullname(false, false)]: value })), {});
}
setColumns(columns) {
this.columns = columns;
}
hasColumn(name) {
return this.getColumns().filter(([c]) => c.getFullname(false, false) === name).length === 1;
}
getProperty(key) {
return this.properties[key];
}
setProperty(key, value) {
this.properties[key] = value;
}
isReadonly() {
return this.readonly;
}
remove() {
this.getParent().removeIndex(this);
}
getCollection() {
return this.getParent();
}
getObject() {
return {
name: this.name,
columns: this.getColumnsNormalize(),
properties: {
unique: this.properties.unique ? true : undefined,
sparse: this.properties.sparse ? true : undefined,
},
readonly: this.readonly ? true : undefined,
};
}
}
exports.default = IndexDataset;