UNPKG

lexica-dialog-repository

Version:
123 lines 3.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Page_1 = require("./Page"); const Shared_1 = require("./Shared"); class CRUDRepository { constructor(schemaModel) { this.indexedCallbacks = []; this.model = schemaModel; this.model.on('index', () => { this.indexed = true; this.indexedCallbacks.forEach(callback => callback(this.indexed)); this.indexedCallbacks = []; }); } isIndexed() { if (this.indexed === undefined) { return new Promise((resolve) => { this.indexedCallbacks.push((resolve)); }); } return Promise.resolve(this.indexed); } async create(data) { return this.save(data); } async save(entity) { if (entity.hasOwnProperty('save')) { return entity.save(); } return new this.model(entity).save(); } async count(query) { return this.model.count(query); } async countAll() { return this.count({}); } async find(query, sorts = []) { return this.createFind(query, sorts); } async findPage(pageable, query) { const { limit, skip, sort } = this.toPagination(pageable); const totalElements = await this.count(query); const elements = await this .createFind(query, pageable.sorts) .limit(limit) .skip(skip); return new Page_1.PageResult(elements, totalElements, pageable); } async findOne(query) { return this.model.findOne(query); } async findAll(sorts) { return this.find({}, sorts); } async findAllPage(pageable) { return this.findPage(pageable, {}); } async findById(id) { return this.model.findById(id); } async findByIds(ids, sorts, idFieldName = '_id') { return this.model.find({ [idFieldName]: { $in: ids, }, }, sorts); } async aggregate(...aggregations) { return this.createAggregate(...aggregations); } async aggregatePage(pageable, ...aggregations) { const { limit, skip, sort } = this.toPagination(pageable); const countAggregate = await this.createAggregate(...aggregations).append({ $count: 'count', }); const count = countAggregate.length === 0 ? 0 : countAggregate[0].count; let elements = []; if (count > 0) { const aggregateQuery = this.createAggregate(...aggregations); if (sort !== '') { aggregateQuery.sort(sort); } elements = await aggregateQuery.skip(skip).limit(limit); } return new Page_1.PageResult(elements, count, pageable); } async remove(entity) { return entity.remove(); } async removeAll() { return this.model.remove({}); } toMongooseSort(sorts) { return sorts .map(s => `${s.direction === Shared_1.SortDirection.DESC ? '-' : ''}${s.name}`) .join(' ') .trim(); } toPagination(pageable) { const limit = pageable.pageSize; const skip = pageable.pageSize * pageable.pageNumber; const sort = this.toMongooseSort(pageable.sorts); return { limit, skip, sort, }; } createFind(query, sorts = []) { const find = this.model.find(query); const sort = this.toMongooseSort(sorts); if (sort === '') { return find; } return find.sort(sort); } createAggregate(...aggregations) { return this.model.aggregate(...aggregations).allowDiskUse(true); } } exports.default = CRUDRepository; //# sourceMappingURL=CRUDRepository.js.map