mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-08-04 14:57:42 +00:00
fix: preserve file defaults in atomic upsert
This commit is contained in:
parent
6d3a0b955d
commit
64a32166e0
2 changed files with 11 additions and 3 deletions
|
|
@ -1,7 +1,7 @@
|
|||
import mongoose from 'mongoose';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { MongoMemoryServer } from 'mongodb-memory-server';
|
||||
import { EToolResources, FileContext } from 'librechat-data-provider';
|
||||
import { EToolResources, FileContext, FileSources } from 'librechat-data-provider';
|
||||
import { _resetStrictCache } from '~/models/plugins/tenantIsolation';
|
||||
import { runAsSystem } from '~/config/tenantContext';
|
||||
import { createFileMethods } from './file';
|
||||
|
|
@ -63,6 +63,9 @@ describe('File Methods', () => {
|
|||
expect(file).not.toBeNull();
|
||||
expect(file?.file_id).toBe(fileId);
|
||||
expect(file?.filename).toBe('test.txt');
|
||||
expect(file?.object).toBe('file');
|
||||
expect(file?.usage).toBe(0);
|
||||
expect(file?.source).toBe(FileSources.local);
|
||||
expect(file?.expiresAt).toBeDefined();
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import { EToolResources, FileContext } from 'librechat-data-provider';
|
||||
import { EToolResources, FileContext, FileSources } from 'librechat-data-provider';
|
||||
import type { FilterQuery, SortOrder, Model } from 'mongoose';
|
||||
import type { IMongoFile } from '~/types/file';
|
||||
import { tenantSafeBulkWrite } from '~/utils/tenantBulkWrite';
|
||||
|
|
@ -368,6 +368,11 @@ export function createFileMethods(mongoose: typeof import('mongoose')): {
|
|||
const definedFields = Object.fromEntries(
|
||||
Object.entries(fields).filter(([, value]) => value !== undefined),
|
||||
);
|
||||
const insertDefaults = {
|
||||
object: { $ifNull: ['$object', 'file'] },
|
||||
usage: { $ifNull: ['$usage', 0] },
|
||||
source: { $ifNull: ['$source', FileSources.local] },
|
||||
};
|
||||
let expiryUpdate = {};
|
||||
if (expiredAt instanceof Date) {
|
||||
expiryUpdate = {
|
||||
|
|
@ -379,7 +384,7 @@ export function createFileMethods(mongoose: typeof import('mongoose')): {
|
|||
|
||||
return File.findOneAndUpdate(
|
||||
{ file_id: data.file_id },
|
||||
[{ $set: { ...definedFields, ...expiryUpdate } }],
|
||||
[{ $set: { ...insertDefaults, ...definedFields, ...expiryUpdate } }],
|
||||
{
|
||||
new: true,
|
||||
upsert: true,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue