From 76e070048c6f361bc8e993a05be6934c12eca332 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Fri, 30 May 2025 12:20:01 -0400 Subject: [PATCH] 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. --- api/test-role.js | 6 +- packages/data-schemas/src/index.ts | 71 +------------------ packages/data-schemas/src/methods/index.ts | 8 +-- packages/data-schemas/src/models/action.ts | 8 ++- packages/data-schemas/src/models/agent.ts | 8 ++- packages/data-schemas/src/models/assistant.ts | 9 ++- packages/data-schemas/src/models/balance.ts | 9 ++- packages/data-schemas/src/models/banner.ts | 8 ++- packages/data-schemas/src/models/convo.ts | 11 ++- packages/data-schemas/src/models/file.ts | 8 ++- packages/data-schemas/src/models/index.ts | 44 ++++++++---- packages/data-schemas/src/models/message.ts | 9 ++- packages/data-schemas/src/models/session.ts | 9 ++- packages/data-schemas/src/models/token.ts | 8 ++- packages/data-schemas/src/models/user.ts | 8 ++- 15 files changed, 107 insertions(+), 117 deletions(-) diff --git a/api/test-role.js b/api/test-role.js index aee14451b6..56a97b9dba 100644 --- a/api/test-role.js +++ b/api/test-role.js @@ -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(); diff --git a/packages/data-schemas/src/index.ts b/packages/data-schemas/src/index.ts index 6f1521e755..05b39ebcad 100644 --- a/packages/data-schemas/src/index.ts +++ b/packages/data-schemas/src/index.ts @@ -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'; diff --git a/packages/data-schemas/src/methods/index.ts b/packages/data-schemas/src/methods/index.ts index 392ec95c02..324159b0ae 100644 --- a/packages/data-schemas/src/methods/index.ts +++ b/packages/data-schemas/src/methods/index.ts @@ -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'; diff --git a/packages/data-schemas/src/models/action.ts b/packages/data-schemas/src/models/action.ts index b42cda0d43..4778222460 100644 --- a/packages/data-schemas/src/models/action.ts +++ b/packages/data-schemas/src/models/action.ts @@ -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('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('Action', actionSchema); +} diff --git a/packages/data-schemas/src/models/agent.ts b/packages/data-schemas/src/models/agent.ts index 2aad2c3cf6..bff6bae60d 100644 --- a/packages/data-schemas/src/models/agent.ts +++ b/packages/data-schemas/src/models/agent.ts @@ -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('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('Agent', agentSchema); +} diff --git a/packages/data-schemas/src/models/assistant.ts b/packages/data-schemas/src/models/assistant.ts index 437eefd6e6..bf8a9f5ac7 100644 --- a/packages/data-schemas/src/models/assistant.ts +++ b/packages/data-schemas/src/models/assistant.ts @@ -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('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('Assistant', assistantSchema); +} diff --git a/packages/data-schemas/src/models/balance.ts b/packages/data-schemas/src/models/balance.ts index 2cc174fef0..e7ace38937 100644 --- a/packages/data-schemas/src/models/balance.ts +++ b/packages/data-schemas/src/models/balance.ts @@ -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('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('Balance', balanceSchema); +} diff --git a/packages/data-schemas/src/models/banner.ts b/packages/data-schemas/src/models/banner.ts index 625551e3f2..7be6e2e07b 100644 --- a/packages/data-schemas/src/models/banner.ts +++ b/packages/data-schemas/src/models/banner.ts @@ -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('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('Banner', bannerSchema); +} diff --git a/packages/data-schemas/src/models/convo.ts b/packages/data-schemas/src/models/convo.ts index 9de53c577c..13a36e2069 100644 --- a/packages/data-schemas/src/models/convo.ts +++ b/packages/data-schemas/src/models/convo.ts @@ -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('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('Conversation', convoSchema) + ); +} diff --git a/packages/data-schemas/src/models/file.ts b/packages/data-schemas/src/models/file.ts index 3fbc982ba1..c12dbec140 100644 --- a/packages/data-schemas/src/models/file.ts +++ b/packages/data-schemas/src/models/file.ts @@ -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('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('File', fileSchema); +} diff --git a/packages/data-schemas/src/models/index.ts b/packages/data-schemas/src/models/index.ts index febbf31691..dd4f1bd8e1 100644 --- a/packages/data-schemas/src/models/index.ts +++ b/packages/data-schemas/src/models/index.ts @@ -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), + }; +} diff --git a/packages/data-schemas/src/models/message.ts b/packages/data-schemas/src/models/message.ts index 7cd51dc9c7..cb5bd9e7d3 100644 --- a/packages/data-schemas/src/models/message.ts +++ b/packages/data-schemas/src/models/message.ts @@ -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('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('Message', messageSchema); +} diff --git a/packages/data-schemas/src/models/session.ts b/packages/data-schemas/src/models/session.ts index 142ab94c09..3d4eba2761 100644 --- a/packages/data-schemas/src/models/session.ts +++ b/packages/data-schemas/src/models/session.ts @@ -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('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('Session', sessionSchema); +} diff --git a/packages/data-schemas/src/models/token.ts b/packages/data-schemas/src/models/token.ts index 2f8b1a7aa2..870233f615 100644 --- a/packages/data-schemas/src/models/token.ts +++ b/packages/data-schemas/src/models/token.ts @@ -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('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('Token', tokenSchema); +} diff --git a/packages/data-schemas/src/models/user.ts b/packages/data-schemas/src/models/user.ts index df59a05999..1aef66f6d1 100644 --- a/packages/data-schemas/src/models/user.ts +++ b/packages/data-schemas/src/models/user.ts @@ -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('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('User', userSchema); +}