Skip to content

Commit

Permalink
Review
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriiSherman committed Dec 20, 2024
1 parent db33c87 commit 88bfe72
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,50 @@
## Improvements
## API updates

- A new parameter, `version`, was added to the `seed` function options, which will control generator versioning.
We are introducing a new parameter, `version`, to the `seed` function options. This parameter, which controls generator versioning, has been added to make it easier to update deterministic generators in the future. Since values should remain consistent after each regeneration, it is crucial to provide a well-designed API for gradual updates

```ts
await seed(db, schema, { version: 1 });
await seed(db, schema, { version: 2 });
```

Generator versions will change within a single major version of drizzle-seed.
#### Example:

The new version of the generator will become available if the generator has been changed and the output of the new generator does not match the output of the old generator.
> This is not an actual API change; it is just an example of how we will proceed with `drizzle-seed` versioning
#### For example:
For example, `lastName` generator was changed, and new version, `V2`, of this generator became available.

`LastNameGen` was changed, and new version, `V2`, of this generator became available.

Later, `FirstNameGen` was changed in the next minor version of drizzle-seed, making `V3` version of this generator available.
Later, `firstName` generator was changed, making `V3` version of this generator available.

| | `V1` | `V2` | `V3(latest)` |
| :--------------: | :--------------: | :-------------: | :--------------: |
| **LastNameGen** | `LastNameGenV1` | `LastNameGenV2` | |
| **FirstNameGen** | `FirstNameGenV1` | | `FirstNameGenV3` |

If you omit version, latest version of generators(`V3`) will be used: `FirstNameGen` will use its `V3` version, and `LastNameGen` will use its `V2` version.

If you specify version of 2, `FirstNameGen` will use its `V1` version and `LastNameGen` will use its `V2` version.
##### Use the `firstName` generator of version 3 and the `lastName` generator of version 2
```ts
await seed(db, schema);
```

If you specify version of 1, `FirstNameGen` will use its `V1` version and `LastNameGen` will use its `V1` version.
If you are not ready to use latest generator version right away, you can specify max version to use

##
##### Use the `firstName` generator of version 1 and the `lastName` generator of version 2
```ts
await seed(db, schema, { version: '2' });
```

##### Use the `firstName` generator of version 1 and the `lastName` generator of version 1.
```ts
await seed(db, schema, { version: '1' });
```

Each update with breaking changes for generators will be documented on our docs and in release notes, explaining which version you should use, if you are not ready to upgrade the way generators works

## Breaking changes

### `interval` unique generator was changed and upgraded to v2
### `string` generators were changed and upgraded to v2

## New Features

- added `fields` as new parameter in `interval` generator

Expand Down
53 changes: 52 additions & 1 deletion drizzle-seed/src/services/GeneratorFuncs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
GenerateYear,
WeightedRandomGenerator,
} from './Generators.ts';
import { GenerateStringV2 } from './GeneratorVersions/GeneratorsV2.ts';
import { GenerateStringV2 } from './versioning/v2.ts';

function createGenerator<GeneratorType extends AbstractGenerator<T>, T>(
generatorConstructor: new(params: T) => GeneratorType,
Expand Down Expand Up @@ -735,3 +735,54 @@ export const generatorsFuncs = {
*/
weightedRandom: createGenerator(WeightedRandomGenerator),
};

export const generatorsList: (new(params: any) => AbstractGenerator<any>)[] = [
GenerateBoolean,
GenerateCity,
GenerateCompanyName,
GenerateCountry,
GenerateDate,
GenerateDatetime,
GenerateDefault,
GenerateEmail,
GenerateFirstName,
GenerateFullName,
GenerateInt,
GenerateInterval,
GenerateIntPrimaryKey,
GenerateJobTitle,
GenerateJson,
GenerateLastName,
GenerateLine,
GenerateLoremIpsum,
GenerateNumber,
GeneratePhoneNumber,
GeneratePoint,
GeneratePostcode,
GenerateState,
GenerateStreetAdddress,
GenerateTime,
GenerateTimestamp,
GenerateUUID,
GenerateValuesFromArray,
GenerateYear,
WeightedRandomGenerator,
];

//
const generatorsObj = {
[entityKind]: [
GenerateStreetAdddress,
GenerateUUID,
],
[entityKind]: [
GenerateVarchar,
GenerateVarcharV2,
GenerateVarcharV3,
],
[entityKind]: [
GenerateVarchar,
GenerateVarcharV2,
GenerateVarcharV3,
],
};
5 changes: 1 addition & 4 deletions drizzle-seed/src/services/Generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ import states, { maxStringLength as maxStateLength } from '../datasets/states.ts
import streetSuffix, { maxStringLength as maxStreetSuffixLength } from '../datasets/streetSuffix.ts';
import { fastCartesianProduct, fillTemplate, getWeightedIndices, isObject } from './utils.ts';

export const latestVersion = 2;
export const version = 1;

export abstract class AbstractGenerator<T = {}> {
static readonly [entityKind]: string = 'AbstractGenerator';
static readonly ['version']: number = 2;
static readonly version: number = 1;

public isUnique = false;
public notNull = false;
Expand Down
22 changes: 11 additions & 11 deletions drizzle-seed/src/services/SeedService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import type {
import type { Column, Prettify, Relation, Table } from '../types/tables.ts';
import type { AbstractGenerator } from './Generators.ts';

import * as Generators from './Generators.ts';
import * as GeneratorsV2 from './GeneratorVersions/GeneratorsV2.ts';
import { latestVersion } from './apiVersion.ts';
// import * as Generators from './Generators.ts';
import { equalSets, generateHashFromString } from './utils.ts';
import * as GeneratorsV2 from './versioning/v2.ts';

export class SeedService {
static readonly [entityKind]: string = 'SeedService';
Expand All @@ -39,9 +40,9 @@ export class SeedService {
let columnPossibleGenerator: Prettify<GeneratePossibleGeneratorsColumnType>;
let tablePossibleGenerators: Prettify<GeneratePossibleGeneratorsTableType>;
const customSeed = options?.seed === undefined ? 0 : options.seed;
const version = options?.version === undefined ? Generators.latestVersion : options.version;
if (version < Generators.version || version > Generators.latestVersion) {
throw new Error(`Version should be in range [${Generators.version}, ${Generators.latestVersion}].`);
const version = options?.version === undefined ? latestVersion : options.version;
if (version < 1 || version > latestVersion) {
throw new Error(`Version should be in range [1, ${latestVersion}].`);
}

// sorting table in order which they will be filled up (tables with foreign keys case)
Expand Down Expand Up @@ -248,6 +249,8 @@ export class SeedService {
);
}

// check if new version exists

columnPossibleGenerator.generator.isUnique = col.isUnique;
columnPossibleGenerator.generator.dataType = col.dataType;
columnPossibleGenerator.generator.stringLength = col.typeParams.length;
Expand Down Expand Up @@ -418,15 +421,12 @@ export class SeedService {
};

selectGeneratorOfVersion = (version: number, generatorEntityKind: string) => {
const GeneratorVersions = [GeneratorsV2, Generators];

type GeneratorConstructorT = new(params: any) => AbstractGenerator<any>;
const GeneratorVersions = [...Object.values(GeneratorsV2), ...Object.values(Generators)];

let generatorConstructor: GeneratorConstructorT | undefined;
for (const gens of GeneratorVersions) {
const { version: gensVersion, ...filteredGens } = gens;
if (gensVersion > version) continue;

for (const gen of Object.values(filteredGens)) {
for (const gen of Object.values(gens)) {
const abstractGen = gen as typeof AbstractGenerator<any>;
if (abstractGen[entityKind] === generatorEntityKind) {
generatorConstructor = abstractGen as unknown as GeneratorConstructorT;
Expand Down
1 change: 1 addition & 0 deletions drizzle-seed/src/services/apiVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const latestVersion = 2;
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ import { entityKind } from 'drizzle-orm';
import prand from 'pure-rand';
import { AbstractGenerator, GenerateInterval } from '../Generators.ts';

export const version = 2;

export class GenerateIntervalV2 extends GenerateInterval {
static override readonly [entityKind]: string = 'GenerateInterval';
static override readonly ['version']: number = 2;
override readonly version: number = 2;
override uniqueVersionOfGen = GenerateUniqueIntervalV2;
}

Expand All @@ -28,7 +26,7 @@ export class GenerateUniqueIntervalV2 extends AbstractGenerator<{
isUnique?: boolean;
}> {
static override readonly [entityKind]: string = 'GenerateUniqueInterval';
static override readonly ['version']: number = 2;
override readonly version: number = 2;

private state: {
rng: prand.RandomGenerator;
Expand Down Expand Up @@ -122,7 +120,7 @@ export class GenerateStringV2 extends AbstractGenerator<{
arraySize?: number;
}> {
static override readonly [entityKind]: string = 'GenerateString';
static override readonly ['version']: number = 2;
override readonly version: number = 2;

private state: {
rng: prand.RandomGenerator;
Expand Down Expand Up @@ -178,7 +176,7 @@ export class GenerateStringV2 extends AbstractGenerator<{

export class GenerateUniqueStringV2 extends AbstractGenerator<{ isUnique?: boolean }> {
static override readonly [entityKind]: string = 'GenerateUniqueString';
static override readonly ['version']: number = 2;
override readonly version: number = 2;

private state: {
rng: prand.RandomGenerator;
Expand Down

0 comments on commit 88bfe72

Please sign in to comment.