Home / Class/ GenerateUniqueStringV2 Class — drizzle-orm Architecture

GenerateUniqueStringV2 Class — drizzle-orm Architecture

Architecture documentation for the GenerateUniqueStringV2 class in v2.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  edccb078_72bf_7593_a17b_79784ccdfa56["GenerateUniqueStringV2"]
  78da996c_90cc_6154_e2a5_4b2639015e5b["v2.ts"]
  edccb078_72bf_7593_a17b_79784ccdfa56 -->|defined in| 78da996c_90cc_6154_e2a5_4b2639015e5b
  f6745cad_d323_9e92_2e27_ddd99bbf4404["init()"]
  edccb078_72bf_7593_a17b_79784ccdfa56 -->|method| f6745cad_d323_9e92_2e27_ddd99bbf4404
  5785a7c5_bd5d_0341_0c88_1df53ae29c8a["generate()"]
  edccb078_72bf_7593_a17b_79784ccdfa56 -->|method| 5785a7c5_bd5d_0341_0c88_1df53ae29c8a

Relationship Graph

Source Code

drizzle-seed/src/services/versioning/v2.ts lines 171–232

export class GenerateUniqueStringV2 extends AbstractGenerator<{ isUnique?: boolean }> {
	static override readonly 'entityKind': string = 'GenerateUniqueString';
	static override readonly version: number = 2;

	private state: {
		rng: prand.RandomGenerator;
		minStringLength: number;
		maxStringLength: number;
	} | undefined;
	public override isUnique = true;

	override init({ seed, count }: { seed: number; count: number }) {
		const rng = prand.xoroshiro128plus(seed);

		let minStringLength = 7;
		let maxStringLength = 20;
		// TODO: revise later
		if (this.stringLength !== undefined) {
			maxStringLength = this.stringLength;
			if (maxStringLength === 1 || maxStringLength < minStringLength) minStringLength = maxStringLength;
		}

		if (maxStringLength < count.toString(16).length) {
			throw new Error(
				`You can't generate ${count} unique strings, with a maximum string length of ${maxStringLength}.`,
			);
		}

		this.state = { rng, minStringLength, maxStringLength };
	}

	generate({ i }: { i: number }) {
		if (this.state === undefined) {
			throw new Error('state is not defined.');
		}

		const minStringLength = this.state.minStringLength,
			maxStringLength = this.state.maxStringLength;
		const stringChars = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		let idx: number,
			strLength: number;
		let currStr: string;

		currStr = '';
		const uniqueStr = i.toString(16);
		[strLength, this.state.rng] = prand.uniformIntDistribution(
			minStringLength,
			maxStringLength - uniqueStr.length,
			this.state.rng,
		);
		for (let j = 0; j < strLength - uniqueStr.length; j++) {
			[idx, this.state.rng] = prand.uniformIntDistribution(
				0,
				stringChars.length - 1,
				this.state.rng,
			);
			currStr += stringChars[idx];
		}

		return uniqueStr + currStr;
	}
}

Domain

Frequently Asked Questions

What is the GenerateUniqueStringV2 class?
GenerateUniqueStringV2 is a class in the drizzle-orm codebase, defined in drizzle-seed/src/services/versioning/v2.ts.
Where is GenerateUniqueStringV2 defined?
GenerateUniqueStringV2 is defined in drizzle-seed/src/services/versioning/v2.ts at line 171.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free