Home / Class/ GeneratePostcode Class — drizzle-orm Architecture

GeneratePostcode Class — drizzle-orm Architecture

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

Entity Profile

Dependency Diagram

graph TD
  83e6e27d_e6f9_bff6_68c3_56410960af01["GeneratePostcode"]
  e5cee001_0354_7e76_ef0a_06dca71dc8ce["Generators.ts"]
  83e6e27d_e6f9_bff6_68c3_56410960af01 -->|defined in| e5cee001_0354_7e76_ef0a_06dca71dc8ce
  c25331a4_e996_83dc_5123_644a053a756b["init()"]
  83e6e27d_e6f9_bff6_68c3_56410960af01 -->|method| c25331a4_e996_83dc_5123_644a053a756b
  325ca051_7245_4330_326b_dfef7f0ab70c["generate()"]
  83e6e27d_e6f9_bff6_68c3_56410960af01 -->|method| 325ca051_7245_4330_326b_dfef7f0ab70c

Relationship Graph

Source Code

drizzle-seed/src/services/Generators.ts lines 2411–2466

export class GeneratePostcode extends AbstractGenerator<{
	isUnique?: boolean;
	arraySize?: number;
}> {
	static override readonly entityKind: string = 'GeneratePostcode';

	private state: {
		rng: prand.RandomGenerator;
		templates: string[];
	} | undefined;
	override uniqueVersionOfGen = GenerateUniquePostcode;

	override init({ count, seed }: { count: number; seed: number }) {
		super.init({ count, seed });

		const rng = prand.xoroshiro128plus(seed);
		const templates = ['#####', '#####-####'];

		const maxPostcodeLength = Math.max(...templates.map((template) => template.length));
		if (this.stringLength !== undefined && this.stringLength < maxPostcodeLength) {
			throw new Error(
				`You can't use postcode generator with a db column length restriction of ${this.stringLength}. Set the maximum string length to at least ${maxPostcodeLength}.`,
			);
		}

		this.state = { rng, templates };
	}

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

		let idx: number, postcodeNumber: number;

		[idx, this.state.rng] = prand.uniformIntDistribution(0, this.state.templates.length - 1, this.state.rng);
		const template = this.state.templates[idx]!;

		const iterArray = [...template.matchAll(/#/g)];
		const placeholdersCount = iterArray.length;

		[postcodeNumber, this.state.rng] = prand.uniformIntDistribution(
			0,
			Math.pow(10, placeholdersCount) - 1,
			this.state.rng,
		);
		const postcode = fillTemplate({
			template,
			placeholdersCount,
			values: [...String(postcodeNumber)],
			defaultValue: '0',
		});

		return postcode;
	}
}

Domain

Frequently Asked Questions

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

Analyze Your Own Codebase

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

Try Supermodel Free