Skip to content

Commit

Permalink
feat: implement virtuals factory
Browse files Browse the repository at this point in the history
  • Loading branch information
LotfiMEZIANI committed May 22, 2022
1 parent 6dc07b9 commit 1bbf2e6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/factories/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './definitions.factory';
export * from './schema.factory';
export * from './virtuals.factory';
24 changes: 24 additions & 0 deletions lib/factories/virtuals.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as mongoose from 'mongoose';
import { Type } from '@nestjs/common';
import { TypeMetadataStorage } from '../storages/type-metadata.storage';

export class VirtualsFactory {
static createForClass<TClass = any>(
target: Type<TClass>,
schema: mongoose.Schema<TClass>,
): void {
const virtuals = TypeMetadataStorage.getVirtualsMetadataByTarget(target);

virtuals.forEach(({ options, name, getter, setter }) => {
const virtual = schema.virtual(name, options);

if (getter) {
virtual.get(getter);
}

if (setter) {
virtual.set(setter);
}
});
}
}

0 comments on commit 1bbf2e6

Please sign in to comment.