@xfilecom/core-sdk 1.3.23
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/README.md +664 -0
- package/dist/core.module.d.ts +39 -0
- package/dist/core.module.js +188 -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.example.d.ts +36 -0
- package/dist/database/database.query.example.js +148 -0
- package/dist/database/database.query.example.js.map +1 -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/database/example-usage.d.ts +0 -0
- package/dist/database/example-usage.js +1 -0
- package/dist/database/example-usage.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/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 +29 -0
- package/dist/index.js +91 -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 +21 -0
- package/dist/interceptors/logging.interceptor.js +167 -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 +30 -0
- package/dist/interceptors/response-transform.interceptor.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.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 +58 -0
- package/dist/utils/controller.helpers.js +104 -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/email-hash.utils.d.ts +7 -0
- package/dist/utils/email-hash.utils.js +42 -0
- package/dist/utils/email-hash.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 +6 -0
- package/dist/utils/error.utils.js +65 -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/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 +47 -0
- package/scripts/publish-to-gitlab.mjs +209 -0
|
@@ -0,0 +1,188 @@
|
|
|
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 jwt_auth_guard_1 = require("./guards/jwt-auth.guard");
|
|
20
|
+
const roles_guard_1 = require("./guards/roles.guard");
|
|
21
|
+
const controller_helpers_1 = require("./utils/controller.helpers");
|
|
22
|
+
const service_helpers_1 = require("./utils/service.helpers");
|
|
23
|
+
const error_utils_1 = require("./utils/error.utils");
|
|
24
|
+
const auth_helpers_1 = require("./utils/auth.helpers");
|
|
25
|
+
const logger_helpers_1 = require("./utils/logger.helpers");
|
|
26
|
+
const logging_config_1 = require("./utils/logging.config");
|
|
27
|
+
const defaultTrue = (v) => v !== false;
|
|
28
|
+
let CoreModule = CoreModule_1 = class CoreModule {
|
|
29
|
+
static forRoot(options = {}) {
|
|
30
|
+
const modules = [];
|
|
31
|
+
const providers = [];
|
|
32
|
+
const exports = [];
|
|
33
|
+
if (defaultTrue(options.database?.auto)) {
|
|
34
|
+
try {
|
|
35
|
+
if (options.database?.config && options.database?.schema) {
|
|
36
|
+
modules.push(database_module_1.DatabaseModule.forRoot({
|
|
37
|
+
config: options.database.config,
|
|
38
|
+
schema: options.database.schema,
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
else if (options.database?.schema) {
|
|
42
|
+
modules.push(database_module_1.DatabaseModule.forRootFromEnv(options.database.schema));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
catch (e) {
|
|
46
|
+
console.warn('[CoreModule] Database module initialization failed:', e.message);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const interceptors = options.interceptors || {};
|
|
50
|
+
const useLogging = interceptors.logging !== false;
|
|
51
|
+
const useErrorHandling = defaultTrue(interceptors.errorHandling);
|
|
52
|
+
const useCommonResponse = defaultTrue(options.response?.commonResponse ?? interceptors.responseTransform);
|
|
53
|
+
const useDatabaseCheck = defaultTrue(interceptors.databaseCheck);
|
|
54
|
+
if (useLogging) {
|
|
55
|
+
const { loggerOptions, interceptorOptions: envInterceptorOptions } = (0, logging_config_1.getLoggingConfigFromEnv)();
|
|
56
|
+
const loggingOptions = typeof interceptors.logging === 'object'
|
|
57
|
+
? { ...envInterceptorOptions, ...interceptors.logging }
|
|
58
|
+
: envInterceptorOptions;
|
|
59
|
+
const loggerHelpers = new logger_helpers_1.LoggerHelpers('Request', undefined, undefined, loggerOptions);
|
|
60
|
+
providers.push({
|
|
61
|
+
provide: core_1.APP_INTERCEPTOR,
|
|
62
|
+
useValue: new logging_interceptor_1.LoggingInterceptor(loggerHelpers, loggingOptions),
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
if (useErrorHandling) {
|
|
66
|
+
providers.push({
|
|
67
|
+
provide: core_1.APP_INTERCEPTOR,
|
|
68
|
+
useClass: error_handling_interceptor_1.ErrorHandlingInterceptor,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
if (useCommonResponse &&
|
|
72
|
+
(options.serviceType === 'http-api' || options.serviceType === 'hybrid')) {
|
|
73
|
+
providers.push({
|
|
74
|
+
provide: core_1.APP_INTERCEPTOR,
|
|
75
|
+
useClass: response_transform_interceptor_1.ResponseTransformInterceptor,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
if (useDatabaseCheck) {
|
|
79
|
+
providers.push({
|
|
80
|
+
provide: core_1.APP_INTERCEPTOR,
|
|
81
|
+
useClass: database_check_interceptor_1.DatabaseCheckInterceptor,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
const useExceptionFilter = options.filters?.exception !== false;
|
|
85
|
+
if (useExceptionFilter) {
|
|
86
|
+
const filterOptions = typeof options.filters.exception === 'object'
|
|
87
|
+
? options.filters.exception
|
|
88
|
+
: {};
|
|
89
|
+
providers.push({
|
|
90
|
+
provide: core_1.APP_FILTER,
|
|
91
|
+
useValue: new exception_filter_1.AllExceptionsFilter(filterOptions),
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (options.guards?.jwt === true) {
|
|
95
|
+
try {
|
|
96
|
+
const { ConfigValidator } = require('./utils/config.validator');
|
|
97
|
+
ConfigValidator.validateJwtConfig(false, options.jwt);
|
|
98
|
+
}
|
|
99
|
+
catch (e) {
|
|
100
|
+
}
|
|
101
|
+
providers.push({
|
|
102
|
+
provide: core_1.APP_GUARD,
|
|
103
|
+
useClass: jwt_auth_guard_1.JwtAuthGuard,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
providers.push(core_1.Reflector);
|
|
107
|
+
const jwtSecret = options.jwt?.secret || process.env.JWT_SECRET || 'your-secret-key';
|
|
108
|
+
const jwtExpiresIn = options.jwt?.expiresIn || process.env.JWT_EXPIRES_IN || '7d';
|
|
109
|
+
const saltRounds = parseInt(process.env.BCRYPT_SALT_ROUNDS || '10', 10);
|
|
110
|
+
providers.push({
|
|
111
|
+
provide: 'JWT_SECRET',
|
|
112
|
+
useValue: jwtSecret,
|
|
113
|
+
});
|
|
114
|
+
providers.push({
|
|
115
|
+
provide: 'JWT_EXPIRES_IN',
|
|
116
|
+
useValue: jwtExpiresIn,
|
|
117
|
+
});
|
|
118
|
+
providers.push({
|
|
119
|
+
provide: 'BCRYPT_SALT_ROUNDS',
|
|
120
|
+
useValue: saltRounds,
|
|
121
|
+
});
|
|
122
|
+
providers.push(controller_helpers_1.ControllerHelpers, service_helpers_1.ServiceHelpers, error_utils_1.ErrorUtils, auth_helpers_1.AuthHelpers, roles_guard_1.RolesGuard);
|
|
123
|
+
exports.push(controller_helpers_1.ControllerHelpers, service_helpers_1.ServiceHelpers, error_utils_1.ErrorUtils, auth_helpers_1.AuthHelpers, roles_guard_1.RolesGuard);
|
|
124
|
+
return {
|
|
125
|
+
module: CoreModule_1,
|
|
126
|
+
imports: modules,
|
|
127
|
+
providers,
|
|
128
|
+
exports,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
static forMicroservice(options = {}) {
|
|
132
|
+
return CoreModule_1.forRoot({
|
|
133
|
+
serviceType: 'microservice',
|
|
134
|
+
interceptors: {
|
|
135
|
+
logging: true,
|
|
136
|
+
errorHandling: true,
|
|
137
|
+
databaseCheck: true,
|
|
138
|
+
responseTransform: false,
|
|
139
|
+
...options.interceptors,
|
|
140
|
+
},
|
|
141
|
+
response: { commonResponse: false, ...options.response },
|
|
142
|
+
guards: { jwt: false, ...options.guards },
|
|
143
|
+
filters: { exception: true, ...options.filters },
|
|
144
|
+
database: options.database,
|
|
145
|
+
jwt: options.jwt,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
static forHttpApi(options = {}) {
|
|
149
|
+
return CoreModule_1.forRoot({
|
|
150
|
+
serviceType: 'http-api',
|
|
151
|
+
interceptors: {
|
|
152
|
+
logging: true,
|
|
153
|
+
errorHandling: true,
|
|
154
|
+
responseTransform: true,
|
|
155
|
+
databaseCheck: true,
|
|
156
|
+
...options.interceptors,
|
|
157
|
+
},
|
|
158
|
+
response: { commonResponse: true, ...options.response },
|
|
159
|
+
guards: { jwt: true, ...options.guards },
|
|
160
|
+
filters: { exception: true, ...options.filters },
|
|
161
|
+
database: options.database,
|
|
162
|
+
jwt: options.jwt,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
static forHybrid(options = {}) {
|
|
166
|
+
return CoreModule_1.forRoot({
|
|
167
|
+
serviceType: 'hybrid',
|
|
168
|
+
interceptors: {
|
|
169
|
+
logging: true,
|
|
170
|
+
errorHandling: true,
|
|
171
|
+
responseTransform: true,
|
|
172
|
+
databaseCheck: true,
|
|
173
|
+
...options.interceptors,
|
|
174
|
+
},
|
|
175
|
+
response: { commonResponse: true, ...options.response },
|
|
176
|
+
guards: { jwt: true, ...options.guards },
|
|
177
|
+
filters: { exception: true, ...options.filters },
|
|
178
|
+
database: options.database,
|
|
179
|
+
jwt: options.jwt,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
exports.CoreModule = CoreModule;
|
|
184
|
+
exports.CoreModule = CoreModule = CoreModule_1 = __decorate([
|
|
185
|
+
(0, common_1.Global)(),
|
|
186
|
+
(0, common_1.Module)({})
|
|
187
|
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O,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;AA5MY,gCAAU;qBAAV,UAAU;IAFtB,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,CAAC;GACE,UAAU,CA4MtB"}
|
|
@@ -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,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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
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { DatabaseQuery } from '@xfilecom/core-sdk';
|
|
2
|
+
export declare class UserService {
|
|
3
|
+
private readonly dbQuery;
|
|
4
|
+
constructor(dbQuery: DatabaseQuery);
|
|
5
|
+
exampleSelect(): Promise<{
|
|
6
|
+
allUsers: any;
|
|
7
|
+
userFields: any;
|
|
8
|
+
searchResult: any;
|
|
9
|
+
activeUsers: any;
|
|
10
|
+
complexQuery: any;
|
|
11
|
+
}>;
|
|
12
|
+
exampleInsert(): Promise<{
|
|
13
|
+
newUser1: any;
|
|
14
|
+
newUser2: any;
|
|
15
|
+
}>;
|
|
16
|
+
exampleUpdate(): Promise<{
|
|
17
|
+
updated1: any;
|
|
18
|
+
updated2: any;
|
|
19
|
+
updated3: any;
|
|
20
|
+
}>;
|
|
21
|
+
exampleDelete(): Promise<{
|
|
22
|
+
deleted1: any;
|
|
23
|
+
deleted2: any;
|
|
24
|
+
}>;
|
|
25
|
+
searchUsers(keyword: string, page?: number, limit?: number): Promise<any>;
|
|
26
|
+
createUser(data: {
|
|
27
|
+
email: string;
|
|
28
|
+
name: string;
|
|
29
|
+
password: string;
|
|
30
|
+
}): Promise<any>;
|
|
31
|
+
updateUser(userId: number, data: {
|
|
32
|
+
name?: string;
|
|
33
|
+
email?: string;
|
|
34
|
+
}): Promise<any>;
|
|
35
|
+
deleteUser(userId: number): Promise<any>;
|
|
36
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
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 __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var _a;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.UserService = void 0;
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const core_sdk_1 = require("@xfilecom/core-sdk");
|
|
16
|
+
const users_schema_1 = require("./schema/users.schema");
|
|
17
|
+
let UserService = class UserService {
|
|
18
|
+
constructor(dbQuery) {
|
|
19
|
+
this.dbQuery = dbQuery;
|
|
20
|
+
}
|
|
21
|
+
async exampleSelect() {
|
|
22
|
+
const allUsers = await this.dbQuery.select('users', []);
|
|
23
|
+
const userFields = await this.dbQuery.select('users', ['id', 'email', 'name']);
|
|
24
|
+
const searchResult = await this.dbQuery.select('users', ['id', 'email', 'name'], {
|
|
25
|
+
search: [
|
|
26
|
+
{ keyword: 'test', fields: ['email', 'name'] },
|
|
27
|
+
{ keyword: 'admin', fields: ['role'] },
|
|
28
|
+
],
|
|
29
|
+
sort: [
|
|
30
|
+
{ field: 'createdAt', order: 'desc' },
|
|
31
|
+
{ field: 'id', order: 'asc' },
|
|
32
|
+
],
|
|
33
|
+
pagination: {
|
|
34
|
+
page: 1,
|
|
35
|
+
limit: 10,
|
|
36
|
+
},
|
|
37
|
+
});
|
|
38
|
+
const activeUsers = await this.dbQuery.select('users', ['id', 'email'], {
|
|
39
|
+
where: (0, core_sdk_1.eq)(users_schema_1.users.status, 'active'),
|
|
40
|
+
pagination: {
|
|
41
|
+
page: 1,
|
|
42
|
+
limit: 20,
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
const complexQuery = await this.dbQuery.select('users', [], {
|
|
46
|
+
where: (0, core_sdk_1.and)((0, core_sdk_1.eq)(users_schema_1.users.status, 'active'), (0, core_sdk_1.or)((0, core_sdk_1.eq)(users_schema_1.users.role, 'admin'), (0, core_sdk_1.eq)(users_schema_1.users.role, 'moderator'))),
|
|
47
|
+
sort: [{ field: 'createdAt', order: 'desc' }],
|
|
48
|
+
});
|
|
49
|
+
return {
|
|
50
|
+
allUsers,
|
|
51
|
+
userFields,
|
|
52
|
+
searchResult,
|
|
53
|
+
activeUsers,
|
|
54
|
+
complexQuery,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
async exampleInsert() {
|
|
58
|
+
const newUser1 = await this.dbQuery.insert('users', [], {
|
|
59
|
+
values: {
|
|
60
|
+
email: 'user1@example.com',
|
|
61
|
+
name: 'User 1',
|
|
62
|
+
password: 'hashed_password',
|
|
63
|
+
role: 'user',
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
const newUser2 = await this.dbQuery.insert('users', ['email', 'name', 'password'], {
|
|
67
|
+
values: {
|
|
68
|
+
email: 'user2@example.com',
|
|
69
|
+
name: 'User 2',
|
|
70
|
+
password: 'hashed_password',
|
|
71
|
+
role: 'admin',
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
return { newUser1, newUser2 };
|
|
75
|
+
}
|
|
76
|
+
async exampleUpdate() {
|
|
77
|
+
const updated1 = await this.dbQuery.update('users', [], {
|
|
78
|
+
values: {
|
|
79
|
+
name: 'Updated Name',
|
|
80
|
+
status: 'inactive',
|
|
81
|
+
},
|
|
82
|
+
where: (0, core_sdk_1.eq)(users_schema_1.users.id, 1),
|
|
83
|
+
});
|
|
84
|
+
const updated2 = await this.dbQuery.update('users', ['name'], {
|
|
85
|
+
values: {
|
|
86
|
+
name: 'New Name',
|
|
87
|
+
email: 'new@example.com',
|
|
88
|
+
},
|
|
89
|
+
where: (0, core_sdk_1.eq)(users_schema_1.users.id, 2),
|
|
90
|
+
});
|
|
91
|
+
const updated3 = await this.dbQuery.update('users', ['status'], {
|
|
92
|
+
values: {
|
|
93
|
+
status: 'inactive',
|
|
94
|
+
},
|
|
95
|
+
where: (0, core_sdk_1.and)((0, core_sdk_1.eq)(users_schema_1.users.role, 'user'), (0, core_sdk_1.eq)(users_schema_1.users.status, 'active')),
|
|
96
|
+
});
|
|
97
|
+
return { updated1, updated2, updated3 };
|
|
98
|
+
}
|
|
99
|
+
async exampleDelete() {
|
|
100
|
+
const deleted1 = await this.dbQuery.delete('users', [], {
|
|
101
|
+
where: (0, core_sdk_1.eq)(users_schema_1.users.id, 1),
|
|
102
|
+
});
|
|
103
|
+
const deleted2 = await this.dbQuery.delete('users', [], {
|
|
104
|
+
where: (0, core_sdk_1.and)((0, core_sdk_1.eq)(users_schema_1.users.status, 'inactive'), (0, core_sdk_1.eq)(users_schema_1.users.role, 'user')),
|
|
105
|
+
});
|
|
106
|
+
return { deleted1, deleted2 };
|
|
107
|
+
}
|
|
108
|
+
async searchUsers(keyword, page = 1, limit = 10) {
|
|
109
|
+
const result = await this.dbQuery.select('users', ['id', 'email', 'name', 'createdAt'], {
|
|
110
|
+
search: [
|
|
111
|
+
{ keyword, fields: ['email', 'name'] },
|
|
112
|
+
],
|
|
113
|
+
sort: [
|
|
114
|
+
{ field: 'createdAt', order: 'desc' },
|
|
115
|
+
],
|
|
116
|
+
pagination: {
|
|
117
|
+
page,
|
|
118
|
+
limit,
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
return result;
|
|
122
|
+
}
|
|
123
|
+
async createUser(data) {
|
|
124
|
+
const result = await this.dbQuery.insert('users', ['email', 'name', 'password'], {
|
|
125
|
+
values: data,
|
|
126
|
+
});
|
|
127
|
+
return result[0];
|
|
128
|
+
}
|
|
129
|
+
async updateUser(userId, data) {
|
|
130
|
+
const result = await this.dbQuery.update('users', [], {
|
|
131
|
+
values: data,
|
|
132
|
+
where: (0, core_sdk_1.eq)(users_schema_1.users.id, userId),
|
|
133
|
+
});
|
|
134
|
+
return result[0];
|
|
135
|
+
}
|
|
136
|
+
async deleteUser(userId) {
|
|
137
|
+
const result = await this.dbQuery.delete('users', [], {
|
|
138
|
+
where: (0, core_sdk_1.eq)(users_schema_1.users.id, userId),
|
|
139
|
+
});
|
|
140
|
+
return result[0];
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
exports.UserService = UserService;
|
|
144
|
+
exports.UserService = UserService = __decorate([
|
|
145
|
+
(0, common_1.Injectable)(),
|
|
146
|
+
__metadata("design:paramtypes", [typeof (_a = typeof core_sdk_1.DatabaseQuery !== "undefined" && core_sdk_1.DatabaseQuery) === "function" ? _a : Object])
|
|
147
|
+
], UserService);
|
|
148
|
+
//# sourceMappingURL=database.query.example.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database.query.example.js","sourceRoot":"","sources":["../../src/database/database.query.example.ts"],"names":[],"mappings":";;;;;;;;;;;;;AAMA,2CAA4C;AAC5C,iDAAgE;AAChE,wDAA8C;AAGvC,IAAM,WAAW,GAAjB,MAAM,WAAW;IACtB,YAA6B,OAAsB;QAAtB,YAAO,GAAP,OAAO,CAAe;IAAG,CAAC;IAKvD,KAAK,CAAC,aAAa;QAEjB,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;QAGxD,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;QAG/E,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE;YAC/E,MA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