@ciscode/database-kit 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.
Files changed (56) hide show
  1. package/CHANGELOG.md +206 -0
  2. package/LICENSE +21 -0
  3. package/README.md +446 -0
  4. package/dist/adapters/mongo.adapter.d.ts +44 -0
  5. package/dist/adapters/mongo.adapter.d.ts.map +1 -0
  6. package/dist/adapters/mongo.adapter.js +146 -0
  7. package/dist/adapters/mongo.adapter.js.map +1 -0
  8. package/dist/adapters/postgres.adapter.d.ts +49 -0
  9. package/dist/adapters/postgres.adapter.d.ts.map +1 -0
  10. package/dist/adapters/postgres.adapter.js +229 -0
  11. package/dist/adapters/postgres.adapter.js.map +1 -0
  12. package/dist/config/database.config.d.ts +55 -0
  13. package/dist/config/database.config.d.ts.map +1 -0
  14. package/dist/config/database.config.js +122 -0
  15. package/dist/config/database.config.js.map +1 -0
  16. package/dist/config/database.constants.d.ts +41 -0
  17. package/dist/config/database.constants.d.ts.map +1 -0
  18. package/dist/config/database.constants.js +45 -0
  19. package/dist/config/database.constants.js.map +1 -0
  20. package/dist/contracts/database.contracts.d.ts +191 -0
  21. package/dist/contracts/database.contracts.d.ts.map +1 -0
  22. package/dist/contracts/database.contracts.js +21 -0
  23. package/dist/contracts/database.contracts.js.map +1 -0
  24. package/dist/database-kit.module.d.ts +71 -0
  25. package/dist/database-kit.module.d.ts.map +1 -0
  26. package/dist/database-kit.module.js +158 -0
  27. package/dist/database-kit.module.js.map +1 -0
  28. package/dist/filters/database-exception.filter.d.ts +51 -0
  29. package/dist/filters/database-exception.filter.d.ts.map +1 -0
  30. package/dist/filters/database-exception.filter.js +236 -0
  31. package/dist/filters/database-exception.filter.js.map +1 -0
  32. package/dist/index.d.ts +19 -0
  33. package/dist/index.d.ts.map +1 -0
  34. package/dist/index.js +78 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/middleware/database.decorators.d.ts +31 -0
  37. package/dist/middleware/database.decorators.d.ts.map +1 -0
  38. package/dist/middleware/database.decorators.js +39 -0
  39. package/dist/middleware/database.decorators.js.map +1 -0
  40. package/dist/services/database.service.d.ts +99 -0
  41. package/dist/services/database.service.d.ts.map +1 -0
  42. package/dist/services/database.service.js +205 -0
  43. package/dist/services/database.service.js.map +1 -0
  44. package/dist/services/logger.service.d.ts +63 -0
  45. package/dist/services/logger.service.d.ts.map +1 -0
  46. package/dist/services/logger.service.js +93 -0
  47. package/dist/services/logger.service.js.map +1 -0
  48. package/dist/utils/pagination.utils.d.ts +57 -0
  49. package/dist/utils/pagination.utils.d.ts.map +1 -0
  50. package/dist/utils/pagination.utils.js +113 -0
  51. package/dist/utils/pagination.utils.js.map +1 -0
  52. package/dist/utils/validation.utils.d.ts +60 -0
  53. package/dist/utils/validation.utils.d.ts.map +1 -0
  54. package/dist/utils/validation.utils.js +117 -0
  55. package/dist/utils/validation.utils.js.map +1 -0
  56. package/package.json +83 -0
@@ -0,0 +1,205 @@
1
+ "use strict";
2
+ // src/services/database.service.ts
3
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
4
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
5
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
6
+ 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;
7
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
8
+ };
9
+ var __metadata = (this && this.__metadata) || function (k, v) {
10
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
11
+ };
12
+ var DatabaseService_1;
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.DatabaseService = void 0;
15
+ const common_1 = require("@nestjs/common");
16
+ const mongo_adapter_1 = require("../adapters/mongo.adapter");
17
+ const postgres_adapter_1 = require("../adapters/postgres.adapter");
18
+ /**
19
+ * Main database service that provides a unified interface
20
+ * for database operations across MongoDB and PostgreSQL.
21
+ *
22
+ * This service acts as a facade over the underlying adapters,
23
+ * providing a clean API for creating repositories and managing connections.
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * // In a NestJS service
28
+ * @Injectable()
29
+ * export class UserService {
30
+ * private readonly usersRepo: Repository<User>;
31
+ *
32
+ * constructor(@InjectDatabase() private readonly db: DatabaseService) {
33
+ * this.usersRepo = db.createMongoRepository({ model: UserModel });
34
+ * }
35
+ * }
36
+ * ```
37
+ */
38
+ let DatabaseService = DatabaseService_1 = class DatabaseService {
39
+ constructor(config) {
40
+ this.logger = new common_1.Logger(DatabaseService_1.name);
41
+ this.config = config;
42
+ this.logger.log(`DatabaseService initialized with type: ${config.type}`);
43
+ }
44
+ /**
45
+ * Lifecycle hook called when the module is being destroyed.
46
+ * Gracefully closes all database connections.
47
+ */
48
+ async onModuleDestroy() {
49
+ this.logger.log('Cleaning up database connections...');
50
+ await this.disconnect();
51
+ }
52
+ /**
53
+ * Returns the current database type.
54
+ */
55
+ get type() {
56
+ return this.config.type;
57
+ }
58
+ /**
59
+ * Checks if the database is connected.
60
+ */
61
+ isConnected() {
62
+ var _a, _b, _c, _d;
63
+ switch (this.config.type) {
64
+ case 'mongo':
65
+ return (_b = (_a = this.mongoAdapter) === null || _a === void 0 ? void 0 : _a.isConnected()) !== null && _b !== void 0 ? _b : false;
66
+ case 'postgres':
67
+ return (_d = (_c = this.postgresAdapter) === null || _c === void 0 ? void 0 : _c.isConnected()) !== null && _d !== void 0 ? _d : false;
68
+ default:
69
+ return false;
70
+ }
71
+ }
72
+ /**
73
+ * Initializes the underlying driver connection.
74
+ * Must be awaited before using repositories.
75
+ */
76
+ async connect() {
77
+ switch (this.config.type) {
78
+ case 'mongo': {
79
+ if (!this.mongoAdapter) {
80
+ this.mongoAdapter = new mongo_adapter_1.MongoAdapter(this.config);
81
+ }
82
+ await this.mongoAdapter.connect();
83
+ this.logger.log('MongoDB connection established');
84
+ break;
85
+ }
86
+ case 'postgres': {
87
+ if (!this.postgresAdapter) {
88
+ this.postgresAdapter = new postgres_adapter_1.PostgresAdapter(this.config);
89
+ }
90
+ this.postgresAdapter.connect();
91
+ this.logger.log('PostgreSQL connection pool established');
92
+ break;
93
+ }
94
+ default: {
95
+ // TypeScript exhaustiveness check - this should never happen
96
+ const exhaustiveCheck = this.config;
97
+ throw new Error(`Unsupported database type: ${exhaustiveCheck.type}`);
98
+ }
99
+ }
100
+ }
101
+ /**
102
+ * Gracefully disconnects from the database.
103
+ */
104
+ async disconnect() {
105
+ try {
106
+ if (this.mongoAdapter) {
107
+ await this.mongoAdapter.disconnect();
108
+ this.mongoAdapter = undefined;
109
+ }
110
+ if (this.postgresAdapter) {
111
+ await this.postgresAdapter.disconnect();
112
+ this.postgresAdapter = undefined;
113
+ }
114
+ this.logger.log('All database connections closed');
115
+ }
116
+ catch (error) {
117
+ this.logger.error('Error during database disconnect', error);
118
+ throw error;
119
+ }
120
+ }
121
+ /**
122
+ * Creates a MongoDB repository using a Mongoose model.
123
+ *
124
+ * @param options - Options containing the Mongoose model
125
+ * @returns Repository instance with CRUD methods
126
+ * @throws Error if database type is not 'mongo'
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * const usersRepo = db.createMongoRepository<User>({ model: UserModel });
131
+ * const user = await usersRepo.create({ name: 'John' });
132
+ * ```
133
+ */
134
+ createMongoRepository(options) {
135
+ if (this.config.type !== 'mongo') {
136
+ throw new Error(`Database type is "${this.config.type}". createMongoRepository can only be used when type === "mongo".`);
137
+ }
138
+ if (!this.mongoAdapter) {
139
+ this.mongoAdapter = new mongo_adapter_1.MongoAdapter(this.config);
140
+ }
141
+ return this.mongoAdapter.createRepository(options);
142
+ }
143
+ /**
144
+ * Creates a PostgreSQL repository using a table configuration.
145
+ *
146
+ * @param cfg - Configuration for the entity/table
147
+ * @returns Repository instance with CRUD methods
148
+ * @throws Error if database type is not 'postgres'
149
+ *
150
+ * @example
151
+ * ```typescript
152
+ * const ordersRepo = db.createPostgresRepository<Order>({
153
+ * table: 'orders',
154
+ * primaryKey: 'id',
155
+ * columns: ['id', 'user_id', 'total', 'created_at'],
156
+ * });
157
+ * ```
158
+ */
159
+ createPostgresRepository(cfg) {
160
+ if (this.config.type !== 'postgres') {
161
+ throw new Error(`Database type is "${this.config.type}". createPostgresRepository can only be used when type === "postgres".`);
162
+ }
163
+ if (!this.postgresAdapter) {
164
+ this.postgresAdapter = new postgres_adapter_1.PostgresAdapter(this.config);
165
+ this.postgresAdapter.connect();
166
+ }
167
+ return this.postgresAdapter.createRepository(cfg);
168
+ }
169
+ /**
170
+ * Returns the underlying MongoDB adapter.
171
+ * Useful for advanced operations not covered by the repository interface.
172
+ *
173
+ * @throws Error if database type is not 'mongo'
174
+ */
175
+ getMongoAdapter() {
176
+ if (this.config.type !== 'mongo') {
177
+ throw new Error('getMongoAdapter() is only available for MongoDB connections');
178
+ }
179
+ if (!this.mongoAdapter) {
180
+ this.mongoAdapter = new mongo_adapter_1.MongoAdapter(this.config);
181
+ }
182
+ return this.mongoAdapter;
183
+ }
184
+ /**
185
+ * Returns the underlying PostgreSQL adapter.
186
+ * Useful for advanced operations not covered by the repository interface.
187
+ *
188
+ * @throws Error if database type is not 'postgres'
189
+ */
190
+ getPostgresAdapter() {
191
+ if (this.config.type !== 'postgres') {
192
+ throw new Error('getPostgresAdapter() is only available for PostgreSQL connections');
193
+ }
194
+ if (!this.postgresAdapter) {
195
+ this.postgresAdapter = new postgres_adapter_1.PostgresAdapter(this.config);
196
+ }
197
+ return this.postgresAdapter;
198
+ }
199
+ };
200
+ exports.DatabaseService = DatabaseService;
201
+ exports.DatabaseService = DatabaseService = DatabaseService_1 = __decorate([
202
+ (0, common_1.Injectable)(),
203
+ __metadata("design:paramtypes", [Object])
204
+ ], DatabaseService);
205
+ //# sourceMappingURL=database.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database.service.js","sourceRoot":"","sources":["../../src/services/database.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C;QAC7E,CAAC;QAED,OAAO,IAAI,CAAC,YAAY,CAAC;IAC7B,CAAC;IAED;;;;;OAKG;IACH,kBAAkB;QACd,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;YAClC,MAAM,IAAI,KAAK,CAAC,mEAAmE,CAAC,CAAC;QACzF,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,eAAe,EAAE,CAAC;YACxB,IAAI,CAAC,eAAe,GAAG,IAAI,kCAAe,CAAC,IAAI,CAAC,MAAgC,CAAC,CAAC;QACtF,CAAC;QAED,OAAO,IAAI,CAAC,eAAe,CAAC;IAChC,CAAC;CACJ,CAAA;AA7LY,0CAAe;0BAAf,eAAe;IAD3B,IAAA,mBAAU,GAAE;;GACA,eAAe,CA6L3B"}
@@ -0,0 +1,63 @@
1
+ import { LogLevel } from '@nestjs/common';
2
+ /**
3
+ * Centralized logging service for DatabaseKit.
4
+ * Provides structured logging with context support.
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * @Injectable()
9
+ * export class MyService {
10
+ * constructor(private readonly logger: LoggerService) {}
11
+ *
12
+ * doSomething() {
13
+ * this.logger.log('Operation started', 'MyService');
14
+ * }
15
+ * }
16
+ * ```
17
+ */
18
+ export declare class LoggerService {
19
+ private readonly logger;
20
+ /**
21
+ * Logs a message at the 'log' level.
22
+ *
23
+ * @param message - The message to log
24
+ * @param context - Optional context (usually the class/method name)
25
+ */
26
+ log(message: string, context?: string): void;
27
+ /**
28
+ * Logs a message at the 'error' level.
29
+ *
30
+ * @param message - The error message
31
+ * @param trace - Optional stack trace
32
+ * @param context - Optional context (usually the class/method name)
33
+ */
34
+ error(message: string, trace?: string, context?: string): void;
35
+ /**
36
+ * Logs a message at the 'warn' level.
37
+ *
38
+ * @param message - The warning message
39
+ * @param context - Optional context (usually the class/method name)
40
+ */
41
+ warn(message: string, context?: string): void;
42
+ /**
43
+ * Logs a message at the 'debug' level.
44
+ *
45
+ * @param message - The debug message
46
+ * @param context - Optional context (usually the class/method name)
47
+ */
48
+ debug(message: string, context?: string): void;
49
+ /**
50
+ * Logs a message at the 'verbose' level.
51
+ *
52
+ * @param message - The verbose message
53
+ * @param context - Optional context (usually the class/method name)
54
+ */
55
+ verbose(message: string, context?: string): void;
56
+ /**
57
+ * Sets the log levels to display.
58
+ *
59
+ * @param levels - Array of log levels to enable
60
+ */
61
+ setLogLevels(levels: LogLevel[]): void;
62
+ }
63
+ //# sourceMappingURL=logger.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.service.d.ts","sourceRoot":"","sources":["../../src/services/logger.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAsB,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAE9D;;;;;;;;;;;;;;;GAeG;AACH,qBACa,aAAa;IACtB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA6B;IAEpD;;;;;OAKG;IACH,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI;IAI5C;;;;;;OAMG;IACH,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI;IAI9D;;;;;OAKG;IACH,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI;IAI7C;;;;;OAKG;IACH,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI;IAI9C;;;;;OAKG;IACH,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI;IAIhD;;;;OAIG;IACH,YAAY,CAAC,MAAM,EAAE,QAAQ,EAAE,GAAG,IAAI;CAGzC"}
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ // src/services/logger.service.ts
3
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
4
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
5
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
6
+ 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;
7
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.LoggerService = void 0;
11
+ const common_1 = require("@nestjs/common");
12
+ /**
13
+ * Centralized logging service for DatabaseKit.
14
+ * Provides structured logging with context support.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * @Injectable()
19
+ * export class MyService {
20
+ * constructor(private readonly logger: LoggerService) {}
21
+ *
22
+ * doSomething() {
23
+ * this.logger.log('Operation started', 'MyService');
24
+ * }
25
+ * }
26
+ * ```
27
+ */
28
+ let LoggerService = class LoggerService {
29
+ constructor() {
30
+ this.logger = new common_1.Logger('DatabaseKit');
31
+ }
32
+ /**
33
+ * Logs a message at the 'log' level.
34
+ *
35
+ * @param message - The message to log
36
+ * @param context - Optional context (usually the class/method name)
37
+ */
38
+ log(message, context) {
39
+ this.logger.log(message, context);
40
+ }
41
+ /**
42
+ * Logs a message at the 'error' level.
43
+ *
44
+ * @param message - The error message
45
+ * @param trace - Optional stack trace
46
+ * @param context - Optional context (usually the class/method name)
47
+ */
48
+ error(message, trace, context) {
49
+ this.logger.error(message, trace, context);
50
+ }
51
+ /**
52
+ * Logs a message at the 'warn' level.
53
+ *
54
+ * @param message - The warning message
55
+ * @param context - Optional context (usually the class/method name)
56
+ */
57
+ warn(message, context) {
58
+ this.logger.warn(message, context);
59
+ }
60
+ /**
61
+ * Logs a message at the 'debug' level.
62
+ *
63
+ * @param message - The debug message
64
+ * @param context - Optional context (usually the class/method name)
65
+ */
66
+ debug(message, context) {
67
+ var _a, _b;
68
+ (_b = (_a = this.logger).debug) === null || _b === void 0 ? void 0 : _b.call(_a, message, context);
69
+ }
70
+ /**
71
+ * Logs a message at the 'verbose' level.
72
+ *
73
+ * @param message - The verbose message
74
+ * @param context - Optional context (usually the class/method name)
75
+ */
76
+ verbose(message, context) {
77
+ var _a, _b;
78
+ (_b = (_a = this.logger).verbose) === null || _b === void 0 ? void 0 : _b.call(_a, message, context);
79
+ }
80
+ /**
81
+ * Sets the log levels to display.
82
+ *
83
+ * @param levels - Array of log levels to enable
84
+ */
85
+ setLogLevels(levels) {
86
+ common_1.Logger.overrideLogger(levels);
87
+ }
88
+ };
89
+ exports.LoggerService = LoggerService;
90
+ exports.LoggerService = LoggerService = __decorate([
91
+ (0, common_1.Injectable)()
92
+ ], LoggerService);
93
+ //# sourceMappingURL=logger.service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.service.js","sourceRoot":"","sources":["../../src/services/logger.service.ts"],"names":[],"mappings":";AAAA,iCAAiC;;;;;;;;;AAEjC,2CAA8D;AAE9D;;;;;;;;;;;;;;;GAeG;AAEI,IAAM,aAAa,GAAnB,MAAM,aAAa;IAAnB;QACc,WAAM,GAAG,IAAI,eAAM,CAAC,aAAa,CAAC,CAAC;IA6DxD,CAAC;IA3DG;;;;;OAKG;IACH,GAAG,CAAC,OAAe,EAAE,OAAgB;QACjC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACtC,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,OAAe,EAAE,KAAc,EAAE,OAAgB;QACnD,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;IAC/C,CAAC;IAED;;;;;OAKG;IACH,IAAI,CAAC,OAAe,EAAE,OAAgB;QAClC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IACvC,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,OAAe,EAAE,OAAgB;;QACnC,MAAA,MAAA,IAAI,CAAC,MAAM,EAAC,KAAK,mDAAG,OAAO,EAAE,OAAO,CAAC,CAAC;IAC1C,CAAC;IAED;;;;;OAKG;IACH,OAAO,CAAC,OAAe,EAAE,OAAgB;;QACrC,MAAA,MAAA,IAAI,CAAC,MAAM,EAAC,OAAO,mDAAG,OAAO,EAAE,OAAO,CAAC,CAAC;IAC5C,CAAC;IAED;;;;OAIG;IACH,YAAY,CAAC,MAAkB;QAC3B,eAAM,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;IAClC,CAAC;CACJ,CAAA;AA9DY,sCAAa;wBAAb,aAAa;IADzB,IAAA,mBAAU,GAAE;GACA,aAAa,CA8DzB"}
@@ -0,0 +1,57 @@
1
+ import { PageOptions, PageResult } from '../contracts/database.contracts';
2
+ /**
3
+ * Utility functions for pagination operations.
4
+ */
5
+ /**
6
+ * Normalizes pagination options with defaults and constraints.
7
+ *
8
+ * @param options - The input pagination options
9
+ * @returns Normalized options with validated values
10
+ */
11
+ export declare function normalizePaginationOptions<T = Record<string, unknown>>(options?: PageOptions<T>): Required<PageOptions<T>>;
12
+ /**
13
+ * Calculates pagination metadata from total count.
14
+ *
15
+ * @param total - Total number of items
16
+ * @param page - Current page number
17
+ * @param limit - Items per page
18
+ * @returns Pagination metadata
19
+ */
20
+ export declare function calculatePagination(total: number, page: number, limit: number): {
21
+ pages: number;
22
+ hasNext: boolean;
23
+ hasPrev: boolean;
24
+ };
25
+ /**
26
+ * Creates a page result object.
27
+ *
28
+ * @param data - Array of items for the current page
29
+ * @param page - Current page number
30
+ * @param limit - Items per page
31
+ * @param total - Total number of items
32
+ * @returns Complete page result
33
+ */
34
+ export declare function createPageResult<T>(data: T[], page: number, limit: number, total: number): PageResult<T>;
35
+ /**
36
+ * Parses a sort string into an object.
37
+ * Supports formats like "-createdAt,name" or "+updatedAt,-title"
38
+ *
39
+ * @param sortString - Comma-separated sort fields with optional +/- prefix
40
+ * @returns Object with field names as keys and 'asc' | 'desc' as values
41
+ *
42
+ * @example
43
+ * ```typescript
44
+ * parseSortString('-createdAt,name');
45
+ * // Returns: { createdAt: 'desc', name: 'asc' }
46
+ * ```
47
+ */
48
+ export declare function parseSortString(sortString: string): Record<string, 'asc' | 'desc'>;
49
+ /**
50
+ * Calculates the offset for a given page and limit.
51
+ *
52
+ * @param page - Page number (1-indexed)
53
+ * @param limit - Items per page
54
+ * @returns Offset value for database query
55
+ */
56
+ export declare function calculateOffset(page: number, limit: number): number;
57
+ //# sourceMappingURL=pagination.utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pagination.utils.d.ts","sourceRoot":"","sources":["../../src/utils/pagination.utils.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,UAAU,EAA0B,MAAM,iCAAiC,CAAC;AAElG;;GAEG;AAEH;;;;;GAKG;AACH,wBAAgB,0BAA0B,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACpE,OAAO,GAAE,WAAW,CAAC,CAAC,CAAM,GAC3B,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,CAmB1B;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,GACZ;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,OAAO,CAAA;CAAE,CAMvD;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAChC,IAAI,EAAE,CAAC,EAAE,EACT,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,GACZ,UAAU,CAAC,CAAC,CAAC,CAUf;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,eAAe,CAC7B,UAAU,EAAE,MAAM,GACjB,MAAM,CAAC,MAAM,EAAE,KAAK,GAAG,MAAM,CAAC,CAkBhC;AAED;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAEnE"}
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ // src/utils/pagination.utils.ts
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.normalizePaginationOptions = normalizePaginationOptions;
5
+ exports.calculatePagination = calculatePagination;
6
+ exports.createPageResult = createPageResult;
7
+ exports.parseSortString = parseSortString;
8
+ exports.calculateOffset = calculateOffset;
9
+ const database_contracts_1 = require("../contracts/database.contracts");
10
+ /**
11
+ * Utility functions for pagination operations.
12
+ */
13
+ /**
14
+ * Normalizes pagination options with defaults and constraints.
15
+ *
16
+ * @param options - The input pagination options
17
+ * @returns Normalized options with validated values
18
+ */
19
+ function normalizePaginationOptions(options = {}) {
20
+ var _a, _b, _c, _d;
21
+ const { DEFAULT_PAGE_SIZE, MAX_PAGE_SIZE } = database_contracts_1.DATABASE_KIT_CONSTANTS;
22
+ let page = (_a = options.page) !== null && _a !== void 0 ? _a : 1;
23
+ let limit = (_b = options.limit) !== null && _b !== void 0 ? _b : DEFAULT_PAGE_SIZE;
24
+ // Ensure page is at least 1
25
+ if (page < 1)
26
+ page = 1;
27
+ // Ensure limit is within bounds
28
+ if (limit < 1)
29
+ limit = 1;
30
+ if (limit > MAX_PAGE_SIZE)
31
+ limit = MAX_PAGE_SIZE;
32
+ return {
33
+ filter: (_c = options.filter) !== null && _c !== void 0 ? _c : {},
34
+ page,
35
+ limit,
36
+ sort: (_d = options.sort) !== null && _d !== void 0 ? _d : {},
37
+ };
38
+ }
39
+ /**
40
+ * Calculates pagination metadata from total count.
41
+ *
42
+ * @param total - Total number of items
43
+ * @param page - Current page number
44
+ * @param limit - Items per page
45
+ * @returns Pagination metadata
46
+ */
47
+ function calculatePagination(total, page, limit) {
48
+ const pages = Math.max(1, Math.ceil(total / limit));
49
+ const hasNext = page < pages;
50
+ const hasPrev = page > 1;
51
+ return { pages, hasNext, hasPrev };
52
+ }
53
+ /**
54
+ * Creates a page result object.
55
+ *
56
+ * @param data - Array of items for the current page
57
+ * @param page - Current page number
58
+ * @param limit - Items per page
59
+ * @param total - Total number of items
60
+ * @returns Complete page result
61
+ */
62
+ function createPageResult(data, page, limit, total) {
63
+ const pages = Math.max(1, Math.ceil(total / limit));
64
+ return {
65
+ data,
66
+ page,
67
+ limit,
68
+ total,
69
+ pages,
70
+ };
71
+ }
72
+ /**
73
+ * Parses a sort string into an object.
74
+ * Supports formats like "-createdAt,name" or "+updatedAt,-title"
75
+ *
76
+ * @param sortString - Comma-separated sort fields with optional +/- prefix
77
+ * @returns Object with field names as keys and 'asc' | 'desc' as values
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * parseSortString('-createdAt,name');
82
+ * // Returns: { createdAt: 'desc', name: 'asc' }
83
+ * ```
84
+ */
85
+ function parseSortString(sortString) {
86
+ const result = {};
87
+ if (!sortString)
88
+ return result;
89
+ const fields = sortString.split(',').map((f) => f.trim()).filter(Boolean);
90
+ for (const field of fields) {
91
+ if (field.startsWith('-')) {
92
+ result[field.slice(1)] = 'desc';
93
+ }
94
+ else if (field.startsWith('+')) {
95
+ result[field.slice(1)] = 'asc';
96
+ }
97
+ else {
98
+ result[field] = 'asc';
99
+ }
100
+ }
101
+ return result;
102
+ }
103
+ /**
104
+ * Calculates the offset for a given page and limit.
105
+ *
106
+ * @param page - Page number (1-indexed)
107
+ * @param limit - Items per page
108
+ * @returns Offset value for database query
109
+ */
110
+ function calculateOffset(page, limit) {
111
+ return Math.max(0, (page - 1) * limit);
112
+ }
113
+ //# sourceMappingURL=pagination.utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pagination.utils.js","sourceRoot":"","sources":["../../src/utils/pagination.utils.ts"],"names":[],"mappings":";AAAA,gCAAgC;;AAchC,gEAqBC;AAUD,kDAUC;AAWD,4CAeC;AAeD,0CAoBC;AASD,0CAEC;AA7HD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AC,CAAC;IAEpD,OAAO;QACL,IAAI;QACJ,IAAI;QACJ,KAAK;QACL,KAAK;QACL,KAAK;KACN,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,eAAe,CAC7B,UAAkB;IAElB,MAAM,MAAM,GAAmC,EAAE,CAAC;IAElD,IAAI,CAAC,UAAU;QAAE,OAAO,MAAM,CAAC;IAE/B,MAAM,MAAM,GAAG,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAE1E,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;QAC3B,IAAI,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YAC1B,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC;QAClC,CAAC;aAAM,IAAI,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YACjC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;QACjC,CAAC;aAAM,CAAC;YACN,MAAM,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC;QACxB,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,eAAe,CAAC,IAAY,EAAE,KAAa;IACzD,OAAO,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC;AACzC,CAAC"}
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Utility functions for validation operations.
3
+ */
4
+ /**
5
+ * Validates that a value is a valid MongoDB ObjectId string.
6
+ *
7
+ * @param id - The string to validate
8
+ * @returns True if valid ObjectId format
9
+ */
10
+ export declare function isValidMongoId(id: string): boolean;
11
+ /**
12
+ * Validates that a value is a valid UUID (v4).
13
+ *
14
+ * @param id - The string to validate
15
+ * @returns True if valid UUID format
16
+ */
17
+ export declare function isValidUuid(id: string): boolean;
18
+ /**
19
+ * Validates that a value is a positive integer.
20
+ *
21
+ * @param value - The value to validate
22
+ * @returns True if positive integer
23
+ */
24
+ export declare function isPositiveInteger(value: unknown): boolean;
25
+ /**
26
+ * Sanitizes a filter object by removing undefined and null values.
27
+ *
28
+ * @param filter - The filter object to sanitize
29
+ * @returns Sanitized filter object
30
+ */
31
+ export declare function sanitizeFilter<T extends Record<string, unknown>>(filter: T): Partial<T>;
32
+ /**
33
+ * Validates that all required fields are present in an object.
34
+ *
35
+ * @param obj - The object to validate
36
+ * @param requiredFields - Array of required field names
37
+ * @returns Object with isValid boolean and missing fields array
38
+ */
39
+ export declare function validateRequiredFields(obj: Record<string, unknown>, requiredFields: string[]): {
40
+ isValid: boolean;
41
+ missing: string[];
42
+ };
43
+ /**
44
+ * Extracts only allowed fields from an object.
45
+ * Useful for preventing mass assignment vulnerabilities.
46
+ *
47
+ * @param obj - The source object
48
+ * @param allowedFields - Array of allowed field names
49
+ * @returns New object with only allowed fields
50
+ */
51
+ export declare function pickFields<T extends Record<string, unknown>>(obj: T, allowedFields: string[]): Partial<T>;
52
+ /**
53
+ * Omits specified fields from an object.
54
+ *
55
+ * @param obj - The source object
56
+ * @param fieldsToOmit - Array of field names to omit
57
+ * @returns New object without the omitted fields
58
+ */
59
+ export declare function omitFields<T extends Record<string, unknown>>(obj: T, fieldsToOmit: string[]): Partial<T>;
60
+ //# sourceMappingURL=validation.utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.utils.d.ts","sourceRoot":"","sources":["../../src/utils/validation.utils.ts"],"names":[],"mappings":"AAEA;;GAEG;AAEH;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAGlD;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAG/C;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CASzD;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC9D,MAAM,EAAE,CAAC,GACR,OAAO,CAAC,CAAC,CAAC,CAUZ;AAED;;;;;;GAMG;AACH,wBAAgB,sBAAsB,CACpC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC5B,cAAc,EAAE,MAAM,EAAE,GACvB;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,OAAO,EAAE,MAAM,EAAE,CAAA;CAAE,CAazC;AAED;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC1D,GAAG,EAAE,CAAC,EACN,aAAa,EAAE,MAAM,EAAE,GACtB,OAAO,CAAC,CAAC,CAAC,CAUZ;AAED;;;;;;GAMG;AACH,wBAAgB,UAAU,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC1D,GAAG,EAAE,CAAC,EACN,YAAY,EAAE,MAAM,EAAE,GACrB,OAAO,CAAC,CAAC,CAAC,CAQZ"}