UNPKG

artmapper

Version:

Spring Boot clone for Node.js with TypeScript/JavaScript - JPA-like ORM, Lombok decorators, dependency injection, and MySQL support

65 lines 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BaseRepository = void 0; require("reflect-metadata"); const EntityManager_1 = require("./EntityManager"); const entity_1 = require("../decorators/entity"); /** * Base repository implementation */ class BaseRepository { constructor(pool) { this.pool = pool; this.entityManager = new EntityManager_1.EntityManager(pool); } async save(entity, fieldsToUpdate) { return this.entityManager.persist(entity, fieldsToUpdate); } async saveAll(entities) { const saved = []; for (const entity of entities) { saved.push(await this.save(entity)); } return saved; } async findById(id) { return this.entityManager.find(this.getEntityClass(), id); } async findAll() { return this.entityManager.findAll(this.getEntityClass()); } async existsById(id) { const entity = await this.findById(id); return entity !== null; } async count() { const entityClass = this.getEntityClass(); const tableMetadata = Reflect.getMetadata(entity_1.TABLE_METADATA_KEY, entityClass); const tableName = tableMetadata?.name || entityClass.name.toLowerCase(); const result = await this.entityManager.queryOne(`SELECT COUNT(*) as count FROM ${tableName}`); return result?.count || 0; } async deleteById(id) { const entity = await this.findById(id); if (entity) { await this.entityManager.remove(entity); } } async delete(entity) { await this.entityManager.remove(entity); } async deleteAll() { const entities = await this.findAll(); for (const entity of entities) { await this.delete(entity); } } /** * Create a query builder for custom queries */ createQueryBuilder() { return this.entityManager.createQueryBuilder(this.getEntityClass()); } } exports.BaseRepository = BaseRepository; //# sourceMappingURL=Repository.js.map