"ts" field formatting

When creating/updating a document I would like to get the “updatedAt” formatted in order to get the data closer to my model.

  return q.Let(
    {
      doc: q.Create("col", {
        data: {
          a: "b"
        },
      }),
    },
    {
      doc: {
        updatedAt: q.Select(["ts"], q.Var("doc")),
      },
    }
  );

I tried this (as suggested on Slack):

ToTime(Divide(q.Select(["ts"], q.Var("doc"))),1000))

But I get a Number expected, Unresolved Transaction Time provided. error. I assume that the problem is that I’m technically within a transaction that haven’t succeeded yet so the timestamp is not yet “resolved”.

I’m not sure if I can actually do anything here besides transforming the data in the host language after the query is done.

It’s indeed not possible to use the transaction time when it’s not resolved yet. However, we have to go back to the question of why you would need that. There are different approaches depending on what you are trying to achieve.

  • Format at Query time. If it’s simply to format the value when querying then you probably don’t need to save it in a separate field. You could transform the ts (which is a microseconds timestamp) to a Time at query time with the same query snippet as you provided in your question. This won’t be possible from GraphQL though unless you write the resolver yourself which might make it a bit more complex.

  • If you don’t like the idea of processing when you query you could opt to calculate this value in an index binding. Same comment counts here, for GraphQL this is not an easy option.

  • Store the Now() instead which represents the start of the transaction, that’s not entirely the same as the ‘transaction time’ but it is probably close enough in case you are looking for the updated time. Disadvantage is that you are then responsible for updating the Now() each time you update. The two approaches above don’t work in case you are trying to read with GraphQL. This approach is probably the best approach in case you are ok with updating/writing via FQL and want to read with GraphQL.

1 Like