xcally-nest-library 0.0.19 → 0.0.21

Sign up to get free protection for your applications and to get access to all the features.
package/index.ts CHANGED
@@ -4,7 +4,6 @@ export * from './src/interceptors/serialize.interceptor';
4
4
  export * from './src/modules/logger/winston.service';
5
5
  export * from './src/modules/logger/winston.module';
6
6
  export * from './src/modules/tracer/tracer.module';
7
- export * from './src/db/typeorm/generic.repository';
8
7
  export * from './src/decorators/utils.decorators';
9
8
  export * from './src/config/env.validation';
10
9
  export * from './src/modules/logger/pino/logger.service';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xcally-nest-library",
3
- "version": "0.0.19",
3
+ "version": "0.0.21",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -41,7 +41,6 @@
41
41
  "datadog-winston": "^1.6.0",
42
42
  "nest-winston": "^1.9.4",
43
43
  "nestjs-grpc-exceptions": "^0.2.2",
44
- "nestjs-paginate": "^8.6.2",
45
44
  "nestjs-pino": "^4.1.0",
46
45
  "pino": "^9.4.0",
47
46
  "pino-pretty": "^11.2.2",
@@ -1,74 +1,74 @@
1
- import { DynamicModule, Global, Module } from '@nestjs/common';
1
+ import { Global, Module } from '@nestjs/common';
2
2
  import { ConfigModule, ConfigService } from '@nestjs/config';
3
3
  import { LoggerModule as LoggerModulePino } from 'nestjs-pino';
4
4
  import { LoggerService } from './logger.service';
5
5
 
6
6
  @Global()
7
- @Module({})
8
- export class LoggerModule {
9
- static forRoot(): DynamicModule {
10
- return {
11
- module: LoggerModule,
12
- providers: [LoggerService],
13
- exports: [LoggerService],
14
- imports: [
15
- LoggerModulePino.forRootAsync({
16
- imports: [ConfigModule],
17
- inject: [ConfigService],
18
- useFactory: async (config: ConfigService) => {
19
- const projectName = process.env.npm_package_name || 'default_project';
20
- return {
21
- pinoHttp: {
22
- level: config.get('LOG_LEVEL') || 'info',
23
- // customErrorMessage?: ((req: IM, res: SR, error: Error) => string) | undefined;
24
- // customErrorMessage: (req, res, error) => {
25
- // return 'Auto generated error' + error?.message;
26
- // },
27
- // customSuccessMessage(req, res, responseTime) {
28
- // return 'Auto generated success';
29
- // },
30
- // autoLogging: true,
31
- // level: config.get('LOG_LEVEL') || 'info',
32
- // stream: pino.destination(`./logs/info.log`),
33
- redact: {
34
- paths: ['req.headers.cookie', 'req.headers.authorization'],
7
+ @Module({
8
+ providers: [LoggerService],
9
+ exports: [LoggerService],
10
+ imports: [
11
+ LoggerModulePino.forRootAsync({
12
+ imports: [ConfigModule],
13
+ inject: [ConfigService],
14
+ useFactory: async (config: ConfigService) => {
15
+ const projectName = process.env.npm_package_name || 'default_project';
16
+ return {
17
+ pinoHttp: {
18
+ customLogLevel: (req, res) => {
19
+ if (res.statusCode >= 500) return 'error';
20
+ if (res.statusCode >= 400) return 'warn';
21
+ return 'info';
22
+ },
23
+ level: config.get('LOG_LEVEL') || 'info',
24
+ // customErrorMessage?: ((req: IM, res: SR, error: Error) => string) | undefined;
25
+ // customErrorMessage: (req, res, error) => {
26
+ // return 'Auto generated error' + error?.message;
27
+ // },
28
+ // customSuccessMessage(req, res, responseTime) {
29
+ // return 'Auto generated success';
30
+ // },
31
+ // autoLogging: true,
32
+ // level: config.get('LOG_LEVEL') || 'info',
33
+ // stream: pino.destination(`./logs/info.log`),
34
+ redact: {
35
+ paths: ['req.headers.cookie', 'req.headers.authorization'],
36
+ },
37
+ transport: {
38
+ targets: [
39
+ {
40
+ level: 'debug',
41
+ target: 'pino-pretty',
42
+ options: {
43
+ colorize: true,
44
+ },
35
45
  },
36
- transport: {
37
- targets: [
38
- {
39
- level: 'debug',
40
- target: 'pino-pretty',
41
- options: {
42
- colorize: true,
43
- },
44
- },
45
- // {
46
- // level: 'error',
47
- // target: 'pino/file',
48
- // options: {
49
- // destination: './logs/error.log',
50
- // },
51
- // },
52
- {
53
- level: 'info',
54
- target: 'pino/file',
55
- options: {
56
- destination: `./logs/${projectName}-info.log`,
57
- mkdir: true,
58
- append: process.env.NODE_ENV !== 'production' ? false : true,
59
- },
60
- },
61
- ],
62
- },
63
- // genReqId: () => { // Non't needed with pino instrumentation
64
- // const activeSpan = api.trace.getSpan(api.context.active());
65
- // return activeSpan.spanContext().traceId;
46
+ // {
47
+ // level: 'error',
48
+ // target: 'pino/file',
49
+ // options: {
50
+ // destination: './logs/error.log',
51
+ // },
66
52
  // },
67
- },
68
- };
53
+ {
54
+ level: 'info',
55
+ target: 'pino/file',
56
+ options: {
57
+ destination: `./logs/${projectName}-info.log`,
58
+ mkdir: true,
59
+ append: process.env.NODE_ENV !== 'production' ? false : true,
60
+ },
61
+ },
62
+ ],
63
+ },
64
+ // genReqId: () => { // Non't needed with pino instrumentation
65
+ // const activeSpan = api.trace.getSpan(api.context.active());
66
+ // return activeSpan.spanContext().traceId;
67
+ // },
69
68
  },
70
- }),
71
- ],
72
- };
73
- }
74
- }
69
+ };
70
+ },
71
+ }),
72
+ ],
73
+ })
74
+ export class LoggerModule {}
@@ -1,110 +0,0 @@
1
- import { Repository, EntityTarget, EntityManager, Equal, FindOptionsWhere, ObjectLiteral, DeepPartial } from 'typeorm';
2
- import { PaginateConfig, PaginateQuery, Paginated, paginate } from 'nestjs-paginate';
3
-
4
- interface IId {
5
- id: number;
6
- }
7
-
8
- /**
9
- * Represents a generic repository for managing entities.
10
- */
11
- export class GenericRepository<E extends ObjectLiteral> {
12
- protected repository: Repository<E>;
13
- protected paginateConfig: PaginateConfig<E>;
14
-
15
- constructor(
16
- private readonly entityManager: EntityManager,
17
- entity: EntityTarget<E>,
18
- paginateConfig: PaginateConfig<E>,
19
- ) {
20
- this.repository = this.entityManager.getRepository(entity);
21
- this.paginateConfig = paginateConfig;
22
- }
23
- /**
24
- * Retrieves a paginated list of entities based on the provided query.
25
- *
26
- * @param query - The pagination query parameters.
27
- * @returns A promise that resolves to a paginated list of entities.
28
- */
29
- getMany(query: PaginateQuery): Promise<Paginated<E>> {
30
- return paginate(query, this.repository, this.paginateConfig);
31
- }
32
- /**
33
- * Saves an entity to the database.
34
- *
35
- * @param entity - The entity to be saved.
36
- * @returns A promise that resolves to the saved entity.
37
- */
38
- save<T extends DeepPartial<E>>(entity: T): Promise<E> {
39
- const res = this.repository.create({ ...entity });
40
- return this.repository.save(res);
41
- }
42
-
43
- /**
44
- * Updates an entity in the database.
45
- *
46
- * @param dto - The updated entity data.
47
- * @returns A promise that resolves to the updated entity.
48
- * @throws Error if the entity is not found.
49
- */
50
- async update<T extends DeepPartial<E> & IId>(dto: T): Promise<E> {
51
- const entityUpdated = await this.repository.update(dto.id, dto as any);
52
- if (!entityUpdated.affected) {
53
- return undefined;
54
- }
55
- return this.repository.findOneBy({ id: Equal(dto.id) } as unknown as FindOptionsWhere<E>);
56
- }
57
-
58
- /**
59
- * Counts the number of entities that match the provided options.
60
- *
61
- * @param options - The options to filter the entities.
62
- * @returns A promise that resolves to the number of entities that match the options.
63
- */
64
- count(options: FindOptionsWhere<E>): Promise<number> {
65
- return this.repository.count(options);
66
- }
67
-
68
- /**
69
- * Retrieves all entities from the database.
70
- *
71
- * @returns A promise that resolves to an array of entities.
72
- */
73
- findAll(): Promise<E[]> {
74
- return this.repository.find();
75
- }
76
-
77
- /**
78
- * Retrieves the total count of all entities in the database.
79
- *
80
- * @returns A promise that resolves to the total count of entities.
81
- */
82
- countAll(): Promise<number> {
83
- return this.repository.count();
84
- }
85
-
86
- /**
87
- * Retrieves an entity from the database based on its ID.
88
- *
89
- * @param id - The ID of the entity to retrieve.
90
- * @returns A promise that resolves to the retrieved entity.
91
- */
92
- async findById(id: number): Promise<E> {
93
- return await this.repository.findOneBy({
94
- id: Equal(id),
95
- } as unknown as FindOptionsWhere<E>);
96
- }
97
-
98
- /**
99
- * Removes an entity from the database based on its ID.
100
- *
101
- * @param id - The ID of the entity to remove.
102
- * @returns A promise that resolves when the entity is successfully removed.
103
- */
104
- async remove(id: number): Promise<void> {
105
- await this.repository.delete(id);
106
- }
107
- async remove2(id: number): Promise<void> {
108
- await this.repository.delete(id);
109
- }
110
- }