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
  81c8cbf9_2fb1_f2b8_0172_4f36943e36bf["init()"]
  ebde298b_d6a3_4062_c1b1_f4cfa069de7b["GenerateValuesFromArray"]
  81c8cbf9_2fb1_f2b8_0172_4f36943e36bf -->|defined in| ebde298b_d6a3_4062_c1b1_f4cfa069de7b
  b3c3a50c_06a8_f41b_767f_088142c03301["checks()"]
  81c8cbf9_2fb1_f2b8_0172_4f36943e36bf -->|calls| b3c3a50c_06a8_f41b_767f_088142c03301
  1bc6ff75_57cd_61d0_a164_cb68e7e797c4["isObject()"]
  81c8cbf9_2fb1_f2b8_0172_4f36943e36bf -->|calls| 1bc6ff75_57cd_61d0_a164_cb68e7e797c4
  b944ea69_4ea2_ead0_df3f_03885521d095["getWeightedIndices()"]
  81c8cbf9_2fb1_f2b8_0172_4f36943e36bf -->|calls| b944ea69_4ea2_ead0_df3f_03885521d095
  style 81c8cbf9_2fb1_f2b8_0172_4f36943e36bf fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

drizzle-seed/src/services/Generators.ts lines 283–360

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

		this.checks({ count });

		let { maxRepeatedValuesCount } = this;
		const { params, isUnique, notNull, weightedCountSeed } = this;

		const values = params.values;

		let valuesWeightedIndices;
		if (isObject(values[0])) {
			valuesWeightedIndices = getWeightedIndices((values as { weight: number }[]).map((val) => val.weight));
			if (isUnique === true && notNull === true) {
				let idx: number, valueIdx: number, rng = prand.xoroshiro128plus(seed);
				const indicesCounter: { [key: number]: number } = {};
				for (let i = 0; i < count; i++) {
					[idx, rng] = prand.uniformIntDistribution(0, valuesWeightedIndices.length - 1, rng);
					valueIdx = valuesWeightedIndices[idx]!;
					if (!Object.hasOwn(indicesCounter, valueIdx)) indicesCounter[valueIdx] = 0;
					indicesCounter[valueIdx]! += 1;
				}

				for (const [idx, value] of values.entries()) {
					if ((value as { values: (number | string | boolean | undefined)[] }).values.length < indicesCounter[idx]!) {
						throw new Error(
							'weighted values arrays is too small to generate values with specified probability for unique not null column.'
								+ `it's planned to generate: ${
									Object.entries(indicesCounter).map(([idx, count]) => {
										return `${count} values with probability ${(values as { weight: number }[])[Number(idx)]?.weight}`;
									}).join(',')
								}`,
						);
					}
				}
			}
		}
		if (isUnique === true && maxRepeatedValuesCount === undefined) {
			maxRepeatedValuesCount = 1;
		}
		let genMaxRepeatedValuesCount: GenerateDefault | GenerateWeightedCount | undefined;
		if (typeof maxRepeatedValuesCount === 'number') {
			genMaxRepeatedValuesCount = new GenerateDefault({ defaultValue: maxRepeatedValuesCount });
		} else if (typeof maxRepeatedValuesCount === 'object') {
			genMaxRepeatedValuesCount = new GenerateWeightedCount({});
			(genMaxRepeatedValuesCount as GenerateWeightedCount).init(
				{
					count: maxRepeatedValuesCount,
					seed: weightedCountSeed === undefined ? seed : weightedCountSeed,
				},
			);
		}

		let genIndicesObj: GenerateUniqueInt | undefined;
		let genIndicesObjList: GenerateUniqueInt[] | undefined;

		if (maxRepeatedValuesCount !== undefined) {
			if (!isObject(values[0])) {
				genIndicesObj = new GenerateUniqueInt({ minValue: 0, maxValue: values.length - 1 });
				genIndicesObj.genMaxRepeatedValuesCount = genMaxRepeatedValuesCount;
				genIndicesObj.skipCheck = true;
				genIndicesObj.init({ count, seed });
			} else if (isObject(values[0])) {
				genIndicesObjList = [];
				for (const obj of values as { weight: number; values: (number | string | boolean | undefined)[] }[]) {
					const genIndicesObj = new GenerateUniqueInt({ minValue: 0, maxValue: obj.values.length - 1 });
					genIndicesObj.genMaxRepeatedValuesCount = genMaxRepeatedValuesCount;
					genIndicesObj.skipCheck = true;
					genIndicesObj.init({ count, seed });
					genIndicesObjList.push(genIndicesObj);
				}
			}
		}

		const rng = prand.xoroshiro128plus(seed);

		this.state = { rng, values, valuesWeightedIndices, genMaxRepeatedValuesCount, genIndicesObj, genIndicesObjList };
	}

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 283.
What does init() call?
init() calls 3 function(s): checks, getWeightedIndices, isObject.

Analyze Your Own Codebase

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

Try Supermodel Free