0

Currently we have it set up such that the start date and end date are required to query results, looking something like this.

interface GetStuffIdArgs extends DdbPaginationQueryArgs {
  // Required query params
  startTime: string;
  endTime: string;
async getOtherStuff(id: string | number, args: GetOtherStuffIdArgs) {
    const { startTime, endTime, id, someType, order = "DESC" } = args;

    // convert times to ISO (utc 0);
    const startTimeISO = new Date(startTime).toISOString();
    const endTimeISO = new Date(endTime).toISOString();
      /**
       * Query between start and end time
       */
      KeyConditionExpression: `${this.PK} = :pk and ${this.SK} BETWEEN :starttime AND :endtime`

I've tried a few things that I thought could make the parameter optional but am not sure how the query itself would change based off of it.

interface GetStuffIdArgs extends DdbPaginationQueryArgs {
  // Required query params
  startTime: string;
  endTime: string | null;
  async getOtherStuff(id: string | number, args: GetOtherStuffIdArgs) {
    const { startTime, endTime, id, someType, order = "DESC" } = args;

    // convert times to ISO (utc 0);
    const startTimeISO = new Date(startTime).toISOString();
    const endTimeISO = endTime ? new Date(endTime).toISOString() : null;

If this is even the correct way, I'm not quite sure how to make the query change based off of it.

First time I've ever looked at dynamodb. I'm curious how I can make the :et optional such that if it is null, it will simply query everything from the start date and on? Perhaps the better question would be: is it possible to make the KeyConditionExpression conditional such that it could be one of two things depending on params?

kastaplastislife
  • 293
  • 2
  • 16

1 Answers1

0

If the data is historical (always in the past) you could just do this:

const endTimeISO = endTime ? new Date(endTime).toISOString() : new Date().toISOString();

If the date may be in the future you'd just need to use a date beyond the max.

const endTimeISO = endTime ? new Date(endTime).toISOString() : new Date(8640000000000000).toISOString();

(according to this post the above is the max date value for JS)

Jason Wadsworth
  • 8,059
  • 19
  • 32