mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-28 22:28:51 +01:00
| .. | ||
| index.ts | ||
| README.md | ||
| session.ts | ||
| token.ts | ||
| user.ts | ||
Methods
This directory contains pure functions that replace the static methods from the schema files. This refactoring improves testability, type safety, and code modularity.
Structure
userMethods.ts- Functions for user operationssessionMethods.ts- Functions for session operationstokenMethods.ts- Functions for token operationsindex.ts- Exports all methods for convenient importing
Migration from Static Methods
Instead of calling static methods on models:
// OLD: Using static methods
const user = await UserModel.findUser({ email: 'test@example.com' });
const result = await UserModel.deleteUserById(userId);
Use the pure functions with the model as the first parameter:
// NEW: Using pure functions
import { findUser, deleteUserById } from '~/methods';
import UserModel from '~/schema/user';
const user = await findUser(UserModel, { email: 'test@example.com' });
const result = await deleteUserById(UserModel, userId);
Benefits
- Pure Functions: Methods are now side-effect free and testable
- Better Types: Proper TypeScript typing throughout
- Dependency Injection: Models are passed as parameters
- Modular: Functions can be imported individually or as a group
- No Magic: Clear explicit dependencies
Usage Examples
User Methods
import { createUser, findUser, updateUser } from '~/methods';
import UserModel from '~/schema/user';
// Create a user
const newUser = await createUser(
UserModel,
{ email: 'user@example.com', name: 'John' },
{ enabled: true, startBalance: 100 }
);
// Find a user
const user = await findUser(UserModel, { email: 'user@example.com' });
// Update a user
const updated = await updateUser(UserModel, userId, { name: 'Jane' });
Session Methods
import { createSession, findSession, deleteSession } from '~/methods';
import SessionModel from '~/schema/session';
// Create session
const { session, refreshToken } = await createSession(SessionModel, userId);
// Find session
const foundSession = await findSession(SessionModel, { refreshToken });
// Delete session
await deleteSession(SessionModel, { sessionId });
Token Methods
import { createToken, findToken, deleteTokens } from '~/methods';
import TokenModel from '~/schema/token';
// Create token
const token = await createToken(TokenModel, {
userId,
token: 'abc123',
expiresIn: 3600
});
// Find token
const foundToken = await findToken(TokenModel, { token: 'abc123' });
// Delete tokens
await deleteTokens(TokenModel, { userId });