@librechat/data-schemas 0.0.14 โ†’ 0.0.16

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 (51) hide show
  1. package/README.md +318 -0
  2. package/dist/index.cjs +2668 -1041
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.es.js +2650 -1018
  5. package/dist/index.es.js.map +1 -1
  6. package/dist/types/common/enum.d.ts +13 -0
  7. package/dist/types/common/index.d.ts +1 -0
  8. package/dist/types/index.d.ts +2 -0
  9. package/dist/types/methods/accessRole.d.ts +42 -0
  10. package/dist/types/methods/accessRole.spec.d.ts +1 -0
  11. package/dist/types/methods/aclEntry.d.ts +52 -0
  12. package/dist/types/methods/aclEntry.spec.d.ts +1 -0
  13. package/dist/types/methods/agentCategory.d.ts +48 -0
  14. package/dist/types/methods/group.d.ts +40 -0
  15. package/dist/types/methods/group.spec.d.ts +1 -0
  16. package/dist/types/methods/index.d.ts +116 -6
  17. package/dist/types/methods/pluginAuth.d.ts +1 -1
  18. package/dist/types/methods/user.d.ts +9 -1
  19. package/dist/types/methods/userGroup.d.ts +73 -0
  20. package/dist/types/methods/userGroup.methods.spec.d.ts +1 -0
  21. package/dist/types/methods/userGroup.roles.spec.d.ts +1 -0
  22. package/dist/types/methods/userGroup.spec.d.ts +1 -0
  23. package/dist/types/models/accessRole.d.ts +30 -0
  24. package/dist/types/models/aclEntry.d.ts +30 -0
  25. package/dist/types/models/agentCategory.d.ts +30 -0
  26. package/dist/types/models/group.d.ts +30 -0
  27. package/dist/types/models/index.d.ts +4 -0
  28. package/dist/types/schema/accessRole.d.ts +39 -0
  29. package/dist/types/schema/aclEntry.d.ts +39 -0
  30. package/dist/types/schema/agentCategory.d.ts +39 -0
  31. package/dist/types/schema/defaults.d.ts +4 -0
  32. package/dist/types/schema/group.d.ts +37 -0
  33. package/dist/types/schema/index.d.ts +2 -0
  34. package/dist/types/schema/preset.d.ts +1 -0
  35. package/dist/types/schema/prompt.d.ts +2 -9
  36. package/dist/types/schema/promptGroup.d.ts +4 -18
  37. package/dist/types/types/accessRole.d.ts +40 -0
  38. package/dist/types/types/aclEntry.d.ts +52 -0
  39. package/dist/types/types/agent.d.ts +8 -0
  40. package/dist/types/types/agentCategory.d.ts +41 -0
  41. package/dist/types/types/convo.d.ts +1 -0
  42. package/dist/types/types/group.d.ts +45 -0
  43. package/dist/types/types/index.d.ts +5 -0
  44. package/dist/types/types/pluginAuth.d.ts +1 -0
  45. package/dist/types/types/prompts.d.ts +50 -0
  46. package/dist/types/types/user.d.ts +2 -0
  47. package/dist/types/utils/index.d.ts +1 -0
  48. package/dist/types/utils/object-traverse.d.ts +23 -0
  49. package/dist/types/utils/transactions.d.ts +14 -0
  50. package/package.json +1 -3
  51. package/dist/types/utils/traverse-wrapper.d.ts +0 -9
package/README.md ADDED
@@ -0,0 +1,318 @@
1
+ # LibreChat Data Schemas Package
2
+
3
+ This package provides the database schemas, models, types, and methods for LibreChat using Mongoose ODM.
4
+
5
+ ## ๐Ÿ“ Package Structure
6
+
7
+ ```
8
+ packages/data-schemas/
9
+ โ”œโ”€โ”€ src/
10
+ โ”‚ โ”œโ”€โ”€ schema/ # Mongoose schema definitions
11
+ โ”‚ โ”œโ”€โ”€ models/ # Model factory functions
12
+ โ”‚ โ”œโ”€โ”€ types/ # TypeScript type definitions
13
+ โ”‚ โ”œโ”€โ”€ methods/ # Database operation methods
14
+ โ”‚ โ”œโ”€โ”€ common/ # Shared constants and enums
15
+ โ”‚ โ”œโ”€โ”€ config/ # Configuration files (winston, etc.)
16
+ โ”‚ โ””โ”€โ”€ index.ts # Main package exports
17
+ ```
18
+
19
+ ## ๐Ÿ—๏ธ Architecture Patterns
20
+
21
+ ### 1. Schema Files (`src/schema/`)
22
+
23
+ Schema files define the Mongoose schema structure. They follow these conventions:
24
+
25
+ - **Naming**: Use lowercase filenames (e.g., `user.ts`, `accessRole.ts`)
26
+ - **Imports**: Import types from `~/types` for TypeScript support
27
+ - **Exports**: Export only the schema as default
28
+
29
+ **Example:**
30
+ ```typescript
31
+ import { Schema } from 'mongoose';
32
+ import type { IUser } from '~/types';
33
+
34
+ const userSchema = new Schema<IUser>(
35
+ {
36
+ name: { type: String },
37
+ email: { type: String, required: true },
38
+ // ... other fields
39
+ },
40
+ { timestamps: true }
41
+ );
42
+
43
+ export default userSchema;
44
+ ```
45
+
46
+ ### 2. Type Definitions (`src/types/`)
47
+
48
+ Type files define TypeScript interfaces and types. They follow these conventions:
49
+
50
+ - **Base Type**: Define a plain type without Mongoose Document properties
51
+ - **Document Interface**: Extend the base type with Document and `_id`
52
+ - **Enums/Constants**: Place related enums in the type file or `common/` if shared
53
+
54
+ **Example:**
55
+ ```typescript
56
+ import type { Document, Types } from 'mongoose';
57
+
58
+ export type User = {
59
+ name?: string;
60
+ email: string;
61
+ // ... other fields
62
+ };
63
+
64
+ export type IUser = User &
65
+ Document & {
66
+ _id: Types.ObjectId;
67
+ };
68
+ ```
69
+
70
+ ### 3. Model Factory Functions (`src/models/`)
71
+
72
+ Model files create Mongoose models using factory functions. They follow these conventions:
73
+
74
+ - **Function Name**: `create[EntityName]Model`
75
+ - **Singleton Pattern**: Check if model exists before creating
76
+ - **Type Safety**: Use the corresponding interface from types
77
+
78
+ **Example:**
79
+ ```typescript
80
+ import userSchema from '~/schema/user';
81
+ import type * as t from '~/types';
82
+
83
+ export function createUserModel(mongoose: typeof import('mongoose')) {
84
+ return mongoose.models.User || mongoose.model<t.IUser>('User', userSchema);
85
+ }
86
+ ```
87
+
88
+ ### 4. Database Methods (`src/methods/`)
89
+
90
+ Method files contain database operations for each entity. They follow these conventions:
91
+
92
+ - **Function Name**: `create[EntityName]Methods`
93
+ - **Return Type**: Export a type for the methods object
94
+ - **Operations**: Include CRUD operations and entity-specific queries
95
+
96
+ **Example:**
97
+ ```typescript
98
+ import type { Model } from 'mongoose';
99
+ import type { IUser } from '~/types';
100
+
101
+ export function createUserMethods(mongoose: typeof import('mongoose')) {
102
+ async function findUserById(userId: string): Promise<IUser | null> {
103
+ const User = mongoose.models.User as Model<IUser>;
104
+ return await User.findById(userId).lean();
105
+ }
106
+
107
+ async function createUser(userData: Partial<IUser>): Promise<IUser> {
108
+ const User = mongoose.models.User as Model<IUser>;
109
+ return await User.create(userData);
110
+ }
111
+
112
+ return {
113
+ findUserById,
114
+ createUser,
115
+ // ... other methods
116
+ };
117
+ }
118
+
119
+ export type UserMethods = ReturnType<typeof createUserMethods>;
120
+ ```
121
+
122
+ ### 5. Main Exports (`src/index.ts`)
123
+
124
+ The main index file exports:
125
+ - `createModels()` - Factory function for all models
126
+ - `createMethods()` - Factory function for all methods
127
+ - Type exports from `~/types`
128
+ - Shared utilities and constants
129
+
130
+ ## ๐Ÿš€ Adding a New Entity
131
+
132
+ To add a new entity to the data-schemas package, follow these steps:
133
+
134
+ ### Step 1: Create the Type Definition
135
+
136
+ Create `src/types/[entityName].ts`:
137
+
138
+ ```typescript
139
+ import type { Document, Types } from 'mongoose';
140
+
141
+ export type EntityName = {
142
+ /** Field description */
143
+ fieldName: string;
144
+ // ... other fields
145
+ };
146
+
147
+ export type IEntityName = EntityName &
148
+ Document & {
149
+ _id: Types.ObjectId;
150
+ };
151
+ ```
152
+
153
+ ### Step 2: Update Types Index
154
+
155
+ Add to `src/types/index.ts`:
156
+
157
+ ```typescript
158
+ export * from './entityName';
159
+ ```
160
+
161
+ ### Step 3: Create the Schema
162
+
163
+ Create `src/schema/[entityName].ts`:
164
+
165
+ ```typescript
166
+ import { Schema } from 'mongoose';
167
+ import type { IEntityName } from '~/types';
168
+
169
+ const entityNameSchema = new Schema<IEntityName>(
170
+ {
171
+ fieldName: { type: String, required: true },
172
+ // ... other fields
173
+ },
174
+ { timestamps: true }
175
+ );
176
+
177
+ export default entityNameSchema;
178
+ ```
179
+
180
+ ### Step 4: Create the Model Factory
181
+
182
+ Create `src/models/[entityName].ts`:
183
+
184
+ ```typescript
185
+ import entityNameSchema from '~/schema/entityName';
186
+ import type * as t from '~/types';
187
+
188
+ export function createEntityNameModel(mongoose: typeof import('mongoose')) {
189
+ return (
190
+ mongoose.models.EntityName ||
191
+ mongoose.model<t.IEntityName>('EntityName', entityNameSchema)
192
+ );
193
+ }
194
+ ```
195
+
196
+ ### Step 5: Update Models Index
197
+
198
+ Add to `src/models/index.ts`:
199
+
200
+ 1. Import the factory function:
201
+ ```typescript
202
+ import { createEntityNameModel } from './entityName';
203
+ ```
204
+
205
+ 2. Add to the return object in `createModels()`:
206
+ ```typescript
207
+ EntityName: createEntityNameModel(mongoose),
208
+ ```
209
+
210
+ ### Step 6: Create Database Methods
211
+
212
+ Create `src/methods/[entityName].ts`:
213
+
214
+ ```typescript
215
+ import type { Model, Types } from 'mongoose';
216
+ import type { IEntityName } from '~/types';
217
+
218
+ export function createEntityNameMethods(mongoose: typeof import('mongoose')) {
219
+ async function findEntityById(id: string | Types.ObjectId): Promise<IEntityName | null> {
220
+ const EntityName = mongoose.models.EntityName as Model<IEntityName>;
221
+ return await EntityName.findById(id).lean();
222
+ }
223
+
224
+ // ... other methods
225
+
226
+ return {
227
+ findEntityById,
228
+ // ... other methods
229
+ };
230
+ }
231
+
232
+ export type EntityNameMethods = ReturnType<typeof createEntityNameMethods>;
233
+ ```
234
+
235
+ ### Step 7: Update Methods Index
236
+
237
+ Add to `src/methods/index.ts`:
238
+
239
+ 1. Import the methods:
240
+ ```typescript
241
+ import { createEntityNameMethods, type EntityNameMethods } from './entityName';
242
+ ```
243
+
244
+ 2. Add to the return object in `createMethods()`:
245
+ ```typescript
246
+ ...createEntityNameMethods(mongoose),
247
+ ```
248
+
249
+ 3. Add to the `AllMethods` type:
250
+ ```typescript
251
+ export type AllMethods = UserMethods &
252
+ // ... other methods
253
+ EntityNameMethods;
254
+ ```
255
+
256
+ ## ๐Ÿ“ Best Practices
257
+
258
+ 1. **Consistent Naming**: Use lowercase for filenames, PascalCase for types/interfaces
259
+ 2. **Type Safety**: Always use TypeScript types, avoid `any`
260
+ 3. **JSDoc Comments**: Document complex fields and methods
261
+ 4. **Indexes**: Define database indexes in schema files for query performance
262
+ 5. **Validation**: Use Mongoose schema validation for data integrity
263
+ 6. **Lean Queries**: Use `.lean()` for read operations when you don't need Mongoose document methods
264
+
265
+ ## ๐Ÿ”ง Common Patterns
266
+
267
+ ### Enums and Constants
268
+
269
+ Place shared enums in `src/common/`:
270
+
271
+ ```typescript
272
+ // src/common/permissions.ts
273
+ export enum PermissionBits {
274
+ VIEW = 1,
275
+ EDIT = 2,
276
+ DELETE = 4,
277
+ SHARE = 8,
278
+ }
279
+ ```
280
+
281
+ ### Compound Indexes
282
+
283
+ For complex queries, add compound indexes:
284
+
285
+ ```typescript
286
+ schema.index({ field1: 1, field2: 1 });
287
+ schema.index(
288
+ { uniqueField: 1 },
289
+ {
290
+ unique: true,
291
+ partialFilterExpression: { uniqueField: { $exists: true } }
292
+ }
293
+ );
294
+ ```
295
+
296
+ ### Virtual Properties
297
+
298
+ Add computed properties using virtuals:
299
+
300
+ ```typescript
301
+ schema.virtual('fullName').get(function() {
302
+ return `${this.firstName} ${this.lastName}`;
303
+ });
304
+ ```
305
+
306
+ ## ๐Ÿงช Testing
307
+
308
+ When adding new entities, ensure:
309
+ - Types compile without errors
310
+ - Models can be created successfully
311
+ - Methods handle edge cases (null checks, validation)
312
+ - Indexes are properly defined for query patterns
313
+
314
+ ## ๐Ÿ“š Resources
315
+
316
+ - [Mongoose Documentation](https://mongoosejs.com/docs/)
317
+ - [TypeScript Handbook](https://www.typescriptlang.org/docs/)
318
+ - [MongoDB Indexes](https://docs.mongodb.com/manual/indexes/)