DEV Community

Cover image for Introducing $createdAt And $updatedAt On Each Resource
Everly Precia Suresh for Appwrite

Posted on

Introducing $createdAt And $updatedAt On Each Resource

Appwrite is an open-source backend-as-a-service that abstracts all the complexity involved in building a modern application by providing you with a set of REST APIs for your core backend needs. Appwrite handles user authentication and authorization, real-time databases, cloud functions, webhooks, and much more!

In our latest 0.15 release, we introduced many exciting new features, one among them is the addition of two new timestamp attributes $createdAt and $updatedAt πŸ₯³. We’ll talk about what one can do with it in this blog.

πŸ‘€ What’s New

From 0.15, you should be able to see two newly added attributes to your resources. You can simply check this in the console by clicking on the document's View as JSON option. Navigate to Databases -> [Your database] -> [Your collection] -> [Any document] and on the right side you should see an option to view as JSON. Clicking on that will reveal the newly added attributes.

$createdAtand$updatedAt

As the name suggests, $createdAt will hold the time the resource was created and $updatedAt will hold the latest time the resource was last modified, both in UNIX time format in seconds. These newly added attributes give more flexibility to developers and make the implementation of certain use cases fairly simple. Let's take a look at how! 😎

🧠 Use Cases

1) ⚑ Sorting Documents

One of the use cases is the ability to sort documents based on the latest/oldest. This is ideal in a chat application.

If you want to sort by latest, you can simply use the newly added attributes as an order parameter:


import { Databases } from "appwrite";

const databases = new Databases(client, '[DATABASE_ID]'); // client should be Appwrite Client

const documents = await databases.listDocuments(
  '[COLLECTION_ID]',
  undefined,
  undefined,
  undefined,
  undefined,
  undefined,
  ['$createdAt'],
  ['DESC'],
);
Enter fullscreen mode Exit fullscreen mode

This will pull in the list of documents created and ordered by the latest.

2) πŸ•’ Displaying Time Information

In a blog application, you would want to display the time at which the blog was posted. Another example could be a Q/A forum application where you would also be interested in displaying when the content was last updated.

You can do that by printing a date from a timestamp coming from Appwrite:


import { Databases } from "appwrite";

const databases = new Databases(client, '[DATABASE_ID]'); // client should be Appwrite Client

const document = await database.getDocument('[COLLECTION_ID]', '[DOCUMENT_ID]');

const created = new Date(document.$createdAt * 1000); // Date class expects milliseconds
const updated = new Date(document.$updatedAt * 1000);

console.log(`Document was created on ${created.toGMTString()}, and last updated on ${updated.toGMTString()}.`);
Enter fullscreen mode Exit fullscreen mode

3) πŸ€– Querying Documents

The newly added attributes can be used with the Query API as well, If you want to only list the documents that were created in the last 7 days under the β€œlatest” tab of your application, you can simply do that by using $createdAt in a query:


import { Databases, Query } from "appwrite";

const databases = new Databases(client, '[DATABASE_ID]'); // client should be Appwrite Client

const lastWeekTimestamp = Date.now() - 604800000;

const documents = await databases.listDocuments(
  '[COLLECTION_ID]',
  [ Query.greaterEqual('​​$createdAt', lastWeekTimestamp) ]
);
Enter fullscreen mode Exit fullscreen mode

This will pull in the documents created in the last 7 days.

The above are some of the basic ways the attributes can be utilized. Earlier this would have only been possible with cloud functions making it a little complicated to implement but thanks to the latest release πŸ₯³, it has made such use cases accessible. It now depends on the developers on how they want to utilize the attributes. In general, these attributes are useful to audit system data, order data, query data, and display time information.

With this, we come to an end 🀩. If you have a project to share, need help, or simply want to become a part of the Appwrite community, I would love for you to join the official Appwrite Discord server.

πŸ“š Learn more

You can use the following resources to learn more and get help:

Discussion (0)