typescript 如何将typeDORM与NestJS和AWS Lambda配合使用

xxls0lw8  于 2022-12-19  发布在  TypeScript
关注(0)|答案(1)|浏览(131)

我正在尝试构建一个NestJS项目,并使用typeDORM来保存和接收来自DynamoDB的项目。最终,应用程序应该部署为AWS Lambda。对于本地测试,我使用serverless offline
下面是一个项目来证明我的问题:https://github.com/m-kotek/serverless-typedorm

有什么问题

如果应用程序以serverless offline startserverless invoke local -f main启动,则Lambda崩溃。
我按照typeDORM's github上的指令在main.ts中调用了createConnection(),但是如果getEntityManager()现在由app.service.ts的构造函数执行,则输出如下:

[Nest] 24576  - 23.11.2022, 19:36:20     LOG [NestFactory] Starting Nest application...
[Nest] 24576  - 23.11.2022, 19:36:20   ERROR [ExceptionHandler] No such connection with name "default" exists
✖ Lambda timeout.

我还尝试了Singleton模式方法,其中entityManager被初始化,然后导出到main.ts中,这导致了以下输出:

无服务器脱机启动时的输出

[Nest] 25733  - 23.11.2022, 19:47:11     LOG [NestFactory] Starting Nest application...
[Nest] 25733  - 23.11.2022, 19:47:11   ERROR [ExceptionHandler]
✖ Lambda timeout.

使用无服务器调用local -f main的输出

[Nest] 26873  - 23.11.2022, 20:47:22     LOG [NestFactory] Starting Nest application... +2ms
[Nest] 26873  - 23.11.2022, 20:47:22   ERROR [ExceptionHandler]
TypeError
at Reflect.getMetadata (/home/mon/Desktop/api-master-serverless/node_modules/reflect-metadata/Reflect.js:354:23)
at DependenciesScanner.isInjectable (/home/mon/Desktop/api-master-serverless/node_modules/@nestjs/core/scanner.js:302:26)
at DependenciesScanner.insertModule (/home/mon/Desktop/api-master-serverless/node_modules/@nestjs/core/scanner.js:73:18)
at DependenciesScanner.scanForModules (/home/mon/Desktop/api-master-serverless/node_modules/@nestjs/core/scanner.js:34:43)
at DependenciesScanner.scan (/home/mon/Desktop/api-master-serverless/node_modules/@nestjs/core/scanner.js:27:20)
at async /home/mon/Desktop/api-master-serverless/node_modules/@nestjs/core/nest-factory.js:95:17
at async Function.asyncRun (/home/mon/Desktop/api-master-serverless/node_modules/@nestjs/core/errors/exceptions-zone.js:22:13)
at async NestFactoryStatic.initialize (/home/mon/Desktop/api-master-serverless/node_modules/@nestjs/core/nest-factory.js:94:13)
at async NestFactoryStatic.create (/home/mon/Desktop/api-master-serverless/node_modules/@nestjs/core/nest-factory.js:37:9)
at async bootstrap (/home/mon/Desktop/api-master-serverless/dist/main.js:33:17)

main.ts:

const documentClient = new DocumentClientV3(new DynamoDBClient({}));

export const masterTable = new Table({
  name: 'master',
  partitionKey: 'PK',
  sortKey: 'SK',
});

export let entityManager: EntityManager = null;

async function bootstrap() {
  config.update({
    region: '--',
    accessKeyId: '--',
    secretAccessKey: '--',
    signatureVersion: '--',
  });

  createConnection({
    table: masterTable,
    name: 'default',
    entities: [Organization],
    documentClient,
  });

  entityManager = getEntityManager();

  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}

bootstrap();

我尝试在index.ts、main.ts中导入“reflect-metadata”,并同时在两者中导入,结果得到相同的输出。
以前有人遇到过这种情况吗?
预先感谢你的帮助

mklgxw1f

mklgxw1f1#

我能够解决这个问题。我完全按照您上面所做的做了,但使用了以下配置:

const documentClient = new DocumentClientV3(new DynamoDBClient({
    region: "local",
    endpoint: "http://localhost:8000"
}));

config.update({
    region: "local",
    dynamodb: {
        endpoint: "http://localhost:8000"
    }
})

注意:我也在本地运行了dynamodb,请按照下面的说明在本地运行dynamodb:Deploying DynamoDB locally on your computer

相关问题