Home / Function/ init() — drizzle-orm Function Reference

init() — drizzle-orm Function Reference

Architecture documentation for the init() function in Generators.ts from the drizzle-orm codebase.

Entity Profile

Dependency Diagram

graph TD
  1e5fa5ec_0fe2_b4f4_c81b_d7c555a6ab40["init()"]
  06b964e7_b23f_279e_aab9_e842d84085a5["GeneratePhoneNumber"]
  1e5fa5ec_0fe2_b4f4_c81b_d7c555a6ab40 -->|defined in| 06b964e7_b23f_279e_aab9_e842d84085a5
  style 1e5fa5ec_0fe2_b4f4_c81b_d7c555a6ab40 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-seed/src/services/Generators.ts lines 1905–2024

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

		let { generatedDigitsNumbers } = this.params;
		const { prefixes, template } = this.params;

		const rng = prand.xoroshiro128plus(seed);

		if (template !== undefined) {
			if (this.stringLength !== undefined && this.stringLength < template.length) {
				throw new Error(
					`Length of phone number template is shorter than db column length restriction: ${this.stringLength}. 
					Set the maximum string length to at least ${template.length}.`,
				);
			}

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

			const maxUniquePhoneNumbersCount = Math.pow(10, placeholdersCount);
			if (maxUniquePhoneNumbersCount < count) {
				throw new RangeError(
					`count exceeds max number of unique phone numbers(${maxUniquePhoneNumbersCount}).`,
				);
			}

			const generatorsMap = new Map<string, GenerateUniqueInt>();
			const genObj = new GenerateUniqueInt({ minValue: 0, maxValue: maxUniquePhoneNumbersCount - 1 });
			genObj.init({
				count,
				seed,
			});

			generatorsMap.set(
				template,
				genObj,
			);

			const prefixesArray: string[] = [];
			const generatedDigitsNumbers: number[] = [];
			const phoneNumbersSet = new Set<string>();

			this.state = { rng, placeholdersCount, generatorsMap, prefixesArray, generatedDigitsNumbers, phoneNumbersSet };
			return;
		}

		let prefixesArray: string[];
		if (prefixes === undefined || prefixes.length === 0) {
			prefixesArray = phonesInfo.map((phoneInfo) => phoneInfo.split(',').slice(0, -1).join(' '));
			generatedDigitsNumbers = phonesInfo.map((phoneInfo) => {
				// tokens = ["380","99","9"] =
				// = ["country prefix", "operator prefix", "number length including operator prefix and excluding country prefix"]
				const tokens = phoneInfo.split(',');
				const operatorPrefixLength = tokens[1]!.replaceAll(' ', '').length;

				return Number(tokens[2]) - operatorPrefixLength;
			});
		} else {
			prefixesArray = prefixes;
			if (typeof generatedDigitsNumbers === 'number') {
				generatedDigitsNumbers = Array.from<number>({ length: prefixes.length }).fill(
					generatedDigitsNumbers,
				);
			} else if (
				generatedDigitsNumbers === undefined
				|| generatedDigitsNumbers.length === 0
			) {
				generatedDigitsNumbers = Array.from<number>({ length: prefixes.length }).fill(7);
			}
		}

		const maxPrefixLength = Math.max(...prefixesArray.map((prefix) => prefix.length));
		const maxGeneratedDigits = Math.max(...generatedDigitsNumbers);

		if (this.stringLength !== undefined && this.stringLength < (maxPrefixLength + maxGeneratedDigits)) {
			throw new Error(
				`You can't use phone number generator with a db column length restriction of ${this.stringLength}. Set the maximum string length to at least ${
					maxPrefixLength + maxGeneratedDigits
				}.`,
			);
		}

Domain

Subdomains

Frequently Asked Questions

What does init() do?
init() is a function in the drizzle-orm codebase, defined in drizzle-seed/src/services/Generators.ts.
Where is init() defined?
init() is defined in drizzle-seed/src/services/Generators.ts at line 1905.

Analyze Your Own Codebase

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

Try Supermodel Free