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

Change ModelOrObject to Partial<T> #329

Merged
merged 6 commits into from
Mar 16, 2017
Merged

Conversation

pvatterott
Copy link
Contributor

As far as I can tell, the functions that accept ModelOrObject parameters
(Insert, InsertGraphAndFetch, insertAndFetch. update*, patch,
relate) all allow Object as well as Model so that the caller can provide
an object that does not define all the properties of the Model.

This seems like a weaker type check than is possible with the Partial
type that is provided by typescript:
https://www.typescriptlang.org/docs/handbook/advanced-types.html#mapped-types

This commit removes the ModelOrObject and ModelsOrObjects types and replaces
them with Partial and Partial[] where appropriate. This will provide stronger
type checking in the affected calls: callers will no longer be able to set arbitrary
properties in their insert/update/patch calls. They will be restricted to the subset of
properties that exist on the model definition.

As far as I can tell, the functions that accept ModelOrObject parameters
(Insert<T>, InsertGraphAndFetch<T>, insertAndFetch<T>. update*, patch,
relate) all allow Object as well as Model so that the caller can provide
an object that does not define all the properties of the Model.

This seems like a weaker type check than is possible with the Partial<T>
type that is provided by typescript:

https://www.typescriptlang.org/docs/handbook/advanced-types.html#mapped-types

This commit changes the ModelOrObject and ModelsOrObjects types to
PartialModel and PartialOrPartials types and makes appropriate
replacements.
@coveralls
Copy link

coveralls commented Mar 13, 2017

Coverage Status

Coverage remained the same at 94.452% when pulling 367e9e3 on Vitallyio:partial-typing into 73edf6d on Vincit:master.

@koskimas
Copy link
Collaborator

Sounds good. @mceachen Do you have any thoughts about this?

@mceachen
Copy link
Collaborator

This sounds great, I'll review the PR later today.

Copy link
Collaborator

@mceachen mceachen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with the one small change. Thanks for doing this!

@@ -302,15 +299,14 @@ declare module "objection" {
export interface QueryBuilder<T> extends QueryBuilderBase<T>, Promise<T[]> { }

interface Insert<T> {
<M extends Model[] | Object[]>(modelsOrObjects?: M): QueryBuilder<T>;
<M extends ModelOrObject>(modelOrObject?: M): QueryBuilderSingle<T>;
(modelsOrObjects?: Array<Partial<T>>): QueryBuilder<T>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's generally preferred to use Partial<T>[] instead of Array<Partial<T>>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good! Thanks for taking a look

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, could you add an example to tests/ts/examples.ts? Something as simple as

// Verify that insert accepts Partial<Person>:

Person.query()
  .insert({ firstName: "test" })
  .then((p: Person) => console.log("inserted", p))

would be great. Thanks.

@coveralls
Copy link

coveralls commented Mar 15, 2017

Coverage Status

Coverage increased (+2.5%) to 96.934% when pulling 3d9c746 on Vitallyio:partial-typing into 73edf6d on Vincit:master.

@pvatterott
Copy link
Contributor Author

Let me know what to do about the build failure here. The build fails on two older node versions when compiling a dependency (fs-extra). But, the dependency doesn't support that older node version:
npm WARN engine [email protected]: wanted: {"node":">=4.5.0"} (current: {"node":"0.12.18","npm":"2.15.11"})
Looks like fs-extra 2.1 was released 2 hours ago: https://github.com/jprichardson/node-fs-extra/releases

Easy solution would be to pin to 2.0.0 but I'll let your team decide on the best solution.

Copy link
Collaborator

@mceachen mceachen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@coveralls
Copy link

coveralls commented Mar 15, 2017

Coverage Status

Coverage increased (+2.5%) to 96.934% when pulling b2904e3 on Vitallyio:partial-typing into 73edf6d on Vincit:master.

@mceachen
Copy link
Collaborator

Looks like fs-extra 2.1 was released 2 hours ago

And jprichardson/node-fs-extra#390 is tracking the fix. I think it's fine to pin to the prior version so they can merge this.

@coveralls
Copy link

coveralls commented Mar 15, 2017

Coverage Status

Coverage increased (+2.5%) to 96.953% when pulling 8f4fbc0 on Vitallyio:partial-typing into 73edf6d on Vincit:master.

3 similar comments
@coveralls
Copy link

Coverage Status

Coverage increased (+2.5%) to 96.953% when pulling 8f4fbc0 on Vitallyio:partial-typing into 73edf6d on Vincit:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+2.5%) to 96.953% when pulling 8f4fbc0 on Vitallyio:partial-typing into 73edf6d on Vincit:master.

@coveralls
Copy link

Coverage Status

Coverage increased (+2.5%) to 96.953% when pulling 8f4fbc0 on Vitallyio:partial-typing into 73edf6d on Vincit:master.

@pvatterott
Copy link
Contributor Author

Should be good to merge. Added the type assertion and pinned to 2.0.0

@koskimas
Copy link
Collaborator

Thanks!

@koskimas koskimas merged commit 88b1058 into Vincit:master Mar 16, 2017
@pvatterott pvatterott deleted the partial-typing branch March 16, 2017 15:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants