@nodeboot/starter-persistence
Version:
Nodeboot starter package for persistence. Supports data access layer auto-configuration providing features like database initialization, consistency check, entity mapping, repository pattern, transactions, paging, migrations, persistence listeners, persis
65 lines • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DataRepository = void 0;
const PersistenceContext_1 = require("../PersistenceContext");
const types_1 = require("../types");
/**
* Helper function to determine the repository type based on prototype chain.
* It checks if the prototype is an instance of Repository, MongoRepository, or TreeRepository.
*
* @param {any} prototype - The prototype object of the repository class.
* @returns {RepositoryType | undefined} The repository type if found, otherwise undefined.
*
* @author Manuel Santos <https://github.com/manusant>
*/
function getRepositoryType(prototype) {
while (prototype) {
if (prototype.constructor.name === "Repository") {
return types_1.RepositoryType.SQL;
}
if (prototype.constructor.name === "MongoRepository") {
return types_1.RepositoryType.MONGO;
}
if (prototype.constructor.name === "TreeRepository") {
return types_1.RepositoryType.TREE;
}
prototype = Object.getPrototypeOf(prototype);
}
return undefined;
}
/**
* Class decorator for marking a class as a data repository.
*
* Registers the repository with the PersistenceContext and associates it with an entity.
* Validates that the repository extends one of TypeORM's Repository classes (Repository, MongoRepository, TreeRepository).
*
* @param {Function} entity - The entity class associated with the repository.
* @returns {ClassDecorator} The class decorator function.
*
* @throws {Error} If the target class does not extend a supported repository class.
*
* @example
* ```ts
* @DataRepository(User)
* export class UserRepository extends Repository<User> { }
* ```
*
* @author Manuel Santos <https://github.com/manusant>
*/
const DataRepository = (entity) => {
return (target) => {
const repoType = getRepositoryType(target.prototype);
if (!repoType) {
throw new Error(`Invalid repository type for repository ${target.prototype.name}. Please extend from Repository, MongoRepository or TreeRepository`);
}
Reflect.defineMetadata("custom:repotype", repoType, target.prototype);
Reflect.defineMetadata("__isRepository", true, target);
PersistenceContext_1.PersistenceContext.get().repositories.push({
target,
entity,
type: repoType,
});
};
};
exports.DataRepository = DataRepository;
//# sourceMappingURL=DataRepository.js.map