@xfilecom/backend-core 1.0.0
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.
- package/dist/core.module.d.ts +40 -0
- package/dist/core.module.js +195 -0
- package/dist/core.module.js.map +1 -0
- package/dist/database/database.constants.d.ts +2 -0
- package/dist/database/database.constants.js +6 -0
- package/dist/database/database.constants.js.map +1 -0
- package/dist/database/database.module.d.ts +19 -0
- package/dist/database/database.module.js +56 -0
- package/dist/database/database.module.js.map +1 -0
- package/dist/database/database.query.d.ts +103 -0
- package/dist/database/database.query.js +369 -0
- package/dist/database/database.query.js.map +1 -0
- package/dist/database/database.service.d.ts +18 -0
- package/dist/database/database.service.js +110 -0
- package/dist/database/database.service.js.map +1 -0
- package/dist/decorators/public.decorator.d.ts +2 -0
- package/dist/decorators/public.decorator.js +8 -0
- package/dist/decorators/public.decorator.js.map +1 -0
- package/dist/decorators/roles.decorator.d.ts +2 -0
- package/dist/decorators/roles.decorator.js +8 -0
- package/dist/decorators/roles.decorator.js.map +1 -0
- package/dist/decorators/user.decorator.d.ts +7 -0
- package/dist/decorators/user.decorator.js +10 -0
- package/dist/decorators/user.decorator.js.map +1 -0
- package/dist/filters/exception.filter.d.ts +36 -0
- package/dist/filters/exception.filter.js +201 -0
- package/dist/filters/exception.filter.js.map +1 -0
- package/dist/filters/i18n-exception.filter.d.ts +18 -0
- package/dist/filters/i18n-exception.filter.js +227 -0
- package/dist/filters/i18n-exception.filter.js.map +1 -0
- package/dist/filters/microservice-gateway-exception.filter.d.ts +4 -0
- package/dist/filters/microservice-gateway-exception.filter.js +64 -0
- package/dist/filters/microservice-gateway-exception.filter.js.map +1 -0
- package/dist/filters/rpc-response-exception.filter.d.ts +6 -0
- package/dist/filters/rpc-response-exception.filter.js +24 -0
- package/dist/filters/rpc-response-exception.filter.js.map +1 -0
- package/dist/guards/jwt-auth.guard.d.ts +14 -0
- package/dist/guards/jwt-auth.guard.js +103 -0
- package/dist/guards/jwt-auth.guard.js.map +1 -0
- package/dist/guards/roles.guard.d.ts +7 -0
- package/dist/guards/roles.guard.js +47 -0
- package/dist/guards/roles.guard.js.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +107 -0
- package/dist/index.js.map +1 -0
- package/dist/interceptors/database-check.interceptor.d.ts +12 -0
- package/dist/interceptors/database-check.interceptor.js +60 -0
- package/dist/interceptors/database-check.interceptor.js.map +1 -0
- package/dist/interceptors/error-handling.interceptor.d.ts +8 -0
- package/dist/interceptors/error-handling.interceptor.js +33 -0
- package/dist/interceptors/error-handling.interceptor.js.map +1 -0
- package/dist/interceptors/logging.interceptor.d.ts +22 -0
- package/dist/interceptors/logging.interceptor.js +178 -0
- package/dist/interceptors/logging.interceptor.js.map +1 -0
- package/dist/interceptors/response-transform.interceptor.d.ts +5 -0
- package/dist/interceptors/response-transform.interceptor.js +32 -0
- package/dist/interceptors/response-transform.interceptor.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/utils/auth-lookup.utils.d.ts +23 -0
- package/dist/utils/auth-lookup.utils.js +61 -0
- package/dist/utils/auth-lookup.utils.js.map +1 -0
- package/dist/utils/auth.helpers.d.ts +19 -0
- package/dist/utils/auth.helpers.js +77 -0
- package/dist/utils/auth.helpers.js.map +1 -0
- package/dist/utils/config-loader.utils.d.ts +22 -0
- package/dist/utils/config-loader.utils.js +77 -0
- package/dist/utils/config-loader.utils.js.map +1 -0
- package/dist/utils/config-secret.utils.d.ts +9 -0
- package/dist/utils/config-secret.utils.js +26 -0
- package/dist/utils/config-secret.utils.js.map +1 -0
- package/dist/utils/config.validator.d.ts +13 -0
- package/dist/utils/config.validator.js +82 -0
- package/dist/utils/config.validator.js.map +1 -0
- package/dist/utils/controller.helpers.d.ts +59 -0
- package/dist/utils/controller.helpers.js +117 -0
- package/dist/utils/controller.helpers.js.map +1 -0
- package/dist/utils/crypto.utils.d.ts +12 -0
- package/dist/utils/crypto.utils.js +53 -0
- package/dist/utils/crypto.utils.js.map +1 -0
- package/dist/utils/env.utils.d.ts +8 -0
- package/dist/utils/env.utils.js +27 -0
- package/dist/utils/env.utils.js.map +1 -0
- package/dist/utils/error.utils.d.ts +7 -0
- package/dist/utils/error.utils.js +72 -0
- package/dist/utils/error.utils.js.map +1 -0
- package/dist/utils/hash-verification.utils.d.ts +35 -0
- package/dist/utils/hash-verification.utils.js +133 -0
- package/dist/utils/hash-verification.utils.js.map +1 -0
- package/dist/utils/logger.helpers.d.ts +71 -0
- package/dist/utils/logger.helpers.js +293 -0
- package/dist/utils/logger.helpers.js.map +1 -0
- package/dist/utils/logging.config.d.ts +6 -0
- package/dist/utils/logging.config.js +42 -0
- package/dist/utils/logging.config.js.map +1 -0
- package/dist/utils/rpc-exception.utils.d.ts +7 -0
- package/dist/utils/rpc-exception.utils.js +53 -0
- package/dist/utils/rpc-exception.utils.js.map +1 -0
- package/dist/utils/service.helpers.d.ts +22 -0
- package/dist/utils/service.helpers.js +73 -0
- package/dist/utils/service.helpers.js.map +1 -0
- package/dist/utils/yaml-config.loader.d.ts +15 -0
- package/dist/utils/yaml-config.loader.js +219 -0
- package/dist/utils/yaml-config.loader.js.map +1 -0
- package/package.json +56 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
import { DatabaseConfig } from './database/database.module';
|
|
3
|
+
import { LoggingInterceptorOptions } from './interceptors/logging.interceptor';
|
|
4
|
+
import { ExceptionFilterOptions } from './filters/exception.filter';
|
|
5
|
+
export interface CoreModuleOptions {
|
|
6
|
+
database?: {
|
|
7
|
+
auto?: boolean;
|
|
8
|
+
config?: DatabaseConfig;
|
|
9
|
+
schema?: any;
|
|
10
|
+
};
|
|
11
|
+
jwt?: {
|
|
12
|
+
secret?: string;
|
|
13
|
+
expiresIn?: string;
|
|
14
|
+
issuer?: string;
|
|
15
|
+
audience?: string;
|
|
16
|
+
};
|
|
17
|
+
response?: {
|
|
18
|
+
commonResponse?: boolean;
|
|
19
|
+
};
|
|
20
|
+
interceptors?: {
|
|
21
|
+
logging?: boolean | LoggingInterceptorOptions;
|
|
22
|
+
errorHandling?: boolean;
|
|
23
|
+
responseTransform?: boolean;
|
|
24
|
+
databaseCheck?: boolean;
|
|
25
|
+
};
|
|
26
|
+
guards?: {
|
|
27
|
+
jwt?: boolean;
|
|
28
|
+
};
|
|
29
|
+
filters?: {
|
|
30
|
+
exception?: boolean | ExceptionFilterOptions;
|
|
31
|
+
rpcException?: boolean;
|
|
32
|
+
};
|
|
33
|
+
serviceType?: 'microservice' | 'http-api' | 'hybrid';
|
|
34
|
+
}
|
|
35
|
+
export declare class CoreModule {
|
|
36
|
+
static forRoot(options?: CoreModuleOptions): DynamicModule;
|
|
37
|
+
static forMicroservice(options?: CoreModuleOptions): DynamicModule;
|
|
38
|
+
static forHttpApi(options?: CoreModuleOptions): DynamicModule;
|
|
39
|
+
static forHybrid(options?: CoreModuleOptions): DynamicModule;
|
|
40
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var CoreModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.CoreModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const core_1 = require("@nestjs/core");
|
|
13
|
+
const database_module_1 = require("./database/database.module");
|
|
14
|
+
const logging_interceptor_1 = require("./interceptors/logging.interceptor");
|
|
15
|
+
const error_handling_interceptor_1 = require("./interceptors/error-handling.interceptor");
|
|
16
|
+
const response_transform_interceptor_1 = require("./interceptors/response-transform.interceptor");
|
|
17
|
+
const database_check_interceptor_1 = require("./interceptors/database-check.interceptor");
|
|
18
|
+
const exception_filter_1 = require("./filters/exception.filter");
|
|
19
|
+
const rpc_response_exception_filter_1 = require("./filters/rpc-response-exception.filter");
|
|
20
|
+
const jwt_auth_guard_1 = require("./guards/jwt-auth.guard");
|
|
21
|
+
const roles_guard_1 = require("./guards/roles.guard");
|
|
22
|
+
const controller_helpers_1 = require("./utils/controller.helpers");
|
|
23
|
+
const service_helpers_1 = require("./utils/service.helpers");
|
|
24
|
+
const error_utils_1 = require("./utils/error.utils");
|
|
25
|
+
const auth_helpers_1 = require("./utils/auth.helpers");
|
|
26
|
+
const logger_helpers_1 = require("./utils/logger.helpers");
|
|
27
|
+
const logging_config_1 = require("./utils/logging.config");
|
|
28
|
+
const defaultTrue = (v) => v !== false;
|
|
29
|
+
let CoreModule = CoreModule_1 = class CoreModule {
|
|
30
|
+
static forRoot(options = {}) {
|
|
31
|
+
const modules = [];
|
|
32
|
+
const providers = [];
|
|
33
|
+
const exports = [];
|
|
34
|
+
if (defaultTrue(options.database?.auto)) {
|
|
35
|
+
try {
|
|
36
|
+
if (options.database?.config && options.database?.schema) {
|
|
37
|
+
modules.push(database_module_1.DatabaseModule.forRoot({
|
|
38
|
+
config: options.database.config,
|
|
39
|
+
schema: options.database.schema,
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
else if (options.database?.schema) {
|
|
43
|
+
modules.push(database_module_1.DatabaseModule.forRootFromEnv(options.database.schema));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
console.warn('[CoreModule] Database module initialization failed:', e.message);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const interceptors = options.interceptors || {};
|
|
51
|
+
const useLogging = interceptors.logging !== false;
|
|
52
|
+
const useErrorHandling = defaultTrue(interceptors.errorHandling);
|
|
53
|
+
const useCommonResponse = defaultTrue(options.response?.commonResponse ?? interceptors.responseTransform);
|
|
54
|
+
const useDatabaseCheck = defaultTrue(interceptors.databaseCheck);
|
|
55
|
+
if (useLogging) {
|
|
56
|
+
const { loggerOptions, interceptorOptions: envInterceptorOptions } = (0, logging_config_1.getLoggingConfigFromEnv)();
|
|
57
|
+
const loggingOptions = typeof interceptors.logging === 'object'
|
|
58
|
+
? { ...envInterceptorOptions, ...interceptors.logging }
|
|
59
|
+
: envInterceptorOptions;
|
|
60
|
+
const loggerHelpers = new logger_helpers_1.LoggerHelpers('Request', undefined, undefined, loggerOptions);
|
|
61
|
+
providers.push({
|
|
62
|
+
provide: core_1.APP_INTERCEPTOR,
|
|
63
|
+
useValue: new logging_interceptor_1.LoggingInterceptor(loggerHelpers, loggingOptions),
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (useErrorHandling) {
|
|
67
|
+
providers.push({
|
|
68
|
+
provide: core_1.APP_INTERCEPTOR,
|
|
69
|
+
useClass: error_handling_interceptor_1.ErrorHandlingInterceptor,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
if (useCommonResponse &&
|
|
73
|
+
(options.serviceType === 'http-api' || options.serviceType === 'hybrid')) {
|
|
74
|
+
providers.push({
|
|
75
|
+
provide: core_1.APP_INTERCEPTOR,
|
|
76
|
+
useClass: response_transform_interceptor_1.ResponseTransformInterceptor,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (useDatabaseCheck) {
|
|
80
|
+
providers.push({
|
|
81
|
+
provide: core_1.APP_INTERCEPTOR,
|
|
82
|
+
useClass: database_check_interceptor_1.DatabaseCheckInterceptor,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
const useExceptionFilter = options.filters?.exception !== false;
|
|
86
|
+
if (useExceptionFilter) {
|
|
87
|
+
const filterOptions = typeof options.filters.exception === 'object'
|
|
88
|
+
? options.filters.exception
|
|
89
|
+
: {};
|
|
90
|
+
providers.push({
|
|
91
|
+
provide: core_1.APP_FILTER,
|
|
92
|
+
useValue: new exception_filter_1.AllExceptionsFilter(filterOptions),
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (options.filters?.rpcException === true) {
|
|
96
|
+
providers.push({
|
|
97
|
+
provide: core_1.APP_FILTER,
|
|
98
|
+
useClass: rpc_response_exception_filter_1.RpcResponseExceptionFilter,
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
if (options.guards?.jwt === true) {
|
|
102
|
+
try {
|
|
103
|
+
const { ConfigValidator } = require('./utils/config.validator');
|
|
104
|
+
ConfigValidator.validateJwtConfig(false, options.jwt);
|
|
105
|
+
}
|
|
106
|
+
catch (e) {
|
|
107
|
+
}
|
|
108
|
+
providers.push({
|
|
109
|
+
provide: core_1.APP_GUARD,
|
|
110
|
+
useClass: jwt_auth_guard_1.JwtAuthGuard,
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
providers.push(core_1.Reflector);
|
|
114
|
+
const jwtSecret = options.jwt?.secret || process.env.JWT_SECRET || 'your-secret-key';
|
|
115
|
+
const jwtExpiresIn = options.jwt?.expiresIn || process.env.JWT_EXPIRES_IN || '7d';
|
|
116
|
+
const saltRounds = parseInt(process.env.BCRYPT_SALT_ROUNDS || '10', 10);
|
|
117
|
+
providers.push({
|
|
118
|
+
provide: 'JWT_SECRET',
|
|
119
|
+
useValue: jwtSecret,
|
|
120
|
+
});
|
|
121
|
+
providers.push({
|
|
122
|
+
provide: 'JWT_EXPIRES_IN',
|
|
123
|
+
useValue: jwtExpiresIn,
|
|
124
|
+
});
|
|
125
|
+
providers.push({
|
|
126
|
+
provide: 'BCRYPT_SALT_ROUNDS',
|
|
127
|
+
useValue: saltRounds,
|
|
128
|
+
});
|
|
129
|
+
providers.push(controller_helpers_1.ControllerHelpers, service_helpers_1.ServiceHelpers, error_utils_1.ErrorUtils, auth_helpers_1.AuthHelpers, roles_guard_1.RolesGuard);
|
|
130
|
+
exports.push(controller_helpers_1.ControllerHelpers, service_helpers_1.ServiceHelpers, error_utils_1.ErrorUtils, auth_helpers_1.AuthHelpers, roles_guard_1.RolesGuard);
|
|
131
|
+
return {
|
|
132
|
+
module: CoreModule_1,
|
|
133
|
+
imports: modules,
|
|
134
|
+
providers,
|
|
135
|
+
exports,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
static forMicroservice(options = {}) {
|
|
139
|
+
return CoreModule_1.forRoot({
|
|
140
|
+
serviceType: 'microservice',
|
|
141
|
+
interceptors: {
|
|
142
|
+
logging: true,
|
|
143
|
+
errorHandling: true,
|
|
144
|
+
databaseCheck: true,
|
|
145
|
+
responseTransform: false,
|
|
146
|
+
...options.interceptors,
|
|
147
|
+
},
|
|
148
|
+
response: { commonResponse: false, ...options.response },
|
|
149
|
+
guards: { jwt: false, ...options.guards },
|
|
150
|
+
filters: { exception: false, rpcException: true, ...options.filters },
|
|
151
|
+
database: options.database,
|
|
152
|
+
jwt: options.jwt,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
static forHttpApi(options = {}) {
|
|
156
|
+
return CoreModule_1.forRoot({
|
|
157
|
+
serviceType: 'http-api',
|
|
158
|
+
interceptors: {
|
|
159
|
+
logging: true,
|
|
160
|
+
errorHandling: true,
|
|
161
|
+
responseTransform: true,
|
|
162
|
+
databaseCheck: true,
|
|
163
|
+
...options.interceptors,
|
|
164
|
+
},
|
|
165
|
+
response: { commonResponse: true, ...options.response },
|
|
166
|
+
guards: { jwt: true, ...options.guards },
|
|
167
|
+
filters: { exception: true, ...options.filters },
|
|
168
|
+
database: options.database,
|
|
169
|
+
jwt: options.jwt,
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
static forHybrid(options = {}) {
|
|
173
|
+
return CoreModule_1.forRoot({
|
|
174
|
+
serviceType: 'hybrid',
|
|
175
|
+
interceptors: {
|
|
176
|
+
logging: true,
|
|
177
|
+
errorHandling: true,
|
|
178
|
+
responseTransform: true,
|
|
179
|
+
databaseCheck: true,
|
|
180
|
+
...options.interceptors,
|
|
181
|
+
},
|
|
182
|
+
response: { commonResponse: true, ...options.response },
|
|
183
|
+
guards: { jwt: true, ...options.guards },
|
|
184
|
+
filters: { exception: true, ...options.filters },
|
|
185
|
+
database: options.database,
|
|
186
|
+
jwt: options.jwt,
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
exports.CoreModule = CoreModule;
|
|
191
|
+
exports.CoreModule = CoreModule = CoreModule_1 = __decorate([
|
|
192
|
+
(0, common_1.Global)(),
|
|
193
|
+
(0, common_1.Module)({})
|
|
194
|
+
], CoreModule);
|
|
195
|
+
//# sourceMappingURL=core.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"core.module.js","sourceRoot":"","sources":["../src/core.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAA+D;AAC/D,uCAAiF;AACjF,gEAA4E;AAG5E,4EAAmG;AACnG,0FAAqF;AACrF,kGAA6F;AAC7F,0FAAqF;AAGrF,iEAAyF;AACzF,2FAAqF;AAGrF,4DAAuD;AACvD,sDAAkD;AAGlD,mEAA+D;AAC/D,6DAAyD;AACzD,qDAAiD;AACjD,uDAAmD;AACnD,2DAAuD;AACvD,2DAAiE;AAGjE,MAAM,WAAW,GAAG,CAAC,CAAsB,EAAE,EAAE,CAAC,CAAC,KAAK,KAAK,CAAC;AAmDrD,IAAM,UAAU,kBAAhB,MAAM,UAAU;IACrB,MAAM,CAAC,OAAO,CAAC,UAA6B,EAAE;QAC5C,MAAM,OAAO,GAAU,EAAE,CAAC;QAC1B,MAAM,SAAS,GAAU,EAAE,CAAC;QAC5B,MAAM,OAAO,GAAU,EAAE,CAAC;QAG1B,IAAI,WAAW,CAAC,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,CAAC;YACxC,IAAI,CAAC;gBACH,IAAI,OAAO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MAAM,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE;YACxC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE;YAChD,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,GAAG,EAAE,OAAO,CAAC,GAAG;SACjB,CAAC,CAAC;IACL,CAAC;IAED,MAAM,CAAC,SAAS,CAAC,UAA6B,EAAE;QAC9C,OAAO,YAAU,CAAC,OAAO,CAAC;YACxB,WAAW,EAAE,QAAQ;YACrB,YAAY,EAAE;gBACZ,OAAO,EAAE,IAAI;gBACb,aAAa,EAAE,IAAI;gBACnB,iBAAiB,EAAE,IAAI;gBACvB,aAAa,EAAE,IAAI;gBACnB,GAAG,OAAO,CAAC,YAAY;aACxB;YACD,QAAQ,EAAE,EAAE,cAAc,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,QAAQ,EAAE;YACvD,MAAM,EAAE,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE;YACxC,OAAO,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,OAAO,EAAE;YAChD,QAAQ,EAAE,OAAO,CAAC,QAAQ;YAC1B,GAAG,EAAE,OAAO,CAAC,GAAG;SACjB,CAAC,CAAC;IACL,CAAC;CACF,CAAA;AAnNY,gCAAU;qBAAV,UAAU;IAFtB,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,UAAU,CAmNtB"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DATABASE_SCHEMA = exports.DATABASE_CONFIG = void 0;
|
|
4
|
+
exports.DATABASE_CONFIG = 'DATABASE_CONFIG';
|
|
5
|
+
exports.DATABASE_SCHEMA = 'DATABASE_SCHEMA';
|
|
6
|
+
//# sourceMappingURL=database.constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.constants.js","sourceRoot":"","sources":["../../src/database/database.constants.ts"],"names":[],"mappings":";;;AAAa,QAAA,eAAe,GAAG,iBAAiB,CAAC;AACpC,QAAA,eAAe,GAAG,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { DynamicModule } from '@nestjs/common';
|
|
2
|
+
export interface DatabaseConfig {
|
|
3
|
+
type: 'mysql';
|
|
4
|
+
host: string;
|
|
5
|
+
port: number;
|
|
6
|
+
user: string;
|
|
7
|
+
password: string;
|
|
8
|
+
database: string;
|
|
9
|
+
ssl?: boolean;
|
|
10
|
+
connectionLimit?: number;
|
|
11
|
+
}
|
|
12
|
+
export interface DatabaseModuleOptions {
|
|
13
|
+
config: DatabaseConfig;
|
|
14
|
+
schema: any;
|
|
15
|
+
}
|
|
16
|
+
export declare class DatabaseModule {
|
|
17
|
+
static forRoot(options: DatabaseModuleOptions): DynamicModule;
|
|
18
|
+
static forRootFromEnv(schema: any): DynamicModule;
|
|
19
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var DatabaseModule_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.DatabaseModule = void 0;
|
|
11
|
+
const common_1 = require("@nestjs/common");
|
|
12
|
+
const database_service_1 = require("./database.service");
|
|
13
|
+
const database_query_1 = require("./database.query");
|
|
14
|
+
const database_constants_1 = require("./database.constants");
|
|
15
|
+
let DatabaseModule = DatabaseModule_1 = class DatabaseModule {
|
|
16
|
+
static forRoot(options) {
|
|
17
|
+
return {
|
|
18
|
+
module: DatabaseModule_1,
|
|
19
|
+
providers: [
|
|
20
|
+
{
|
|
21
|
+
provide: database_constants_1.DATABASE_CONFIG,
|
|
22
|
+
useValue: options.config,
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
provide: database_constants_1.DATABASE_SCHEMA,
|
|
26
|
+
useValue: options.schema,
|
|
27
|
+
},
|
|
28
|
+
database_service_1.DatabaseService,
|
|
29
|
+
database_query_1.DatabaseQuery,
|
|
30
|
+
],
|
|
31
|
+
exports: [database_service_1.DatabaseService, database_query_1.DatabaseQuery],
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
static forRootFromEnv(schema) {
|
|
35
|
+
const { ConfigValidator } = require('../utils/config.validator');
|
|
36
|
+
ConfigValidator.validateDatabaseConfig(false);
|
|
37
|
+
const password = process.env.DB_PASSWORD;
|
|
38
|
+
const config = {
|
|
39
|
+
type: 'mysql',
|
|
40
|
+
host: process.env.DB_HOST || 'localhost',
|
|
41
|
+
port: ConfigValidator.validatePort(process.env.DB_PORT, 3306),
|
|
42
|
+
user: process.env.DB_USER || 'root',
|
|
43
|
+
password: password !== undefined ? password : undefined,
|
|
44
|
+
database: process.env.DB_NAME || process.env.DB_DATABASE || 'test',
|
|
45
|
+
ssl: ConfigValidator.parseBoolean(process.env.DB_SSL, false),
|
|
46
|
+
connectionLimit: ConfigValidator.parseNumber(process.env.DB_CONNECTION_LIMIT, 10, 1, 100),
|
|
47
|
+
};
|
|
48
|
+
return DatabaseModule_1.forRoot({ config, schema });
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
exports.DatabaseModule = DatabaseModule;
|
|
52
|
+
exports.DatabaseModule = DatabaseModule = DatabaseModule_1 = __decorate([
|
|
53
|
+
(0, common_1.Global)(),
|
|
54
|
+
(0, common_1.Module)({})
|
|
55
|
+
], DatabaseModule);
|
|
56
|
+
//# sourceMappingURL=database.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.module.js","sourceRoot":"","sources":["../../src/database/database.module.ts"],"names":[],"mappings":";;;;;;;;;;AAAA,2CAA+D;AAC/D,yDAAqD;AACrD,qDAAiD;AACjD,6DAAwE;AAoBjE,IAAM,cAAc,sBAApB,MAAM,cAAc;IACzB,MAAM,CAAC,OAAO,CAAC,OAA8B;QAC3C,OAAO;YACL,MAAM,EAAE,gBAAc;YACtB,SAAS,EAAE;gBACT;oBACE,OAAO,EAAE,oCAAe;oBACxB,QAAQ,EAAE,OAAO,CAAC,MAAM;iBACzB;gBACD;oBACE,OAAO,EAAE,oCAAe;oBACxB,QAAQ,EAAE,OAAO,CAAC,MAAM;iBACzB;gBACD,kCAAe;gBACf,8BAAa;aACd;YACD,OAAO,EAAE,CAAC,kCAAe,EAAE,8BAAa,CAAC;SAC1C,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,cAAc,CAAC,MAAW;QAE/B,MAAM,EAAE,eAAe,EAAE,GAAG,OAAO,CAAC,2BAA2B,CAAC,CAAC;QAGjE,eAAe,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC;QAE9C,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC;QAGzC,MAAM,MAAM,GAAmB;YAC7B,IAAI,EAAE,OAAO;YACb,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC,OAAO,IAAI,WAAW;YACxC,IAAI,EAAE,eAAe,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC;YAC7D,IAAI,EAAE,OAAO,CAAC,GAAG,CAAC,OAAO,IAAI,MAAM;YACnC,QAAQ,EAAE,QAAQ,KAAK,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS;YACvD,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,OAAO,IAAI,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,MAAM;YAClE,GAAG,EAAE,eAAe,CAAC,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC;YAC5D,eAAe,EAAE,eAAe,CAAC,WAAW,CAC1C,OAAO,CAAC,GAAG,CAAC,mBAAmB,EAC/B,EAAE,EACF,CAAC,EACD,GAAG,CACJ;SACF,CAAC;QAEF,OAAO,gBAAc,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IACpD,CAAC;CACF,CAAA;AAhDY,wCAAc;yBAAd,cAAc;IAF1B,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,cAAc,CAgD1B"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { SQL } from 'drizzle-orm';
|
|
2
|
+
import { DatabaseService } from './database.service';
|
|
3
|
+
export interface SearchOption {
|
|
4
|
+
keyword: string;
|
|
5
|
+
fields: string[];
|
|
6
|
+
}
|
|
7
|
+
export interface SortOption {
|
|
8
|
+
field: string;
|
|
9
|
+
order: 'asc' | 'desc';
|
|
10
|
+
}
|
|
11
|
+
export interface PaginationOption {
|
|
12
|
+
page: number;
|
|
13
|
+
limit: number;
|
|
14
|
+
}
|
|
15
|
+
export type WhereClause = SQL | Record<string, any>;
|
|
16
|
+
export interface QueryOptions {
|
|
17
|
+
values?: Record<string, any>;
|
|
18
|
+
sort?: SortOption[];
|
|
19
|
+
pagination?: PaginationOption;
|
|
20
|
+
search?: SearchOption[];
|
|
21
|
+
where?: WhereClause;
|
|
22
|
+
}
|
|
23
|
+
export interface UnifiedQueryOptions {
|
|
24
|
+
table: string;
|
|
25
|
+
fields?: string[];
|
|
26
|
+
sort?: SortOption[];
|
|
27
|
+
pagination?: PaginationOption;
|
|
28
|
+
search?: SearchOption[];
|
|
29
|
+
where?: WhereClause;
|
|
30
|
+
}
|
|
31
|
+
export interface SelectOptions {
|
|
32
|
+
table: string;
|
|
33
|
+
fields?: string[];
|
|
34
|
+
sort?: SortOption[];
|
|
35
|
+
pagination?: PaginationOption;
|
|
36
|
+
search?: SearchOption[];
|
|
37
|
+
where?: WhereClause;
|
|
38
|
+
}
|
|
39
|
+
export interface UnifiedInsertOptions {
|
|
40
|
+
table: string;
|
|
41
|
+
fields?: string[];
|
|
42
|
+
values: Record<string, any>;
|
|
43
|
+
}
|
|
44
|
+
export interface UnifiedUpdateOptions {
|
|
45
|
+
table: string;
|
|
46
|
+
fields?: string[];
|
|
47
|
+
values: Record<string, any>;
|
|
48
|
+
where: WhereClause;
|
|
49
|
+
}
|
|
50
|
+
export interface UnifiedDeleteOptions {
|
|
51
|
+
table: string;
|
|
52
|
+
where: WhereClause;
|
|
53
|
+
}
|
|
54
|
+
export interface FindOneOptions {
|
|
55
|
+
where?: WhereClause;
|
|
56
|
+
fields?: string[];
|
|
57
|
+
sort?: SortOption[];
|
|
58
|
+
search?: SearchOption[];
|
|
59
|
+
}
|
|
60
|
+
export interface QueryResult<T = any> {
|
|
61
|
+
data: T[];
|
|
62
|
+
pagination?: {
|
|
63
|
+
total: number;
|
|
64
|
+
page: number;
|
|
65
|
+
limit: number;
|
|
66
|
+
totalPages: number;
|
|
67
|
+
hasNext: boolean;
|
|
68
|
+
hasPrev: boolean;
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
export declare class DatabaseQuery {
|
|
72
|
+
private readonly databaseService;
|
|
73
|
+
private readonly schema;
|
|
74
|
+
constructor(databaseService: DatabaseService, schema: any);
|
|
75
|
+
private getDb;
|
|
76
|
+
private getTable;
|
|
77
|
+
private selectFields;
|
|
78
|
+
private buildSearchCondition;
|
|
79
|
+
private isPlainObjectWhere;
|
|
80
|
+
private buildWhereFromObject;
|
|
81
|
+
private normalizeWhere;
|
|
82
|
+
private buildSortCondition;
|
|
83
|
+
private getTableIdColumn;
|
|
84
|
+
findOne<T = any>(table: string, options?: FindOneOptions): Promise<T | undefined>;
|
|
85
|
+
findById<T = any>(table: string, id: string | number, options?: {
|
|
86
|
+
idField?: string;
|
|
87
|
+
fields?: string[];
|
|
88
|
+
}): Promise<T | undefined>;
|
|
89
|
+
count(table: string, options?: {
|
|
90
|
+
where?: WhereClause;
|
|
91
|
+
}): Promise<number>;
|
|
92
|
+
exists(table: string, options?: {
|
|
93
|
+
where?: WhereClause;
|
|
94
|
+
}): Promise<boolean>;
|
|
95
|
+
query<T = any>(options: UnifiedQueryOptions): Promise<QueryResult<T>>;
|
|
96
|
+
select<T = any>(tableOrOptions: string | SelectOptions, fields?: string[], options?: QueryOptions): Promise<QueryResult<T>>;
|
|
97
|
+
insertOne<T = any>(options: UnifiedInsertOptions): Promise<T[]>;
|
|
98
|
+
insert<T = any>(table: string, fields: string[], options?: QueryOptions): Promise<T[]>;
|
|
99
|
+
updateOne<T = any>(options: UnifiedUpdateOptions): Promise<T[]>;
|
|
100
|
+
update<T = any>(table: string, fields: string[], options?: QueryOptions): Promise<T[]>;
|
|
101
|
+
deleteOne<T = any>(options: UnifiedDeleteOptions): Promise<T[]>;
|
|
102
|
+
delete<T = any>(table: string, fields: string[], options?: QueryOptions): Promise<T[]>;
|
|
103
|
+
}
|