Question: Type defintion of QueryBuilderUpdate extends Executable<number>?
I'm using the .patch method and when returning from a promise chain, I'm getting a type mismatch. The QueryBuilderUpdate interface extends Executable<number>, should it be extending Executable<T>?
Looking at the .patchmethod it seems to return QueryBuilderUpdate<T>.
I just noticed that patch does return a number – I believe, the amount of rows affected? Perhaps using a Generic with a default? Executable<T = number>?
/**
* Query builder for update operations
*/
export interface QueryBuilderUpdate<T>
extends QueryBuilderBase<T>,
ThrowIfNotFound,
Executable<T = number> {
returning(columns: string | string[]): QueryBuilder<T>;
runAfter(fn: (result: number, builder: this) => any): this;
}
I came across this issue because from a promise, I'm returning a new object if it doesn't exist or returning a patched object if it does exist.
@mceachen Could you take a look at this?
@koskimas you changed these lines in 2a077f10f, and I think it had been correct. Which is the correct signature? Do you expect patch and update to return a number, or the updated model instance?
@mceachen patch, update and delete return a number by default, but chaining returning changes the result to model instances. I changed those so that the model type can be recovered when returning is chained. Shouldn't typescript catch the returned type from then and realise that the promise is a Promise<T> after that?
@hardeep the QueryBuilderUpdate shouldn't extend Executable--see the discussion and the diff.
With respect to your Executable<T = number> suggestion, I don't believe a default is appropriate there, as T captures the query builder model type, which would never be a number.
@koskimas It seems like you're talking about a different issue from OP?
I think it'd help to share example code with expected types to help clarify what we all are talking about here, perhaps?
@mceachen Umm.. I'm confused. I commented on this
you changed these lines in 2a077f10f, and I think it had been correct
by explaining why I changed it. I'm reading the quoted line like "I think koskimas broke it". Did I misunderstand it?
The return type of patch was number before my change and is number after my change. The return type should remain number since the query really returns a number.
As for the original issue, @hardeep said
I came across this issue because from a promise, I'm returning a new object if it doesn't exist or returning a patched object if it does exist.
I fail to see how this has anything to do with objection typings. I would expect typescript to infer the return type of Promise.then in a case like this:
const horse: Horse = Person
.query()
.patch({foo: 'bar'}) // Promise<number>
.then(() => new Person()) // Promise<Person>
.then(() => new Horse()) // Promise<Horse>
The typings for patch() and relate() currently yield a Model rather than a number. See #717.
This was resolved by #753