refactor(data-schemas): update model and method creation for improved modularity

- Refactored model creation functions to enhance clarity and consistency across the data-schemas.
- Introduced createModels and createMethods functions to streamline the instantiation of Mongoose models and methods.
- Updated test-role.js to utilize the new createModels and createMethods for better organization.
This commit is contained in:
Danny Avila
2025-05-30 12:20:01 -04:00
parent 728d19e361
commit 76e070048c
15 changed files with 107 additions and 117 deletions

View File

@@ -1,9 +1,9 @@
require('dotenv').config({ path: '../.env' });
const mongoose = require('mongoose');
const connect = require('../config/connect');
const { createRoleMethods, createRoleModel } = require('@librechat/data-schemas');
createRoleModel(mongoose);
const { listRoles } = createRoleMethods(mongoose);
const { createModels, createMethods } = require('@librechat/data-schemas');
createModels(mongoose);
const { listRoles } = createMethods(mongoose);
(async () => {
await connect();

View File

@@ -1,68 +1,3 @@
// export { default as roleSchema } from './schema/role';
export { createRoleModel } from './models/role';
export { createRoleMethods } from './methods/role';
// export { default as logger } from './config/winston';
// export { default as meiliLogger } from './config/meiliLogger';
// export * from './types';
// export * from './models';
// export * from './methods';
// // Export schemas (if needed for direct access)
// export { default as userSchema } from './schema/user';
// export { default as sessionSchema } from './schema/session';
// export { default as tokenSchema } from './schema/token';
// // Export utility functions from schemas
// export { signPayload, hashToken } from './schema/session';
// export { default as actionSchema } from './schema/action';
// export { default as agentSchema } from './schema/agent';
// export { default as assistantSchema } from './schema/assistant';
// export { default as balanceSchema } from './schema/balance';
// export { default as bannerSchema } from './schema/banner';
// export type { IBanner } from './schema/banner';
// export { default as categoriesSchema } from './schema/categories';
// export type { ICategory } from './schema/categories';
// export { default as conversationTagSchema } from './schema/conversationTag';
// export type { IConversationTag } from './schema/conversationTag';
// export { default as convoSchema } from './schema/convo';
// export { default as fileSchema } from './schema/file';
// export { default as keySchema } from './schema/key';
// export { default as messageSchema } from './schema/message';
// export type { IMessage } from './schema/message';
// export { default as pluginAuthSchema } from './schema/pluginAuth';
// export type { IPluginAuth } from './schema/pluginAuth';
// export { default as presetSchema } from './schema/preset';
// export type { IPreset } from './schema/preset';
// export { default as projectSchema } from './schema/project';
// export type { IMongoProject } from './schema/project';
// export { default as promptSchema } from './schema/prompt';
// export type { IPrompt } from './schema/prompt';
// export { default as promptGroupSchema } from './schema/promptGroup';
// export type { IPromptGroup, IPromptGroupDocument } from './schema/promptGroup';
// export { default as roleSchema } from './schema/role';
// export { default as shareSchema } from './schema/share';
// export type { ISharedLink } from './schema/share';
// export { default as toolCallSchema } from './schema/toolCall';
// export type { IToolCallData } from './schema/toolCall';
// export { default as transactionSchema } from './schema/transaction';
// export type { ITransaction } from './schema/transaction';
export { createModels } from './models';
export { createMethods } from './methods';
export type * from './types';

View File

@@ -6,7 +6,7 @@ import { createRoleMethods, type RoleMethods } from './role';
/**
* Creates all database methods for all collections
*/
export function createAllMethods(mongoose: typeof import('mongoose')) {
export function createMethods(mongoose: typeof import('mongoose')) {
return {
...createUserMethods(mongoose),
...createSessionMethods(mongoose),
@@ -16,9 +16,3 @@ export function createAllMethods(mongoose: typeof import('mongoose')) {
}
export type AllMethods = UserMethods & SessionMethods & TokenMethods & RoleMethods;
// Also export individual factory functions for granular usage if needed
export { createUserMethods, type UserMethods } from './user';
export { createSessionMethods, type SessionMethods } from './session';
export { createTokenMethods, type TokenMethods } from './token';
export { createRoleMethods, type RoleMethods } from './role';

View File

@@ -1,5 +1,9 @@
import mongoose from 'mongoose';
import actionSchema from '~/schema/action';
import type { IAction } from '~/types';
export const Action = mongoose.models.Action || mongoose.model<IAction>('Action', actionSchema);
/**
* Creates or returns the Action model using the provided mongoose instance and schema
*/
export function createActionModel(mongoose: typeof import('mongoose')) {
return mongoose.models.Action || mongoose.model<IAction>('Action', actionSchema);
}

View File

@@ -1,5 +1,9 @@
import mongoose from 'mongoose';
import agentSchema from '~/schema/agent';
import type { IAgent } from '~/types';
export const Agent = mongoose.models.Agent || mongoose.model<IAgent>('Agent', agentSchema);
/**
* Creates or returns the Agent model using the provided mongoose instance and schema
*/
export function createAgentModel(mongoose: typeof import('mongoose')) {
return mongoose.models.Agent || mongoose.model<IAgent>('Agent', agentSchema);
}

View File

@@ -1,6 +1,9 @@
import mongoose from 'mongoose';
import assistantSchema from '~/schema/assistant';
import type { IAssistant } from '~/types';
export const Assistant =
mongoose.models.Assistant || mongoose.model<IAssistant>('Assistant', assistantSchema);
/**
* Creates or returns the Assistant model using the provided mongoose instance and schema
*/
export function createAssistantModel(mongoose: typeof import('mongoose')) {
return mongoose.models.Assistant || mongoose.model<IAssistant>('Assistant', assistantSchema);
}

View File

@@ -1,6 +1,9 @@
import mongoose from 'mongoose';
import balanceSchema from '~/schema/balance';
import type * as t from '~/types';
export const Balance =
mongoose.models.Balance || mongoose.model<t.IBalance>('Balance', balanceSchema);
/**
* Creates or returns the Balance model using the provided mongoose instance and schema
*/
export function createBalanceModel(mongoose: typeof import('mongoose')) {
return mongoose.models.Balance || mongoose.model<t.IBalance>('Balance', balanceSchema);
}

View File

@@ -1,5 +1,9 @@
import mongoose from 'mongoose';
import bannerSchema from '~/schema/banner';
import type { IBanner } from '~/types';
export const Banner = mongoose.models.Banner || mongoose.model<IBanner>('Banner', bannerSchema);
/**
* Creates or returns the Banner model using the provided mongoose instance and schema
*/
export function createBannerModel(mongoose: typeof import('mongoose')) {
return mongoose.models.Banner || mongoose.model<IBanner>('Banner', bannerSchema);
}

View File

@@ -1,6 +1,11 @@
import mongoose from 'mongoose';
import type * as t from '~/types';
import convoSchema from '~/schema/convo';
export const Conversation =
mongoose.models.Conversation || mongoose.model<t.IConversation>('Conversation', convoSchema);
/**
* Creates or returns the Conversation model using the provided mongoose instance and schema
*/
export function createConversationModel(mongoose: typeof import('mongoose')) {
return (
mongoose.models.Conversation || mongoose.model<t.IConversation>('Conversation', convoSchema)
);
}

View File

@@ -1,5 +1,9 @@
import mongoose from 'mongoose';
import fileSchema from '~/schema/file';
import type { IMongoFile } from '~/types';
export const File = mongoose.models.File || mongoose.model<IMongoFile>('File', fileSchema);
/**
* Creates or returns the File model using the provided mongoose instance and schema
*/
export function createFileModel(mongoose: typeof import('mongoose')) {
return mongoose.models.File || mongoose.model<IMongoFile>('File', fileSchema);
}

View File

@@ -1,12 +1,32 @@
// Export models from their individual files
export { User } from './user';
export { Token } from './token';
export { Session } from './session';
export { Balance } from './balance';
export { Conversation } from './convo';
export { Message } from './message';
export { Agent } from './agent';
export { Role } from './role';
export { Action } from './action';
export { Assistant } from './assistant';
export { File } from './file';
import { createUserModel } from './user';
import { createTokenModel } from './token';
import { createSessionModel } from './session';
import { createBalanceModel } from './balance';
import { createConversationModel } from './convo';
import { createMessageModel } from './message';
import { createAgentModel } from './agent';
import { createRoleModel } from './role';
import { createActionModel } from './action';
import { createAssistantModel } from './assistant';
import { createFileModel } from './file';
import { createBannerModel } from './banner';
/**
* Creates all database models for all collections
*/
export function createModels(mongoose: typeof import('mongoose')) {
return {
User: createUserModel(mongoose),
Token: createTokenModel(mongoose),
Session: createSessionModel(mongoose),
Balance: createBalanceModel(mongoose),
Conversation: createConversationModel(mongoose),
Message: createMessageModel(mongoose),
Agent: createAgentModel(mongoose),
Role: createRoleModel(mongoose),
Action: createActionModel(mongoose),
Assistant: createAssistantModel(mongoose),
File: createFileModel(mongoose),
Banner: createBannerModel(mongoose),
};
}

View File

@@ -1,6 +1,9 @@
import mongoose from 'mongoose';
import messageSchema from '~/schema/message';
import type * as t from '~/types';
export const Message =
mongoose.models.Message || mongoose.model<t.IMessage>('Message', messageSchema);
/**
* Creates or returns the Message model using the provided mongoose instance and schema
*/
export function createMessageModel(mongoose: typeof import('mongoose')) {
return mongoose.models.Message || mongoose.model<t.IMessage>('Message', messageSchema);
}

View File

@@ -1,6 +1,9 @@
import mongoose from 'mongoose';
import sessionSchema from '~/schema/session';
import type * as t from '~/types';
export const Session =
mongoose.models.Session || mongoose.model<t.ISession>('Session', sessionSchema);
/**
* Creates or returns the Session model using the provided mongoose instance and schema
*/
export function createSessionModel(mongoose: typeof import('mongoose')) {
return mongoose.models.Session || mongoose.model<t.ISession>('Session', sessionSchema);
}

View File

@@ -1,5 +1,9 @@
import mongoose from 'mongoose';
import tokenSchema from '~/schema/token';
import type * as t from '~/types';
export const Token = mongoose.models.Token || mongoose.model<t.IToken>('Token', tokenSchema);
/**
* Creates or returns the Token model using the provided mongoose instance and schema
*/
export function createTokenModel(mongoose: typeof import('mongoose')) {
return mongoose.models.Token || mongoose.model<t.IToken>('Token', tokenSchema);
}

View File

@@ -1,5 +1,9 @@
import mongoose from 'mongoose';
import userSchema from '~/schema/user';
import type * as t from '~/types';
export const User = mongoose.models.User || mongoose.model<t.IUser>('User', userSchema);
/**
* Creates or returns the User model using the provided mongoose instance and schema
*/
export function createUserModel(mongoose: typeof import('mongoose')) {
return mongoose.models.User || mongoose.model<t.IUser>('User', userSchema);
}