Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model functions? #382

Closed
schealex opened this issue Jul 16, 2021 · 1 comment
Closed

Model functions? #382

schealex opened this issue Jul 16, 2021 · 1 comment
Labels
question Further information is requested

Comments

@schealex
Copy link

Hey there,

I need to be able to use model document functions as documented here: https://dynamoosejs.com/guide/Model/#modelmethodsdocumentsetname-function

is there a way of doing it using this?

PS: Thanks for this great library!

@hardyscc hardyscc added the enhancement New feature or request label Jul 19, 2021
@hardyscc
Copy link
Owner

hardyscc commented Aug 5, 2021

@schealex you can do the model initialization under the module directly like that

@Module({
  imports: [
    DynamooseModule.forFeature([
      {
        name: 'notification',
        schema: NotificationSchema,
      },
    ]),
  ],
  providers: [NotificationService],
  controllers: [NotificationController],
})
export class NotificationModule implements OnModuleInit {
  constructor(
    @InjectModel('notification')
    private readonly notificationModel: any,
  ) {}

  onModuleInit() {
    this.notificationModel.methods.document.set('logMsg', (msg: string) => {
      console.log(msg);
    });
  }
}

later on, u can call this method as normal

@Injectable()
export class NotificationService {
  constructor(
    @InjectModel('notification')
    private readonly model: Model<Notification, NotificationKey>,
  ) {}

  async findOne(key: NotificationKey) {
    const notification = await this.model.get(key);
    // @ts-ignore
    notification.logMsg('hello');
    return notification;
  }

@hardyscc hardyscc added question Further information is requested and removed enhancement New feature or request labels Aug 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants