Skip to content
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

Reuse preloaded lazy relations #2965

Closed
StrikeForceZero opened this issue Oct 22, 2018 · 0 comments
Closed

Reuse preloaded lazy relations #2965

StrikeForceZero opened this issue Oct 22, 2018 · 0 comments

Comments

@StrikeForceZero
Copy link
Contributor

StrikeForceZero commented Oct 22, 2018

Issue type:

[ ] question
[ ] bug report
[x] feature request
[ ] documentation issue

Database system/driver:

[x] all?
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

import { Note } from './note';

@TypeOrm.Entity()
export class Person {
    @TypeOrm.PrimaryGeneratedColumn()
    public id: number;

    @TypeOrm.Column()
    public name: string;

    @TypeOrm.OneToMany(type => Note, note => note.owner, { lazy: true })
    public notes: Promise<Note[]> | Note[];
}
import { Person } from './person';

@TypeOrm.Entity()
export class Note {
    @TypeOrm.PrimaryGeneratedColumn()
    public id: number;

    @TypeOrm.Column()
    public label: string;

    @TypeOrm.ManyToOne(type => Person, { lazy: true })
    public owner: Promise<Person> | Person;
}
const repoPerson = connection.getRepository(Person);
const repoNote = connection.getRepository(Note);

const personA  = await repoPerson.create({ name: 'personA' });
const personB    = await repoPerson.create({ name: 'personB' });

await repoPerson.save([
        personA,
        personB,
])

await repoNote.insert({ label: 'note1', owner: personA });
await repoNote.insert({ label: 'note2', owner: personB });

const res = await repoPerson.find({ relations: [ 'notes' ] });
console.log(res);
console.log(await res[ 0 ].notes);

results:

query: SELECT "Person"."id" AS "Person_id", "Person"."name" AS "Person_name", "Person_notes"."id" AS "Person_notes_id", "Person_notes"."label" AS "Person_notes_label", "Person_notes"."ownerId" AS "Person_notes_ownerId" FROM "person" "Person" LEFT JOIN "note" "Person_notes" ON "Person_notes"."ownerId"="Person"."id"
[ Person { id: 1, name: 'personA', __notes__: [ [Object] ] },
  Person { id: 2, name: 'personB', __notes__: [ [Object] ] } ]
query: SELECT "notes"."id" AS "notes_id", "notes"."label" AS "notes_label", "notes"."ownerId" AS "notes_ownerId" FROM "note" "notes" WHERE "notes"."ownerId" IN (?) -- PARAMETERS: [1]
[ Note { id: 1, label: 'note1' } ]

I'd expect it to reuse the relationship that was preloaded instead of reselecting:

query: SELECT "Person"."id" AS "Person_id", "Person"."name" AS "Person_name", "Person_notes"."id" AS "Person_notes_id", "Person_notes"."label" AS "Person_notes_label", "Person_notes"."ownerId" AS "Person_notes_ownerId" FROM "person" "Person" LEFT JOIN "note" "Person_notes" ON "Person_notes"."ownerId"="Person"."id"
[ Person { id: 1, name: 'personA', __notes__: [ [Object] ] },
  Person { id: 2, name: 'personB', __notes__: [ [Object] ] } ]
[ Note { id: 1, label: 'note1' } ]

PR #2966

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants