@common-stack/store-mongo 7.1.1-alpha.8 → 7.1.1-alpha.9

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.
@@ -0,0 +1,139 @@
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
+ /**
24
+ * Generic filter type that doesn't depend on specific database implementation
25
+ * Can be used for any database query operations
26
+ */
27
+ export type FilterCriteria<T> = Partial<T> | Record<string, any>;
28
+
29
+ /**
30
+ * Interface for defining sort operations
31
+ * Keys are field names, values are sort direction (1 for ascending, -1 for descending)
32
+ *
33
+ * @example
34
+ * // Sort by creation date descending, then by name ascending
35
+ * const sort: ISort = { createdAt: -1, name: 1 };
36
+ */
37
+ /**
38
+ * Interface for the arguments used in getAll methods
39
+ *
40
+ * @property criteria - Filter criteria to narrow down results
41
+ * @property sort - Sorting configuration with field names and directions
42
+ * @property skip - Number of documents to skip (for pagination)
43
+ * @property limit - Maximum number of documents to return (for pagination)
44
+ * @property selectedFields - String of space-separated field names to include in the results
45
+ *
46
+ * @example
47
+ * // Get active users, sorted by creation date, second page with 10 items per page
48
+ * const options: GetAllArgs<UserSchema> = {
49
+ * criteria: { active: true },
50
+ * sort: { createdAt: -1 },
51
+ * skip: 10,
52
+ * limit: 10,
53
+ * selectedFields: 'name email createdAt'
54
+ * };
55
+ */
56
+ export interface GetAllArgs<T> {
57
+ criteria?: FilterCriteria<T>;
58
+ sort?: ISort;
59
+ skip?: number;
60
+ limit?: number;
61
+ selectedFields?: string;
62
+ }
63
+
64
+ /**
65
+ * Type transformer that converts database schema types to domain types
66
+ * For MongoDB, this typically means converting _id to id
67
+ *
68
+ * @example
69
+ * // MongoDB document
70
+ * type UserSchema = {
71
+ * _id: ObjectId;
72
+ * name: string;
73
+ * email: string;
74
+ * };
75
+ *
76
+ * // Transformed domain object
77
+ * type UserDomain = AsDomainType<UserSchema>;
78
+ * // Equivalent to:
79
+ * // {
80
+ * // id: string;
81
+ * // name: string;
82
+ * // email: string;
83
+ * // }
84
+ */
85
+ export type AsDomainType<T> = Omit<T, '_id'> & { id: string };
86
+
87
+ /**
88
+ * Type for paginated results that include both data and total count
89
+ * Used for implementing pagination UI components
90
+ *
91
+ * @property data - Array of domain objects matching the query criteria
92
+ * @property totalCount - Total number of documents that match the criteria (without pagination)
93
+ *
94
+ * @example
95
+ * const result: GetAllWithCountResult<UserSchema> = {
96
+ * data: [{ id: '1', name: 'John' }, { id: '2', name: 'Jane' }],
97
+ * totalCount: 50
98
+ * };
99
+ */
100
+ export type GetAllWithCountResult<SchemaType> = {
101
+ data: AsDomainType<SchemaType>[];
102
+ totalCount: number;
103
+ };
104
+
105
+ /**
106
+ * Default pagination settings used throughout the application
107
+ * These values are used when no explicit pagination parameters are provided
108
+ */
109
+ export enum PAGINATION_OPTIONS {
110
+ limit = 10,
111
+ skip = 0,
112
+ }
113
+
114
+ /**
115
+ * Type for create operations - omits system-generated fields
116
+ * This ensures that clients cannot specify fields that should be generated by the database
117
+ *
118
+ * @example
119
+ * // Valid create operation data
120
+ * const newUser: CreateType<UserSchema> = {
121
+ * name: 'John Doe',
122
+ * email: 'john@example.com',
123
+ * role: 'user'
124
+ * };
125
+ */
126
+ export type CreateType<SchemaType> = Omit<Partial<SchemaType>, '_id' | 'id' | 'createdAt' | 'updatedAt'>;
127
+
128
+ /**
129
+ * Type for update operations - partial of the schema without system fields
130
+ * This allows for partial updates where only changed fields are specified
131
+ *
132
+ * @example
133
+ * // Update a user's email and role
134
+ * const updateData: UpdateType<UserSchema> = {
135
+ * email: 'new.email@example.com',
136
+ * role: 'admin'
137
+ * };
138
+ */
139
+ export type UpdateType<SchemaType> = Partial<Omit<SchemaType, '_id'>>;
@@ -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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@common-stack/store-mongo",
3
- "version": "7.1.1-alpha.8",
3
+ "version": "7.1.1-alpha.9",
4
4
  "description": "Sample core for higher packages to depend on",
5
5
  "license": "ISC",
6
6
  "author": "CDMBase LLC",
@@ -58,5 +58,5 @@
58
58
  "typescript": {
59
59
  "definition": "lib/index.d.ts"
60
60
  },
61
- "gitHead": "3f3020d76d93d311d8a3c7457544557fa8ddeb77"
61
+ "gitHead": "54c1746625cac7261ce4df690ae05104064a0e88"
62
62
  }