Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use optional client output location when it exists #18

Merged
merged 1 commit into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,5 @@ console.log(fakeProfileComplete());

<sup><sub>Inspired by https://github.com/toyamarinyon/prisma-factory-generator</sub></sup>

<sup><sub>This generator was bootstraped using [create-prisma-generator](https://github.com/YassinEldeeb/create-prisma-generator)
<sup><sub>This generator was bootstrapped using [create-prisma-generator](https://github.com/YassinEldeeb/create-prisma-generator)
</sub></sup>
200 changes: 200 additions & 0 deletions src/__tests__/__snapshots__/createMethods.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,206 @@ import { faker } from '@faker-js/faker';



export function fakeUser() {
return {
email: faker.internet.email(),
name: faker.person.fullName(),
age: faker.number.int({min: 0, max: 99}),
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
string: faker.lorem.words(5),
nullableString: null,
boolean: faker.datatype.boolean(),
nullableBoolean: null,
int: faker.number.int(),
nullableInt: null,
bigInt: faker.number.int(),
nullableBigInt: null,
float: faker.number.float(),
nullableFloat: null,
dateTime: faker.date.anytime(),
nullableDateTime: null,
stringArray: faker.lorem.words(5).split(' '),
intArray: [faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int()],
bigIntArray: [faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int()],
floatArray: [faker.number.float(),faker.number.float(),faker.number.float(),faker.number.float(),faker.number.float()],
booleanArray: [faker.datatype.boolean(),faker.datatype.boolean(),faker.datatype.boolean(),faker.datatype.boolean(),faker.datatype.boolean()],
dateTimeArray: [faker.date.anytime(),faker.date.anytime(),faker.date.anytime(),faker.date.anytime(),faker.date.anytime()],
json: {test: faker.lorem.word()},
nullableJson: null,
enum: faker.helpers.arrayElement([Enum.A, Enum.B, Enum.C] as const),
nullableEnum: null,
};
}
export function fakeUserComplete() {
return {
id: faker.string.uuid(),
email: faker.internet.email(),
name: faker.person.fullName(),
age: faker.number.int({min: 0, max: 99}),
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
string: faker.lorem.words(5),
stringWithDefault: 'default',
nullableString: null,
boolean: faker.datatype.boolean(),
booleanWithDefault: true,
nullableBoolean: null,
int: faker.number.int(),
intWithDefault: 1,
nullableInt: null,
bigInt: faker.number.int(),
bigIntWithDefault: 1,
nullableBigInt: null,
float: faker.number.float(),
floatWithDefault: 1.1,
nullableFloat: null,
dateTime: faker.date.anytime(),
dateTimeWithDefault: new Date(),
nullableDateTime: null,
stringArray: faker.lorem.words(5).split(' '),
intArray: [faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int()],
bigIntArray: [faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int()],
floatArray: [faker.number.float(),faker.number.float(),faker.number.float(),faker.number.float(),faker.number.float()],
booleanArray: [faker.datatype.boolean(),faker.datatype.boolean(),faker.datatype.boolean(),faker.datatype.boolean(),faker.datatype.boolean()],
dateTimeArray: [faker.date.anytime(),faker.date.anytime(),faker.date.anytime(),faker.date.anytime(),faker.date.anytime()],
json: {test: faker.lorem.word()},
jsonWithDefault: {},
jsonWithDefaultAndFake: {test2: faker.lorem.word()},
nullableJson: null,
enum: faker.helpers.arrayElement([Enum.A, Enum.B, Enum.C] as const),
enumWithDefault: Enum.A,
nullableEnum: null,
enums: [Enum.A],
};
}
export function fakeUser2Complete() {
return {
id: faker.number.int(),
};
}
export function fakeUserRelationComplete() {
return {
id: faker.string.uuid(),
userId: faker.string.uuid(),
userId2: faker.string.uuid(),
};
}
export function fakeUser2RelationComplete() {
return {
id: faker.string.uuid(),
user2Id: faker.number.int(),
};
}
"
`;

exports[`createMethods with custom clientImportPath 1`] = `
"import { Enum } from './src/generated/client';
import { faker } from '@faker-js/faker';



export function fakeUser() {
return {
email: faker.internet.email(),
name: faker.person.fullName(),
age: faker.number.int({min: 0, max: 99}),
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
string: faker.lorem.words(5),
nullableString: null,
boolean: faker.datatype.boolean(),
nullableBoolean: null,
int: faker.number.int(),
nullableInt: null,
bigInt: faker.number.int(),
nullableBigInt: null,
float: faker.number.float(),
nullableFloat: null,
dateTime: faker.date.anytime(),
nullableDateTime: null,
stringArray: faker.lorem.words(5).split(' '),
intArray: [faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int()],
bigIntArray: [faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int()],
floatArray: [faker.number.float(),faker.number.float(),faker.number.float(),faker.number.float(),faker.number.float()],
booleanArray: [faker.datatype.boolean(),faker.datatype.boolean(),faker.datatype.boolean(),faker.datatype.boolean(),faker.datatype.boolean()],
dateTimeArray: [faker.date.anytime(),faker.date.anytime(),faker.date.anytime(),faker.date.anytime(),faker.date.anytime()],
json: {test: faker.lorem.word()},
nullableJson: null,
enum: faker.helpers.arrayElement([Enum.A, Enum.B, Enum.C] as const),
nullableEnum: null,
};
}
export function fakeUserComplete() {
return {
id: faker.string.uuid(),
email: faker.internet.email(),
name: faker.person.fullName(),
age: faker.number.int({min: 0, max: 99}),
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
string: faker.lorem.words(5),
stringWithDefault: 'default',
nullableString: null,
boolean: faker.datatype.boolean(),
booleanWithDefault: true,
nullableBoolean: null,
int: faker.number.int(),
intWithDefault: 1,
nullableInt: null,
bigInt: faker.number.int(),
bigIntWithDefault: 1,
nullableBigInt: null,
float: faker.number.float(),
floatWithDefault: 1.1,
nullableFloat: null,
dateTime: faker.date.anytime(),
dateTimeWithDefault: new Date(),
nullableDateTime: null,
stringArray: faker.lorem.words(5).split(' '),
intArray: [faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int()],
bigIntArray: [faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int(),faker.number.int()],
floatArray: [faker.number.float(),faker.number.float(),faker.number.float(),faker.number.float(),faker.number.float()],
booleanArray: [faker.datatype.boolean(),faker.datatype.boolean(),faker.datatype.boolean(),faker.datatype.boolean(),faker.datatype.boolean()],
dateTimeArray: [faker.date.anytime(),faker.date.anytime(),faker.date.anytime(),faker.date.anytime(),faker.date.anytime()],
json: {test: faker.lorem.word()},
jsonWithDefault: {},
jsonWithDefaultAndFake: {test2: faker.lorem.word()},
nullableJson: null,
enum: faker.helpers.arrayElement([Enum.A, Enum.B, Enum.C] as const),
enumWithDefault: Enum.A,
nullableEnum: null,
enums: [Enum.A],
};
}
export function fakeUser2Complete() {
return {
id: faker.number.int(),
};
}
export function fakeUserRelationComplete() {
return {
id: faker.string.uuid(),
userId: faker.string.uuid(),
userId2: faker.string.uuid(),
};
}
export function fakeUser2RelationComplete() {
return {
id: faker.string.uuid(),
user2Id: faker.number.int(),
};
}
"
`;

exports[`createMethods with default clientImportPath 1`] = `
"import { Enum } from '@prisma/client';
import { faker } from '@faker-js/faker';



export function fakeUser() {
return {
email: faker.internet.email(),
Expand Down
14 changes: 14 additions & 0 deletions src/__tests__/createMethods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,17 @@ test('createMethods with `null` as `emptyValueAs`', async () => {
await createMethods(sampleDMMF.datamodel, undefined, undefined, 'null'),
).toMatchSnapshot();
});

test('createMethods with default clientImportPath', async () => {
const sampleDMMF = await getSampleDMMF();
expect(
await createMethods(sampleDMMF.datamodel, undefined, undefined, 'null', undefined),
).toMatchSnapshot();
});

test('createMethods with custom clientImportPath', async () => {
const sampleDMMF = await getSampleDMMF();
expect(
await createMethods(sampleDMMF.datamodel, undefined, undefined, 'null', './src/generated/client'),
).toMatchSnapshot();
});
2 changes: 2 additions & 0 deletions src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { generatorHandler, GeneratorOptions } from '@prisma/generator-helper';
import { GENERATOR_NAME } from './constants';
import { createMethods } from './helpers/createMethods';
import { extractClientPath } from './utils/generatorUtils';
import { writeFileSafely } from './utils/writeFileSafely';
import invariant from 'tiny-invariant';

Expand Down Expand Up @@ -36,6 +37,7 @@ generatorHandler({
options.generator.config.extraImport as string | undefined,
options.generator.config.extraExport as string | undefined,
options.generator.config.emptyValueAs as string | undefined,
extractClientPath(options),
);

await writeFileSafely(options.generator.output?.value!, fakeMethods);
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/createMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export async function createMethods(
extraImport?: string,
extraExport?: string,
emptyValueAs = 'undefined',
clientImportPath: string = '@prisma/client',
) {
const functions: string[] = [];

Expand All @@ -169,7 +170,7 @@ export async function createMethods(
createFakeFunctionsWithFKs(models, m, enums, functions, emptyValueAs);
});
const enumNames = enums.map((it) => it.name).join(', ');
return await `import { ${enumNames} } from '@prisma/client';
return await `import { ${enumNames} } from '${clientImportPath}';
import { faker } from '@faker-js/faker';
${extraImport || ''}
${extraExport || ''}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/generatorUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { GeneratorOptions } from '@prisma/generator-helper';

export function extractClientPath(options: GeneratorOptions) {
return options.otherGenerators.find((g) => g?.provider?.value === 'prisma-client-js')?.output?.value || undefined;
}
Loading