mirror of
https://github.com/hexastack/hexabot
synced 2025-03-12 07:03:07 +00:00
fix: base-repository.ts methods returning types
This commit is contained in:
parent
da783c0ef8
commit
d9a87e99bf
@ -22,6 +22,7 @@ import {
|
|||||||
SortOrder,
|
SortOrder,
|
||||||
UpdateQuery,
|
UpdateQuery,
|
||||||
UpdateWithAggregationPipeline,
|
UpdateWithAggregationPipeline,
|
||||||
|
UpdateWriteOpResult,
|
||||||
} from 'mongoose';
|
} from 'mongoose';
|
||||||
|
|
||||||
import { TFilterQuery } from '@/utils/types/filter.types';
|
import { TFilterQuery } from '@/utils/types/filter.types';
|
||||||
@ -70,7 +71,7 @@ export abstract class BaseRepository<
|
|||||||
this.registerLifeCycleHooks();
|
this.registerLifeCycleHooks();
|
||||||
}
|
}
|
||||||
|
|
||||||
getPopulate() {
|
getPopulate(): P[] {
|
||||||
return this.populate;
|
return this.populate;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +80,7 @@ export abstract class BaseRepository<
|
|||||||
return `hook:${entity}:${suffix}` as `hook:${IHookEntities}:${TNormalizedEvents}`;
|
return `hook:${entity}:${suffix}` as `hook:${IHookEntities}:${TNormalizedEvents}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
private registerLifeCycleHooks() {
|
private registerLifeCycleHooks(): void {
|
||||||
const repository = this;
|
const repository = this;
|
||||||
const hooks = LifecycleHookManager.getHooks(this.cls.name);
|
const hooks = LifecycleHookManager.getHooks(this.cls.name);
|
||||||
|
|
||||||
@ -202,7 +203,7 @@ export abstract class BaseRepository<
|
|||||||
protected async execute<R extends Omit<T, P>>(
|
protected async execute<R extends Omit<T, P>>(
|
||||||
query: Query<T[], T>,
|
query: Query<T[], T>,
|
||||||
cls: new () => R,
|
cls: new () => R,
|
||||||
) {
|
): Promise<R[]> {
|
||||||
const resultSet = await query.lean(this.leanOpts).exec();
|
const resultSet = await query.lean(this.leanOpts).exec();
|
||||||
return resultSet.map((doc) => plainToClass(cls, doc, this.transformOpts));
|
return resultSet.map((doc) => plainToClass(cls, doc, this.transformOpts));
|
||||||
}
|
}
|
||||||
@ -211,7 +212,7 @@ export abstract class BaseRepository<
|
|||||||
query: Query<T, T>,
|
query: Query<T, T>,
|
||||||
cls: new () => R,
|
cls: new () => R,
|
||||||
options?: ClassTransformOptions,
|
options?: ClassTransformOptions,
|
||||||
) {
|
): Promise<R> {
|
||||||
const doc = await query.lean(this.leanOpts).exec();
|
const doc = await query.lean(this.leanOpts).exec();
|
||||||
return plainToClass(cls, doc, options ?? this.transformOpts);
|
return plainToClass(cls, doc, options ?? this.transformOpts);
|
||||||
}
|
}
|
||||||
@ -219,7 +220,7 @@ export abstract class BaseRepository<
|
|||||||
protected findOneQuery(
|
protected findOneQuery(
|
||||||
criteria: string | TFilterQuery<T>,
|
criteria: string | TFilterQuery<T>,
|
||||||
projection?: ProjectionType<T>,
|
projection?: ProjectionType<T>,
|
||||||
) {
|
): Query<T, T, object, T, 'findOne', object> {
|
||||||
if (!criteria) {
|
if (!criteria) {
|
||||||
// An empty criteria would return the first document that it finds
|
// An empty criteria would return the first document that it finds
|
||||||
throw new Error('findOneQuery() should not have an empty criteria');
|
throw new Error('findOneQuery() should not have an empty criteria');
|
||||||
@ -247,7 +248,7 @@ export abstract class BaseRepository<
|
|||||||
async findOneAndPopulate(
|
async findOneAndPopulate(
|
||||||
criteria: string | TFilterQuery<T>,
|
criteria: string | TFilterQuery<T>,
|
||||||
projection?: ProjectionType<T>,
|
projection?: ProjectionType<T>,
|
||||||
) {
|
): Promise<TFull> {
|
||||||
this.ensureCanPopulate();
|
this.ensureCanPopulate();
|
||||||
const query = this.findOneQuery(criteria, projection).populate(
|
const query = this.findOneQuery(criteria, projection).populate(
|
||||||
this.populate,
|
this.populate,
|
||||||
@ -262,19 +263,19 @@ export abstract class BaseRepository<
|
|||||||
filter: TFilterQuery<T>,
|
filter: TFilterQuery<T>,
|
||||||
pageQuery?: QuerySortDto<T>,
|
pageQuery?: QuerySortDto<T>,
|
||||||
projection?: ProjectionType<T>,
|
projection?: ProjectionType<T>,
|
||||||
);
|
): Query<T[], T, object, T, 'find', object>;
|
||||||
|
|
||||||
protected findQuery(
|
protected findQuery(
|
||||||
filter: TFilterQuery<T>,
|
filter: TFilterQuery<T>,
|
||||||
pageQuery?: PageQueryDto<T>,
|
pageQuery?: PageQueryDto<T>,
|
||||||
projection?: ProjectionType<T>,
|
projection?: ProjectionType<T>,
|
||||||
);
|
): Query<T[], T, object, T, 'find', object>;
|
||||||
|
|
||||||
protected findQuery(
|
protected findQuery(
|
||||||
filter: TFilterQuery<T>,
|
filter: TFilterQuery<T>,
|
||||||
pageQuery?: QuerySortDto<T> | PageQueryDto<T>,
|
pageQuery?: QuerySortDto<T> | PageQueryDto<T>,
|
||||||
projection?: ProjectionType<T>,
|
projection?: ProjectionType<T>,
|
||||||
) {
|
): Query<T[], T, object, T, 'find', object> {
|
||||||
if (Array.isArray(pageQuery)) {
|
if (Array.isArray(pageQuery)) {
|
||||||
const query = this.model.find<T>(filter, projection);
|
const query = this.model.find<T>(filter, projection);
|
||||||
return query.sort([pageQuery] as [string, SortOrder][]);
|
return query.sort([pageQuery] as [string, SortOrder][]);
|
||||||
@ -321,7 +322,7 @@ export abstract class BaseRepository<
|
|||||||
return await this.execute(query, this.cls);
|
return await this.execute(query, this.cls);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ensureCanPopulate() {
|
private ensureCanPopulate(): void {
|
||||||
if (!this.populate || !this.clsPopulate) {
|
if (!this.populate || !this.clsPopulate) {
|
||||||
throw new Error('Cannot populate query');
|
throw new Error('Cannot populate query');
|
||||||
}
|
}
|
||||||
@ -361,15 +362,17 @@ export abstract class BaseRepository<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected findAllQuery(sort?: QuerySortDto<T>) {
|
protected findAllQuery(
|
||||||
|
sort?: QuerySortDto<T>,
|
||||||
|
): Query<T[], T, object, T, 'find', object> {
|
||||||
return this.findQuery({}, { limit: 0, skip: 0, sort });
|
return this.findQuery({}, { limit: 0, skip: 0, sort });
|
||||||
}
|
}
|
||||||
|
|
||||||
async findAll(sort?: QuerySortDto<T>) {
|
async findAll(sort?: QuerySortDto<T>): Promise<T[]> {
|
||||||
return await this.find({}, { limit: 0, skip: 0, sort });
|
return await this.find({}, { limit: 0, skip: 0, sort });
|
||||||
}
|
}
|
||||||
|
|
||||||
async findAllAndPopulate(sort?: QuerySortDto<T>) {
|
async findAllAndPopulate(sort?: QuerySortDto<T>): Promise<TFull[]> {
|
||||||
this.ensureCanPopulate();
|
this.ensureCanPopulate();
|
||||||
const query = this.findAllQuery(sort).populate(this.populate);
|
const query = this.findAllQuery(sort).populate(this.populate);
|
||||||
return await this.execute(query, this.clsPopulate);
|
return await this.execute(query, this.clsPopulate);
|
||||||
@ -381,7 +384,7 @@ export abstract class BaseRepository<
|
|||||||
protected findPageQuery(
|
protected findPageQuery(
|
||||||
filters: TFilterQuery<T>,
|
filters: TFilterQuery<T>,
|
||||||
{ skip, limit, sort }: PageQueryDto<T>,
|
{ skip, limit, sort }: PageQueryDto<T>,
|
||||||
) {
|
): Query<T[], T, object, T, 'find', object> {
|
||||||
return this.findQuery(filters)
|
return this.findQuery(filters)
|
||||||
.skip(skip)
|
.skip(skip)
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
@ -405,7 +408,7 @@ export abstract class BaseRepository<
|
|||||||
async findPageAndPopulate(
|
async findPageAndPopulate(
|
||||||
filters: TFilterQuery<T>,
|
filters: TFilterQuery<T>,
|
||||||
pageQuery: PageQueryDto<T>,
|
pageQuery: PageQueryDto<T>,
|
||||||
) {
|
): Promise<TFull[]> {
|
||||||
this.ensureCanPopulate();
|
this.ensureCanPopulate();
|
||||||
const query = this.findPageQuery(filters, pageQuery).populate(
|
const query = this.findPageQuery(filters, pageQuery).populate(
|
||||||
this.populate,
|
this.populate,
|
||||||
@ -431,7 +434,7 @@ export abstract class BaseRepository<
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async createMany(dtoArray: U[]) {
|
async createMany(dtoArray: U[]): Promise<T[]> {
|
||||||
const docs = await this.model.create(dtoArray);
|
const docs = await this.model.create(dtoArray);
|
||||||
|
|
||||||
return docs.map((doc) =>
|
return docs.map((doc) =>
|
||||||
@ -460,7 +463,7 @@ export abstract class BaseRepository<
|
|||||||
async updateMany<D extends Partial<U>>(
|
async updateMany<D extends Partial<U>>(
|
||||||
filter: TFilterQuery<T>,
|
filter: TFilterQuery<T>,
|
||||||
dto: UpdateQuery<D>,
|
dto: UpdateQuery<D>,
|
||||||
) {
|
): Promise<UpdateWriteOpResult> {
|
||||||
return await this.model.updateMany<T>(filter, {
|
return await this.model.updateMany<T>(filter, {
|
||||||
$set: dto,
|
$set: dto,
|
||||||
});
|
});
|
||||||
@ -476,19 +479,19 @@ export abstract class BaseRepository<
|
|||||||
return await this.model.deleteMany(criteria);
|
return await this.model.deleteMany(criteria);
|
||||||
}
|
}
|
||||||
|
|
||||||
async preValidate(_doc: HydratedDocument<T>) {
|
async preValidate(_doc: HydratedDocument<T>): Promise<void> {
|
||||||
// Nothing ...
|
// Nothing ...
|
||||||
}
|
}
|
||||||
|
|
||||||
async postValidate(_validated: HydratedDocument<T>) {
|
async postValidate(_validated: HydratedDocument<T>): Promise<void> {
|
||||||
// Nothing ...
|
// Nothing ...
|
||||||
}
|
}
|
||||||
|
|
||||||
async preCreate(_doc: HydratedDocument<T>) {
|
async preCreate(_doc: HydratedDocument<T>): Promise<void> {
|
||||||
// Nothing ...
|
// Nothing ...
|
||||||
}
|
}
|
||||||
|
|
||||||
async postCreate(_created: HydratedDocument<T>) {
|
async postCreate(_created: HydratedDocument<T>): Promise<void> {
|
||||||
// Nothing ...
|
// Nothing ...
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -496,7 +499,7 @@ export abstract class BaseRepository<
|
|||||||
_query: Query<D, D, unknown, T, 'findOneAndUpdate'>,
|
_query: Query<D, D, unknown, T, 'findOneAndUpdate'>,
|
||||||
_criteria: TFilterQuery<T>,
|
_criteria: TFilterQuery<T>,
|
||||||
_updates: UpdateWithAggregationPipeline | UpdateQuery<D>,
|
_updates: UpdateWithAggregationPipeline | UpdateQuery<D>,
|
||||||
) {
|
): Promise<void> {
|
||||||
// Nothing ...
|
// Nothing ...
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -504,35 +507,35 @@ export abstract class BaseRepository<
|
|||||||
_query: Query<D, D, unknown, T, 'updateMany'>,
|
_query: Query<D, D, unknown, T, 'updateMany'>,
|
||||||
_criteria: TFilterQuery<T>,
|
_criteria: TFilterQuery<T>,
|
||||||
_updates: UpdateWithAggregationPipeline | UpdateQuery<D>,
|
_updates: UpdateWithAggregationPipeline | UpdateQuery<D>,
|
||||||
) {
|
): Promise<void> {
|
||||||
// Nothing ...
|
// Nothing ...
|
||||||
}
|
}
|
||||||
|
|
||||||
async postUpdateMany(
|
async postUpdateMany(
|
||||||
_query: Query<D, D, unknown, T, 'updateMany'>,
|
_query: Query<D, D, unknown, T, 'updateMany'>,
|
||||||
_updated: any,
|
_updated: any,
|
||||||
) {
|
): Promise<void> {
|
||||||
// Nothing ...
|
// Nothing ...
|
||||||
}
|
}
|
||||||
|
|
||||||
async postUpdate(
|
async postUpdate(
|
||||||
_query: Query<D, D, unknown, T, 'findOneAndUpdate'>,
|
_query: Query<D, D, unknown, T, 'findOneAndUpdate'>,
|
||||||
_updated: T,
|
_updated: T,
|
||||||
) {
|
): Promise<void> {
|
||||||
// Nothing ...
|
// Nothing ...
|
||||||
}
|
}
|
||||||
|
|
||||||
async preDelete(
|
async preDelete(
|
||||||
_query: Query<DeleteResult, D, unknown, T, 'deleteOne' | 'deleteMany'>,
|
_query: Query<DeleteResult, D, unknown, T, 'deleteOne' | 'deleteMany'>,
|
||||||
_criteria: TFilterQuery<T>,
|
_criteria: TFilterQuery<T>,
|
||||||
) {
|
): Promise<void> {
|
||||||
// Nothing ...
|
// Nothing ...
|
||||||
}
|
}
|
||||||
|
|
||||||
async postDelete(
|
async postDelete(
|
||||||
_query: Query<DeleteResult, D, unknown, T, 'deleteOne' | 'deleteMany'>,
|
_query: Query<DeleteResult, D, unknown, T, 'deleteOne' | 'deleteMany'>,
|
||||||
_result: DeleteResult,
|
_result: DeleteResult,
|
||||||
) {
|
): Promise<void> {
|
||||||
// Nothing ...
|
// Nothing ...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user