11import { applyDecorators } from '@nestjs/common' ;
2- import { Prop , PropOptions } from '@nestjs/mongoose' ;
32import { FileColumn , FileColumnOptions } from './file-column.decorator' ;
43
5- export type FileSchemaFieldOptions = PropOptions & FileColumnOptions ;
4+ export type FileSchemaFieldOptions = FileColumnOptions & {
5+ // Allow any additional Mongoose-specific options without depending on @nestjs/mongoose types
6+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
7+ [ key : string ] : any ;
8+ } ;
9+
10+ type PropDecoratorFn = ( options ?: unknown ) => PropertyDecorator ;
11+
12+ let cachedPropDecorator : PropDecoratorFn | null | undefined ;
13+ const optionalRequire : ( ( moduleId : string ) => unknown ) | null = ( ( ) : ( (
14+ moduleId : string ,
15+ ) => unknown ) | null => {
16+ try {
17+ return ( Function (
18+ 'return typeof require !== "undefined" ? require : null' ,
19+ ) as ( ) => ( ( moduleId : string ) => unknown ) | null ) ( ) ;
20+ } catch {
21+ return null ;
22+ }
23+ } ) ( ) ;
24+
25+ function getMongoosePropDecorator ( ) : PropDecoratorFn | null {
26+ if ( cachedPropDecorator !== undefined ) {
27+ return cachedPropDecorator ;
28+ }
29+
30+ try {
31+ if ( ! optionalRequire ) {
32+ cachedPropDecorator = null ;
33+ return cachedPropDecorator ;
34+ }
35+ const mongooseModule = optionalRequire ( '@nestjs/mongoose' ) as { Prop ?: PropDecoratorFn } ;
36+ const prop = mongooseModule ?. Prop ;
37+ cachedPropDecorator = typeof prop === 'function' ? prop : null ;
38+ } catch {
39+ cachedPropDecorator = null ;
40+ }
41+
42+ return cachedPropDecorator ;
43+ }
644
745export function FileSchemaField ( options : FileSchemaFieldOptions = { } ) : PropertyDecorator {
846 const fileOptions = {
@@ -11,5 +49,11 @@ export function FileSchemaField(options: FileSchemaFieldOptions = {}): PropertyD
1149 bucketName : options . bucketName || 'media-files-bucket' ,
1250 } ;
1351
14- return applyDecorators ( FileColumn ( options ) , Prop ( fileOptions ) ) ;
52+ const propDecorator = getMongoosePropDecorator ( ) ;
53+
54+ if ( ! propDecorator ) {
55+ return FileColumn ( options ) ;
56+ }
57+
58+ return applyDecorators ( FileColumn ( options ) , propDecorator ( fileOptions ) ) ;
1559}
0 commit comments