forked from twentyhq/twenty
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: create deterministic uuids for standards relation on custom obje…
…ct (twentyhq#4642) * fix: create deterministic uuids for standards relation on custom object * fix: remove check if standardId already exist to override old ones
- Loading branch information
Showing
4 changed files
with
79 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 27 additions & 2 deletions
29
.../engine/workspace-manager/workspace-sync-metadata/utils/create-deterministic-uuid.util.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,35 @@ | ||
import { createHash } from 'crypto'; | ||
|
||
export const createDeterministicUuid = (inputUuid: string): string => { | ||
const hash = createHash('sha256').update(inputUuid).digest('hex'); | ||
export function createDeterministicUuid(uuid: string): string; | ||
export function createDeterministicUuid(uuids: string[]): string; | ||
|
||
export function createDeterministicUuid( | ||
uuidOrUuids: string[] | string, | ||
): string { | ||
const inputForHash = Array.isArray(uuidOrUuids) | ||
? uuidOrUuids.join('-') | ||
: uuidOrUuids; | ||
const hash = createHash('sha256').update(inputForHash).digest('hex'); | ||
|
||
return `20202020-${hash.substring(0, 4)}-4${hash.substring( | ||
4, | ||
7, | ||
)}-8${hash.substring(7, 10)}-${hash.substring(10, 22)}`; | ||
} | ||
|
||
type UuidPair = { | ||
objectId: string; | ||
standardId: string; | ||
}; | ||
|
||
export const createRelationDeterministicUuid = (uuidPair: UuidPair): string => { | ||
// Chaging the order in the array will result in different UUIDs | ||
return createDeterministicUuid([uuidPair.objectId, uuidPair.standardId]); | ||
}; | ||
|
||
export const createForeignKeyDeterministicUuid = ( | ||
uuidPair: UuidPair, | ||
): string => { | ||
// Chaging the order in the array will result in different UUIDs | ||
return createDeterministicUuid([uuidPair.standardId, uuidPair.objectId]); | ||
}; |