import {query,HEX2uuid,UUID2hex} from '@commtool/sql-query'
// get an UID from the database
// it is better to get all UID's generated at the database server, so that thye are from the same range
// this keeps the search tree inside the database for the UID index more efficient
export const getUID=async (req)=>
{
if(req.body.UID)
return UUID2hex(req.body.UID)
else
{
const [{UID}]=await query(`SELECT UIDV1() AS UID`)
req.body.UID=HEX2uuid(UID)
return UID
}
}
/**
* Checks if the given UID is a valid UUID string with a specific prefix.
*
* The UID must be a string that matches the following pattern:
* - Starts with "UUID-"
* - Follows the UUID version 1-5 format
*
* @param {string} UID - The UID string to validate.
* @returns {boolean} True if the UID is valid, false otherwise.
*/
export const isValidUID = (UID) => {
return typeof UID === 'string' && UID.match(/^UUID\-[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[1..5][0-9A-Fa-f]{3}-[89ABab][0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}$/)
}