🐛 fix: Move MemoryEntry and PluginAuth model retrieval inside methods for Runtime Usage

This commit is contained in:
Danny Avila 2025-06-25 20:58:34 -04:00
parent cbda3cb529
commit 799f0e5810
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
2 changed files with 10 additions and 5 deletions

View file

@ -11,8 +11,6 @@ const formatDate = (date: Date): string => {
// Factory function that takes mongoose instance and returns the methods // Factory function that takes mongoose instance and returns the methods
export function createMemoryMethods(mongoose: typeof import('mongoose')) { export function createMemoryMethods(mongoose: typeof import('mongoose')) {
const MemoryEntry = mongoose.models.MemoryEntry;
/** /**
* Creates a new memory entry for a user * Creates a new memory entry for a user
* Throws an error if a memory with the same key already exists * Throws an error if a memory with the same key already exists
@ -28,6 +26,7 @@ export function createMemoryMethods(mongoose: typeof import('mongoose')) {
return { ok: false }; return { ok: false };
} }
const MemoryEntry = mongoose.models.MemoryEntry;
const existingMemory = await MemoryEntry.findOne({ userId, key }); const existingMemory = await MemoryEntry.findOne({ userId, key });
if (existingMemory) { if (existingMemory) {
throw new Error('Memory with this key already exists'); throw new Error('Memory with this key already exists');
@ -63,6 +62,7 @@ export function createMemoryMethods(mongoose: typeof import('mongoose')) {
return { ok: false }; return { ok: false };
} }
const MemoryEntry = mongoose.models.MemoryEntry;
await MemoryEntry.findOneAndUpdate( await MemoryEntry.findOneAndUpdate(
{ userId, key }, { userId, key },
{ {
@ -89,6 +89,7 @@ export function createMemoryMethods(mongoose: typeof import('mongoose')) {
*/ */
async function deleteMemory({ userId, key }: t.DeleteMemoryParams): Promise<t.MemoryResult> { async function deleteMemory({ userId, key }: t.DeleteMemoryParams): Promise<t.MemoryResult> {
try { try {
const MemoryEntry = mongoose.models.MemoryEntry;
const result = await MemoryEntry.findOneAndDelete({ userId, key }); const result = await MemoryEntry.findOneAndDelete({ userId, key });
return { ok: !!result }; return { ok: !!result };
} catch (error) { } catch (error) {
@ -105,6 +106,7 @@ export function createMemoryMethods(mongoose: typeof import('mongoose')) {
userId: string | Types.ObjectId, userId: string | Types.ObjectId,
): Promise<t.IMemoryEntryLean[]> { ): Promise<t.IMemoryEntryLean[]> {
try { try {
const MemoryEntry = mongoose.models.MemoryEntry;
return (await MemoryEntry.find({ userId }).lean()) as t.IMemoryEntryLean[]; return (await MemoryEntry.find({ userId }).lean()) as t.IMemoryEntryLean[];
} catch (error) { } catch (error) {
throw new Error( throw new Error(

View file

@ -1,16 +1,14 @@
import type { DeleteResult, Model } from 'mongoose'; import type { DeleteResult, Model } from 'mongoose';
import type { IPluginAuth } from '~/schema/pluginAuth';
import type { import type {
FindPluginAuthsByKeysParams, FindPluginAuthsByKeysParams,
UpdatePluginAuthParams, UpdatePluginAuthParams,
DeletePluginAuthParams, DeletePluginAuthParams,
FindPluginAuthParams, FindPluginAuthParams,
IPluginAuth,
} from '~/types'; } from '~/types';
// Factory function that takes mongoose instance and returns the methods // Factory function that takes mongoose instance and returns the methods
export function createPluginAuthMethods(mongoose: typeof import('mongoose')) { export function createPluginAuthMethods(mongoose: typeof import('mongoose')) {
const PluginAuth: Model<IPluginAuth> = mongoose.models.PluginAuth;
/** /**
* Finds a single plugin auth entry by userId and authField * Finds a single plugin auth entry by userId and authField
*/ */
@ -19,6 +17,7 @@ export function createPluginAuthMethods(mongoose: typeof import('mongoose')) {
authField, authField,
}: FindPluginAuthParams): Promise<IPluginAuth | null> { }: FindPluginAuthParams): Promise<IPluginAuth | null> {
try { try {
const PluginAuth: Model<IPluginAuth> = mongoose.models.PluginAuth;
return await PluginAuth.findOne({ userId, authField }).lean(); return await PluginAuth.findOne({ userId, authField }).lean();
} catch (error) { } catch (error) {
throw new Error( throw new Error(
@ -39,6 +38,7 @@ export function createPluginAuthMethods(mongoose: typeof import('mongoose')) {
return []; return [];
} }
const PluginAuth: Model<IPluginAuth> = mongoose.models.PluginAuth;
return await PluginAuth.find({ return await PluginAuth.find({
userId, userId,
pluginKey: { $in: pluginKeys }, pluginKey: { $in: pluginKeys },
@ -60,6 +60,7 @@ export function createPluginAuthMethods(mongoose: typeof import('mongoose')) {
value, value,
}: UpdatePluginAuthParams): Promise<IPluginAuth> { }: UpdatePluginAuthParams): Promise<IPluginAuth> {
try { try {
const PluginAuth: Model<IPluginAuth> = mongoose.models.PluginAuth;
const existingAuth = await PluginAuth.findOne({ userId, pluginKey, authField }).lean(); const existingAuth = await PluginAuth.findOne({ userId, pluginKey, authField }).lean();
if (existingAuth) { if (existingAuth) {
@ -95,6 +96,7 @@ export function createPluginAuthMethods(mongoose: typeof import('mongoose')) {
all = false, all = false,
}: DeletePluginAuthParams): Promise<DeleteResult> { }: DeletePluginAuthParams): Promise<DeleteResult> {
try { try {
const PluginAuth: Model<IPluginAuth> = mongoose.models.PluginAuth;
if (all) { if (all) {
const filter: DeletePluginAuthParams = { userId }; const filter: DeletePluginAuthParams = { userId };
if (pluginKey) { if (pluginKey) {
@ -120,6 +122,7 @@ export function createPluginAuthMethods(mongoose: typeof import('mongoose')) {
*/ */
async function deleteAllUserPluginAuths(userId: string): Promise<DeleteResult> { async function deleteAllUserPluginAuths(userId: string): Promise<DeleteResult> {
try { try {
const PluginAuth: Model<IPluginAuth> = mongoose.models.PluginAuth;
return await PluginAuth.deleteMany({ userId }); return await PluginAuth.deleteMany({ userId });
} catch (error) { } catch (error) {
throw new Error( throw new Error(