Couchbase wrong expiry timestamp

After inserting a document with expiry date document has the wrong time zone.

how document created

function create(data) {
  return new Promise((resolve, reject) => {
    const id = createObjectID();

    const body = JSON.parse(data.body);
    delete data.body;

    // const expiry = new Date(
    //   moment(new Date()).add(41, "days").utc().format()
    // ).getTime(); //  1 day

    bucket.insert(
      id,
      { ...data, ...body },
      { expiry: 60 * 60 * 24 }, // expiry in one day
      (error) => {
        if (error) reject(error);
        else resolve({ id });
      }
    );
  });
}

what document expiry got

{
  "meta": {
    "id": "5f356e1187fe6895539dd4f1",
    "rev": "1-162ae1a26b4200005f36bf9102000000",
    "expiration": 1597423505,
    "flags": 33554432,
    "type": "json"
  },
  "xattrs": {}
}

expiration timestamp date, Why?

> new Date(1597423505)
1970-01-19T11:43:43.505Z

How to set document to expiry in one day?

Hey @chawki,

Node.js Date object’s represent time in milliseconds as opposed to the seconds you receive in a unix timestamp. So in your case, you’re actually looking to do something like this:

> new Date(1597423505 * 1000)
2020-08-14T16:45:05.000Z

Cheers, Brett

thanks for reply @brett19

then how to set the current timestamp to expiry in one day?

 bucket.insert(
      id,
      { ...data, ...body },
      { expiry:  what to set here to make document expiry in 1 day },
      (error) => {
        if (error) reject(error);
        else resolve({ id });
      }
    );

Hey @chawki,

Your initial code does what you expect and sets the expiry to 1 day.

Cheers, Brett

Hey @brett19

Actually I need to expiry my document in 40 days

What I did

 const expiry = new Date(
      moment(new Date()).add(40, "days").utc().format()
    ).getTime(); //
    console.log(expiry); // 1600796215000 -> 2020-09-22T17:36:55.000Z

    bucket.insert(
      id,
      { ...data, ...body },
      { expiry: expiry  },
      (error) => {
        if (error) reject(error);
        else resolve({ id });
      }
    );

What it returns

TypeError: expiry option needs to between 0 and 2147483647.

https://docs.couchbase.com/java-sdk/3.0/concept-docs/documents.html#expiry

For Couchbase SDKs which accept simple integer expiry values (as opposed to a proper date or time object) allow expiration to be specified in two flavors.

  1. As an offset from the current time.
  2. As an absolute Unix time stamp

If the absolute value of the expiry is less than 30 days ( 60 * 60 * 24 * 30 seconds), it is considered an offset . If the value is greater, it is considered an absolute time stamp .

It might be preferable for applications to normalize the expiration value, such as by always converting it to an absolute time stamp. The conversion is performed to avoid issues when the intended offset is larger than 30 days, in which case it is taken to mean a Unix time stamp and, as a result, the document will expire automatically as soon as it is stored.

2 Likes

Hey @chawki,

Just to add to @vsr1’s answer: Node.js deals with dates almost entirely in terms of ‘milliseconds since epoch’, where as standard unix timestamps are ‘seconds since epoch’. So in your case, again you need to perform that conversion:

// Using Dates's directly:
expiry: (new Date()).getTime() / 1000

// Since I saw you are using moment:
expiry: moment().add(40, "days").unix()

Cheers, Brett

1 Like