@librechat/data-schemas 0.0.15 โ 0.0.17
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/README.md +318 -0
- package/dist/index.cjs +3025 -1653
- package/dist/index.cjs.map +1 -1
- package/dist/index.es.js +3055 -1687
- package/dist/index.es.js.map +1 -1
- package/dist/types/common/enum.d.ts +13 -0
- package/dist/types/common/index.d.ts +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/methods/accessRole.d.ts +42 -0
- package/dist/types/methods/accessRole.spec.d.ts +1 -0
- package/dist/types/methods/aclEntry.d.ts +52 -0
- package/dist/types/methods/aclEntry.spec.d.ts +1 -0
- package/dist/types/methods/agentCategory.d.ts +48 -0
- package/dist/types/methods/index.d.ts +9 -55
- package/dist/types/methods/pluginAuth.d.ts +1 -1
- package/dist/types/methods/user.d.ts +9 -1
- package/dist/types/methods/userGroup.d.ts +73 -0
- package/dist/types/methods/userGroup.methods.spec.d.ts +1 -0
- package/dist/types/methods/userGroup.roles.spec.d.ts +1 -0
- package/dist/types/methods/userGroup.spec.d.ts +1 -0
- package/dist/types/models/accessRole.d.ts +30 -0
- package/dist/types/models/aclEntry.d.ts +30 -0
- package/dist/types/models/agentCategory.d.ts +30 -0
- package/dist/types/models/group.d.ts +30 -0
- package/dist/types/models/index.d.ts +4 -0
- package/dist/types/schema/accessRole.d.ts +39 -0
- package/dist/types/schema/aclEntry.d.ts +39 -0
- package/dist/types/schema/agentCategory.d.ts +39 -0
- package/dist/types/schema/defaults.d.ts +4 -0
- package/dist/types/schema/group.d.ts +37 -0
- package/dist/types/schema/index.d.ts +2 -0
- package/dist/types/schema/preset.d.ts +1 -0
- package/dist/types/schema/prompt.d.ts +2 -9
- package/dist/types/schema/promptGroup.d.ts +4 -18
- package/dist/types/types/accessRole.d.ts +40 -0
- package/dist/types/types/aclEntry.d.ts +52 -0
- package/dist/types/types/agent.d.ts +8 -0
- package/dist/types/types/agentCategory.d.ts +41 -0
- package/dist/types/types/convo.d.ts +1 -0
- package/dist/types/types/group.d.ts +46 -0
- package/dist/types/types/index.d.ts +5 -0
- package/dist/types/types/pluginAuth.d.ts +1 -0
- package/dist/types/types/prompts.d.ts +50 -0
- package/dist/types/types/user.d.ts +2 -0
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/transactions.d.ts +14 -0
- package/package.json +1 -1
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/)
|