diff --git a/README.md b/README.md index 90fd130..dae4470 100644 --- a/README.md +++ b/README.md @@ -176,6 +176,12 @@ await UserFactory.createList(3); await Promise.all([0, 1, 2].map(() => UserFactory.create())); ``` +The 2nd argument(optional) accepts an object which is assignable to `Partial` : + +```ts +await UserFactory.createList(3, { name: "Bob" }); +``` + You can also pass list data assignable to `Partial[]` : ```ts diff --git a/examples/example-prj/src/__generated__/fabbrica/index.d.ts b/examples/example-prj/src/__generated__/fabbrica/index.d.ts index f193189..7fb187d 100644 --- a/examples/example-prj/src/__generated__/fabbrica/index.d.ts +++ b/examples/example-prj/src/__generated__/fabbrica/index.d.ts @@ -39,10 +39,12 @@ export interface UserFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: User): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } export interface UserFactoryInterface = Record, TOptions extends UserFactoryDefineOptions = UserFactoryDefineOptions> extends UserFactoryInterfaceWithoutTraits { @@ -80,10 +82,12 @@ export interface LoginLogFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: LoginLog): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } export interface LoginLogFactoryInterface = Record, TOptions extends LoginLogFactoryDefineOptions = LoginLogFactoryDefineOptions> extends LoginLogFactoryInterfaceWithoutTraits { @@ -128,10 +132,12 @@ export interface PostFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: Post): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } export interface PostFactoryInterface = Record, TOptions extends PostFactoryDefineOptions = PostFactoryDefineOptions> extends PostFactoryInterfaceWithoutTraits { @@ -179,10 +185,12 @@ export interface CommentFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: Comment): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } export interface CommentFactoryInterface = Record, TOptions extends CommentFactoryDefineOptions = CommentFactoryDefineOptions> extends CommentFactoryInterfaceWithoutTraits { @@ -219,10 +227,12 @@ export interface CategoryFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: Category): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } export interface CategoryFactoryInterface = Record, TOptions extends CategoryFactoryDefineOptions = CategoryFactoryDefineOptions> extends CategoryFactoryInterfaceWithoutTraits { diff --git a/examples/example-prj/src/__generated__/fabbrica/index.js b/examples/example-prj/src/__generated__/fabbrica/index.js index 6c39d07..58f16e7 100644 --- a/examples/example-prj/src/__generated__/fabbrica/index.js +++ b/examples/example-prj/src/__generated__/fabbrica/index.js @@ -101,7 +101,7 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver, onAfterBu await handleAfterBuild(data, transientFields); return data; }; - const buildList = (inputData) => Promise.all((0, internal_1.normalizeList)(inputData).map(data => build(data))); + const buildList = (...args) => Promise.all((0, internal_1.normalizeList)(...args).map(data => build(data))); const pickForConnect = (inputData) => ({ id: inputData.id }); @@ -113,7 +113,7 @@ function defineUserFactoryInternal({ defaultData: defaultDataResolver, onAfterBu await handleAfterCreate(createdData, transientFields); return createdData; }; - const createList = (inputData) => Promise.all((0, internal_1.normalizeList)(inputData).map(data => create(data))); + const createList = (...args) => Promise.all((0, internal_1.normalizeList)(...args).map(data => create(data))); const createForConnect = (inputData = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User", @@ -188,7 +188,7 @@ function defineLoginLogFactoryInternal({ defaultData: defaultDataResolver, onAft await handleAfterBuild(data, transientFields); return data; }; - const buildList = (inputData) => Promise.all((0, internal_1.normalizeList)(inputData).map(data => build(data))); + const buildList = (...args) => Promise.all((0, internal_1.normalizeList)(...args).map(data => build(data))); const pickForConnect = (inputData) => ({ id: inputData.id }); @@ -200,7 +200,7 @@ function defineLoginLogFactoryInternal({ defaultData: defaultDataResolver, onAft await handleAfterCreate(createdData, transientFields); return createdData; }; - const createList = (inputData) => Promise.all((0, internal_1.normalizeList)(inputData).map(data => create(data))); + const createList = (...args) => Promise.all((0, internal_1.normalizeList)(...args).map(data => create(data))); const createForConnect = (inputData = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "LoginLog", @@ -282,7 +282,7 @@ function definePostFactoryInternal({ defaultData: defaultDataResolver, onAfterBu await handleAfterBuild(data, transientFields); return data; }; - const buildList = (inputData) => Promise.all((0, internal_1.normalizeList)(inputData).map(data => build(data))); + const buildList = (...args) => Promise.all((0, internal_1.normalizeList)(...args).map(data => build(data))); const pickForConnect = (inputData) => ({ id: inputData.id }); @@ -294,7 +294,7 @@ function definePostFactoryInternal({ defaultData: defaultDataResolver, onAfterBu await handleAfterCreate(createdData, transientFields); return createdData; }; - const createList = (inputData) => Promise.all((0, internal_1.normalizeList)(inputData).map(data => create(data))); + const createList = (...args) => Promise.all((0, internal_1.normalizeList)(...args).map(data => create(data))); const createForConnect = (inputData = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Post", @@ -382,7 +382,7 @@ function defineCommentFactoryInternal({ defaultData: defaultDataResolver, onAfte await handleAfterBuild(data, transientFields); return data; }; - const buildList = (inputData) => Promise.all((0, internal_1.normalizeList)(inputData).map(data => build(data))); + const buildList = (...args) => Promise.all((0, internal_1.normalizeList)(...args).map(data => build(data))); const pickForConnect = (inputData) => ({ id: inputData.id }); @@ -394,7 +394,7 @@ function defineCommentFactoryInternal({ defaultData: defaultDataResolver, onAfte await handleAfterCreate(createdData, transientFields); return createdData; }; - const createList = (inputData) => Promise.all((0, internal_1.normalizeList)(inputData).map(data => create(data))); + const createList = (...args) => Promise.all((0, internal_1.normalizeList)(...args).map(data => create(data))); const createForConnect = (inputData = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Comment", @@ -469,7 +469,7 @@ function defineCategoryFactoryInternal({ defaultData: defaultDataResolver, onAft await handleAfterBuild(data, transientFields); return data; }; - const buildList = (inputData) => Promise.all((0, internal_1.normalizeList)(inputData).map(data => build(data))); + const buildList = (...args) => Promise.all((0, internal_1.normalizeList)(...args).map(data => build(data))); const pickForConnect = (inputData) => ({ id: inputData.id }); @@ -481,7 +481,7 @@ function defineCategoryFactoryInternal({ defaultData: defaultDataResolver, onAft await handleAfterCreate(createdData, transientFields); return createdData; }; - const createList = (inputData) => Promise.all((0, internal_1.normalizeList)(inputData).map(data => create(data))); + const createList = (...args) => Promise.all((0, internal_1.normalizeList)(...args).map(data => create(data))); const createForConnect = (inputData = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Category", diff --git a/examples/example-prj/src/transient.test.ts b/examples/example-prj/src/transient.test.ts index 88542dc..4649260 100644 --- a/examples/example-prj/src/transient.test.ts +++ b/examples/example-prj/src/transient.test.ts @@ -8,7 +8,7 @@ const UserFactory = defineUserFactory.withTransientFields({ loginCount: 0, })({ onAfterCreate: async (user, { loginCount }) => { - await LoginLogFactory.createList([...new Array(loginCount).keys()].map(() => ({ userId: user.id }))); + await LoginLogFactory.createList(loginCount, { userId: user.id }); }, }); diff --git a/packages/artifact-testing/fixtures/field-variation/__generated__/fabbrica/index.ts b/packages/artifact-testing/fixtures/field-variation/__generated__/fabbrica/index.ts index 0b3ca3b..13b5662 100644 --- a/packages/artifact-testing/fixtures/field-variation/__generated__/fabbrica/index.ts +++ b/packages/artifact-testing/fixtures/field-variation/__generated__/fabbrica/index.ts @@ -74,10 +74,12 @@ export interface UserFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: User): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } @@ -131,7 +133,7 @@ function defineUserFactoryInternal, await handleAfterBuild(data, transientFields); return data; }; - const buildList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => build(data))); + const buildList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => build(data))); const pickForConnect = (inputData: User) => ({ id: inputData.id }); @@ -143,7 +145,7 @@ function defineUserFactoryInternal, await handleAfterCreate(createdData, transientFields); return createdData; }; - const createList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => create(data))); + const createList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => create(data))); const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, @@ -212,10 +214,12 @@ export interface ComplexIdModelFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: ComplexIdModel): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } @@ -269,7 +273,7 @@ function defineComplexIdModelFactoryInternal[]) => Promise.all(normalizeList(inputData).map(data => build(data))); + const buildList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => build(data))); const pickForConnect = (inputData: ComplexIdModel) => ({ firstName: inputData.firstName, lastName: inputData.lastName @@ -282,7 +286,7 @@ function defineComplexIdModelFactoryInternal[]) => Promise.all(normalizeList(inputData).map(data => create(data))); + const createList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => create(data))); const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "ComplexIdModel" as const, @@ -375,10 +379,12 @@ export interface FieldTypePatternModelFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: FieldTypePatternModel): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } @@ -439,7 +445,7 @@ function defineFieldTypePatternModelFactoryInternal[]) => Promise.all(normalizeList(inputData).map(data => build(data))); + const buildList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => build(data))); const pickForConnect = (inputData: FieldTypePatternModel) => ({ id: inputData.id }); @@ -451,7 +457,7 @@ function defineFieldTypePatternModelFactoryInternal[]) => Promise.all(normalizeList(inputData).map(data => create(data))); + const createList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => create(data))); const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "FieldTypePatternModel" as const, @@ -518,10 +524,12 @@ export interface NoPkModelFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: NoPkModel): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } @@ -574,7 +582,7 @@ function defineNoPkModelFactoryInternal[]) => Promise.all(normalizeList(inputData).map(data => build(data))); + const buildList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => build(data))); const pickForConnect = (inputData: NoPkModel) => ({ id: inputData.id }); @@ -586,7 +594,7 @@ function defineNoPkModelFactoryInternal[]) => Promise.all(normalizeList(inputData).map(data => create(data))); + const createList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => create(data))); const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "NoPkModel" as const, diff --git a/packages/artifact-testing/fixtures/relations-many-to-many/__generated__/fabbrica/index.ts b/packages/artifact-testing/fixtures/relations-many-to-many/__generated__/fabbrica/index.ts index 976b249..0fb7545 100644 --- a/packages/artifact-testing/fixtures/relations-many-to-many/__generated__/fabbrica/index.ts +++ b/packages/artifact-testing/fixtures/relations-many-to-many/__generated__/fabbrica/index.ts @@ -67,10 +67,12 @@ export interface PostFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: Post): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } @@ -124,7 +126,7 @@ function definePostFactoryInternal, await handleAfterBuild(data, transientFields); return data; }; - const buildList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => build(data))); + const buildList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => build(data))); const pickForConnect = (inputData: Post) => ({ id: inputData.id }); @@ -136,7 +138,7 @@ function definePostFactoryInternal, await handleAfterCreate(createdData, transientFields); return createdData; }; - const createList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => create(data))); + const createList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => create(data))); const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Post" as const, @@ -206,10 +208,12 @@ export interface CategoryFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: Category): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } @@ -263,7 +267,7 @@ function defineCategoryFactoryInternal[]) => Promise.all(normalizeList(inputData).map(data => build(data))); + const buildList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => build(data))); const pickForConnect = (inputData: Category) => ({ id: inputData.id }); @@ -275,7 +279,7 @@ function defineCategoryFactoryInternal[]) => Promise.all(normalizeList(inputData).map(data => create(data))); + const createList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => create(data))); const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Category" as const, diff --git a/packages/artifact-testing/fixtures/relations-one-to-many/__generated__/fabbrica/index.ts b/packages/artifact-testing/fixtures/relations-one-to-many/__generated__/fabbrica/index.ts index ae1bb48..4a151f0 100644 --- a/packages/artifact-testing/fixtures/relations-one-to-many/__generated__/fabbrica/index.ts +++ b/packages/artifact-testing/fixtures/relations-one-to-many/__generated__/fabbrica/index.ts @@ -88,10 +88,12 @@ export interface UserFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: User): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } @@ -145,7 +147,7 @@ function defineUserFactoryInternal, await handleAfterBuild(data, transientFields); return data; }; - const buildList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => build(data))); + const buildList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => build(data))); const pickForConnect = (inputData: User) => ({ id: inputData.id }); @@ -157,7 +159,7 @@ function defineUserFactoryInternal, await handleAfterCreate(createdData, transientFields); return createdData; }; - const createList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => create(data))); + const createList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => create(data))); const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, @@ -237,10 +239,12 @@ export interface PostFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: Post): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } @@ -298,7 +302,7 @@ function definePostFactoryInternal, await handleAfterBuild(data, transientFields); return data; }; - const buildList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => build(data))); + const buildList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => build(data))); const pickForConnect = (inputData: Post) => ({ id: inputData.id }); @@ -310,7 +314,7 @@ function definePostFactoryInternal, await handleAfterCreate(createdData, transientFields); return createdData; }; - const createList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => create(data))); + const createList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => create(data))); const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Post" as const, @@ -399,10 +403,12 @@ export interface ReviewFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: Review): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } @@ -463,7 +469,7 @@ function defineReviewFactoryInternal await handleAfterBuild(data, transientFields); return data; }; - const buildList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => build(data))); + const buildList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => build(data))); const pickForConnect = (inputData: Review) => ({ id: inputData.id }); @@ -475,7 +481,7 @@ function defineReviewFactoryInternal await handleAfterCreate(createdData, transientFields); return createdData; }; - const createList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => create(data))); + const createList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => create(data))); const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Review" as const, diff --git a/packages/artifact-testing/fixtures/relations-one-to-one/__generated__/fabbrica/index.ts b/packages/artifact-testing/fixtures/relations-one-to-one/__generated__/fabbrica/index.ts index eefe60d..b0a86ae 100644 --- a/packages/artifact-testing/fixtures/relations-one-to-one/__generated__/fabbrica/index.ts +++ b/packages/artifact-testing/fixtures/relations-one-to-one/__generated__/fabbrica/index.ts @@ -76,10 +76,12 @@ export interface UserFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: User): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } @@ -137,7 +139,7 @@ function defineUserFactoryInternal, await handleAfterBuild(data, transientFields); return data; }; - const buildList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => build(data))); + const buildList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => build(data))); const pickForConnect = (inputData: User) => ({ id: inputData.id }); @@ -149,7 +151,7 @@ function defineUserFactoryInternal, await handleAfterCreate(createdData, transientFields); return createdData; }; - const createList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => create(data))); + const createList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => create(data))); const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, @@ -226,10 +228,12 @@ export interface ProfileFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: Profile): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } @@ -286,7 +290,7 @@ function defineProfileFactoryInternal[]) => Promise.all(normalizeList(inputData).map(data => build(data))); + const buildList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => build(data))); const pickForConnect = (inputData: Profile) => ({ id: inputData.id }); @@ -298,7 +302,7 @@ function defineProfileFactoryInternal[]) => Promise.all(normalizeList(inputData).map(data => create(data))); + const createList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => create(data))); const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "Profile" as const, diff --git a/packages/artifact-testing/fixtures/simple-model/__generated__/fabbrica/index.ts b/packages/artifact-testing/fixtures/simple-model/__generated__/fabbrica/index.ts index e8cb4f5..f53aed8 100644 --- a/packages/artifact-testing/fixtures/simple-model/__generated__/fabbrica/index.ts +++ b/packages/artifact-testing/fixtures/simple-model/__generated__/fabbrica/index.ts @@ -54,10 +54,12 @@ export interface UserFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: User): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } @@ -111,7 +113,7 @@ function defineUserFactoryInternal, await handleAfterBuild(data, transientFields); return data; }; - const buildList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => build(data))); + const buildList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => build(data))); const pickForConnect = (inputData: User) => ({ id: inputData.id }); @@ -123,7 +125,7 @@ function defineUserFactoryInternal, await handleAfterCreate(createdData, transientFields); return createdData; }; - const createList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => create(data))); + const createList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => create(data))); const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, diff --git a/packages/prisma-fabbrica/src/helpers/list.ts b/packages/prisma-fabbrica/src/helpers/list.ts index 977a811..67dedd5 100644 --- a/packages/prisma-fabbrica/src/helpers/list.ts +++ b/packages/prisma-fabbrica/src/helpers/list.ts @@ -1,7 +1,10 @@ -export function normalizeList>(input: number | readonly T[]) { - if (typeof input === "number") { - return [...new Array(input).keys()].map(() => ({}) as T); +export function normalizeList>(...args: any[]): T[] { + const [countOrList, item = {}] = args; + if (typeof countOrList === "number") { + return [...new Array(countOrList).keys()].map(() => item) as T[]; + } else if (Array.isArray(countOrList)) { + return countOrList as T[]; } else { - return input; + throw new Error("Illegal Argument"); } } 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 354e039..ee17194 100644 --- a/packages/prisma-fabbrica/src/templates/__snapshots__/getSourceFile.test.ts.snap +++ b/packages/prisma-fabbrica/src/templates/__snapshots__/getSourceFile.test.ts.snap @@ -57,10 +57,12 @@ export interface UserFactoryInterfaceWithoutTraits): PromiseLike; buildCreateInput(inputData?: Partial): PromiseLike; - buildList(inputData: number | readonly Partial[]): PromiseLike; + buildList(list: readonly Partial[]): PromiseLike; + buildList(count: number, item?: Partial): PromiseLike; pickForConnect(inputData: User): Pick; create(inputData?: Partial): PromiseLike; - createList(inputData: number | readonly Partial[]): PromiseLike; + createList(list: readonly Partial[]): PromiseLike; + createList(count: number, item?: Partial): PromiseLike; createForConnect(inputData?: Partial): PromiseLike>; } @@ -114,7 +116,7 @@ function defineUserFactoryInternal, await handleAfterBuild(data, transientFields); return data; }; - const buildList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => build(data))); + const buildList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => build(data))); const pickForConnect = (inputData: User) => ({ id: inputData.id }); @@ -126,7 +128,7 @@ function defineUserFactoryInternal, await handleAfterCreate(createdData, transientFields); return createdData; }; - const createList = (inputData: number | readonly Partial[]) => Promise.all(normalizeList(inputData).map(data => create(data))); + const createList = (...args: unknown[]) => Promise.all(normalizeList>(...args).map(data => create(data))); const createForConnect = (inputData: Partial = {}) => create(inputData).then(pickForConnect); return { _factoryFor: "User" as const, diff --git a/packages/prisma-fabbrica/src/templates/index.ts b/packages/prisma-fabbrica/src/templates/index.ts index 0c5b3c1..51d87c2 100644 --- a/packages/prisma-fabbrica/src/templates/index.ts +++ b/packages/prisma-fabbrica/src/templates/index.ts @@ -293,10 +293,12 @@ export const modelFactoryInterfaceWithoutTraits = (model: DMMF.Model) => readonly _factoryFor: ${() => ast.literalTypeNode(ast.stringLiteral(model.name))} build(inputData?: CREATE_INPUT_TYPE): PromiseLike buildCreateInput(inputData?: CREATE_INPUT_TYPE): PromiseLike - buildList(inputData: number | readonly CREATE_INPUT_TYPE[]): PromiseLike + buildList(list: readonly CREATE_INPUT_TYPE[]): PromiseLike + buildList(count: number, item?: CREATE_INPUT_TYPE): PromiseLike pickForConnect(inputData: MODEL_TYPE): Pick create(inputData?: CREATE_INPUT_TYPE): PromiseLike - createList(inputData: number | readonly CREATE_INPUT_TYPE[]): PromiseLike + createList(list: readonly CREATE_INPUT_TYPE[]): PromiseLike + createList(count: number, item?: CREATE_INPUT_TYPE): PromiseLike createForConnect(inputData?: CREATE_INPUT_TYPE): PromiseLike> } `({ @@ -453,9 +455,7 @@ export const defineModelFactoryInternal = (model: DMMF.Model, inputType: DMMF.In return data; }; - const buildList = ( - inputData: number | readonly CREATE_INPUT_TYPE[] - ) => Promise.all(normalizeList(inputData).map(data => build(data))); + const buildList = (...args: unknown[]) => Promise.all(normalizeList(...args).map(data => build(data))); const pickForConnect = (inputData: ${() => ast.typeReferenceNode(model.name)}) => ( ${() => @@ -478,9 +478,7 @@ export const defineModelFactoryInternal = (model: DMMF.Model, inputType: DMMF.In return createdData; }; - const createList = ( - inputData: number | readonly CREATE_INPUT_TYPE[] - ) => Promise.all(normalizeList(inputData).map(data => create(data))); + const createList = (...args: unknown[]) => Promise.all(normalizeList(...args).map(data => create(data))); const createForConnect = (inputData: CREATE_INPUT_TYPE = {}) => create(inputData).then(pickForConnect);