@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,140 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
/**
|
|
3
|
+
* @file CommonTypes.ts
|
|
4
|
+
* @description Defines common types and interfaces used across the data access layer for database operations.
|
|
5
|
+
* These types are database-agnostic and can be used with any database implementation (MongoDB, PostgreSQL, etc.).
|
|
6
|
+
*
|
|
7
|
+
* Key components include:
|
|
8
|
+
* - GetAllArgs: Interface for pagination, sorting, and filtering when retrieving collections of documents
|
|
9
|
+
* - AsDomainType: Type transformer that converts database schema types to domain types
|
|
10
|
+
* - GetAllWithCountResult: Type for paginated results that include both data and total count
|
|
11
|
+
* - CreateType/UpdateType: Utility types that define the shape of data for create and update operations
|
|
12
|
+
* - FilterCriteria: Generic type for filtering data across different database implementations
|
|
13
|
+
* - ISort: Interface for defining sort operations
|
|
14
|
+
*
|
|
15
|
+
* These types ensure consistency across repositories and services, providing type safety and
|
|
16
|
+
* clear contracts for data manipulation throughout the application. They form the foundation
|
|
17
|
+
* of the data access pattern used across the entire system.
|
|
18
|
+
*
|
|
19
|
+
* @see IBaseService - Service layer that uses these types
|
|
20
|
+
* @see IBaseMongoRepository - MongoDB repository implementation using these types
|
|
21
|
+
*/
|
|
22
|
+
import { ISort } from 'common';
|
|
23
|
+
import type { ObjectId } from 'mongodb';
|
|
24
|
+
/**
|
|
25
|
+
* Generic filter type that doesn't depend on specific database implementation
|
|
26
|
+
* Can be used for any database query operations
|
|
27
|
+
*/
|
|
28
|
+
export type FilterCriteria<T> = Partial<T> | Record<string, any>;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Interface for defining sort operations
|
|
32
|
+
* Keys are field names, values are sort direction (1 for ascending, -1 for descending)
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* // Sort by creation date descending, then by name ascending
|
|
36
|
+
* const sort: ISort = { createdAt: -1, name: 1 };
|
|
37
|
+
*/
|
|
38
|
+
/**
|
|
39
|
+
* Interface for the arguments used in getAll methods
|
|
40
|
+
*
|
|
41
|
+
* @property criteria - Filter criteria to narrow down results
|
|
42
|
+
* @property sort - Sorting configuration with field names and directions
|
|
43
|
+
* @property skip - Number of documents to skip (for pagination)
|
|
44
|
+
* @property limit - Maximum number of documents to return (for pagination)
|
|
45
|
+
* @property selectedFields - String of space-separated field names to include in the results
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* // Get active users, sorted by creation date, second page with 10 items per page
|
|
49
|
+
* const options: GetAllArgs<UserSchema> = {
|
|
50
|
+
* criteria: { active: true },
|
|
51
|
+
* sort: { createdAt: -1 },
|
|
52
|
+
* skip: 10,
|
|
53
|
+
* limit: 10,
|
|
54
|
+
* selectedFields: 'name email createdAt'
|
|
55
|
+
* };
|
|
56
|
+
*/
|
|
57
|
+
export interface GetAllArgs<T> {
|
|
58
|
+
criteria?: FilterCriteria<T>;
|
|
59
|
+
sort?: ISort;
|
|
60
|
+
skip?: number;
|
|
61
|
+
limit?: number;
|
|
62
|
+
selectedFields?: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Type transformer that converts database schema types to domain types
|
|
67
|
+
* For MongoDB, this typically means converting _id to id
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* // MongoDB document
|
|
71
|
+
* type UserSchema = {
|
|
72
|
+
* _id: ObjectId;
|
|
73
|
+
* name: string;
|
|
74
|
+
* email: string;
|
|
75
|
+
* };
|
|
76
|
+
*
|
|
77
|
+
* // Transformed domain object
|
|
78
|
+
* type UserDomain = AsDomainType<UserSchema>;
|
|
79
|
+
* // Equivalent to:
|
|
80
|
+
* // {
|
|
81
|
+
* // id: string;
|
|
82
|
+
* // name: string;
|
|
83
|
+
* // email: string;
|
|
84
|
+
* // }
|
|
85
|
+
*/
|
|
86
|
+
export type AsDomainType<SchemaType> = Omit<SchemaType, '_id'> & { _id?: ObjectId } & { id: string };
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Type for paginated results that include both data and total count
|
|
90
|
+
* Used for implementing pagination UI components
|
|
91
|
+
*
|
|
92
|
+
* @property data - Array of domain objects matching the query criteria
|
|
93
|
+
* @property totalCount - Total number of documents that match the criteria (without pagination)
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* const result: GetAllWithCountResult<UserSchema> = {
|
|
97
|
+
* data: [{ id: '1', name: 'John' }, { id: '2', name: 'Jane' }],
|
|
98
|
+
* totalCount: 50
|
|
99
|
+
* };
|
|
100
|
+
*/
|
|
101
|
+
export type GetAllWithCountResult<SchemaType> = {
|
|
102
|
+
data: AsDomainType<SchemaType>[];
|
|
103
|
+
totalCount: number;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Default pagination settings used throughout the application
|
|
108
|
+
* These values are used when no explicit pagination parameters are provided
|
|
109
|
+
*/
|
|
110
|
+
export enum PAGINATION_OPTIONS {
|
|
111
|
+
limit = 10,
|
|
112
|
+
skip = 0,
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Type for create operations - omits system-generated fields
|
|
117
|
+
* This ensures that clients cannot specify fields that should be generated by the database
|
|
118
|
+
*
|
|
119
|
+
* @example
|
|
120
|
+
* // Valid create operation data
|
|
121
|
+
* const newUser: CreateType<UserSchema> = {
|
|
122
|
+
* name: 'John Doe',
|
|
123
|
+
* email: 'john@example.com',
|
|
124
|
+
* role: 'user'
|
|
125
|
+
* };
|
|
126
|
+
*/
|
|
127
|
+
export type CreateType<SchemaType> = Omit<Partial<SchemaType>, '_id' | 'id' | 'createdAt' | 'updatedAt'>;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Type for update operations - partial of the schema without system fields
|
|
131
|
+
* This allows for partial updates where only changed fields are specified
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* // Update a user's email and role
|
|
135
|
+
* const updateData: UpdateType<UserSchema> = {
|
|
136
|
+
* email: 'new.email@example.com',
|
|
137
|
+
* role: 'admin'
|
|
138
|
+
* };
|
|
139
|
+
*/
|
|
140
|
+
export type UpdateType<SchemaType> = Partial<Omit<SchemaType, '_id'>>;
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
|
2
|
+
/**
|
|
3
|
+
* @file moleculer-event-handler.ts
|
|
4
|
+
* @description Decorator for marking service methods as Moleculer event handlers
|
|
5
|
+
*
|
|
6
|
+
* This allows event handlers to be defined directly in the service class
|
|
7
|
+
* and automatically registered as Moleculer events.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import 'reflect-metadata';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Moleculer namespace containing event handler utilities
|
|
14
|
+
*/
|
|
15
|
+
export namespace Moleculer {
|
|
16
|
+
/**
|
|
17
|
+
* Metadata key for storing event handler information
|
|
18
|
+
*/
|
|
19
|
+
export const EVENT_HANDLER_METADATA_KEY = 'moleculer:eventHandler';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Event handler metadata interface
|
|
23
|
+
*/
|
|
24
|
+
export interface EventHandlerMetadata {
|
|
25
|
+
eventName: string;
|
|
26
|
+
methodName: string;
|
|
27
|
+
group?: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Decorator for marking a service method as a Moleculer event handler
|
|
32
|
+
*
|
|
33
|
+
* @param eventName - The event name to listen for
|
|
34
|
+
* @param options - Optional event configuration
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* class OrganizationService implements IOrganizationService {
|
|
39
|
+
* @Moleculer.EventHandler(UserBroadcasterAction.OnUserCreated)
|
|
40
|
+
* async onUserCreated(event: IUserCreationEvent): Promise<void> {
|
|
41
|
+
* await this.createDefaultOrganization(event.user);
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* @Moleculer.EventHandler(OrganizationServiceAction.OnOrganizationCreated, { group: 'org-setup' })
|
|
45
|
+
* async onOrganizationCreated(event: IOrganizationCreatedEvent): Promise<void> {
|
|
46
|
+
* // Handle organization creation
|
|
47
|
+
* }
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export function EventHandler(
|
|
52
|
+
eventName: string,
|
|
53
|
+
options?: { group?: string }
|
|
54
|
+
): MethodDecorator {
|
|
55
|
+
return function moleculerEventHandlerDecorator(
|
|
56
|
+
target: object,
|
|
57
|
+
propertyKey: string | symbol,
|
|
58
|
+
descriptor: PropertyDescriptor
|
|
59
|
+
): PropertyDescriptor {
|
|
60
|
+
// Get existing event handlers for this class
|
|
61
|
+
const existingHandlers: EventHandlerMetadata[] =
|
|
62
|
+
Reflect.getMetadata(EVENT_HANDLER_METADATA_KEY, target.constructor) || [];
|
|
63
|
+
|
|
64
|
+
// Add this handler to the list
|
|
65
|
+
const metadata: EventHandlerMetadata = {
|
|
66
|
+
eventName,
|
|
67
|
+
methodName: propertyKey.toString(),
|
|
68
|
+
group: options?.group,
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
existingHandlers.push(metadata);
|
|
72
|
+
|
|
73
|
+
// Store the updated list
|
|
74
|
+
Reflect.defineMetadata(EVENT_HANDLER_METADATA_KEY, existingHandlers, target.constructor);
|
|
75
|
+
|
|
76
|
+
return descriptor;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get all Moleculer event handlers defined on a service class
|
|
82
|
+
*
|
|
83
|
+
* @param serviceClassOrInstance - The service class constructor or instance
|
|
84
|
+
* @returns Array of event handler metadata
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const handlers = Moleculer.getEventHandlers(OrganizationService);
|
|
89
|
+
* // Returns: [
|
|
90
|
+
* // { eventName: 'user.created', methodName: 'onUserCreated' },
|
|
91
|
+
* // { eventName: 'org.created', methodName: 'onOrganizationCreated' }
|
|
92
|
+
* // ]
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export function getEventHandlers(
|
|
96
|
+
serviceClassOrInstance: (new (...args: unknown[]) => unknown) | object
|
|
97
|
+
): EventHandlerMetadata[] {
|
|
98
|
+
const target = typeof serviceClassOrInstance === 'function'
|
|
99
|
+
? serviceClassOrInstance
|
|
100
|
+
: (serviceClassOrInstance as { constructor: new (...args: unknown[]) => unknown }).constructor;
|
|
101
|
+
return Reflect.getMetadata(EVENT_HANDLER_METADATA_KEY, target) || [];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Check if a method is marked as a Moleculer event handler
|
|
106
|
+
*
|
|
107
|
+
* @param serviceClassOrInstance - The service class constructor or instance
|
|
108
|
+
* @param methodName - The method name to check
|
|
109
|
+
* @returns Event handler metadata if found, undefined otherwise
|
|
110
|
+
*/
|
|
111
|
+
export function isEventHandler(
|
|
112
|
+
serviceClassOrInstance: (new (...args: unknown[]) => unknown) | object,
|
|
113
|
+
methodName: string
|
|
114
|
+
): EventHandlerMetadata | undefined {
|
|
115
|
+
const handlers = getEventHandlers(serviceClassOrInstance);
|
|
116
|
+
return handlers.find(h => h.methodName === methodName);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Document } from 'mongoose';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Helper type to convert a Mongoose document to a plain object
|
|
5
|
+
* Useful when working with Mongoose documents that need to be converted to domain objects
|
|
6
|
+
*/
|
|
7
|
+
export type AsObject<T> = T extends Document ? Omit<T, keyof Document> : T;
|
|
8
|
+
|
|
9
|
+
export interface IMongoOptions {
|
|
10
|
+
collectionName?: string;
|
|
11
|
+
sessionCollectionName?: string;
|
|
12
|
+
timestamps?: {
|
|
13
|
+
createdAt: string;
|
|
14
|
+
updatedAt: string;
|
|
15
|
+
};
|
|
16
|
+
convertUserIdToMongoObjectId?: boolean;
|
|
17
|
+
convertSessionIdToMongoObjectId?: boolean;
|
|
18
|
+
caseSensitiveUserName?: boolean;
|
|
19
|
+
idProvider?: () => string | Object;
|
|
20
|
+
dateProvider?: (date?: Date) => any;
|
|
21
|
+
}
|