From 3f2438ca7c9be3e7de8fd1563b60e6712e91bc22 Mon Sep 17 00:00:00 2001 From: Priyank Gaur Date: Tue, 17 Mar 2026 02:13:59 +0530 Subject: [PATCH] fix(client-engine-runtime): make NowGenerator lazy to avoid synchronous new Date() calls (#28724) The Issue: The GeneratorRegistry.snapshot() method creates a snapshot of all generators, including **NowGenerator** ``` class NowGenerator implements ValueGenerator { #now: Date = new Date() generate(): string { return this.#now.toISOString() } } ``` The **NowGenerator** was initializing new Date() in its property declaration, meaning it was called synchronously during every query preparation, even if now() was not used. The Fix: I modified **NowGenerator** to be lazy. ``` class NowGenerator implements ValueGenerator { #now: Date | undefined generate(): string { if (this.#now === undefined) { this.#now = new Date() } return this.#now.toISOString() } } ``` The #now field is now initialized as undefined, and new Date() is only called inside the **generate()** ``` class NanoIdGenerator implements ValueGenerator { generate(arg: unknown): string { if (typeof arg === 'number') { return nanoid(arg) } else if (arg === undefined) { return nanoid() } else { throw new Error('Invalid Nanoid generator arguments') } } } ``` method if it hasn't been initialized yet. Fixes #28588 Co-authored-by: jacek-prisma --- packages/client-engine-runtime/src/interpreter/generators.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/client-engine-runtime/src/interpreter/generators.ts b/packages/client-engine-runtime/src/interpreter/generators.ts index 2fac994fd513..e1f4a972594b 100644 --- a/packages/client-engine-runtime/src/interpreter/generators.ts +++ b/packages/client-engine-runtime/src/interpreter/generators.ts @@ -45,9 +45,12 @@ export interface ValueGenerator { } class NowGenerator implements ValueGenerator { - #now: Date = new Date() + #now: Date | undefined generate(): string { + if (this.#now === undefined) { + this.#now = new Date() + } return this.#now.toISOString() } }