UNPKG

@mas-soft/mas-core-server

Version:

main application

146 lines 5.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const repository_1 = require("../repository"); const debugFac = require('debug'); const debug = debugFac('mas:entity:repository'); class MasEntityRepository { constructor(entityClass, dataSource) { this.entityClass = entityClass; this.dataSource = dataSource; this.connector = dataSource.connector; } toModels(data) { return data.then(items => items.map(i => new this.entityClass(i))); } toModel(data) { return data.then(d => new this.entityClass(d)); } create(entity, options) { return this.toModel(this.connector.create(this.entityClass, entity, options)); } createAll(entities, options) { return this.toModels(this.connector.createAll(this.entityClass, entities, options)); } async createOrUpdate(entity, options) { const id = this.entityClass.getIdOf(entity); let count = 0; if (id) { count = (await this.count(id, options)).count; } if (count > 1) { debug("conflict:more than on eentity have the same key"); debug(id); throw new Error("conflict:more than on eentity have the same key"); } if (count == 0) { return await this.create(entity, options); } else { return await this.save(entity, options); } } async save(entity, options) { return this.toModel(this.connector.save(this.entityClass, entity, options)); } find(filter, options) { return this.toModels(this.connector.find(this.entityClass, filter, options)); } async findById(id, filter, options) { if (typeof this.connector.findById === 'function') { return this.toModel(this.connector.findById(this.entityClass, id, options)); } const where = this.entityClass.buildWhereForId(id); const entities = await this.toModels(this.connector.find(this.entityClass, { where: where }, options)); if (!entities.length) { throw new repository_1.EntityNotFoundError(this.entityClass, id); } return entities[0]; } update(entity, options) { return this.updateById(this.entityClass.getIdOf(entity), entity, options); } delete(entity, options) { return this.deleteById(this.entityClass.getIdOf(entity), options); } updateAll(data, where, options) { return this.connector.updateAll(this.entityClass, data, where, options); } async updateById(id, data, options) { let success; if (typeof this.connector.updateById === 'function') { success = await this.connector.updateById(this.entityClass, id, data, options); } else { const where = this.entityClass.buildWhereForId(id); const result = await this.updateAll(data, where, options); success = result.count > 0; } if (!success) { throw new repository_1.EntityNotFoundError(this.entityClass, id); } } async replaceById(id, data, options) { let success; if (typeof this.connector.replaceById === 'function') { success = await this.connector.replaceById(this.entityClass, id, data, options); } else { const inst = data; const where = this.entityClass.buildWhereForId(id); const result = await this.updateAll(data, where, options); success = result.count > 0; } if (!success) { throw new repository_1.EntityNotFoundError(this.entityClass, id); } } deleteAll(where, options) { return this.connector.deleteAll(this.entityClass, where, options); } async deleteById(id, options) { if (id == null) { throw new Error("Invalied Id can not delete"); } let success; if (typeof this.connector.deleteById === 'function') { success = await this.connector.deleteById(this.entityClass, id, options); } else { const where = this.entityClass.buildWhereForId(id); if (Object.keys(where).length == 0) { throw new Error("Invalied Id can not delete"); } const result = await this.deleteAll(where, options); success = result.count > 0; } if (!success) { throw new repository_1.EntityNotFoundError(this.entityClass, id); } } count(where, options) { return this.connector.count(this.entityClass, where, options); } exists(id, options) { if (typeof this.connector.exists === 'function') { return this.connector.exists(this.entityClass, id, options); } else { const where = this.entityClass.buildWhereForId(id); return this.count(where, options).then(result => result.count > 0); } } execute(command, parameters, options) { if (typeof this.connector.execute !== 'function') { throw new Error('Not implemented'); } return this.connector.execute(command, parameters, options); } async beginTransaction() { return await this.dataSource.connector.beginTransaction(); } async findOne(filter, options) { return await this.dataSource.connector.findOne(this.entityClass, filter || {}, options); } } exports.MasEntityRepository = MasEntityRepository; //# sourceMappingURL=entity-repository.js.map