首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Dynamoose: Model对象不允许配置作为参数

Dynamoose: Model对象不允许配置作为参数
EN

Stack Overflow用户
提问于 2022-02-18 12:23:17
回答 1查看 524关注 0票数 0

当我在本地机器上运行后端函数时,我已经创建了一个模型,但是当它使用AWS时,当查询表时,我得到和身份验证问题:

代码语言:javascript
复制
2022-02-18T08:54:58.149Z    31785a81-ea8c-434b-832f-6dcff583c01c    ERROR   Unhandled Promise Rejection     
{
    "errorType": "Runtime.UnhandledPromiseRejection",
    "errorMessage": "AccessDeniedException: User: arn:aws:sts::xxxxxxxxx:assumed-role/dev-production-history-role/ppc-backend-functions-dev-queryProductionHistoryItems is not authorized to perform: dynamodb:CreateTable on resource: arn:aws:dynamodb:eu-west-1:xxxxxxxxxxxx:table/dev-production-history-table",
    "trace": [
        "Runtime.UnhandledPromiseRejection: AccessDeniedException: User: arn:aws:sts::xxxxxxxxx:assumed-role/dev-production-history-role/ppc-backend-functions-dev-queryProductionHistoryItems is not authorized to perform: dynamodb:CreateTable on resource: arn:aws:dynamodb:eu-west-1:xxxxxxxxx:table/dev-production-history-table",
        "    at process.<anonymous> (/var/runtime/index.js:35:15)",
        "    at process.emit (events.js:400:28)",
        "    at processPromiseRejections (internal/process/promises.js:245:33)",
        "    at processTicksAndRejections (internal/process/task_queues.js:96:32)"
    ]
}

我的模型就是这样定义的:

代码语言:javascript
复制
const model = dynamoose.model<ProductionHistory>(DatabaseTableNames.productionHistoryTable, {schema});

从可能的解决方案来看,向参数中添加{“create”: false}似乎可以解决这个问题,但在Dynamoose的第3版中,您不能添加三个参数,因此这将无法工作:

代码语言:javascript
复制
 const model = dynamoose.model<ProductionHistory>(DatabaseTableNames.productionHistoryTable,
    schema, {“create”: false});

有人知道如何克服这个问题吗?这样它就能与Dynamoose版本3一起工作吗?

我做了查理·菲什建议的修改,现在我得到了以下错误:

代码语言:javascript
复制
2022-02-18T16:39:39.211Z    b00a36b8-c612-4886-b9fc-da7084527bf0    INFO    AccessDeniedException: User: arn:aws:sts::874124979428:assumed-role/dev-production-history-role/ppc-backend-functions-dev-queryProductionHistoryItems is not authorized to perform: dynamodb:Query on resource: arn:aws:dynamodb:eu-west-1:874124979428:table/dev-production-history-table
    at deserializeAws_json1_0QueryCommandError (/var/task/node_modules/dynamoose/node_modules/@aws-sdk/client-dynamodb/dist-cjs/protocols/Aws_json1_0.js:2984:41)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async /var/task/node_modules/dynamoose/node_modules/@aws-sdk/middleware-serde/dist-cjs/deserializerMiddleware.js:7:24
    at async /var/task/node_modules/dynamoose/node_modules/@aws-sdk/middleware-signing/dist-cjs/middleware.js:11:20
    at async StandardRetryStrategy.retry (/var/task/node_modules/dynamoose/node_modules/@aws-sdk/middleware-retry/dist-cjs/StandardRetryStrategy.js:51:46)
    at async /var/task/node_modules/dynamoose/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:6:22
    at async main (/var/task/node_modules/dynamoose/dist/aws/ddb/internal.js:6:20)
    at async /var/task/node_modules/dynamoose/dist/ItemRetriever.js:105:32
    at async Object.queryByDate (/var/task/functions/production-history/query.js:1:1723)
    at async Runtime.l [as handler] (/var/task/functions/production-history/query.js:1:1974) {
  __type: 'com.amazon.coral.service#AccessDeniedException',
  '$fault': 'client',
  '$metadata': {
    httpStatusCode: 400,
    requestId: 'DCB6SNOH9O2NTRAS9LL3OJGEU7VV4KQNSO5AEMVJF66Q9ASUAAJG',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  '$response': HttpResponse {
    statusCode: 400,
    headers: {
      server: 'Server',
      date: 'Fri, 18 Feb 2022 16:39:39 GMT',
      'content-type': 'application/x-amz-json-1.0',
      'content-length': '331',
      connection: 'keep-alive',
      'x-amzn-requestid': 'DCB6SNOH9O2NTRAS9LL3OJGEU7VV4KQNSO5AEMVJF66Q9ASUAAJG',
      'x-amz-crc32': '2950006190'
    },
    body: IncomingMessage {
      _readableState: [ReadableState],
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      socket: null,
      httpVersionMajor: 1,
      httpVersionMinor: 1,
      httpVersion: '1.1',
      complete: true,
      headers: [Object],
      rawHeaders: [Array],
      trailers: {},
      rawTrailers: [],
      aborted: false,
      upgrade: false,
      url: '',
      method: null,
      statusCode: 400,
      statusMessage: 'Bad Request',
      client: [TLSSocket],
      _consuming: false,
      _dumped: false,
      req: [ClientRequest],
      [Symbol(kCapture)]: false,
      [Symbol(RequestTimeout)]: undefined
    }
  }
}

这是我现在的代码:

代码语言:javascript
复制
const model = dynamoose.model<ProductionHistory>(DatabaseTableNames.productionHistoryTable, schema);
const Table = new dynamoose.Table(DatabaseTableNames.productionHistoryTable, [model], {"create": false, "waitForActive": false});

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-18 15:23:17

免责声明:这个答案是基于Dynamoose v3.0.0 beta 1的。基于beta版本的答案可能很快就过时了,所以一定要检查您的Dynamoose版本的任何更新细节。

在Dynamoose v3中,引入了一个名为Table的新类。这表示一个DynamoDB表。在以前版本的Dynamoose中,Model表示一个单一的DynamoDB表,但是基于这个API还可以表示数据结构中的一个特定实体或模型(例如。电影、订单、用户等)。当涉及到单表设计结构时,这会导致复杂和混乱。

就代码而言,这意味着以下内容。

代码语言:javascript
复制
// If you have the following code in v2:

const User = dynamoose.model("User", {"id": String});

// It will be converted to this in v3:

const User = dynamoose.model("User", {"id": String});
const DBTable = new dynamoose.Table("DBTable", [User]);

基本上,您可以基于您的模型创建一个新的表实例。在v3中,如果您尝试使用您的模型而没有基于它创建一个表实例,它将抛出一个错误。

一旦您这样做,您的表构造函数的第三个参数,您可以传递设置。其中一次是create。因此,您可以将其设置为false作为参数。

具体而言,您的代码如下所示:

代码语言:javascript
复制
const model = dynamoose.model<ProductionHistory(DatabaseTableNames.productionHistoryTable, schema);
const DBTable = new dynamoose.Table(DatabaseTableNames.productionHistoryTable, [model], {"create": false});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71173429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档