插入数据时的猫鼬触发器

Mongoose trigger on insert data

我将 nest js 框架与猫鼬一起使用,我需要在我的 table 之一上实施触发器并在记录插入时向用户发送通知 ro table 请告诉我什么是最好的在 nest js

中练习这样做

NestJS 猫鼬模块支持钩子。查看文档 here.

@Module({
  imports: [
    MongooseModule.forFeatureAsync([
      {
        name: Cat.name,
        useFactory: () => {
          const schema = CatsSchema;
          schema.pre('save', function() { console.log('Hello from pre save') });
          return schema;
        },
      },
    ]),
  ],
})
export class AppModule {}

您可能感兴趣的另一个途径是称为 Change Streams 的东西,它在 MongoDB 中是原生的。本质上,您“观察”collection/document.

上的变化

https://docs.mongodb.com/drivers/node/current/usage-examples/changeStream/