Comments (6)
Thanks for reporting this @rofreg and for your comment, @casey-chow!
I just started a Prisma project to understand the problem better and am experimenting with solutions. Would it solve your problem if we were to add a new third template argument so the return type of create
is customizable? Something like:
type User = {
name: string;
};
type SavedUser = User & {
id: number;
createdAt: 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<User, any, SavedUser>(({ onCreate }) => {
onCreate((user) => prisma.user.create(user));
return {
name: "Stephen",
};
});
const user = userFactory.create();
user.id // works
I've started putting together some work on this and think it will likely be feasible.
from fishery.
@rofreg @casey-chow @t3hpr1m3 : I've finally opened a PR, #74, with the functionality I proposed (a different return type for create
). Take a look if you get a chance!
from fishery.
One stopgap solution would be to create a factory of User | UserCreateInput
so that it still typechecks on creation. Alternatively (and this is the ugly solution imo) you can also shove a sentinel value (like -1
) into the id field that is removed before calling Prisma in your onCreate
hook.
from fishery.
I'm currently working around this problem by using the supplied sequence
to derive my id's, and in some cases, ignoring the derived id altogether on save. Basically, I have a helper method:
const TEST_ID_OFFSET = 10000;
const testId = (id: number): number => (
id + TEST_ID_OFFSET
);
This gives me an id that leaves room for more than enough auto-incrementing records to be added to the database before collision. I plan on working on a PR that would allow "seeding" the initial sequence value.
Ignoring the sequence
id on save is fairly straightforward:
const userFactory = Factory.define<User>(({ sequence, onCreate }) => {
onCreate((user): Promise<User> => {
const { id, ...userProps } = user;
return userRepo.save(userProps);
});
return {
id: testId(sequence),
email: `user${sequence}@example.com`
};
});
It isn't elegant, but we're able to avoid collisions.
from fishery.
I've started putting together some work on this and think it will likely be feasible.
@stevehanson I see there's an in-progress branch, did you encounter any blockers working on this? I'm migrating my codebase to Prisma at the moment, starting with the factories, so I think I could justify putting some time into this feature.
from fishery.
@rofreg @casey-chow @t3hpr1m3 : I've finally opened a PR, #74, with the functionality I proposed (a different return type for
create
). Take a look if you get a chance!
Looks great! I've been using the old branch (merged with the current tip for main
) and it's been working just great for me.
from fishery.
Related Issues (20)
- `Date` params are hard to use with `DeepPartial` HOT 1
- buildList and createList typing doesn't outrule empty array from results HOT 1
- Confusion about updating associations dynamically
- Guidance on Production Use? HOT 2
- Expose the number in the list of the current instance in a `createList` or `buildList` scenario
- Subset of items for buildList should be build for each item HOT 4
- Difficulty using objects with literal values in params HOT 5
- Symbol properties inside objects passed to the build function as associations are not getting merged into result HOT 1
- Factory produces data incompatible to its type constraint HOT 2
- Extending a factory with new fields HOT 2
- How to use rewind sequence HOT 2
- Object properties are merged instead overriden HOT 1
- Loose inferred return type of generator function
- Support for required values typing on build/create, not on define HOT 1
- Question: how to share onCreate code for multiple factories? HOT 1
- Use 'in transientParams' for defaults instead of destructuring
- onBuild function HOT 1
- Iād Love To See an `attributesFor` Option Like in `factory_bot` HOT 5
- Factory cannot build class with methods HOT 2
- Invalid object produced for optional properties that have multiple nested required properties
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fishery.