@common-stack/store-mongo 8.0.2-alpha.0 → 8.1.1-alpha.13
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/LICENSE +34 -21
- package/lib/containers/container.d.ts +14 -0
- package/lib/containers/container.js +17 -0
- package/lib/containers/container.js.map +1 -0
- package/lib/containers/index.d.ts +1 -0
- package/lib/dataloaders/bulk-dataloader-v2.d.ts +40 -0
- package/lib/dataloaders/bulk-dataloader-v2.js +118 -0
- package/lib/dataloaders/bulk-dataloader-v2.js.map +1 -0
- package/lib/dataloaders/bulk-dataloader-v2.test.d.ts +1 -0
- package/lib/dataloaders/bulk-dataloader.d.ts +1 -1
- package/lib/dataloaders/bulk-dataloader.js.map +1 -1
- package/lib/dataloaders/bulk-dataloader.test.d.ts +1 -0
- package/lib/dataloaders/index.d.ts +1 -0
- package/lib/graphql/schema/base-services.graphql +238 -0
- package/lib/helpers/mongoose-connection.js.map +1 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.js +1 -1
- package/lib/interfaces/base-repository.d.ts +1 -5
- package/lib/interfaces/base-service.d.ts +1 -1
- package/lib/interfaces/getAllArgs.d.ts +135 -0
- package/lib/interfaces/getAllArgs.js +39 -0
- package/lib/interfaces/getAllArgs.js.map +1 -0
- package/lib/interfaces/index.d.ts +1 -4
- package/lib/interfaces/index.old.d.ts +3 -0
- package/lib/mixins/BaseServiceMixin.d.ts +45 -0
- package/lib/mixins/BaseServiceMixin.js +212 -0
- package/lib/mixins/BaseServiceMixin.js.map +1 -0
- package/lib/mixins/__tests__/BaseServiceMixin.test.d.ts +1 -0
- package/lib/mixins/base-service-mixin.js.map +1 -1
- package/lib/mixins/index.d.ts +1 -0
- package/lib/module.d.ts +2 -0
- package/lib/module.js +4 -0
- package/lib/module.js.map +1 -0
- package/lib/services/BaseProxyService.d.ts +28 -0
- package/lib/services/BaseProxyService.js +52 -0
- package/lib/services/BaseProxyService.js.map +1 -0
- package/lib/services/BaseService.d.ts +23 -0
- package/lib/services/BaseService.js +65 -0
- package/lib/services/BaseService.js.map +1 -0
- package/lib/services/BaseService.test.d.ts +1 -0
- package/lib/services/ConnectionPoolManager.d.ts +54 -0
- package/lib/services/ConnectionPoolManager.js +163 -0
- package/lib/services/ConnectionPoolManager.js.map +1 -0
- package/lib/services/base-proxy-service.d.ts +2 -1
- package/lib/services/base-proxy-service.js.map +1 -1
- package/lib/services/base-service.d.ts +2 -1
- package/lib/services/base-service.js.map +1 -1
- package/lib/services/index.d.ts +3 -0
- package/lib/store/models/common-options-v2.d.ts +16 -0
- package/lib/store/models/common-options-v2.js +40 -0
- package/lib/store/models/common-options-v2.js.map +1 -0
- package/lib/store/models/common-options.js.map +1 -1
- package/lib/store/models/index.d.ts +1 -0
- package/lib/store/repositories/BaseMongoRepository.d.ts +72 -0
- package/lib/store/repositories/BaseMongoRepository.js +423 -0
- package/lib/store/repositories/BaseMongoRepository.js.map +1 -0
- package/lib/store/repositories/BaseMongoRepository.test.d.ts +1 -0
- package/lib/store/repositories/CustomIdRepository.test.d.ts +1 -0
- package/lib/store/repositories/base-repository.d.ts +2 -1
- package/lib/store/repositories/base-repository.js +1 -1
- package/lib/store/repositories/base-repository.js.map +1 -1
- package/lib/store/repositories/index.d.ts +1 -0
- package/lib/templates/constants/SERVER_TYPES.ts.template +7 -0
- package/lib/templates/repositories/DataLoader.ts.template +211 -0
- package/lib/templates/repositories/DatabaseMigration.ts.template +13 -0
- package/lib/templates/repositories/IBaseMongoRepository.ts.template +284 -0
- package/lib/templates/repositories/IBaseService.ts.template +231 -0
- package/lib/templates/repositories/IBaseServiceMixin.ts.template +477 -0
- package/lib/templates/repositories/dbCommonTypes.ts.template +140 -0
- package/lib/templates/repositories/moleculerEventHandler.ts.template.toberemoved +118 -0
- package/lib/templates/repositories/mongoCommonTypes.ts.template +21 -0
- package/lib/templates/repositories/typedMoleculerService.ts.template.toberemoved +1188 -0
- package/lib/templates/repositories/zodToMoleculer.ts.template.toberemoved +133 -0
- package/package.json +22 -5
- package/lib/interfaces/base-repository.js +0 -5
- package/lib/interfaces/base-repository.js.map +0 -1
- package/lib/interfaces/get-all-args.d.ts +0 -9
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
/**
|
|
3
|
+
* @file IDataLoader.ts
|
|
4
|
+
* @description Defines interfaces for implementing the DataLoader pattern to efficiently batch and cache database queries.
|
|
5
|
+
*
|
|
6
|
+
* The DataLoader pattern helps prevent the N+1 query problem by consolidating multiple individual requests
|
|
7
|
+
* into a single batch request. This significantly improves performance for nested queries and relationship
|
|
8
|
+
* traversal in GraphQL or REST APIs.
|
|
9
|
+
*
|
|
10
|
+
* Architecture Context:
|
|
11
|
+
* - Part of the data access optimization layer
|
|
12
|
+
* - Sits between repositories and services/resolvers
|
|
13
|
+
* - Implements request batching and caching for efficient data access
|
|
14
|
+
* - Particularly valuable for GraphQL implementations to solve the N+1 query problem
|
|
15
|
+
* - Works with MongoDB collections through Mongoose
|
|
16
|
+
*
|
|
17
|
+
* Key features:
|
|
18
|
+
* - Request batching: Combines multiple individual requests into a single database query
|
|
19
|
+
* - In-memory caching: Prevents duplicate requests for the same entity
|
|
20
|
+
* - Custom loading options: Supports flexible query criteria beyond simple ID lookups
|
|
21
|
+
* - Clear cache methods: Allows for cache invalidation when data changes
|
|
22
|
+
*
|
|
23
|
+
* Usage Patterns:
|
|
24
|
+
* - Create a DataLoader instance for each entity type in your application
|
|
25
|
+
* - Use the load method for single entity retrieval by ID
|
|
26
|
+
* - Use loadMany for retrieving multiple entities by their IDs
|
|
27
|
+
* - Use withOptions for more complex queries with custom criteria
|
|
28
|
+
* - Clear cache entries when entities are modified
|
|
29
|
+
*
|
|
30
|
+
* Performance Impact:
|
|
31
|
+
* - Reduces database round trips by batching requests
|
|
32
|
+
* - Minimizes redundant queries through caching
|
|
33
|
+
* - Particularly effective for nested relationship queries in GraphQL
|
|
34
|
+
* - Can dramatically improve response times for complex data graphs
|
|
35
|
+
*
|
|
36
|
+
* @see IBaseMongoRepository - Repository layer that provides the underlying data access
|
|
37
|
+
* @see AsDomainType - Type transformation for domain objects
|
|
38
|
+
*/
|
|
39
|
+
|
|
40
|
+
import DataLoader from 'dataloader';
|
|
41
|
+
import type { FilterQuery } from 'mongoose';
|
|
42
|
+
import { AsDomainType } from './dbCommonTypes';
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Options for configuring how entities are loaded through the DataLoader
|
|
46
|
+
*
|
|
47
|
+
* @property id - Unique identifier for the request (used for caching)
|
|
48
|
+
* @property searchKey - The field to search by (typically 'id' or another indexed field)
|
|
49
|
+
* @property comparator - Optional custom function to determine if an item matches the search criteria
|
|
50
|
+
* @property criteria - Optional additional filter criteria to apply to the query
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* // Basic options for loading by ID
|
|
54
|
+
* const options: DataLoaderOptions<UserSchema> = {
|
|
55
|
+
* id: '123',
|
|
56
|
+
* searchKey: 'id'
|
|
57
|
+
* };
|
|
58
|
+
*
|
|
59
|
+
* // Advanced options with custom criteria
|
|
60
|
+
* const options: DataLoaderOptions<UserSchema> = {
|
|
61
|
+
* id: 'active-admins',
|
|
62
|
+
* searchKey: 'role',
|
|
63
|
+
* criteria: { active: true, role: 'admin' },
|
|
64
|
+
* comparator: (opt, item) => item.role === 'admin' && item.active === true
|
|
65
|
+
* };
|
|
66
|
+
*/
|
|
67
|
+
export interface DataLoaderOptions<SchemaType> {
|
|
68
|
+
/** Unique identifier for the request (used for caching) */
|
|
69
|
+
id: string;
|
|
70
|
+
|
|
71
|
+
/** The field to search by (typically 'id' or another indexed field) */
|
|
72
|
+
searchKey: keyof AsDomainType<SchemaType> | string;
|
|
73
|
+
|
|
74
|
+
/** Optional custom function to determine if an item matches the search criteria */
|
|
75
|
+
comparator?: (option: DataLoaderOptions<SchemaType>, item: AsDomainType<SchemaType>) => boolean;
|
|
76
|
+
|
|
77
|
+
/** Optional additional filter criteria to apply to the query */
|
|
78
|
+
criteria?: FilterQuery<SchemaType>;
|
|
79
|
+
|
|
80
|
+
/** Additional custom properties can be added as needed */
|
|
81
|
+
[key: string]: any;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Interface for implementing the DataLoader pattern to efficiently batch and cache database queries
|
|
86
|
+
*
|
|
87
|
+
* The IDataLoader interface provides methods for loading single or multiple entities by ID,
|
|
88
|
+
* clearing cache entries, and loading entities with custom options. This implementation
|
|
89
|
+
* supports MongoDB collections through Mongoose.
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* // Create a user data loader
|
|
93
|
+
* const userLoader = new MongoDataLoader(UserModel);
|
|
94
|
+
*
|
|
95
|
+
* // Load a user by ID (batched and cached)
|
|
96
|
+
* const user1 = await userLoader.load('user1');
|
|
97
|
+
* const user2 = await userLoader.load('user2');
|
|
98
|
+
*
|
|
99
|
+
* // Load multiple users by ID in a single batch
|
|
100
|
+
* const users = await userLoader.loadMany(['user3', 'user4', 'user5']);
|
|
101
|
+
*
|
|
102
|
+
* // Load users with custom criteria
|
|
103
|
+
* const activeAdmins = await userLoader.withOptions.load({
|
|
104
|
+
* id: 'active-admins',
|
|
105
|
+
* searchKey: 'role',
|
|
106
|
+
* criteria: { active: true, role: 'admin' }
|
|
107
|
+
* });
|
|
108
|
+
*
|
|
109
|
+
* // Clear cache when a user is updated
|
|
110
|
+
* userLoader.clear('user1');
|
|
111
|
+
*/
|
|
112
|
+
export interface IDataLoader<SchemaType> {
|
|
113
|
+
/**
|
|
114
|
+
* Load a single entity by ID
|
|
115
|
+
*
|
|
116
|
+
* This method retrieves a single entity by its unique identifier.
|
|
117
|
+
* Requests are batched with other load calls in the same tick of the event loop
|
|
118
|
+
* and results are cached to prevent duplicate database queries.
|
|
119
|
+
*
|
|
120
|
+
* @param id - The unique identifier of the entity to load
|
|
121
|
+
* @returns Promise resolving to the domain entity or null if not found
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* // Load a user by ID
|
|
125
|
+
* const user = await userLoader.load('user123');
|
|
126
|
+
*
|
|
127
|
+
* // Handle case where entity might not exist
|
|
128
|
+
* const product = await productLoader.load('product456');
|
|
129
|
+
* if (product) {
|
|
130
|
+
* // Product exists, use it
|
|
131
|
+
* } else {
|
|
132
|
+
* // Product not found
|
|
133
|
+
* }
|
|
134
|
+
*/
|
|
135
|
+
load: (id: string) => Promise<AsDomainType<SchemaType> | null>;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Load multiple entities by their IDs
|
|
139
|
+
*
|
|
140
|
+
* This method retrieves multiple entities by their unique identifiers in a single batch.
|
|
141
|
+
* Results are cached to prevent duplicate database queries.
|
|
142
|
+
*
|
|
143
|
+
* @param ids - Array of unique identifiers of the entities to load
|
|
144
|
+
* @returns Promise resolving to an array of domain entities, nulls, or errors
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* // Load multiple users by their IDs
|
|
148
|
+
* const users = await userLoader.loadMany(['user1', 'user2', 'user3']);
|
|
149
|
+
*
|
|
150
|
+
* // Handle results which may include nulls for not found entities
|
|
151
|
+
* const validUsers = users.filter(user => user !== null && !(user instanceof Error));
|
|
152
|
+
*/
|
|
153
|
+
loadMany: (ids: string[]) => Promise<(AsDomainType<SchemaType> | null | Error)[]>;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Clear a single entity from the cache
|
|
157
|
+
*
|
|
158
|
+
* This method removes a specific entity from the cache, forcing the next load
|
|
159
|
+
* request for this ID to query the database. Use this when an entity is updated
|
|
160
|
+
* or deleted to ensure fresh data is loaded.
|
|
161
|
+
*
|
|
162
|
+
* @param id - The unique identifier of the entity to remove from cache
|
|
163
|
+
* @returns The DataLoader instance for method chaining
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* // Update a user and clear the cache
|
|
167
|
+
* await userService.update('user123', { name: 'New Name' });
|
|
168
|
+
* userLoader.clear('user123');
|
|
169
|
+
*/
|
|
170
|
+
clear: (id: string) => DataLoader<string, AsDomainType<SchemaType> | null>;
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Clear all entities from the cache
|
|
174
|
+
*
|
|
175
|
+
* This method removes all entities from the cache, forcing the next load
|
|
176
|
+
* requests to query the database. Use this when multiple entities are updated
|
|
177
|
+
* or when you need to ensure all data is fresh.
|
|
178
|
+
*
|
|
179
|
+
* @returns The DataLoader instance for method chaining
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* // After a bulk update operation, clear the entire cache
|
|
183
|
+
* await userService.bulkUpdate({ role: 'user' }, { active: true });
|
|
184
|
+
* userLoader.clearAll();
|
|
185
|
+
*/
|
|
186
|
+
clearAll: () => DataLoader<string, AsDomainType<SchemaType> | null>;
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* DataLoader for loading entities with custom options
|
|
190
|
+
*
|
|
191
|
+
* This property provides a DataLoader instance that supports more complex
|
|
192
|
+
* loading scenarios beyond simple ID lookups. Use this when you need to
|
|
193
|
+
* load entities based on other criteria or with additional filtering.
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* // Load active users with the admin role
|
|
197
|
+
* const adminUsers = await userLoader.withOptions.load({
|
|
198
|
+
* id: 'active-admins', // Unique cache key
|
|
199
|
+
* searchKey: 'role',
|
|
200
|
+
* criteria: { active: true, role: 'admin' }
|
|
201
|
+
* });
|
|
202
|
+
*
|
|
203
|
+
* // Load products in a specific price range
|
|
204
|
+
* const affordableProducts = await productLoader.withOptions.load({
|
|
205
|
+
* id: 'affordable-products',
|
|
206
|
+
* searchKey: 'price',
|
|
207
|
+
* criteria: { price: { $lt: 100 } }
|
|
208
|
+
* });
|
|
209
|
+
*/
|
|
210
|
+
withOptions: DataLoader<DataLoaderOptions<SchemaType>, AsDomainType<SchemaType>[]>;
|
|
211
|
+
}
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file BaseMongoRepository.ts
|
|
3
|
+
* @description Defines the IBaseMongoRepository interface that provides a standardized set of methods for interacting with MongoDB collections.
|
|
4
|
+
* This interface serves as a contract for repository implementations, ensuring consistent data access patterns across the application.
|
|
5
|
+
* It includes methods for CRUD operations, querying with filters, pagination, and aggregation pipelines.
|
|
6
|
+
*
|
|
7
|
+
* The interface is generic, accepting a SchemaType parameter that represents the Mongoose schema structure for the collection.
|
|
8
|
+
* Methods return domain objects (with 'id' instead of '_id') using the AsDomainType transformation.
|
|
9
|
+
*
|
|
10
|
+
* Key features:
|
|
11
|
+
* - Consistent data access patterns across different entity types
|
|
12
|
+
* - Type-safe operations with MongoDB collections
|
|
13
|
+
* - Support for pagination, filtering, and sorting
|
|
14
|
+
* - Aggregation pipeline integration for complex queries
|
|
15
|
+
* - Bulk operations for performance optimization
|
|
16
|
+
*
|
|
17
|
+
* This repository pattern creates a clean separation between the data access layer and business logic,
|
|
18
|
+
* making the codebase more maintainable and testable.
|
|
19
|
+
*
|
|
20
|
+
* @see IBaseService2 - Service layer that uses repositories for business logic
|
|
21
|
+
* @see IDataLoader2 - Efficient data loading pattern that can utilize repositories
|
|
22
|
+
* @see GetAllArgs - Common arguments for querying collections
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import type { FilterQuery, Model, PipelineStage, UpdateQuery } from 'mongoose';
|
|
26
|
+
import { AsDomainType, CreateType, GetAllArgs, GetAllWithCountResult, UpdateType } from './dbCommonTypes';
|
|
27
|
+
|
|
28
|
+
export interface IBaseMongoRepository<SchemaType> {
|
|
29
|
+
/**
|
|
30
|
+
* The Mongoose model associated with this repository
|
|
31
|
+
* SchemaType & { id: string } represents the schema with virtual id field included
|
|
32
|
+
*/
|
|
33
|
+
model: Model<SchemaType & { id: string }>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Counts documents matching the specified conditions
|
|
37
|
+
*
|
|
38
|
+
* @param conditions - Optional filter criteria to count specific documents
|
|
39
|
+
* @returns Promise resolving to the count of matching documents
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* // Count all active users
|
|
43
|
+
* const activeUserCount = await userRepository.count({ active: true });
|
|
44
|
+
*/
|
|
45
|
+
count(conditions?: FilterQuery<SchemaType>): Promise<number>;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Retrieves documents matching the specified options
|
|
49
|
+
*
|
|
50
|
+
* @param options - Query options including filtering, sorting, pagination, and field selection
|
|
51
|
+
* @returns Promise resolving to an array of domain objects
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* // Get recently created users with pagination
|
|
55
|
+
* const users = await userRepository.getAll({
|
|
56
|
+
* criteria: { active: true },
|
|
57
|
+
* sort: { createdAt: -1 },
|
|
58
|
+
* skip: 0,
|
|
59
|
+
* limit: 10
|
|
60
|
+
* });
|
|
61
|
+
*/
|
|
62
|
+
getAll(options: GetAllArgs<SchemaType>): Promise<AsDomainType<SchemaType>[]>;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Retrieves documents using a custom aggregation pipeline
|
|
66
|
+
*
|
|
67
|
+
* @param options - Base query options for filtering and pagination
|
|
68
|
+
* @param customPipeline - Optional MongoDB aggregation pipeline stages
|
|
69
|
+
* @returns Promise resolving to an array of domain objects
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* // Get users with computed fields using aggregation
|
|
73
|
+
* const users = await userRepository.getAllWithPipeline(
|
|
74
|
+
* { limit: 10 },
|
|
75
|
+
* [
|
|
76
|
+
* { $lookup: { from: 'posts', localField: '_id', foreignField: 'userId', as: 'posts' } },
|
|
77
|
+
* { $addFields: { postCount: { $size: '$posts' } } }
|
|
78
|
+
* ]
|
|
79
|
+
* );
|
|
80
|
+
*/
|
|
81
|
+
getAllWithPipeline(
|
|
82
|
+
options: GetAllArgs<SchemaType>,
|
|
83
|
+
customPipeline?: PipelineStage[],
|
|
84
|
+
): Promise<AsDomainType<SchemaType>[]>;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Retrieves documents with pagination and total count
|
|
88
|
+
*
|
|
89
|
+
* @param options - Query options including filtering, sorting, pagination, and field selection
|
|
90
|
+
* @returns Promise resolving to object with data array and total count
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* // Get paginated users with total count for UI pagination
|
|
94
|
+
* const { data, totalCount } = await userRepository.getAllWithCount({
|
|
95
|
+
* criteria: { role: 'user' },
|
|
96
|
+
* skip: 20,
|
|
97
|
+
* limit: 10
|
|
98
|
+
* });
|
|
99
|
+
*/
|
|
100
|
+
getAllWithCount(options: GetAllArgs<SchemaType>): Promise<GetAllWithCountResult<SchemaType>>;
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Retrieves documents with pagination, total count, and custom aggregation pipeline
|
|
104
|
+
*
|
|
105
|
+
* @param options - Base query options for filtering and pagination
|
|
106
|
+
* @param customPipeline - Optional MongoDB aggregation pipeline stages
|
|
107
|
+
* @returns Promise resolving to object with data array and total count
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* // Get paginated users with related data and total count
|
|
111
|
+
* const result = await userRepository.getAllWithCountAndPipeline(
|
|
112
|
+
* { limit: 10, skip: 0 },
|
|
113
|
+
* [{ $lookup: { from: 'departments', localField: 'departmentId', foreignField: '_id', as: 'department' } }]
|
|
114
|
+
* );
|
|
115
|
+
*/
|
|
116
|
+
getAllWithCountAndPipeline(
|
|
117
|
+
options: GetAllArgs<SchemaType>,
|
|
118
|
+
customPipeline?: PipelineStage[],
|
|
119
|
+
): Promise<{
|
|
120
|
+
data: AsDomainType<SchemaType>[];
|
|
121
|
+
totalCount: number;
|
|
122
|
+
}>;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Retrieves a single document matching the specified conditions
|
|
126
|
+
*
|
|
127
|
+
* @param conditions - Filter criteria to find the document
|
|
128
|
+
* @param selectedFields - Optional space-separated string of fields to include
|
|
129
|
+
* @returns Promise resolving to a domain object or null if not found
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* // Get user by email with selected fields
|
|
133
|
+
* const user = await userRepository.get({ email: 'user@example.com' }, 'name email role');
|
|
134
|
+
*/
|
|
135
|
+
get(conditions?: FilterQuery<SchemaType>, selectedFields?: string): Promise<AsDomainType<SchemaType> | null>;
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Finds a single document matching the specified conditions
|
|
139
|
+
* Similar to get() but with partial filter query support
|
|
140
|
+
*
|
|
141
|
+
* @param conditions - Partial filter criteria to find the document
|
|
142
|
+
* @param selectedFields - Optional space-separated string of fields to include
|
|
143
|
+
* @returns Promise resolving to a domain object or null if not found
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* // Find user by partial match
|
|
147
|
+
* const user = await userRepository.find({ name: { $regex: 'John' } });
|
|
148
|
+
*/
|
|
149
|
+
find(
|
|
150
|
+
conditions: Partial<FilterQuery<SchemaType>>,
|
|
151
|
+
selectedFields?: string,
|
|
152
|
+
): Promise<AsDomainType<SchemaType> | null>;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Creates a new document
|
|
156
|
+
*
|
|
157
|
+
* @param data - Data for the new document
|
|
158
|
+
* @returns Promise resolving to the created domain object
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* // Create a new user
|
|
162
|
+
* const newUser = await userRepository.create({
|
|
163
|
+
* name: 'John Doe',
|
|
164
|
+
* email: 'john@example.com',
|
|
165
|
+
* role: 'user'
|
|
166
|
+
* });
|
|
167
|
+
*/
|
|
168
|
+
create<T = CreateType<SchemaType>>(data: T): Promise<AsDomainType<SchemaType>>;
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Updates a document if it exists, otherwise creates it
|
|
172
|
+
*
|
|
173
|
+
* @param conditions - Filter criteria to find the document to update
|
|
174
|
+
* @param update - Data to update or create the document
|
|
175
|
+
* @param options - Optional MongoDB options for the operation
|
|
176
|
+
* @returns Promise resolving to the updated or created domain object
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* // Upsert a user by email
|
|
180
|
+
* const user = await userRepository.upsert(
|
|
181
|
+
* { email: 'john@example.com' },
|
|
182
|
+
* { name: 'John Doe', role: 'admin' }
|
|
183
|
+
* );
|
|
184
|
+
*/
|
|
185
|
+
upsert<T = UpdateType<SchemaType>>(
|
|
186
|
+
conditions: FilterQuery<SchemaType>,
|
|
187
|
+
update: T,
|
|
188
|
+
options?: Record<string, unknown>,
|
|
189
|
+
): Promise<AsDomainType<SchemaType>>;
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Updates a single document matching the criteria
|
|
193
|
+
*
|
|
194
|
+
* @param criteria - Filter criteria to find the document to update
|
|
195
|
+
* @param update - Data to update the document
|
|
196
|
+
* @param options - Optional MongoDB options for the operation
|
|
197
|
+
* @returns Promise resolving to the updated domain object
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* // Update a user's role
|
|
201
|
+
* const updatedUser = await userRepository.update(
|
|
202
|
+
* { _id: userId },
|
|
203
|
+
* { role: 'admin' }
|
|
204
|
+
* );
|
|
205
|
+
*/
|
|
206
|
+
update<T = UpdateType<SchemaType>>(
|
|
207
|
+
criteria: FilterQuery<SchemaType>,
|
|
208
|
+
update: T | UpdateQuery<SchemaType>,
|
|
209
|
+
options?: Record<string, unknown>,
|
|
210
|
+
): Promise<AsDomainType<SchemaType>>;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Updates multiple documents matching the criteria
|
|
214
|
+
*
|
|
215
|
+
* @param criteria - Filter criteria to find documents to update
|
|
216
|
+
* @param update - Data to update the documents
|
|
217
|
+
* @param options - Optional MongoDB options for the operation
|
|
218
|
+
* @returns Promise resolving to an array of updated domain objects
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* // Update all inactive users to active
|
|
222
|
+
* const updatedUsers = await userRepository.bulkUpdate(
|
|
223
|
+
* { active: false },
|
|
224
|
+
* { active: true }
|
|
225
|
+
* );
|
|
226
|
+
*/
|
|
227
|
+
bulkUpdate<T = UpdateType<SchemaType>>(
|
|
228
|
+
criteria: FilterQuery<SchemaType>,
|
|
229
|
+
update: T | UpdateQuery<SchemaType>,
|
|
230
|
+
options?: Record<string, unknown>,
|
|
231
|
+
): Promise<AsDomainType<SchemaType>[]>;
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Deletes a document matching the criteria
|
|
235
|
+
*
|
|
236
|
+
* @param criteria - Filter criteria to find the document to delete
|
|
237
|
+
* @returns Promise resolving to boolean indicating success
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* // Delete a user by ID
|
|
241
|
+
* const success = await userRepository.delete({ _id: userId });
|
|
242
|
+
*/
|
|
243
|
+
delete(criteria: FilterQuery<SchemaType>): Promise<boolean>;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Retrieves multiple documents by their IDs
|
|
247
|
+
*
|
|
248
|
+
* @param ids - Array of document IDs to retrieve
|
|
249
|
+
* @param selectedFields - Optional space-separated string of fields to include
|
|
250
|
+
* @returns Promise resolving to an array of domain objects
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* // Get multiple users by their IDs
|
|
254
|
+
* const users = await userRepository.bulkGet(['id1', 'id2', 'id3']);
|
|
255
|
+
*/
|
|
256
|
+
bulkGet(ids: string[], selectedFields?: string): Promise<AsDomainType<SchemaType>[]>;
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Creates multiple documents in a single operation
|
|
260
|
+
*
|
|
261
|
+
* @param data - Array of data for the new documents
|
|
262
|
+
* @returns Promise resolving to an array of created domain objects
|
|
263
|
+
*
|
|
264
|
+
* @example
|
|
265
|
+
* // Create multiple users at once
|
|
266
|
+
* const newUsers = await userRepository.bulkCreate([
|
|
267
|
+
* { name: 'John', email: 'john@example.com' },
|
|
268
|
+
* { name: 'Jane', email: 'jane@example.com' }
|
|
269
|
+
* ]);
|
|
270
|
+
*/
|
|
271
|
+
bulkCreate<T = CreateType<SchemaType>>(data: T[]): Promise<AsDomainType<SchemaType>[]>;
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Deletes multiple documents matching the criteria
|
|
275
|
+
*
|
|
276
|
+
* @param criteria - Filter criteria to find documents to delete
|
|
277
|
+
* @returns Promise resolving to the number of deleted documents
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* // Delete all inactive users
|
|
281
|
+
* const deletedCount = await userRepository.bulkDelete({ active: false });
|
|
282
|
+
*/
|
|
283
|
+
bulkDelete(criteria: FilterQuery<SchemaType>): Promise<number>;
|
|
284
|
+
}
|