Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I define and use a UUID field?

Tags:

strapi

I need a UUID field in a content type, with the help of the introduction below I have modified the file "MyType.settings.json".

https://strapi.io/documentation/3.x.x/guides/models.html#define-the-attributes

"uid": {
  "default": "",
  "type": "uuid"
},

I thought a UUID is automatically saved, but nothing happens.

How can I define and use a UUID field? Can someone give me a hint? Should I also modify the file \api\MyType\controllers\MyType.js?

Thanks in advance!

Benjamin

like image 424
Benjamin Reeves Avatar asked May 31 '19 15:05

Benjamin Reeves


People also ask

How do you define UUID?

What is a UUID. Universally Unique Identifiers, or UUIDS, are 128 bit numbers, composed of 16 octets and represented as 32 base-16 characters, that can be used to identify information across a computer system.

What is a UUID column?

Overview of the UUID data type. A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems. You can create a UUID and use it to uniquely identify something.

What is UUID example?

UUIDs are constructed in a sequence of digits equal to 128 bits. The ID is in hexadecimal digits, meaning it uses the numbers 0 through 9 and letters A through F. The hexadecimal digits are grouped as 32 hexadecimal characters with four hyphens: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.

What is the UUID command?

To generate a single UUID, run the uuidgen command without any arguments. Out of the box, uuidgen will generate a random UUID each time it runs. Execute the following command in your terminal: uuidgen.


1 Answers

You will have to use uuid node module. So keep your attribute and in the lifeCyle functions, set your uuid with the lib.

'use strict';

const uuid = require('uuid');

module.exports = {
  beforeCreate: async (model) => {
    model.set('uid', uuid());
  }
};
like image 180
Jim LAURIE Avatar answered Oct 01 '22 13:10

Jim LAURIE