GenerateUniquePostcode Class — drizzle-orm Architecture
Architecture documentation for the GenerateUniquePostcode class in Generators.ts from the drizzle-orm codebase.
Entity Profile
Dependency Diagram
graph TD e186ff0b_6b52_7de3_f892_3be699886eea["GenerateUniquePostcode"] e5cee001_0354_7e76_ef0a_06dca71dc8ce["Generators.ts"] e186ff0b_6b52_7de3_f892_3be699886eea -->|defined in| e5cee001_0354_7e76_ef0a_06dca71dc8ce cec5f179_9219_61fb_0b05_38ba25d083bc["init()"] e186ff0b_6b52_7de3_f892_3be699886eea -->|method| cec5f179_9219_61fb_0b05_38ba25d083bc 3b4d6f86_0d65_cc71_18b7_d8e575d5d02d["generate()"] e186ff0b_6b52_7de3_f892_3be699886eea -->|method| 3b4d6f86_0d65_cc71_18b7_d8e575d5d02d
Relationship Graph
Source Code
drizzle-seed/src/services/Generators.ts lines 2468–2551
export class GenerateUniquePostcode extends AbstractGenerator<{ isUnique?: boolean }> {
static override readonly entityKind: string = 'GenerateUniquePostcode';
private state: {
rng: prand.RandomGenerator;
templates: {
template: string;
indicesGen: GenerateUniqueInt;
placeholdersCount: number;
count: number;
maxUniquePostcodeNumber: number;
}[];
} | undefined;
public override isUnique = true;
override init({ count, seed }: { count: number; seed: number }) {
const maxUniquePostcodeNumber = Math.pow(10, 5) + Math.pow(10, 9);
if (count > maxUniquePostcodeNumber) {
throw new RangeError(
`count exceeds max number of unique postcodes(${maxUniquePostcodeNumber}).`,
);
}
const rng = prand.xoroshiro128plus(seed);
const templates = [
{
template: '#####',
indicesGen: new GenerateUniqueInt({ minValue: 0, maxValue: Math.pow(10, 5) - 1 }),
placeholdersCount: 5,
count: 0,
maxUniquePostcodeNumber: Math.pow(10, 5),
},
{
template: '#####-####',
indicesGen: new GenerateUniqueInt({ minValue: 0, maxValue: Math.pow(10, 9) - 1 }),
placeholdersCount: 9,
count: 0,
maxUniquePostcodeNumber: Math.pow(10, 9),
},
];
const maxPostcodeLength = Math.max(...templates.map((template) => 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}.`,
);
}
for (const templateObj of templates) {
templateObj.indicesGen.skipCheck = true;
templateObj.indicesGen.init({ count, seed });
}
this.state = { rng, templates };
}
generate() {
if (this.state === undefined) {
throw new Error('state is not defined.');
}
let idx: number;
[idx, this.state.rng] = prand.uniformIntDistribution(0, this.state.templates.length - 1, this.state.rng);
const templateObj = this.state.templates[idx]!;
const postcodeNumber = templateObj.indicesGen.generate() as number;
templateObj.count += 1;
if (templateObj.count === templateObj.maxUniquePostcodeNumber) {
this.state.templates[idx] = this.state.templates.at(-1)!;
this.state.templates.pop();
}
const postcode = fillTemplate({
template: templateObj.template,
placeholdersCount: templateObj.placeholdersCount,
values: [...String(postcodeNumber)],
defaultValue: '0',
});
Domain
Defined In
Source
Frequently Asked Questions
What is the GenerateUniquePostcode class?
GenerateUniquePostcode is a class in the drizzle-orm codebase, defined in drizzle-seed/src/services/Generators.ts.
Where is GenerateUniquePostcode defined?
GenerateUniquePostcode is defined in drizzle-seed/src/services/Generators.ts at line 2468.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free