Category: How do I do X?
Updated

This solution is summarized from an archived support forum post. This information may have changed. If you notice an error, please let us know in Discord.

How to generate the UUIDv4 value?

Issue

I need to insert a new record into a PostgreSQL table and specify the value for the ID field using UUIDv4. I am wondering if there is an existing function to generate UUID, or if I need to implement one myself using Math.random. I also noticed that the browser has a built-in crypto function for UUID generation, but it is not yet supported in Appsmith.

Resolution

The task is to insert a new record into a PostgreSQL database table and specify an ID value using UUIDv4. One way to generate UUID is to use Math.random, but it is not advisable.

A better approach is to use built-in PostgreSQL functionality to generate UUID or use JavaScript's built-in crypto functions if appsmith supports it. The following code example shows how to generate UUID in PostgreSQL:

INSERT INTO mytable (id, column1, column2)
VALUES (uuid_generate_v4(), 'value1', 'value2');

If JavaScript's built-in crypto functions are available, then generating UUID becomes easier, as shown in the following code example:

let myUUID = crypto.randomUUID();

This uses the Crypto interface available in the current context to access a cryptographically strong random number generator and cryptographic primitives.