【问题标题】:Best practise for getting access to the app instance访问应用实例的最佳实践
【发布时间】:2022-08-13 21:08:44
【问题描述】:

我正在创建一个带有 Firebase 云功能的 NestJS 应用程序。我必须在 NestJS 应用程序中同时使用来自 Firebase 的 onRequestonCreate(Firestore 事件)。如何处理onRequest 事件非常简单。但是,我不确定在必须同时做这两件事时我是否做得对。为了让我将onCreate 事件更改和上下文传递给服务层,我需要访问AppService 类。为此,我需要访问已创建的 app 实例。但是,我觉得我正在创建app 的两个实例(请参阅代码)。我想知道我目前的实施是否最好实践,或者是否有任何方法可以改进。请注意,我是一名前端开发人员,所以这项工作超出了我的舒适范围。我想知道做到这一点的最佳方法,特别是如果我必须处理更多事件,例如onUpateonDelete 等。

import { NestFactory } from \'@nestjs/core\';
import { ExpressAdapter } from \'@nestjs/platform-express\';
import { AppModule } from \'./app.module\';
import * as express from \'express\';
import * as functions from \'firebase-functions\';
import * as admin from \'firebase-admin\';
import { Express } from \'express-serve-static-core\';
import { AppService } from \'./app.service\';

const server = express();

export const createNestServer = async (expressInstance: Express) => {
  //FIRST APP INSTANCE
  const app = await NestFactory.create(
    AppModule,
    new ExpressAdapter(expressInstance),
  );
  admin.initializeApp();
  return app.init();
};

createNestServer(server)
  .then((v) => console.log(\'Nest Ready\'))
  .catch((err) => console.error(\'Nest broken\', err));

//exporting all onRequest events
export const api = functions.https.onRequest(server);
//exporting the  onUserCreate event
exports.onUserCreate = functions.firestore
  .document(\'users/{docId}\')
  .onWrite(async (change, context) => {
    console.log(\'Changes are tracked\');
    //SECOND APP INSTANCE
    const app = await NestFactory.create(AppModule);
    return app.get(AppService).onCreate(change, context);
  });

    标签: node.js firebase google-cloud-firestore google-cloud-functions nestjs


    【解决方案1】:

    我假设您有类似UsersController(和其他)的东西,它作为 NestJS 应用程序的一部分处理检索、创建、更新用户。只需将 Express 实例(由 NestJS 适配器包装)传递给 Cloud Function HTTPS 处理程序 (onRequest)。 NestJS 将负责设置路由。

    export const api = functions.https.onRequest(server)
    
    @Controller('users')
    export class UsersController {
      constructor(private readonly usersService: UserService) {}
    
      @Get(':userId')
      async getUser(@Param('userId') id: String) {
        // retrieve user
      }
    
      @Post()
      async create(@Body() createUserDto: CreateUserDto) {
        // create a new user
      }
    
      ...
    }
    
    @Module({
      controllers: [UsersController],
      providers: [AppService, UsersService],
      exports: [AppService, UsersService]
    })
    export class AppModule {}
    

    AppService 是在AppModule 中注册的提供程序。您可以从 INestApplication 实例中提取它。

    const server = express();
    let appService: AppService;
    
    export const createNestServer = async (expressInstance: Express) => {
      const app = await NestFactory.create(
        AppModule,
        new ExpressAdapter(expressInstance),
      );
      admin.initializeApp();
      appService = app.get(AppService);
      return app.init();
    };
    

    然后你可以在onWrite firestore 触发器中使用它。

    似乎您正在尝试使用同一个 NestJS 应用程序执行 2 个不同的任务。只需让 NestJS(或 ExpressJS)应用程序处理 HTTPS 请求。而对于onCreateonWrite ...触发器实现不同的解决方案。也许不要依赖 NestJS 来处理这些类型的触发器。

    NestJS 个人觉得有点矫枉过正,你可以只使用 ExpressJS。您不必将AppService 注册为提供者并跳过箍。

    【讨论】:

    • 嘿 Chrostophe 非常感谢您的回答。我确实理解这有点矫枉过正,但我​​也在努力学习这个过程中的平台。我已经在下面发布了一个答案,很想听听您对此的反馈,并让我知道它是否是一种好的和可接受的方法。干杯。
    【解决方案2】:

    以下方法使我能够解决应用程序实例的多个实例化问题。希望这是一个好的和可接受的方式来做到这一点。反馈非常受欢迎。

    const server = express();
    
    export const createNestServer = async (expressInstance: Express) => {
      //FIRST APP INSTANCE
      const app = await NestFactory.create(
        AppModule,
        new ExpressAdapter(expressInstance),
      );
    
      admin.initializeApp();
      return app.init();
    };
    
    const main = createNestServer(server);
    
    export const api = functions.https.onRequest(server);
    
    exports.onUserWrite = functions.firestore
      .document('users/{docId}')
      .onWrite((change, context) =>
        main.then((app) => {
          return app.get(AppService).onCreate(change, context);
        }),
      );
    

    【讨论】:

      猜你喜欢
      • 2011-06-27
      • 2013-01-08
      • 1970-01-01
      • 2011-02-21
      • 2012-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多