-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
524cd7a
commit 6fdec1b
Showing
27 changed files
with
9,118 additions
and
265 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* eslint-disable canonical/no-use-extend-native */ | ||
/* eslint-disable @typescript-eslint/naming-convention,sonarjs/cognitive-complexity */ | ||
|
||
import { compact, map } from 'lodash'; | ||
import type { ObjectLiteral } from 'typeorm'; | ||
|
||
import type { AbstractEntity } from './common/abstract.entity'; | ||
import type { AbstractDto } from './common/dto/abstract.dto'; | ||
import type { KeyOfType } from './types'; | ||
|
||
declare global { | ||
interface Array<T> { | ||
toDtos<Dto extends AbstractDto>(this: T[], options?: unknown): Dto[]; | ||
toGrpces<K>(this: T[]): K[]; | ||
} | ||
} | ||
|
||
declare module 'http' { | ||
interface IncomingHttpHeaders { | ||
'x-org-id'?: string; | ||
'x-user-info'?: string; | ||
authorization?: string; | ||
} | ||
} | ||
|
||
declare module 'typeorm' { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
interface QueryBuilder<Entity> { | ||
searchByString(q: string, columnNames: string[]): this; | ||
} | ||
|
||
interface SelectQueryBuilder<Entity> { | ||
leftJoinAndSelect<AliasEntity extends AbstractEntity, A extends string>( | ||
this: SelectQueryBuilder<Entity>, | ||
property: `${A}.${Exclude<KeyOfType<AliasEntity, AbstractEntity>, symbol>}`, | ||
alias: string, | ||
condition?: string, | ||
parameters?: ObjectLiteral, | ||
): this; | ||
|
||
leftJoin<AliasEntity extends AbstractEntity, A extends string>( | ||
this: SelectQueryBuilder<Entity>, | ||
property: `${A}.${Exclude<KeyOfType<AliasEntity, AbstractEntity>, symbol>}`, | ||
alias: string, | ||
condition?: string, | ||
parameters?: ObjectLiteral, | ||
): this; | ||
|
||
innerJoinAndSelect<AliasEntity extends AbstractEntity, A extends string>( | ||
this: SelectQueryBuilder<Entity>, | ||
property: `${A}.${Exclude<KeyOfType<AliasEntity, AbstractEntity>, symbol>}`, | ||
alias: string, | ||
condition?: string, | ||
parameters?: ObjectLiteral, | ||
): this; | ||
|
||
innerJoin<AliasEntity extends AbstractEntity, A extends string>( | ||
this: SelectQueryBuilder<Entity>, | ||
property: `${A}.${Exclude<KeyOfType<AliasEntity, AbstractEntity>, symbol>}`, | ||
alias: string, | ||
condition?: string, | ||
parameters?: ObjectLiteral, | ||
): this; | ||
} | ||
} | ||
|
||
Array.prototype.toDtos = function <Entity extends AbstractEntity<Dto>, Dto extends AbstractDto>( | ||
options?: unknown, | ||
): Dto[] { | ||
return compact(map<Entity, Dto>(this as Entity[], (item) => item.toDto(options as never))); | ||
}; | ||
|
||
Array.prototype.toGrpces = function <T extends { toGrpc: () => K }, K>(): K[] { | ||
return compact(map<T, K>(this as T[], (item) => item.toGrpc())); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import type { DeepPartial, FindManyOptions, FindOneOptions, FindOptionsWhere, ObjectLiteral } from 'typeorm'; | ||
import { MongoRepository } from 'typeorm'; | ||
|
||
@Injectable() | ||
export abstract class AbstractRepository<T extends ObjectLiteral> { | ||
constructor(protected readonly repository: MongoRepository<T>) {} | ||
|
||
/** | ||
* Find all entities with optional filtering. | ||
* @param options - Query options | ||
*/ | ||
async findAll(options?: FindManyOptions<T>): Promise<T[]> { | ||
return this.repository.find({ ...options }); | ||
} | ||
|
||
/** | ||
* Find one entity based on criteria. | ||
* @param options - Query options | ||
*/ | ||
async findOne(options: FindOneOptions<T>): Promise<T | null> { | ||
return this.repository.findOne({ ...options }); | ||
} | ||
|
||
/** | ||
* Find an entity by its ID. | ||
* @param id - ID of the entity | ||
*/ | ||
async findById(id: string | number): Promise<T | null> { | ||
return this.repository.findOne({ | ||
where: { id } as unknown as FindOptionsWhere<T>, // Cast the where clause to match the entity type | ||
}); | ||
} | ||
|
||
/** | ||
* Save a single entity or multiple entities. | ||
* @param data - Entity or entities to save | ||
*/ | ||
async save(data: DeepPartial<T>): Promise<T> { | ||
return this.repository.save(data); | ||
} | ||
|
||
/** | ||
* Delete an entity by ID. | ||
* @param id - ID of the entity to delete | ||
*/ | ||
async deleteById(id: string | number): Promise<void> { | ||
await this.repository.delete(id); | ||
} | ||
|
||
/** | ||
* Count entities matching given criteria. | ||
* @param options - Query options | ||
*/ | ||
async count(options?: FindManyOptions<T>): Promise<number> { | ||
return this.repository.count({ ...options }); | ||
} | ||
|
||
/** | ||
* Soft delete an entity by ID (if entity supports soft deletes). | ||
* @param id - ID of the entity | ||
*/ | ||
async softDeleteById(id: string | number): Promise<void> { | ||
await this.repository.softDelete(id); | ||
} | ||
|
||
/** | ||
* Restore a soft-deleted entity by ID. | ||
* @param id - ID of the entity | ||
*/ | ||
async restoreById(id: string | number): Promise<void> { | ||
await this.repository.restore(id); | ||
} | ||
|
||
create(entityLike: DeepPartial<T>) { | ||
return this.repository.create(entityLike); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import type { Request, Response } from 'express'; | ||
import type { NestMiddleware } from '@nestjs/common'; | ||
import { Injectable } from '@nestjs/common'; | ||
import * as bodyParser from 'body-parser'; | ||
import { Injectable, NestMiddleware } from '@nestjs/common'; | ||
import type { Request, Response } from 'express'; | ||
|
||
@Injectable() | ||
export class JsonBodyMiddleware implements NestMiddleware { | ||
use(req: Request, res: Response, next: () => any) { | ||
bodyParser.json()(req, res, next); | ||
} | ||
use(req: Request, res: Response, next: () => any) { | ||
bodyParser.json()(req, res, next); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { Injectable, NestMiddleware } from '@nestjs/common'; | ||
import type { Request, Response } from 'express'; | ||
import type { NestMiddleware } from '@nestjs/common'; | ||
import { Injectable } from '@nestjs/common'; | ||
import * as bodyParser from 'body-parser'; | ||
import type { Request, Response } from 'express'; | ||
|
||
@Injectable() | ||
export class RawBodyMiddleware implements NestMiddleware { | ||
use(req: Request, res: Response, next: () => any) { | ||
bodyParser.raw({type: '*/*'})(req, res, next); | ||
} | ||
use(req: Request, res: Response, next: () => any) { | ||
bodyParser.raw({ type: '*/*' })(req, res, next); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import { Column } from 'typeorm'; | ||
|
||
|
||
export class RetentionEntity { | ||
@Column() | ||
time?: number; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.