Use UUIDs in group database schema

This commit is contained in:
Fedor Indutny
2021-10-26 15:59:08 -07:00
committed by GitHub
parent 74fde10ff5
commit 63fcdbe787
79 changed files with 4530 additions and 3664 deletions

View File

@@ -1,14 +1,21 @@
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { v4 as generateUUID } from 'uuid';
import { strictAssert } from '../util/assert';
import { isValidGuid } from '../util/isValidGuid';
export type UUIDStringType = `${string}-${string}-${string}-${string}-${string}`;
export const isValidUuid = (value: unknown): value is UUIDStringType =>
typeof value === 'string' &&
/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i.test(
value
);
export class UUID {
constructor(protected readonly value: string) {
strictAssert(isValidGuid(value), `Invalid UUID: ${value}`);
strictAssert(isValidUuid(value), `Invalid UUID: ${value}`);
}
public toString(): UUIDStringType {
@@ -41,4 +48,24 @@ export class UUID {
);
return uuid;
}
public static generate(): UUID {
return new UUID(generateUUID());
}
public static cast(value: UUIDStringType): never;
public static cast(value: string): UUIDStringType;
public static cast(value: string): UUIDStringType {
return new UUID(value.toLowerCase()).toString();
}
// For testing
public static fromPrefix(value: string): UUID {
let padded = value;
while (padded.length < 8) {
padded += '0';
}
return new UUID(`${padded}-0000-4000-8000-${'0'.repeat(12)}`);
}
}