Skip to content

Commit e70232a

Browse files
committed
feat: implement virtual decorator
1 parent de9b3fb commit e70232a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/decorators/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './prop.decorator';
22
export * from './schema.decorator';
3+
export * from './virtual.decorator';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { VirtualTypeOptions } from 'mongoose';
2+
import { TypeMetadataStorage } from '../storages/type-metadata.storage';
3+
4+
/**
5+
* Interface defining property options that can be passed to `@Virtual()` decorator.
6+
*/
7+
export interface VirtualOptions {
8+
options?: VirtualTypeOptions;
9+
subPath?: string;
10+
get?: (...args: any[]) => any;
11+
set?: (...args: any[]) => any;
12+
}
13+
14+
/**
15+
* @Virtual decorator is used to mark a specific class property as a Mongoose virtual.
16+
*/
17+
export function Virtual(options?: VirtualOptions): PropertyDecorator {
18+
return (target: object, propertyKey: string | symbol) => {
19+
TypeMetadataStorage.addVirtualMetadata({
20+
target: target.constructor,
21+
options: options?.options,
22+
name:
23+
propertyKey.toString() +
24+
(options?.subPath ? `.${options.subPath}` : ''),
25+
setter: options?.set,
26+
getter: options?.get,
27+
});
28+
};
29+
}

0 commit comments

Comments
 (0)