feat: replace tenants with organization management

This commit is contained in:
2026-07-10 21:27:26 +08:00
parent 8ed1682b90
commit 8f0991a51f
49 changed files with 1292 additions and 698 deletions

View File

@@ -0,0 +1,11 @@
import { uuidV7 } from './uuid-v7';
describe('uuidV7', () => {
it('creates an RFC 9562 version 7 UUID with time-sortable prefixes', () => {
const first = uuidV7(1_700_000_000_000);
const second = uuidV7(1_700_000_000_001);
expect(first).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
expect(first < second).toBe(true);
});
});

View File

@@ -0,0 +1,21 @@
import { randomBytes } from 'node:crypto';
/** Generates an RFC 9562 UUIDv7 using the current Unix timestamp and cryptographic randomness. */
export function uuidV7(now = Date.now()): string {
const bytes = Buffer.alloc(16);
const random = randomBytes(10);
let timestamp = BigInt(now);
for (let index = 5; index >= 0; index -= 1) {
bytes[index] = Number(timestamp & 0xffn);
timestamp >>= 8n;
}
bytes[6] = 0x70 | (random[0] & 0x0f);
bytes[7] = random[1];
bytes[8] = 0x80 | (random[2] & 0x3f);
random.copy(bytes, 9, 3, 10);
const hex = bytes.toString('hex');
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}