objection.js icon indicating copy to clipboard operation
objection.js copied to clipboard

Question: Type defintion of QueryBuilderUpdate extends Executable<number>?

Open hardeep opened this issue 9 years ago • 8 comments

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>.

hardeep avatar Nov 29 '17 23:11 hardeep

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.

hardeep avatar Nov 30 '17 02:11 hardeep

@mceachen Could you take a look at this?

koskimas avatar Dec 01 '17 21:12 koskimas

@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 avatar Dec 01 '17 22:12 mceachen

@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?

koskimas avatar Dec 02 '17 21:12 koskimas

@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 avatar Dec 02 '17 22:12 mceachen

@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>

koskimas avatar Dec 04 '17 06:12 koskimas

The typings for patch() and relate() currently yield a Model rather than a number. See #717.

jtlapp avatar Jan 13 '18 18:01 jtlapp

This was resolved by #753

mceachen avatar Jan 25 '18 20:01 mceachen