2

我已经在 GitHub 上打开了一个相关问题,但也许这里有人可以更快地提供帮助。

概括:

ValidationException: Query key condition not supported 我需要在给定位置的最后(数量)秒内查找记录。很简单,但已经与其他问题相关: 一个另一个

作品

Activity.query('locationId').eq(locationId).exec();

不工作

Activity.query('locationId').eq(locationId).where('createdAt').ge(date).exec();

代码示例:

架构
const Activity = (dynamoose: typeof dynamooseType) => dynamoose.model<ActivityType, {}>('Activity',
    new Schema({
      id: {
        type: String,
        default: () => {
          return uuid();
        },
        hashKey: true,
      },
      userId: {
        type: String,
      },
      locationId: {
        type: String,
        rangeKey: true,
        index: {
          global: true,
        },
      },
      createdAt: { type: Number, rangeKey: true, required: true, default: Date.now },
      action: {
        type: Number,
      },
    }, {
      expires: 60 * 60 * 24 * 30 * 3, //  activity logs to expire after 3 months
    }));
执行函数的代码

有趣的是,我发现这是建议使用的解决方法,直到他们合并 PR 赋予将时间戳指定为键的能力,但不幸的是它不起作用。

async getActivitiesInLastSeconds(locationId: string, timeoutInSeconds: number) {
    const Activity = schema.Activity(this.dynamoose);
    const date = moment().subtract(timeoutInSeconds, 'seconds').valueOf();
    return await Activity.query('locationId').eq(locationId)
      .where('createdAt').ge(date).exec();
  }
4

2 回答 2

5

我怀疑createdAt不是您的表/索引的范围键。您需要执行.filter('createdAt').ge(date)或修改您的表/索引架构。

于 2018-09-22T11:01:36.880 回答
0

我很确定问题在于,当您rangeKey: truecreatedAt属性上指定时,您是在告诉要在全局索引上使用它(我认为这不是正确的术语)。该范围键将链接到该id属性。

我相信最简单的解决方案是将locationId索引更改为如下所示:

index: {
    global: true,
    rangeKey: 'createdAt',
},

这样,您就可以非常明确地将哪个索引设置createdAtrangeKeyfor。

进行更改后,请记住将更改与本地 DynamoDB 服务器或实际 DynamoDB 服务同步,以便将更改填充到您的代码和数据库系统中。

希望这会有所帮助!如果它不能解决您的问题,请随时发表评论,我会进一步帮助您。

于 2018-09-22T23:08:00.533 回答