This is the current documentation of Repository.findOne(...)
:
It seems to accept conditions, options or both as parameters, same for find()
.
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?
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.
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:
Either provide a
FindOneOptions
object, like:To load object and its relations you need to specify them: