-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
factory.create() expected usage with an ORM or 3rd party API #53
Comments
Thanks for reporting this, @avimoondra! I just started a Sequelize project to play around with this. Would it potentially help if we allowed specifying a different return type for // These are all the attributes in the User model
interface UserAttributes {
id: number;
name: string;
}
// Some attributes are optional in `User.build` and `User.create` calls
interface UserCreationAttributes extends Optional<UserAttributes, "id"> {}
class User
extends Model<UserAttributes, UserCreationAttributes>
implements UserAttributes
{
public id!: number; // Note that the `null assertion` `!` is required in strict mode.
public name!: string;
// timestamps!
public readonly createdAt!: Date;
public readonly updatedAt!: Date;
...
}
// template arg 1 (currently exists) = The type that is returned from build()
// template arg 2 (currently exists) = The type of any transient params that your factory takes (defaults to 'any')
// template arg 3 (new) = The type that is returned from create()
const userFactory = Factory.define<UserAttributes, TransientParams, User>(({ onCreate }) => {
onCreate((user) => User.create(user));
return {
id: 1,
name: "Stephen",
};
});
const user = userFactory.create({ name: "Bill" });
user.createdAt // works |
@stevehanson that looks great! Since the library is only a few core files (yet very powerful!) and we heavily use create (vs. build), we decided to port it over for our use with Sequelize v5/v6 with the hopes of coming back around to use Fishery again sometime in the future. Below is what we extracted out... (very similar, using TS generics)
(yes!)
Example model
Example factory
|
@stevehanson that'd be fantastic. I've a similar use case where I need to call plain ol' Typescript class constructors after generating the POJO so your proposal looks perfect. 🙂 |
I'm integrating fishery with Sequelize (https://sequelize.org/), and am not sure if I'm taking the correct approach... Is this the expected usage?
Is there a way to return the created database object back from someFactory.create() calls? So this reads like...
A different approach was taken here - https://paramander.com/en/blog/extending-fishery-to-build-test-data-for-sequelize-models but seems to miss onCreate hooks since super.create is not called in the mixin?
Thanks!
The text was updated successfully, but these errors were encountered: