Change the interface of Transient Fields
mizdra opened this issue · 0 comments
mizdra commented
- Unblock #59
- No need to export internal APIs such as
defineAuthorFactoryInternal
- Easy to define transient fields
- The interface is designed with reference to Quramy/prisma-fabbrica#252
Before
import {
defineAuthorFactoryInternal,
dynamic,
FieldsResolver,
Traits,
AuthorFactoryDefineOptions,
AuthorFactoryInterface,
} from '../__generated__/fabbrica';
import { Author } from '../__generated__/types';
// Prepare custom `defineAuthorFactory` with transient fields
type AuthorTransientFields = {
bookCount: number;
};
function defineAuthorFactoryWithTransientFields<
_DefaultFieldsResolver extends FieldsResolver<Author & AuthorTransientFields>,
_Traits extends Traits<Author, AuthorTransientFields>,
>(
options: AuthorFactoryDefineOptions<AuthorTransientFields, _DefaultFieldsResolver, _Traits>,
): AuthorFactoryInterface<AuthorTransientFields, _DefaultFieldsResolver, _Traits> {
return defineAuthorFactoryInternal(options);
}
// Use custom `defineAuthorFactory`
const AuthorFactory = defineAuthorFactoryWithTransientFields({
defaultFields: {
id: dynamic(({ seq }) => `Author-${seq}`),
name: 'Komata Mikami',
books: dynamic(async ({ get }) => {
const bookCount = (await get('bookCount')) ?? 0;
return BookFactory.buildList(bookCount);
}),
bookCount: 0,
},
});
After
import {
dynamic,
FieldsResolver,
Traits,
AuthorFactoryDefineOptions,
AuthorFactoryInterface,
} from '../__generated__/fabbrica';
import { Author } from '../__generated__/types';
const AuthorFactory = defineAuthorFactory.withTransientFields({
bookCount: 0,
})({
defaultFields: {
id: dynamic(({ seq }) => `Author-${seq}`),
name: 'Komata Mikami',
books: dynamic(async ({ get }) => {
const bookCount = (await get('bookCount')) ?? 0;
return BookFactory.buildList(bookCount);
}),
},
});
Breaking Changes
defineXxxFactoryInternal
is not exported now. UsedefineXxxFactory.withTransientFields
to define Transient Fields.