Typeorm: `find` and `findOne` don't work as documented

Created on 18 Sep 2017  Â·  3Comments  Â·  Source: typeorm/typeorm

This is the current documentation of Repository.findOne(...):
image
It seems to accept conditions, options or both as parameters, same for find().

image

When using one of the methods, my linter warns me that it only accepts one parameter – a FindManyOptions for find() and a FindOneOptions for findOne().

Did the API for that change and the documentation is outdated?
And if there's a new API, how do I use it to load an object with its relations?

question

Most helpful comment

You are right, docs are outdated. Currently website's docs are for 0.0.11. Im planning to update website docs once 0.1.0 final version is released. Expect this somewhere in the end of October, beginning of November. New docs will be based on this directory..

To use find method you can either provide object with conditions, like:

.findOne({ name: "Timber" })

Either provide a FindOneOptions object, like:

.findOne({
    /**
     * Specifies what columns should be retrieved.
     */
    select?: (keyof Entity)[];

    /**
     * Simple condition that should be applied to match entities.
     */
    where?: Partial<Entity>|ObjectLiteral;

    /**
     * Indicates what relations of entity should be loaded (simplified left join form).
     */
    relations?: (keyof Entity)[];

    /**
     * Specifies what relations should be loaded.
     */
    join?: JoinOptions;

    /**
     * Order, in which entities should be ordered.
     */
    order?: { [P in keyof Entity]?: "ASC"|"DESC" };
})

To load object and its relations you need to specify them:

.findOne({ relations: ["category", "tags", ...] })

All 3 comments

You are right, docs are outdated. Currently website's docs are for 0.0.11. Im planning to update website docs once 0.1.0 final version is released. Expect this somewhere in the end of October, beginning of November. New docs will be based on this directory..

To use find method you can either provide object with conditions, like:

.findOne({ name: "Timber" })

Either provide a FindOneOptions object, like:

.findOne({
    /**
     * Specifies what columns should be retrieved.
     */
    select?: (keyof Entity)[];

    /**
     * Simple condition that should be applied to match entities.
     */
    where?: Partial<Entity>|ObjectLiteral;

    /**
     * Indicates what relations of entity should be loaded (simplified left join form).
     */
    relations?: (keyof Entity)[];

    /**
     * Specifies what relations should be loaded.
     */
    join?: JoinOptions;

    /**
     * Order, in which entities should be ordered.
     */
    order?: { [P in keyof Entity]?: "ASC"|"DESC" };
})

To load object and its relations you need to specify them:

.findOne({ relations: ["category", "tags", ...] })

@pleerock Thanks! :smile_cat: I really like this new way of loading relations, it's a lot more readable.

Yes its simple and can (and should) be used in most of cases. But its less flexible. Old method is still can be used using "join" property: find({ join: { ... } }). But if you have really complex queries then its always better to use query builder instead.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SPAHI4 picture SPAHI4  Â·  3Comments

guscastro picture guscastro  Â·  3Comments

pleerock picture pleerock  Â·  3Comments

leixu2txtek picture leixu2txtek  Â·  3Comments

banduk picture banduk  Â·  3Comments