Skip to content

Commit f876301

Browse files
committed
Feature removed mongoose dependancy for typeorm based projects
1 parent 2e274da commit f876301

File tree

4 files changed

+68
-12
lines changed

4 files changed

+68
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ A powerful and flexible NestJS module for integrating MinIO object storage into
3333
- 🎯 TypeScript support
3434
- 📝 Swagger documentation support
3535
- 🔄 RxJS integration
36+
- 🧩 Optional `@nestjs/mongoose` integration (only required if you use `@FileSchemaField`)
3637
- 🤖 Automatic presigned URL detection even for raw QueryBuilder results
3738

3839
## Installation
@@ -219,6 +220,7 @@ These decorators provide:
219220
- 🔄 Seamless MongoDB integration
220221
- 🤖 Automatic presigned URL generation even for raw QueryBuilder objects (bucket names are auto-detected from your MinIO config)
221222
- 🎯 Type safety with TypeScript
223+
- 🧩 Optional Mongoose dependency (install `@nestjs/mongoose` only if you plan to use `@FileSchemaField`)
222224

223225
## Configuration
224226

package-lock.json

Lines changed: 12 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nestjs-minio-backend",
3-
"version": "1.0.14",
3+
"version": "1.0.16",
44
"description": "NestJS module for MinIO integration",
55
"author": "Mishhub",
66
"license": "MIT",
@@ -64,11 +64,16 @@
6464
"@nestjs/core": "^10.0.0 || ^11.0.0",
6565
"@nestjs/mongoose": "^10.0.0 || ^11.0.0",
6666
"@nestjs/platform-express": "^10.0.0 || ^11.0.0",
67-
"@nestjs/swagger": "^8.0.0",
67+
"@nestjs/swagger": "^8.0.0 || ^11.0.0",
6868
"class-validator": "^0.14.1",
6969
"minio": "^8.0.5",
7070
"rxjs": "^7.8.1"
7171
},
72+
"peerDependenciesMeta": {
73+
"@nestjs/mongoose": {
74+
"optional": true
75+
}
76+
},
7277
"devDependencies": {
7378
"@types/jest": "^29.5.14",
7479
"@types/node": "^22.14.0",
Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
11
import { applyDecorators } from '@nestjs/common';
2-
import { Prop, PropOptions } from '@nestjs/mongoose';
32
import { 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

745
export 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

Comments
 (0)