From 036af1c9345e553570fdee72f380041edb1a7833 Mon Sep 17 00:00:00 2001 From: "jiho.park" Date: Fri, 15 Dec 2023 16:36:40 +0900 Subject: [PATCH] feat: use crypto.getRandomValues instead of Math.random --- spec/custom-entity/custom-entity.entity.ts | 4 +++- spec/multiple-primary-key/multiple-primary-key.entity.ts | 7 +++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/spec/custom-entity/custom-entity.entity.ts b/spec/custom-entity/custom-entity.entity.ts index 82326cc..77c46dc 100644 --- a/spec/custom-entity/custom-entity.entity.ts +++ b/spec/custom-entity/custom-entity.entity.ts @@ -1,3 +1,5 @@ +import crypto from 'crypto'; + import { IsString, IsNotEmpty, IsOptional } from 'class-validator'; import { BaseEntity, BeforeInsert, Column, DeleteDateColumn, Entity, PrimaryColumn } from 'typeorm'; @@ -26,6 +28,6 @@ export class CustomEntity extends BaseEntity { @BeforeInsert() setPrimaryKey() { - this.uuid = this.uuid ?? `${Date.now()}${Math.floor(Math.random() * 10_000)}`; + this.uuid = this.uuid ?? `${Date.now()}${crypto.getRandomValues(new Uint16Array(1))[0]}`; } } diff --git a/spec/multiple-primary-key/multiple-primary-key.entity.ts b/spec/multiple-primary-key/multiple-primary-key.entity.ts index 0b776c8..5fb5c10 100644 --- a/spec/multiple-primary-key/multiple-primary-key.entity.ts +++ b/spec/multiple-primary-key/multiple-primary-key.entity.ts @@ -1,7 +1,10 @@ +import crypto from 'crypto'; + import { IsOptional, IsString } from 'class-validator'; import { BaseEntity, BeforeInsert, Column, DeleteDateColumn, Entity, PrimaryColumn } from 'typeorm'; import { GROUP } from '../../src/lib/interface'; + @Entity('base') export class MultiplePrimaryKeyEntity extends BaseEntity { @PrimaryColumn() @@ -22,7 +25,7 @@ export class MultiplePrimaryKeyEntity extends BaseEntity { @BeforeInsert() setPrimaryKey() { - this.uuid1 = this.uuid1 ?? `${Math.floor(Math.random() * 10_000)}${Date.now()}`; - this.uuid2 = this.uuid2 ?? `${Math.floor(Math.random() * 10_000)}${Date.now()}`; + this.uuid1 = this.uuid1 ?? `${crypto.getRandomValues(new Uint16Array(1))[0]}${Date.now()}`; + this.uuid2 = this.uuid2 ?? `${crypto.getRandomValues(new Uint16Array(1))[0]}${Date.now()}`; } }