From 248031e5e5b06084b81225358c1f85d2afbac97f Mon Sep 17 00:00:00 2001 From: Quramy Date: Mon, 28 Nov 2022 01:55:52 +0900 Subject: [PATCH 1/3] feat: Rename buildCreateInput to build --- README.md | 14 ++++---- .../src/__generated__/fabbrica/index.d.ts | 4 ++- .../src/__generated__/fabbrica/index.js | 16 +++++---- .../__snapshots__/getSourceFile.test.ts.snap | 7 ++-- .../prisma-fabbrica/src/templates/index.ts | 12 +++---- .../__generated__/fabbrica/index.ts | 14 ++++---- .../__generated__/fabbrica/index.ts | 14 ++++---- .../__generated__/fabbrica/index.ts | 33 ++++++++++--------- .../__generated__/fabbrica/index.ts | 22 +++++++------ .../__generated__/fabbrica/index.ts | 7 ++-- 10 files changed, 79 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index 5fda147e..40a8649e 100644 --- a/README.md +++ b/README.md @@ -230,17 +230,17 @@ console.log(posts.length); // -> 2 ### Build input data only -`.buildCreateInput` method in factories provides data set to create the model, but never insert. +`.build` method in factories provides data set to create the model, but never insert. ```ts await UserFactory.create(); // The above code is equivalent to the bellow: -const data = await UserFactory.buildCreateInput(); +const data = await UserFactory.build(); await prisma.user.create({ data }); ``` -For example, you can use `.buildCreateInput` method in other model's factory definition: +For example, you can use `.build` method in other model's factory definition: ```ts const UserFactory = defineUserFactory(); @@ -252,7 +252,7 @@ const PostFactory = definePostFactory({ where: { id: "user001", }, - create: await UserFactory.buildCreateInput({ + create: await UserFactory.build({ id: "user001", }), }, @@ -268,19 +268,19 @@ console.log(await prisma.user.count()); // -> 1 ### has-many / has-one relation -Sometimes, you may want a user data whose has post record. You can use `PostFactory.buildCreateInput` too. +Sometimes, you may want a user data whose has post record. You can use `PostFactory.build` too. ```ts await UserFactory.create({ posts: { - create: [await PostFactory.buildCreateInput()], + create: [await PostFactory.build()], }, }); console.log(await prisma.post.count()); // -> 1 ``` -Note: In the above example, `PostFactory.buildCreateInput()` resolves JSON data such as: +Note: In the above example, `PostFactory.build()` resolves JSON data such as: ```ts { diff --git a/examples/example-prj/src/__generated__/fabbrica/index.d.ts b/examples/example-prj/src/__generated__/fabbrica/index.d.ts index 4a99b0c4..8353a537 100644 --- a/examples/example-prj/src/__generated__/fabbrica/index.d.ts +++ b/examples/example-prj/src/__generated__/fabbrica/index.d.ts @@ -16,6 +16,7 @@ type UserFactoryDefineOptions = { }; export declare function defineUserFactory(args?: UserFactoryDefineOptions): { _factoryFor: "User"; + build: (inputData?: Partial) => Promise; buildCreateInput: (inputData?: Partial) => Promise; pickForConnect: (inputData: User) => { id: string; @@ -27,7 +28,7 @@ export declare function defineUserFactory(args?: UserFactoryDefineOptions): { }; type PostauthorFactory = { _factoryFor: "User"; - buildCreateInput: () => PromiseLike; + build: () => PromiseLike; }; type PostFactoryDefineInput = { id?: string; @@ -39,6 +40,7 @@ type PostFactoryDefineOptions = { }; export declare function definePostFactory(args: PostFactoryDefineOptions): { _factoryFor: "Post"; + build: (inputData?: Partial) => Promise; buildCreateInput: (inputData?: Partial) => Promise; pickForConnect: (inputData: Post) => { id: string; diff --git a/examples/example-prj/src/__generated__/fabbrica/index.js b/examples/example-prj/src/__generated__/fabbrica/index.js index 59750bcf..fdde8936 100644 --- a/examples/example-prj/src/__generated__/fabbrica/index.js +++ b/examples/example-prj/src/__generated__/fabbrica/index.js @@ -36,7 +36,7 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }) { const seqKey = {}; const getSeq = () => (0, helpers_1.getSequenceCounter)(seqKey); const screen = (0, relations_1.createScreener)("User", modelFieldDefinitions); - const buildCreateInput = async (inputData = {}) => { + const build = async (inputData = {}) => { const seq = getSeq(); const requiredScalarData = autoGenerateUserScalarsOrEnums({ seq }); const resolveValue = (0, helpers_1.normalizeResolver)(defaultDataResolver ?? {}); @@ -49,13 +49,14 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }) { id: inputData.id }); const create = async (inputData = {}) => { - const data = await buildCreateInput(inputData).then(screen); + const data = await build(inputData).then(screen); return await (0, clientHolder_1.getClient)().user.create({ data }); }; const createForConnect = (inputData = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User", - buildCreateInput, + build, + buildCreateInput: build, pickForConnect, create, createForConnect, @@ -78,14 +79,14 @@ function definePostFactoryInternal({ defaultData: defaultDataResolver }) { const seqKey = {}; const getSeq = () => (0, helpers_1.getSequenceCounter)(seqKey); const screen = (0, relations_1.createScreener)("Post", modelFieldDefinitions); - const buildCreateInput = async (inputData = {}) => { + const build = async (inputData = {}) => { const seq = getSeq(); const requiredScalarData = autoGeneratePostScalarsOrEnums({ seq }); const resolveValue = (0, helpers_1.normalizeResolver)(defaultDataResolver ?? {}); const defaultData = await resolveValue({ seq }); const defaultAssociations = { author: isPostauthorFactory(defaultData.author) ? { - create: await defaultData.author.buildCreateInput() + create: await defaultData.author.build() } : defaultData.author }; const data = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; @@ -95,13 +96,14 @@ function definePostFactoryInternal({ defaultData: defaultDataResolver }) { id: inputData.id }); const create = async (inputData = {}) => { - const data = await buildCreateInput(inputData).then(screen); + const data = await build(inputData).then(screen); return await (0, clientHolder_1.getClient)().post.create({ data }); }; const createForConnect = (inputData = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Post", - buildCreateInput, + build, + buildCreateInput: build, pickForConnect, create, createForConnect, diff --git a/packages/prisma-fabbrica/src/templates/__snapshots__/getSourceFile.test.ts.snap b/packages/prisma-fabbrica/src/templates/__snapshots__/getSourceFile.test.ts.snap index f3d266e0..1dd96668 100644 --- a/packages/prisma-fabbrica/src/templates/__snapshots__/getSourceFile.test.ts.snap +++ b/packages/prisma-fabbrica/src/templates/__snapshots__/getSourceFile.test.ts.snap @@ -39,7 +39,7 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const seqKey = {}; const getSeq = () => getSequenceCounter(seqKey); const screen = createScreener("User", modelFieldDefinitions); - const buildCreateInput = async (inputData: Partial = {}) => { + const build = async (inputData: Partial = {}) => { const seq = getSeq(); const requiredScalarData = autoGenerateUserScalarsOrEnums({ seq }); const resolveValue = normalizeResolver(defaultDataResolver ?? {}); @@ -52,13 +52,14 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac id: inputData.id }); const create = async (inputData: Partial = {}) => { - const data = await buildCreateInput(inputData).then(screen); + const data = await build(inputData).then(screen); return await getClient().user.create({ data }); }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, - buildCreateInput, + build, + buildCreateInput: build, pickForConnect, create, createForConnect, diff --git a/packages/prisma-fabbrica/src/templates/index.ts b/packages/prisma-fabbrica/src/templates/index.ts index 3098457b..f91671a6 100644 --- a/packages/prisma-fabbrica/src/templates/index.ts +++ b/packages/prisma-fabbrica/src/templates/index.ts @@ -160,8 +160,7 @@ export const modelBelongsToRelationFactory = (fieldType: DMMF.SchemaArg, model: return template.statement` type ${() => ast.identifier(`${model.name}${fieldType.name}Factory`)} = { _factoryFor: ${() => ast.literalTypeNode(ast.stringLiteral(targetModel.type))}; - buildCreateInput: () => PromiseLike - ast.identifier(fieldType.inputTypes[0].type as string)}["create"]>; + build: () => PromiseLike ast.identifier(fieldType.inputTypes[0].type as string)}["create"]>; }; `(); }; @@ -272,7 +271,7 @@ export const defineModelFactoryInernal = (model: DMMF.Model, inputType: DMMF.Inp const getSeq = () => getSequenceCounter(seqKey); const screen = createScreener(${() => ast.stringLiteral(model.name)}, modelFieldDefinitions); - const buildCreateInput = async ( + const build = async ( inputData: Partial = {} ) => { const seq = getSeq(); @@ -286,7 +285,7 @@ export const defineModelFactoryInernal = (model: DMMF.Model, inputType: DMMF.Inp field.name, template.expression` IS_MODEL_BELONGS_TO_RELATION_FACTORY(defaultData.FIELD_NAME) ? { - create: await defaultData.FIELD_NAME.buildCreateInput() + create: await defaultData.FIELD_NAME.build() } : defaultData.FIELD_NAME `({ IS_MODEL_BELONGS_TO_RELATION_FACTORY: ast.identifier(`is${model.name}${field.name}Factory`), @@ -313,7 +312,7 @@ export const defineModelFactoryInernal = (model: DMMF.Model, inputType: DMMF.Inp const create = async ( inputData: Partial = {} ) => { - const data = await buildCreateInput(inputData).then(screen); + const data = await build(inputData).then(screen); return await getClient().MODEL_KEY.create({ data }); }; @@ -321,7 +320,8 @@ export const defineModelFactoryInernal = (model: DMMF.Model, inputType: DMMF.Inp return { _factoryFor: ${() => ast.stringLiteral(model.name)} as const, - buildCreateInput, + build, + buildCreateInput: build, pickForConnect, create, createForConnect, diff --git a/packages/ts-compile-testing/fixtures/field-variation/__generated__/fabbrica/index.ts b/packages/ts-compile-testing/fixtures/field-variation/__generated__/fabbrica/index.ts index 1d944913..e7a07451 100644 --- a/packages/ts-compile-testing/fixtures/field-variation/__generated__/fabbrica/index.ts +++ b/packages/ts-compile-testing/fixtures/field-variation/__generated__/fabbrica/index.ts @@ -43,7 +43,7 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const seqKey = {}; const getSeq = () => getSequenceCounter(seqKey); const screen = createScreener("User", modelFieldDefinitions); - const buildCreateInput = async (inputData: Partial = {}) => { + const build = async (inputData: Partial = {}) => { const seq = getSeq(); const requiredScalarData = autoGenerateUserScalarsOrEnums({ seq }); const resolveValue = normalizeResolver(defaultDataResolver ?? {}); @@ -56,13 +56,14 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac id: inputData.id }); const create = async (inputData: Partial = {}) => { - const data = await buildCreateInput(inputData).then(screen); + const data = await build(inputData).then(screen); return await getClient().user.create({ data }); }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, - buildCreateInput, + build, + buildCreateInput: build, pickForConnect, create, createForConnect, @@ -94,7 +95,7 @@ function defineComplexIdModelFactoryInternal({ defaultData: defaultDataResolver const seqKey = {}; const getSeq = () => getSequenceCounter(seqKey); const screen = createScreener("ComplexIdModel", modelFieldDefinitions); - const buildCreateInput = async (inputData: Partial = {}) => { + const build = async (inputData: Partial = {}) => { const seq = getSeq(); const requiredScalarData = autoGenerateComplexIdModelScalarsOrEnums({ seq }); const resolveValue = normalizeResolver(defaultDataResolver ?? {}); @@ -108,13 +109,14 @@ function defineComplexIdModelFactoryInternal({ defaultData: defaultDataResolver lastName: inputData.lastName }); const create = async (inputData: Partial = {}) => { - const data = await buildCreateInput(inputData).then(screen); + const data = await build(inputData).then(screen); return await getClient().complexIdModel.create({ data }); }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "ComplexIdModel" as const, - buildCreateInput, + build, + buildCreateInput: build, pickForConnect, create, createForConnect, diff --git a/packages/ts-compile-testing/fixtures/relations-many-to-many/__generated__/fabbrica/index.ts b/packages/ts-compile-testing/fixtures/relations-many-to-many/__generated__/fabbrica/index.ts index 5e184126..bc1df720 100644 --- a/packages/ts-compile-testing/fixtures/relations-many-to-many/__generated__/fabbrica/index.ts +++ b/packages/ts-compile-testing/fixtures/relations-many-to-many/__generated__/fabbrica/index.ts @@ -49,7 +49,7 @@ function definePostFactoryInternal({ defaultData: defaultDataResolver }: PostFac const seqKey = {}; const getSeq = () => getSequenceCounter(seqKey); const screen = createScreener("Post", modelFieldDefinitions); - const buildCreateInput = async (inputData: Partial = {}) => { + const build = async (inputData: Partial = {}) => { const seq = getSeq(); const requiredScalarData = autoGeneratePostScalarsOrEnums({ seq }); const resolveValue = normalizeResolver(defaultDataResolver ?? {}); @@ -62,13 +62,14 @@ function definePostFactoryInternal({ defaultData: defaultDataResolver }: PostFac id: inputData.id }); const create = async (inputData: Partial = {}) => { - const data = await buildCreateInput(inputData).then(screen); + const data = await build(inputData).then(screen); return await getClient().post.create({ data }); }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Post" as const, - buildCreateInput, + build, + buildCreateInput: build, pickForConnect, create, createForConnect, @@ -101,7 +102,7 @@ function defineCategoryFactoryInternal({ defaultData: defaultDataResolver }: Cat const seqKey = {}; const getSeq = () => getSequenceCounter(seqKey); const screen = createScreener("Category", modelFieldDefinitions); - const buildCreateInput = async (inputData: Partial = {}) => { + const build = async (inputData: Partial = {}) => { const seq = getSeq(); const requiredScalarData = autoGenerateCategoryScalarsOrEnums({ seq }); const resolveValue = normalizeResolver(defaultDataResolver ?? {}); @@ -114,13 +115,14 @@ function defineCategoryFactoryInternal({ defaultData: defaultDataResolver }: Cat id: inputData.id }); const create = async (inputData: Partial = {}) => { - const data = await buildCreateInput(inputData).then(screen); + const data = await build(inputData).then(screen); return await getClient().category.create({ data }); }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Category" as const, - buildCreateInput, + build, + buildCreateInput: build, pickForConnect, create, createForConnect, diff --git a/packages/ts-compile-testing/fixtures/relations-one-to-many/__generated__/fabbrica/index.ts b/packages/ts-compile-testing/fixtures/relations-one-to-many/__generated__/fabbrica/index.ts index 2ae79b4e..e69c1119 100644 --- a/packages/ts-compile-testing/fixtures/relations-one-to-many/__generated__/fabbrica/index.ts +++ b/packages/ts-compile-testing/fixtures/relations-one-to-many/__generated__/fabbrica/index.ts @@ -70,7 +70,7 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const seqKey = {}; const getSeq = () => getSequenceCounter(seqKey); const screen = createScreener("User", modelFieldDefinitions); - const buildCreateInput = async (inputData: Partial = {}) => { + const build = async (inputData: Partial = {}) => { const seq = getSeq(); const requiredScalarData = autoGenerateUserScalarsOrEnums({ seq }); const resolveValue = normalizeResolver(defaultDataResolver ?? {}); @@ -83,13 +83,14 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac id: inputData.id }); const create = async (inputData: Partial = {}) => { - const data = await buildCreateInput(inputData).then(screen); + const data = await build(inputData).then(screen); return await getClient().user.create({ data }); }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, - buildCreateInput, + build, + buildCreateInput: build, pickForConnect, create, createForConnect, @@ -104,7 +105,7 @@ type PostScalarOrEnumFields = { }; type PostauthorFactory = { _factoryFor: "User"; - buildCreateInput: () => PromiseLike; + build: () => PromiseLike; }; type PostFactoryDefineInput = { id?: string; @@ -130,14 +131,14 @@ function definePostFactoryInternal({ defaultData: defaultDataResolver }: PostFac const seqKey = {}; const getSeq = () => getSequenceCounter(seqKey); const screen = createScreener("Post", modelFieldDefinitions); - const buildCreateInput = async (inputData: Partial = {}) => { + const build = async (inputData: Partial = {}) => { const seq = getSeq(); const requiredScalarData = autoGeneratePostScalarsOrEnums({ seq }); const resolveValue = normalizeResolver(defaultDataResolver ?? {}); const defaultData = await resolveValue({ seq }); const defaultAssociations = { author: isPostauthorFactory(defaultData.author) ? { - create: await defaultData.author.buildCreateInput() + create: await defaultData.author.build() } : defaultData.author }; const data: Prisma.PostCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; @@ -147,13 +148,14 @@ function definePostFactoryInternal({ defaultData: defaultDataResolver }: PostFac id: inputData.id }); const create = async (inputData: Partial = {}) => { - const data = await buildCreateInput(inputData).then(screen); + const data = await build(inputData).then(screen); return await getClient().post.create({ data }); }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Post" as const, - buildCreateInput, + build, + buildCreateInput: build, pickForConnect, create, createForConnect, @@ -168,11 +170,11 @@ type ReviewScalarOrEnumFields = { }; type ReviewpostFactory = { _factoryFor: "Post"; - buildCreateInput: () => PromiseLike; + build: () => PromiseLike; }; type ReviewreviewerFactory = { _factoryFor: "User"; - buildCreateInput: () => PromiseLike; + build: () => PromiseLike; }; type ReviewFactoryDefineInput = { id?: string; @@ -201,17 +203,17 @@ function defineReviewFactoryInternal({ defaultData: defaultDataResolver }: Revie const seqKey = {}; const getSeq = () => getSequenceCounter(seqKey); const screen = createScreener("Review", modelFieldDefinitions); - const buildCreateInput = async (inputData: Partial = {}) => { + const build = async (inputData: Partial = {}) => { const seq = getSeq(); const requiredScalarData = autoGenerateReviewScalarsOrEnums({ seq }); const resolveValue = normalizeResolver(defaultDataResolver ?? {}); const defaultData = await resolveValue({ seq }); const defaultAssociations = { post: isReviewpostFactory(defaultData.post) ? { - create: await defaultData.post.buildCreateInput() + create: await defaultData.post.build() } : defaultData.post, reviewer: isReviewreviewerFactory(defaultData.reviewer) ? { - create: await defaultData.reviewer.buildCreateInput() + create: await defaultData.reviewer.build() } : defaultData.reviewer }; const data: Prisma.ReviewCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; @@ -221,13 +223,14 @@ function defineReviewFactoryInternal({ defaultData: defaultDataResolver }: Revie id: inputData.id }); const create = async (inputData: Partial = {}) => { - const data = await buildCreateInput(inputData).then(screen); + const data = await build(inputData).then(screen); return await getClient().review.create({ data }); }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Review" as const, - buildCreateInput, + build, + buildCreateInput: build, pickForConnect, create, createForConnect, diff --git a/packages/ts-compile-testing/fixtures/relations-one-to-one/__generated__/fabbrica/index.ts b/packages/ts-compile-testing/fixtures/relations-one-to-one/__generated__/fabbrica/index.ts index 41972426..980791ca 100644 --- a/packages/ts-compile-testing/fixtures/relations-one-to-one/__generated__/fabbrica/index.ts +++ b/packages/ts-compile-testing/fixtures/relations-one-to-one/__generated__/fabbrica/index.ts @@ -31,7 +31,7 @@ type UserScalarOrEnumFields = { }; type UserprofileFactory = { _factoryFor: "Profile"; - buildCreateInput: () => PromiseLike; + build: () => PromiseLike; }; type UserFactoryDefineInput = { id?: string; @@ -56,14 +56,14 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const seqKey = {}; const getSeq = () => getSequenceCounter(seqKey); const screen = createScreener("User", modelFieldDefinitions); - const buildCreateInput = async (inputData: Partial = {}) => { + const build = async (inputData: Partial = {}) => { const seq = getSeq(); const requiredScalarData = autoGenerateUserScalarsOrEnums({ seq }); const resolveValue = normalizeResolver(defaultDataResolver ?? {}); const defaultData = await resolveValue({ seq }); const defaultAssociations = { profile: isUserprofileFactory(defaultData.profile) ? { - create: await defaultData.profile.buildCreateInput() + create: await defaultData.profile.build() } : defaultData.profile }; const data: Prisma.UserCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; @@ -73,13 +73,14 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac id: inputData.id }); const create = async (inputData: Partial = {}) => { - const data = await buildCreateInput(inputData).then(screen); + const data = await build(inputData).then(screen); return await getClient().user.create({ data }); }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, - buildCreateInput, + build, + buildCreateInput: build, pickForConnect, create, createForConnect, @@ -93,7 +94,7 @@ type ProfileScalarOrEnumFields = { }; type ProfileuserFactory = { _factoryFor: "User"; - buildCreateInput: () => PromiseLike; + build: () => PromiseLike; }; type ProfileFactoryDefineInput = { id?: string; @@ -116,14 +117,14 @@ function defineProfileFactoryInternal({ defaultData: defaultDataResolver }: Prof const seqKey = {}; const getSeq = () => getSequenceCounter(seqKey); const screen = createScreener("Profile", modelFieldDefinitions); - const buildCreateInput = async (inputData: Partial = {}) => { + const build = async (inputData: Partial = {}) => { const seq = getSeq(); const requiredScalarData = autoGenerateProfileScalarsOrEnums({ seq }); const resolveValue = normalizeResolver(defaultDataResolver ?? {}); const defaultData = await resolveValue({ seq }); const defaultAssociations = { user: isProfileuserFactory(defaultData.user) ? { - create: await defaultData.user.buildCreateInput() + create: await defaultData.user.build() } : defaultData.user }; const data: Prisma.ProfileCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; @@ -133,13 +134,14 @@ function defineProfileFactoryInternal({ defaultData: defaultDataResolver }: Prof id: inputData.id }); const create = async (inputData: Partial = {}) => { - const data = await buildCreateInput(inputData).then(screen); + const data = await build(inputData).then(screen); return await getClient().profile.create({ data }); }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Profile" as const, - buildCreateInput, + build, + buildCreateInput: build, pickForConnect, create, createForConnect, diff --git a/packages/ts-compile-testing/fixtures/simple-model/__generated__/fabbrica/index.ts b/packages/ts-compile-testing/fixtures/simple-model/__generated__/fabbrica/index.ts index ec586ba9..b3c0274e 100644 --- a/packages/ts-compile-testing/fixtures/simple-model/__generated__/fabbrica/index.ts +++ b/packages/ts-compile-testing/fixtures/simple-model/__generated__/fabbrica/index.ts @@ -36,7 +36,7 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const seqKey = {}; const getSeq = () => getSequenceCounter(seqKey); const screen = createScreener("User", modelFieldDefinitions); - const buildCreateInput = async (inputData: Partial = {}) => { + const build = async (inputData: Partial = {}) => { const seq = getSeq(); const requiredScalarData = autoGenerateUserScalarsOrEnums({ seq }); const resolveValue = normalizeResolver(defaultDataResolver ?? {}); @@ -49,13 +49,14 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac id: inputData.id }); const create = async (inputData: Partial = {}) => { - const data = await buildCreateInput(inputData).then(screen); + const data = await build(inputData).then(screen); return await getClient().user.create({ data }); }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, - buildCreateInput, + build, + buildCreateInput: build, pickForConnect, create, createForConnect, From 97c4e009f0eb4f163d877584e29b49094fb5b1c7 Mon Sep 17 00:00:00 2001 From: Quramy Date: Mon, 28 Nov 2022 02:07:29 +0900 Subject: [PATCH 2/3] feat: Add createList and buildList method --- .../src/__generated__/fabbrica/index.d.ts | 4 +++ .../src/__generated__/fabbrica/index.js | 20 +++++++++++++ .../example-prj/src/connectOrCreate.test.ts | 6 ++-- examples/example-prj/src/sample.test.ts | 4 +-- .../__snapshots__/getSourceFile.test.ts.snap | 10 +++++++ .../prisma-fabbrica/src/templates/index.ts | 16 ++++++++++ .../__generated__/fabbrica/index.ts | 20 +++++++++++++ .../__generated__/fabbrica/index.ts | 20 +++++++++++++ .../__generated__/fabbrica/index.ts | 30 +++++++++++++++++++ .../__generated__/fabbrica/index.ts | 20 +++++++++++++ .../__generated__/fabbrica/index.ts | 10 +++++++ 11 files changed, 154 insertions(+), 6 deletions(-) diff --git a/examples/example-prj/src/__generated__/fabbrica/index.d.ts b/examples/example-prj/src/__generated__/fabbrica/index.d.ts index 8353a537..c00646f5 100644 --- a/examples/example-prj/src/__generated__/fabbrica/index.d.ts +++ b/examples/example-prj/src/__generated__/fabbrica/index.d.ts @@ -17,11 +17,13 @@ type UserFactoryDefineOptions = { export declare function defineUserFactory(args?: UserFactoryDefineOptions): { _factoryFor: "User"; build: (inputData?: Partial) => Promise; + buildList: (inputData: number | Partial[]) => Promise; buildCreateInput: (inputData?: Partial) => Promise; pickForConnect: (inputData: User) => { id: string; }; create: (inputData?: Partial) => Promise; + createList: (inputData: number | Partial[]) => Promise; createForConnect: (inputData?: Partial) => Promise<{ id: string; }>; @@ -41,11 +43,13 @@ type PostFactoryDefineOptions = { export declare function definePostFactory(args: PostFactoryDefineOptions): { _factoryFor: "Post"; build: (inputData?: Partial) => Promise; + buildList: (inputData: number | Partial[]) => Promise; buildCreateInput: (inputData?: Partial) => Promise; pickForConnect: (inputData: Post) => { id: string; }; create: (inputData?: Partial) => Promise; + createList: (inputData: number | Partial[]) => Promise; createForConnect: (inputData?: Partial) => Promise<{ id: string; }>; diff --git a/examples/example-prj/src/__generated__/fabbrica/index.js b/examples/example-prj/src/__generated__/fabbrica/index.js index fdde8936..f13d7714 100644 --- a/examples/example-prj/src/__generated__/fabbrica/index.js +++ b/examples/example-prj/src/__generated__/fabbrica/index.js @@ -45,6 +45,10 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }) { const data = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; return data; }; + const buildList = (inputData) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => build(data))); + }; const pickForConnect = (inputData) => ({ id: inputData.id }); @@ -52,13 +56,19 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }) { const data = await build(inputData).then(screen); return await (0, clientHolder_1.getClient)().user.create({ data }); }; + const createList = (inputData) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => create(data))); + }; const createForConnect = (inputData = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User", build, + buildList, buildCreateInput: build, pickForConnect, create, + createList, createForConnect, }; } @@ -92,6 +102,10 @@ function definePostFactoryInternal({ defaultData: defaultDataResolver }) { const data = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; return data; }; + const buildList = (inputData) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => build(data))); + }; const pickForConnect = (inputData) => ({ id: inputData.id }); @@ -99,13 +113,19 @@ function definePostFactoryInternal({ defaultData: defaultDataResolver }) { const data = await build(inputData).then(screen); return await (0, clientHolder_1.getClient)().post.create({ data }); }; + const createList = (inputData) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => create(data))); + }; const createForConnect = (inputData = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Post", build, + buildList, buildCreateInput: build, pickForConnect, create, + createList, createForConnect, }; } diff --git a/examples/example-prj/src/connectOrCreate.test.ts b/examples/example-prj/src/connectOrCreate.test.ts index d94e11ce..f1ae44ab 100644 --- a/examples/example-prj/src/connectOrCreate.test.ts +++ b/examples/example-prj/src/connectOrCreate.test.ts @@ -11,7 +11,7 @@ const PostFactory = definePostFactory({ where: { id: "user001", }, - create: await UserFactory.buildCreateInput({ + create: await UserFactory.build({ id: "user001", }), }, @@ -27,9 +27,7 @@ describe("factories", () => { }); it("creates related user at most one", async () => { - await PostFactory.create(); - await PostFactory.create(); - await PostFactory.create(); + await PostFactory.createList(3); await expect(prisma.user.count()).resolves.toBe(1); }); }); diff --git a/examples/example-prj/src/sample.test.ts b/examples/example-prj/src/sample.test.ts index a7832b57..b62fbcec 100644 --- a/examples/example-prj/src/sample.test.ts +++ b/examples/example-prj/src/sample.test.ts @@ -27,11 +27,11 @@ describe("factories", () => { it("creates record with children relation", async () => { await UserFactory.create({ posts: { - create: [await PostFactory.buildCreateInput()], + create: await PostFactory.buildList(2), }, }); const created = await prisma.user.findFirst({ include: { posts: true } }); - expect(created?.posts.length).toBe(1); + expect(created?.posts.length).toBe(2); }); }); diff --git a/packages/prisma-fabbrica/src/templates/__snapshots__/getSourceFile.test.ts.snap b/packages/prisma-fabbrica/src/templates/__snapshots__/getSourceFile.test.ts.snap index 1dd96668..150d64c5 100644 --- a/packages/prisma-fabbrica/src/templates/__snapshots__/getSourceFile.test.ts.snap +++ b/packages/prisma-fabbrica/src/templates/__snapshots__/getSourceFile.test.ts.snap @@ -48,6 +48,10 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const data: Prisma.UserCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; return data; }; + const buildList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => build(data))); + }; const pickForConnect = (inputData: User) => ({ id: inputData.id }); @@ -55,13 +59,19 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const data = await build(inputData).then(screen); return await getClient().user.create({ data }); }; + const createList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => create(data))); + }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, build, + buildList, buildCreateInput: build, pickForConnect, create, + createList, createForConnect, }; } diff --git a/packages/prisma-fabbrica/src/templates/index.ts b/packages/prisma-fabbrica/src/templates/index.ts index f91671a6..654c22d8 100644 --- a/packages/prisma-fabbrica/src/templates/index.ts +++ b/packages/prisma-fabbrica/src/templates/index.ts @@ -299,6 +299,13 @@ export const defineModelFactoryInernal = (model: DMMF.Model, inputType: DMMF.Inp return data; }; + const buildList = ( + inputData: number | Partial[] + ) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => build(data))); + } + const pickForConnect = (inputData: ${() => ast.typeReferenceNode(model.name)}) => ( ${() => ast.objectLiteralExpression( @@ -316,14 +323,23 @@ export const defineModelFactoryInernal = (model: DMMF.Model, inputType: DMMF.Inp return await getClient().MODEL_KEY.create({ data }); }; + const createList = ( + inputData: number | Partial[] + ) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => create(data))); + } + const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: ${() => ast.stringLiteral(model.name)} as const, build, + buildList, buildCreateInput: build, pickForConnect, create, + createList, createForConnect, }; } diff --git a/packages/ts-compile-testing/fixtures/field-variation/__generated__/fabbrica/index.ts b/packages/ts-compile-testing/fixtures/field-variation/__generated__/fabbrica/index.ts index e7a07451..e7ed38ce 100644 --- a/packages/ts-compile-testing/fixtures/field-variation/__generated__/fabbrica/index.ts +++ b/packages/ts-compile-testing/fixtures/field-variation/__generated__/fabbrica/index.ts @@ -52,6 +52,10 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const data: Prisma.UserCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; return data; }; + const buildList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => build(data))); + }; const pickForConnect = (inputData: User) => ({ id: inputData.id }); @@ -59,13 +63,19 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const data = await build(inputData).then(screen); return await getClient().user.create({ data }); }; + const createList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => create(data))); + }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, build, + buildList, buildCreateInput: build, pickForConnect, create, + createList, createForConnect, }; } @@ -104,6 +114,10 @@ function defineComplexIdModelFactoryInternal({ defaultData: defaultDataResolver const data: Prisma.ComplexIdModelCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; return data; }; + const buildList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => build(data))); + }; const pickForConnect = (inputData: ComplexIdModel) => ({ firstName: inputData.firstName, lastName: inputData.lastName @@ -112,13 +126,19 @@ function defineComplexIdModelFactoryInternal({ defaultData: defaultDataResolver const data = await build(inputData).then(screen); return await getClient().complexIdModel.create({ data }); }; + const createList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => create(data))); + }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "ComplexIdModel" as const, build, + buildList, buildCreateInput: build, pickForConnect, create, + createList, createForConnect, }; } diff --git a/packages/ts-compile-testing/fixtures/relations-many-to-many/__generated__/fabbrica/index.ts b/packages/ts-compile-testing/fixtures/relations-many-to-many/__generated__/fabbrica/index.ts index bc1df720..6c057f30 100644 --- a/packages/ts-compile-testing/fixtures/relations-many-to-many/__generated__/fabbrica/index.ts +++ b/packages/ts-compile-testing/fixtures/relations-many-to-many/__generated__/fabbrica/index.ts @@ -58,6 +58,10 @@ function definePostFactoryInternal({ defaultData: defaultDataResolver }: PostFac const data: Prisma.PostCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; return data; }; + const buildList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => build(data))); + }; const pickForConnect = (inputData: Post) => ({ id: inputData.id }); @@ -65,13 +69,19 @@ function definePostFactoryInternal({ defaultData: defaultDataResolver }: PostFac const data = await build(inputData).then(screen); return await getClient().post.create({ data }); }; + const createList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => create(data))); + }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Post" as const, build, + buildList, buildCreateInput: build, pickForConnect, create, + createList, createForConnect, }; } @@ -111,6 +121,10 @@ function defineCategoryFactoryInternal({ defaultData: defaultDataResolver }: Cat const data: Prisma.CategoryCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; return data; }; + const buildList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => build(data))); + }; const pickForConnect = (inputData: Category) => ({ id: inputData.id }); @@ -118,13 +132,19 @@ function defineCategoryFactoryInternal({ defaultData: defaultDataResolver }: Cat const data = await build(inputData).then(screen); return await getClient().category.create({ data }); }; + const createList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => create(data))); + }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Category" as const, build, + buildList, buildCreateInput: build, pickForConnect, create, + createList, createForConnect, }; } diff --git a/packages/ts-compile-testing/fixtures/relations-one-to-many/__generated__/fabbrica/index.ts b/packages/ts-compile-testing/fixtures/relations-one-to-many/__generated__/fabbrica/index.ts index e69c1119..71eec8db 100644 --- a/packages/ts-compile-testing/fixtures/relations-one-to-many/__generated__/fabbrica/index.ts +++ b/packages/ts-compile-testing/fixtures/relations-one-to-many/__generated__/fabbrica/index.ts @@ -79,6 +79,10 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const data: Prisma.UserCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; return data; }; + const buildList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => build(data))); + }; const pickForConnect = (inputData: User) => ({ id: inputData.id }); @@ -86,13 +90,19 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const data = await build(inputData).then(screen); return await getClient().user.create({ data }); }; + const createList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => create(data))); + }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, build, + buildList, buildCreateInput: build, pickForConnect, create, + createList, createForConnect, }; } @@ -144,6 +154,10 @@ function definePostFactoryInternal({ defaultData: defaultDataResolver }: PostFac const data: Prisma.PostCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; return data; }; + const buildList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => build(data))); + }; const pickForConnect = (inputData: Post) => ({ id: inputData.id }); @@ -151,13 +165,19 @@ function definePostFactoryInternal({ defaultData: defaultDataResolver }: PostFac const data = await build(inputData).then(screen); return await getClient().post.create({ data }); }; + const createList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => create(data))); + }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Post" as const, build, + buildList, buildCreateInput: build, pickForConnect, create, + createList, createForConnect, }; } @@ -219,6 +239,10 @@ function defineReviewFactoryInternal({ defaultData: defaultDataResolver }: Revie const data: Prisma.ReviewCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; return data; }; + const buildList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => build(data))); + }; const pickForConnect = (inputData: Review) => ({ id: inputData.id }); @@ -226,13 +250,19 @@ function defineReviewFactoryInternal({ defaultData: defaultDataResolver }: Revie const data = await build(inputData).then(screen); return await getClient().review.create({ data }); }; + const createList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => create(data))); + }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Review" as const, build, + buildList, buildCreateInput: build, pickForConnect, create, + createList, createForConnect, }; } diff --git a/packages/ts-compile-testing/fixtures/relations-one-to-one/__generated__/fabbrica/index.ts b/packages/ts-compile-testing/fixtures/relations-one-to-one/__generated__/fabbrica/index.ts index 980791ca..2b5f7d93 100644 --- a/packages/ts-compile-testing/fixtures/relations-one-to-one/__generated__/fabbrica/index.ts +++ b/packages/ts-compile-testing/fixtures/relations-one-to-one/__generated__/fabbrica/index.ts @@ -69,6 +69,10 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const data: Prisma.UserCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; return data; }; + const buildList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => build(data))); + }; const pickForConnect = (inputData: User) => ({ id: inputData.id }); @@ -76,13 +80,19 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const data = await build(inputData).then(screen); return await getClient().user.create({ data }); }; + const createList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => create(data))); + }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, build, + buildList, buildCreateInput: build, pickForConnect, create, + createList, createForConnect, }; } @@ -130,6 +140,10 @@ function defineProfileFactoryInternal({ defaultData: defaultDataResolver }: Prof const data: Prisma.ProfileCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; return data; }; + const buildList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => build(data))); + }; const pickForConnect = (inputData: Profile) => ({ id: inputData.id }); @@ -137,13 +151,19 @@ function defineProfileFactoryInternal({ defaultData: defaultDataResolver }: Prof const data = await build(inputData).then(screen); return await getClient().profile.create({ data }); }; + const createList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => create(data))); + }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Profile" as const, build, + buildList, buildCreateInput: build, pickForConnect, create, + createList, createForConnect, }; } diff --git a/packages/ts-compile-testing/fixtures/simple-model/__generated__/fabbrica/index.ts b/packages/ts-compile-testing/fixtures/simple-model/__generated__/fabbrica/index.ts index b3c0274e..8b34bc49 100644 --- a/packages/ts-compile-testing/fixtures/simple-model/__generated__/fabbrica/index.ts +++ b/packages/ts-compile-testing/fixtures/simple-model/__generated__/fabbrica/index.ts @@ -45,6 +45,10 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const data: Prisma.UserCreateInput = { ...requiredScalarData, ...defaultData, ...defaultAssociations, ...inputData }; return data; }; + const buildList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => build(data))); + }; const pickForConnect = (inputData: User) => ({ id: inputData.id }); @@ -52,13 +56,19 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver }: UserFac const data = await build(inputData).then(screen); return await getClient().user.create({ data }); }; + const createList = (inputData: number | Partial[]) => { + const list = typeof inputData === "number" ? [...new Array(inputData).keys()].map(() => ({})) : inputData; + return Promise.all(list.map(data => create(data))); + }; const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, build, + buildList, buildCreateInput: build, pickForConnect, create, + createList, createForConnect, }; } From 07bd7a209ac24806d43c422d84b9cbb8f3bc8c5a Mon Sep 17 00:00:00 2001 From: Quramy Date: Mon, 28 Nov 2022 02:24:10 +0900 Subject: [PATCH 3/3] docs: Write about createList and buildList --- README.md | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 40a8649e..594e6fa4 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Prisma generator for model factories. - [Usage of factories](#usage-of-factories) - [Field default values](#field-default-values) - [Use sequence for scalar fields](#use-sequence-for-scalar-fields) + - [Shorthand for create list](#shorthand-for-create-list) - [Required relation](#required-relation) - [Connection helper](#connection-helper) - [Build input data only](#build-input-data-only) @@ -153,6 +154,24 @@ import { resetSequence } from "./__generated__/fabbrica"; beforeEach(() => resetSequence()); ``` +### Shorthand for create list + +Each factory provides `.createList` method to insert multiple records. + +```ts +await UserFactory.createList(3); + +// The above code is equivalent to the following + +await Promise.all([0, 1, 2].map(() => UserFactory.create())); +``` + +You can also pass list data assignable to `Partial[]` : + +```ts +await UserFactory.createList([{ id: "user01" }, { id: "user02" }]); +``` + ### Required relation Sometimes, creating a model requires other model existence. For example, the following model `Post` belongs to other model `User`. @@ -266,18 +285,20 @@ await PostFactory.create(); console.log(await prisma.user.count()); // -> 1 ``` +Like `createList`, `buildList` is also available. + ### has-many / has-one relation -Sometimes, you may want a user data whose has post record. You can use `PostFactory.build` too. +Sometimes, you may want a user data whose has post record. You can use `PostFactory.build` or `PostFactory.buildList` . ```ts await UserFactory.create({ posts: { - create: [await PostFactory.build()], + create: await PostFactory.buildList(2), }, }); -console.log(await prisma.post.count()); // -> 1 +console.log(await prisma.post.count()); // -> 2 ``` Note: In the above example, `PostFactory.build()` resolves JSON data such as: