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
Lazy relation promise persistence #2902
Lazy relation promise persistence #2902
Conversation
I've created this PR to prompt some discussion about how lazy loading might be improved, by enabling the user to provide lazy relation values using One of the main advantages to these changes is that they remove the need for nasty TypeScript type casts when assigning object literals to lazy-load properties (which require In addition to the tests for #2729, there are 2 main changes in this PR:
Change #1 enables the user to provide a @Entity()
class Person {
name: string;
@OneToMany(() => Pet, pet => pet.owner, { lazy: true })
pets: Promise<Pet[]>;
}
@Entity()
class Pet {
name: string;
type: "cat" | "dog" | "dinosaur";
@ManyToOne(() => Person, person => person.pets, { lazy: true })
owner: Promise<Person>;
}
const personRepo = getRespository(Person);
const person = personRepo.create({
name: 'Fred Flintstone',
pets: Promise.resolve([
{ name: 'Dino', type; "dinosaur" }
])
});
// "person" will now be a Person instance, with person.pets a Promise<Pet[]>
// (ie. the object literal resolved by `Promise.resolve()` is transformed to a `Pet` instance). Change #2 allows saving the entities with unresolved user-provided const manager = getEntityManager();
const petRepo = getRespository(Pet);
const person = new Person();
person.name = 'Fred Flintstone';
// e.g. set relation to unresolved Promise loading related records from DB
person.pets = petRepo.findMany({ name: 'Dino' });
// "findMany" promise has not yet resolved, but "save" will now `await` the user-set `Promise`
await manager.save(person); I expect that you TypeORM maintainers will want some improvements to the supporting changes before merging this PR - particularly those around the identification of "dirty" (ie. user-provided) To identify these "dirty" promises, I've added logic to So I thought rather than having these changes wasting away on my SSD, I'd create this PR and start the discussion :) Any ideas / suggestions / responses? |
Im thinking to drop this feature from TypeORM. From the beginning it was experimental and too many people abuse it. And they all had problems. |
You mean whole lazy loading or just some part of it? |
I mean this |
Any updates here? |
This is quite old PR and there are too many conflicts. Closing for now, if somebody interested in these changes please feel free to create a new issue. |
await and persist resolved value of Promise instances on lazy relation properties (closes #2729)