Home / Class/ GenerateUniqueInt Class — drizzle-orm Architecture

GenerateUniqueInt Class — drizzle-orm Architecture

Architecture documentation for the GenerateUniqueInt class in Generators.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  e2c413a5_f1cc_23b4_acd7_ab626330b0d7["GenerateUniqueInt"]
  e5cee001_0354_7e76_ef0a_06dca71dc8ce["Generators.ts"]
  e2c413a5_f1cc_23b4_acd7_ab626330b0d7 -->|defined in| e5cee001_0354_7e76_ef0a_06dca71dc8ce
  b3d8df90_567b_aad8_8bde_70a8cb190d3a["init()"]
  e2c413a5_f1cc_23b4_acd7_ab626330b0d7 -->|method| b3d8df90_567b_aad8_8bde_70a8cb190d3a
  cf7d0536_2315_3048_f863_848c58a61729["generate()"]
  e2c413a5_f1cc_23b4_acd7_ab626330b0d7 -->|method| cf7d0536_2315_3048_f863_848c58a61729
  2a2f15f9_4451_afca_fcc5_0b46ffae1a97["generateNumber()"]
  e2c413a5_f1cc_23b4_acd7_ab626330b0d7 -->|method| 2a2f15f9_4451_afca_fcc5_0b46ffae1a97
  72c3e623_159f_0722_0f85_3d39045d8852["generateBigint()"]
  e2c413a5_f1cc_23b4_acd7_ab626330b0d7 -->|method| 72c3e623_159f_0722_0f85_3d39045d8852

Relationship Graph

Source Code

drizzle-seed/src/services/Generators.ts lines 643–813

export class GenerateUniqueInt extends AbstractGenerator<{
	minValue?: number | bigint;
	maxValue?: number | bigint;
	isUnique?: boolean;
}> {
	static override readonly entityKind: string = 'GenerateUniqueInt';

	public genMaxRepeatedValuesCount: GenerateDefault | GenerateWeightedCount | undefined;
	public skipCheck?: boolean = false;
	public state: {
		rng: prand.RandomGenerator;
		minValue: number | bigint;
		maxValue: number | bigint;
		intervals: (number | bigint)[][];
		integersCount: Map<number | bigint, number>;
	} | undefined;
	public override isUnique = true;
	public override timeSpent = 0;

	override init({ count, seed }: { count: number; seed: number }) {
		const rng = prand.xoroshiro128plus(seed);
		let { minValue, maxValue } = this.params;

		if (maxValue === undefined) {
			maxValue = count * 10;
		}
		if (minValue === undefined) {
			minValue = -maxValue;
		}

		const intervals = [[minValue, maxValue]];

		const integersCount = new Map();

		if (typeof minValue === 'bigint' && typeof maxValue === 'bigint') {
			if (this.skipCheck === false && maxValue - minValue + BigInt(1) < count) {
				throw new Error(
					'count exceeds max number of unique integers in given range(min, max), try to make range wider.',
				);
			}
		} else if (typeof minValue === 'number' && typeof maxValue === 'number') {
			minValue = minValue >= 0 ? Math.ceil(minValue) : Math.floor(minValue);
			maxValue = maxValue >= 0 ? Math.floor(maxValue) : Math.ceil(maxValue);
			if (this.skipCheck === false && maxValue - minValue + 1 < count) {
				throw new Error(
					'count exceeds max number of unique integers in given range(min, max), try to make range wider.',
				);
			}
		} else {
			throw new Error(
				'minValue and maxValue should be the same type.',
			);
		}

		this.state = { rng, minValue, maxValue, intervals, integersCount };
	}

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

		let intervalIdx: number,
			numb: number | bigint | undefined;

		const intervalsToAdd: (number | bigint)[][] = [];

		if (this.state.intervals.length === 0) {
			if (this.skipCheck === false) {
				throw new RangeError(
					'generateUniqueInt: count exceeds max number of unique integers in given range(min, max), try to increase range.',
				);
			} else {
				return;
			}
		}

		[intervalIdx, this.state.rng] = prand.uniformIntDistribution(
			0,
			this.state.intervals.length - 1,
			this.state.rng,

Domain

Frequently Asked Questions

What is the GenerateUniqueInt class?
GenerateUniqueInt is a class in the drizzle-orm codebase, defined in drizzle-seed/src/services/Generators.ts.
Where is GenerateUniqueInt defined?
GenerateUniqueInt is defined in drizzle-seed/src/services/Generators.ts at line 643.

Analyze Your Own Codebase

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

Try Supermodel Free