@pcg/core 1.0.0-alpha.1 → 1.0.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/dist/index.d.ts +21 -1179
  2. package/dist/index.js +76 -1856
  3. package/dist/index.js.map +1 -1
  4. package/package.json +16 -4
  5. package/.turbo/turbo-build.log +0 -16
  6. package/CHANGELOG.md +0 -14
  7. package/src/abstracts/index.ts +0 -3
  8. package/src/abstracts/nestjs-resource-service.ts +0 -154
  9. package/src/abstracts/nestjs-service.ts +0 -25
  10. package/src/configs/app.config.ts +0 -185
  11. package/src/configs/db.config.ts +0 -122
  12. package/src/configs/index.ts +0 -4
  13. package/src/configs/logger.config.ts +0 -62
  14. package/src/context/action-context.ts +0 -34
  15. package/src/context/current-user.ts +0 -49
  16. package/src/context/index.ts +0 -5
  17. package/src/context/platform-method-context.ts +0 -5
  18. package/src/context/service-method-context.ts +0 -47
  19. package/src/db/snake-naming.strategy.ts +0 -277
  20. package/src/enums/app-env.enum.ts +0 -36
  21. package/src/enums/app-mode.enum.ts +0 -5
  22. package/src/enums/app-server.enum.ts +0 -39
  23. package/src/enums/index.ts +0 -4
  24. package/src/enums/worker-mode.enum.ts +0 -11
  25. package/src/errors/access-denied.error.ts +0 -18
  26. package/src/errors/bad-request.error.ts +0 -9
  27. package/src/errors/forbidden.error.ts +0 -9
  28. package/src/errors/index.ts +0 -8
  29. package/src/errors/input-validation.error.ts +0 -16
  30. package/src/errors/nest-error.filter.ts +0 -70
  31. package/src/errors/nest-error.ts +0 -63
  32. package/src/errors/not-found.error.ts +0 -9
  33. package/src/errors/unauthorized.error.ts +0 -9
  34. package/src/exceptions/http-exception-response.ts +0 -34
  35. package/src/exceptions/http-exceptions.filter.ts +0 -95
  36. package/src/index.ts +0 -32
  37. package/src/jwt/extractors.ts +0 -80
  38. package/src/jwt/types.ts +0 -209
  39. package/src/logger/classes/logger-factory.ts +0 -54
  40. package/src/logger/classes/logger.ts +0 -340
  41. package/src/logger/classes/nest-system-logger.ts +0 -63
  42. package/src/logger/classes/typeorm-logger.ts +0 -83
  43. package/src/logger/index.ts +0 -20
  44. package/src/logger/logger.constants.ts +0 -24
  45. package/src/logger/logger.interfaces.ts +0 -98
  46. package/src/logger/logger.module.ts +0 -45
  47. package/src/logger/logger.providers.ts +0 -140
  48. package/src/logger/winston.tools.ts +0 -241
  49. package/src/middlewares/app.middleware.ts +0 -26
  50. package/src/middlewares/index.ts +0 -1
  51. package/src/modules/hooks/base-hook.ts +0 -64
  52. package/src/modules/hooks/decorators/on-hook.decorator.ts +0 -19
  53. package/src/modules/hooks/hooks.module.ts +0 -10
  54. package/src/modules/hooks/hooks.service.ts +0 -28
  55. package/src/modules/hooks/index.ts +0 -11
  56. package/src/modules/id/id.module.ts +0 -26
  57. package/src/modules/id/id.service.ts +0 -57
  58. package/src/modules/id/index.ts +0 -2
  59. package/src/modules/postgres-pubsub/index.ts +0 -3
  60. package/src/modules/postgres-pubsub/postgres-pubsub.module.ts +0 -14
  61. package/src/modules/postgres-pubsub/postgres-pubsub.ts +0 -461
  62. package/src/pagination/constants.ts +0 -9
  63. package/src/pagination/cursor/cursor-pagination.exception.ts +0 -16
  64. package/src/pagination/cursor/cursor-pagination.helpers.ts +0 -145
  65. package/src/pagination/cursor/cursor-pagination.input.ts +0 -96
  66. package/src/pagination/cursor/cursor-pagination.types.ts +0 -127
  67. package/src/pagination/index.ts +0 -9
  68. package/src/pagination/offset/offset-pagination.exception.ts +0 -15
  69. package/src/pagination/offset/offset-pagination.helpers.ts +0 -122
  70. package/src/pagination/offset/offset-pagination.input.ts +0 -30
  71. package/src/pagination/offset/offset-pagination.types.ts +0 -82
  72. package/src/pagination/tools.ts +0 -53
  73. package/src/tools/compose.ts +0 -92
  74. package/src/tools/convert-to-bigint.ts +0 -27
  75. package/src/tools/create-list-meta.ts +0 -64
  76. package/src/tools/define-statuses.ts +0 -15
  77. package/src/tools/env.ts +0 -139
  78. package/src/tools/fetch-total-with-query.ts +0 -48
  79. package/src/tools/generate-entity-id.ts +0 -23
  80. package/src/tools/get-request-language.ts +0 -13
  81. package/src/tools/is-object.ts +0 -1
  82. package/src/tools/postgres/locale-to-pg-collate.ts +0 -21
  83. package/src/tools/remove-undefined-properties.ts +0 -20
  84. package/src/tools/request-id.ts +0 -25
  85. package/src/tools/stringify-opts.ts +0 -20
  86. package/src/tools/typeorm/add-filter.ts +0 -164
  87. package/src/tools/typeorm/ensure-inner-join.ts +0 -36
  88. package/src/tools/typeorm/ensure-left-join.ts +0 -36
  89. package/src/tools/typeorm/is-alias-already-busy.ts +0 -25
  90. package/src/tools/wait.ts +0 -26
  91. package/src/types/express-request.ts +0 -8
  92. package/src/types/list-mehod-options.ts +0 -32
  93. package/src/types/list-meta.ts +0 -16
  94. package/src/types/maybe.ts +0 -2
  95. package/src/validation/index.ts +0 -1
  96. package/src/validation/validation-pipe.ts +0 -14
  97. package/tsconfig.lib.json +0 -9
  98. package/tsdown.config.ts +0 -15
@@ -1,277 +0,0 @@
1
- import {
2
- pluralToSingular,
3
- singularToPlural,
4
- snakeCase,
5
- } from '@pcg/text-kit';
6
- import {
7
- DefaultNamingStrategy, NamingStrategyInterface, Table,
8
- } from 'typeorm';
9
-
10
- /**
11
- * Snake case naming strategy for typeorm.
12
- * Determines table and column names from class and property names.
13
- */
14
- export class SnakeNamingStrategy
15
- extends DefaultNamingStrategy
16
- implements NamingStrategyInterface {
17
- /**
18
- * Create a table name from a class name.
19
- * @param className - The class name of the entity
20
- * @param customName - The custom table name
21
- * @returns The table name
22
- * @example Create table name from class name
23
- * ```ts
24
- * @Entity()
25
- * class User {}
26
- * // returns 'users'
27
- *
28
- * @Entity('custom_table_name')
29
- * class User {}
30
- * // returns 'custom_table_name'
31
- *
32
- *```
33
- */
34
- tableName(className: string, customName?: string): string {
35
- if (customName) {
36
- return customName;
37
- }
38
-
39
- const snakeClassName = snakeCase(className);
40
-
41
- // if many-to-many table, use the table name as is
42
- if (snakeClassName.includes('_to_')) {
43
- return snakeClassName;
44
- }
45
-
46
- const parts = snakeClassName.split('_');
47
- if (parts.length === 1) {
48
- return singularToPlural(snakeClassName);
49
- }
50
-
51
- return parts
52
- .map((name, i) => (i === 0 ? name : singularToPlural(name)))
53
- .join('_');
54
- }
55
-
56
- /**
57
- * Create a column name from a property name.
58
- * @param propertyName - The property name of the entity
59
- * @param customName - The custom column name
60
- * @param embeddedPrefixes - The embedded prefixes
61
- * @returns The table column name
62
- * @example Create column name from property name
63
- * ```ts
64
- * @Entity()
65
- * class User {
66
- * @Column()
67
- * firstName: string; // returns 'first_name'
68
- *
69
- * @Column('custom_last_name')
70
- * lastName: string; // returns 'custom_last_name'
71
- * }
72
- * ```
73
- */
74
- columnName(
75
- propertyName: string,
76
- customName?: string,
77
- embeddedPrefixes: string[] = [],
78
- ): string {
79
- return (
80
- snakeCase(embeddedPrefixes.concat('').join('_')) +
81
- (customName ? customName : snakeCase(propertyName))
82
- );
83
- }
84
-
85
- // ???
86
- relationName(propertyName: string): string {
87
- return snakeCase(propertyName);
88
- }
89
-
90
- // ???
91
- joinColumnName(relationName: string, referencedColumnName: string): string {
92
- return snakeCase(relationName + '_' + referencedColumnName);
93
- }
94
-
95
- /**
96
- * Create join table name for many-to-many relations.
97
- * @returns join table name
98
- * @example
99
- * ```ts
100
- * @Entity()
101
- * class Post {
102
- * @ManyToMany(() => Theme) // post_to_theme
103
- * themes: Theme[];
104
- * }
105
- *
106
- */
107
- joinTableName(firstTableName: string, secondTableName: string): string {
108
- return snakeCase(
109
- pluralToSingular(firstTableName) +
110
- '_to_' +
111
- pluralToSingular(secondTableName),
112
- );
113
- }
114
-
115
- // ???
116
- joinTableColumnName(
117
- tableName: string,
118
- propertyName: string,
119
- columnName?: string,
120
- ): string {
121
- return snakeCase(
122
- pluralToSingular(tableName) + '_' + (columnName ? columnName : propertyName),
123
- );
124
- }
125
-
126
- eagerJoinRelationAlias(alias: string, propertyPath: string): string {
127
- return alias + '__' + propertyPath.replace('.', '_');
128
- }
129
-
130
- /**
131
- * Create a primary key name.
132
- * @param tableOrName - The table or table name
133
- * @param columnNames - The column names
134
- * @returns The primary key name
135
- * @example
136
- * ```ts
137
- * @Entity()
138
- * class User {
139
- * @PrimaryColumn()
140
- * id: string;
141
- * }
142
- *
143
- * // returns 'pk_users__id'
144
- */
145
- primaryKeyName(tableOrName: Table | string, columnNames: string[]): string {
146
- tableOrName =
147
- typeof tableOrName === 'string' ? tableOrName : tableOrName.name;
148
-
149
- let name = tableOrName;
150
- for (const column of columnNames) {
151
- name += `__${column}`;
152
- }
153
-
154
- name = this.simplifyName(name);
155
-
156
- return `pk_${name}`;
157
- }
158
-
159
- /**
160
- * Create a foreign key name.
161
- * @param tableOrName - The table or table name
162
- * @param columnNames - The column names
163
- * @returns The foreign key name
164
- * @example
165
- * ```ts
166
- * @Entity()
167
- * class User {
168
- * @PrimaryColumn()
169
- * id: string;
170
- * }
171
- * @Entity()
172
- * class Post {
173
- * @PrimaryColumn()
174
- * id: string;
175
- *
176
- * @ManyToOne(() => User, user => user.posts)
177
- * user: User;
178
- * }
179
- *
180
- * // returns 'fk_posts__user_id__users__id'
181
- * ```
182
- */
183
- foreignKeyName(tableOrName: Table | string, columnNames: string[]): string {
184
- tableOrName =
185
- typeof tableOrName === 'string' ? tableOrName : tableOrName.name;
186
-
187
- let name = tableOrName;
188
- for (const column of columnNames) {
189
- name += `__${column}`;
190
- }
191
-
192
- name = this.simplifyName(name);
193
-
194
- return `fk_${name}`;
195
- }
196
-
197
- /**
198
- * Create an index name.
199
- * @param tableOrName - The table or table name
200
- * @param columnNames - The column names
201
- * @returns The index name
202
- * @example
203
- * ```ts
204
- * @Entity()
205
- * class User {
206
- * @Index()
207
- * @Column()
208
- * email: string;
209
- * }
210
- * // returns 'idx_users__email'
211
- * ```
212
- */
213
- indexName(tableOrName: Table | string, columnNames: string[]): string {
214
- tableOrName =
215
- typeof tableOrName === 'string' ? tableOrName : tableOrName.name;
216
-
217
- let name = tableOrName;
218
- for (const column of columnNames) {
219
- name += `__${column}`;
220
- }
221
-
222
- name = this.simplifyName(name);
223
-
224
- return `idx_${name}`;
225
- }
226
-
227
- /**
228
- * Create a unique constraint name.
229
- * @param tableOrName - The table or table name
230
- * @param columnNames - The column names
231
- * @returns The unique constraint name
232
- * @example
233
- * ```ts
234
- * @Entity()
235
- * class User {
236
- * @Unique()
237
- * @Column()
238
- * email: string;
239
- * }
240
- * // returns 'uq_users__email'
241
- * ```
242
- */
243
- uniqueConstraintName(
244
- tableOrName: Table | string,
245
- columnNames: string[],
246
- ): string {
247
- tableOrName =
248
- typeof tableOrName === 'string' ? tableOrName : tableOrName.name;
249
-
250
- let name = tableOrName;
251
- for (const column of columnNames) {
252
- name += `__${column}`;
253
- }
254
-
255
- name = this.simplifyName(name);
256
-
257
- return `uq_${name}`;
258
- }
259
-
260
- private simplifyName(name: string): string {
261
- if (name.length > 50) {
262
- const pieces = name.split('__');
263
-
264
- const simplified = pieces.map((piece) => {
265
- if (piece.includes('_to_')) {
266
- return piece.split('_').map((p) => p[0]).join('');
267
- }
268
-
269
- return piece;
270
- });
271
-
272
- return simplified.join('__');
273
- }
274
-
275
- return name;
276
- }
277
- }
@@ -1,36 +0,0 @@
1
- /**
2
- * Application environment enum
3
- */
4
- export enum AppEnv {
5
- /**
6
- * Local environment.
7
- * Used for local development on your machine.
8
- */
9
- LOCAL = 'local',
10
-
11
- /**
12
- * Test environment.
13
- * Used when running tests.
14
- */
15
- TEST = 'test',
16
-
17
- /**
18
- * Development environment.
19
- * Used when deploying to a development server.
20
- * Development server provide early access to new features.
21
- */
22
- DEVELOPMENT = 'development',
23
-
24
- /**
25
- * Stage environment.
26
- * Used when deploying to a stage server.
27
- * Stage is a pre-production environment.
28
- */
29
- STAGE = 'stage',
30
-
31
- /**
32
- * Production environment.
33
- * Used when deploying to a production server.
34
- */
35
- PRODUCTION = 'production',
36
- }
@@ -1,5 +0,0 @@
1
- export enum AppMode {
2
- STANDALONE = 'standalone',
3
- PARENT = 'parent',
4
- CHILD = 'child',
5
- }
@@ -1,39 +0,0 @@
1
- /**
2
- * Enum for the NestJS server mode
3
- */
4
- export enum AppServer {
5
- /**
6
- * NestJS HTTP server with GraphQL
7
- */
8
- HTTP = 'http',
9
-
10
- /**
11
- * NestJS gRPC server
12
- */
13
- GRPC = 'grpc',
14
-
15
- /**
16
- * NestJS WebSocket server
17
- */
18
- WS = 'ws',
19
-
20
- /**
21
- * Run as a worker
22
- */
23
- WORKER = 'worker',
24
-
25
- /**
26
- * Run as an agent
27
- */
28
- AGENT = 'agent',
29
-
30
- /**
31
- * Run as an bot (e.g. Slack bot)
32
- */
33
- BOT = 'bot',
34
-
35
- /**
36
- * Run as Cloud Run job
37
- */
38
- CLOUDRUN_JOB = 'cloudrun-job',
39
- }
@@ -1,4 +0,0 @@
1
- export * from './app-env.enum.js';
2
- export * from './app-mode.enum.js';
3
- export * from './app-server.enum.js';
4
- export * from './worker-mode.enum.js';
@@ -1,11 +0,0 @@
1
- export enum WorkerMode {
2
- /**
3
- * Run jobs in loop
4
- */
5
- LOOP = 'loop',
6
-
7
- /**
8
- * Run one job and exit
9
- */
10
- ONE_JOB = 'one-job',
11
- }
@@ -1,18 +0,0 @@
1
- import { ForbiddenError } from './forbidden.error.js';
2
-
3
- export interface AccessDeniedErrorOptions {
4
- action: string;
5
- context: Record<string, unknown>;
6
- message?: string;
7
- }
8
-
9
- export class AccessDeniedError extends ForbiddenError {
10
- constructor(opts: AccessDeniedErrorOptions) {
11
- super({
12
- key: 'AUTH_ACCESS_DENIED',
13
- message: opts.message || `Access denied: you don't have permission to perform "${opts.action}" action`,
14
- context: opts.context,
15
- });
16
- this.name = 'AccessDeniedError';
17
- }
18
- }
@@ -1,9 +0,0 @@
1
- import { NestError, NestErrorOptions } from './nest-error.js';
2
-
3
- export class BadRequestError extends NestError {
4
- constructor(opts: NestErrorOptions) {
5
- super(opts);
6
- this.name = 'BadRequestError';
7
- this.httpStatusCode = 400;
8
- }
9
- }
@@ -1,9 +0,0 @@
1
- import { NestError, NestErrorOptions } from './nest-error.js';
2
-
3
- export class ForbiddenError extends NestError {
4
- constructor(opts: NestErrorOptions) {
5
- super(opts);
6
- this.name = 'ForbiddenError';
7
- this.httpStatusCode = 403;
8
- }
9
- }
@@ -1,8 +0,0 @@
1
- export * from './access-denied.error.js';
2
- export * from './bad-request.error.js';
3
- export * from './forbidden.error.js';
4
- export * from './input-validation.error.js';
5
- export * from './nest-error.js';
6
- export * from './not-found.error.js';
7
- export * from './unauthorized.error.js';
8
-
@@ -1,16 +0,0 @@
1
- import { ValidationError } from '@nestjs/common';
2
- import { NestError } from './nest-error.js';
3
-
4
- export class InputValidationError extends NestError {
5
- constructor(errors?: ValidationError[]) {
6
- super({
7
- message: 'Input validation failed',
8
- key: 'NST_INPUT_VALIDATION_ERROR',
9
- context: errors ? {
10
- errors,
11
- } : undefined,
12
- });
13
- this.name = 'InputValidationError';
14
- this.httpStatusCode = 400;
15
- }
16
- }
@@ -1,70 +0,0 @@
1
- ;
2
-
3
- import {
4
- ArgumentsHost, Catch, HttpServer,
5
- } from '@nestjs/common';
6
- import { GqlArgumentsHost } from '@nestjs/graphql/dist/services/gql-arguments-host';
7
-
8
- import {
9
- Logger, LoggerContext, LoggerFactory,
10
- } from '#/logger';
11
- import { NestError } from './nest-error.js';
12
-
13
- type ContextType = 'http' | 'ws' | 'rpc' | 'graphql';
14
-
15
- /**
16
- * Http exception filter for NestJS
17
- * It is used to correctly log all http exceptions
18
- * @example
19
- * ```ts
20
- * // main.ts
21
- * const { httpAdapter } = app.get(HttpAdapterHost);
22
- * const loggerFactory = app.get(WINSTON_MODULE_FACTORY_PROVIDER);
23
- * app.useGlobalFilters(new NestErrorFilter(httpAdapter, loggerFactory));
24
- * ```
25
- */
26
- @Catch(NestError)
27
- export class NestErrorFilter {
28
- private readonly logger: Logger;
29
-
30
- constructor(
31
- protected readonly applicationRef: HttpServer,
32
- private readonly loggerFactory: LoggerFactory,
33
- ) {
34
- this.logger = this.loggerFactory.create({
35
- scope: NestErrorFilter.name,
36
- });
37
- }
38
-
39
- catch(error: NestError, host: ArgumentsHost) {
40
- const gqlHost = GqlArgumentsHost.create(host);
41
- const type = gqlHost.getType<ContextType>();
42
-
43
- if (!error.silent) {
44
- this.logger.error(error);
45
- }
46
-
47
- if (type !== 'http') {
48
- return error;
49
- }
50
-
51
- /**
52
- * If request is http, then we need to send response to client
53
- */
54
- const body: {
55
- message: string;
56
- key?: string;
57
- stack?: string;
58
- context?: LoggerContext;
59
- statusCode: number;
60
- } = {
61
- message: error.message,
62
- key: error.key,
63
- stack: error.stack,
64
- context: error.context,
65
- statusCode: error.httpStatusCode,
66
- };
67
-
68
- this.applicationRef.reply(host.getArgByIndex(1), body, error.httpStatusCode);
69
- }
70
- }
@@ -1,63 +0,0 @@
1
- import { HttpStatus } from '@nestjs/common';
2
-
3
- /**
4
- * Configuration options for creating a NestError instance.
5
- *
6
- * @interface NestErrorOptions
7
- */
8
- export interface NestErrorOptions {
9
- /** The human-readable error message describing what went wrong */
10
- message: string;
11
-
12
- /**
13
- * A unique identifier or error code for categorizing and handling the error
14
- * @example
15
- * 'AUTH_USER_NOT_FOUND'
16
- * */
17
- key: string;
18
-
19
- /** The underlying error that caused this error, if any */
20
- cause?: unknown;
21
-
22
- /** Additional contextual information related to the error occurrence */
23
- context?: Record<string, unknown>;
24
-
25
- /**
26
- * Whether the error should be silenced (i.e., not logged or reported)
27
- */
28
- silent?: boolean;
29
- }
30
-
31
- /**
32
- * Custom error class for NestJS applications that extends the native Error class.
33
- *
34
- * This error class provides additional context and structured error handling
35
- * capabilities beyond the standard Error object.
36
- *
37
- * @example
38
- * ```typescript
39
- * throw new NestError({
40
- * key: 'AUTH_USER_NOT_FOUND',
41
- * message: 'User with the specified ID was not found',
42
- * context: { userId: '123' },
43
- * cause: originalError
44
- * });
45
- * ```
46
- */
47
- export class NestError extends Error {
48
- key!: string;
49
- context?: Record<string, unknown>;
50
- httpStatusCode = HttpStatus.INTERNAL_SERVER_ERROR;
51
- silent!: boolean;
52
-
53
- constructor(opts: NestErrorOptions) {
54
- super(opts.message, opts.cause instanceof Error ? {
55
- cause: opts.cause,
56
- } : undefined);
57
-
58
- this.name = 'NestError';
59
- this.key = opts.key;
60
- this.context = opts.context;
61
- this.silent = opts.silent ?? false;
62
- }
63
- }
@@ -1,9 +0,0 @@
1
- import { NestError, NestErrorOptions } from './nest-error.js';
2
-
3
- export class NotFoundError extends NestError {
4
- constructor(opts: NestErrorOptions) {
5
- super(opts);
6
- this.name = 'NotFoundError';
7
- this.httpStatusCode = 404;
8
- }
9
- }
@@ -1,9 +0,0 @@
1
- import { NestError, NestErrorOptions } from './nest-error.js';
2
-
3
- export class UnauthorizedError extends NestError {
4
- constructor(opts: NestErrorOptions) {
5
- super(opts);
6
- this.name = 'UnauthorizedError';
7
- this.httpStatusCode = 401;
8
- }
9
- }
@@ -1,34 +0,0 @@
1
- import { LoggerContext } from '#/logger';
2
-
3
- /**
4
- * NestJS HttpException response object
5
- * @example
6
- * exception.getResponse()
7
- */
8
- export interface HttpExceptionResponse {
9
- /**
10
- * Error message text
11
- */
12
- message: string;
13
-
14
- /**
15
- * Error key from 'enum'
16
- */
17
- key: string;
18
-
19
- /**
20
- * Error object
21
- */
22
- error?: Error;
23
-
24
- /**
25
- * Error context (additional data)
26
- */
27
- context: LoggerContext;
28
-
29
- /**
30
- * Don't log error
31
- */
32
- silent?: boolean;
33
- }
34
-