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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"babel-plugin-transform-runtime": "^6.3.13",
"coveralls": "^2.11.2",
"expect.js": "^0.3.1",
"fs-extra": "^2.0.0",
"fs-extra": "2.0.0",
"glob": "^7.0.3",
"istanbul": "^0.4.0",
"knex": "0.x",
Expand Down
11 changes: 11 additions & 0 deletions tests/ts/examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,14 @@ objection.transaction.start(Person).then((trx: objection.Transaction) => {

const p: Promise<string> = qb.then(() => 'done');

// Verify that we can insert a partial model and relate a partial movie
Person.query()
.insertAndFetch({firstName: "Jim"} as Partial<Person>)
.then((p: Person) => {
console.log(`Inserted ${p}`);
p.$loadRelated('movies')
.relate({title: 'Total Recall'} as Partial<Movie>)
.then((pWithMovie: Person) => {
console.log(`Related ${pWithMovie}`);
});
});
30 changes: 13 additions & 17 deletions typings/objection/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ declare module "objection" {

type IdOrIds = Id | Ids;

type ModelOrObject = Model | Object
type ModelsOrObjects = ModelOrObject | Model[] | Object[];

type RelationOptions = { alias: boolean | string };

interface JoinRelation {
Expand Down Expand Up @@ -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.

(modelOrObject?: Partial<T>): QueryBuilderSingle<T>;
(): this;
}

interface InsertGraphAndFetch<T> {
(modelsOrObjects: Model): QueryBuilderSingle<T>;
(modelsOrObjects: Model[]): QueryBuilder<T>;
(modelsOrObjects?: ModelsOrObjects): QueryBuilderBase<T>;
(modelsOrObjects?: Partial<T>): QueryBuilderSingle<T>;
(modelsOrObjects?: Partial<T>[]): QueryBuilder<T>;
}

interface QueryBuilderBase<T> extends QueryInterface<T> {
Expand All @@ -319,8 +315,8 @@ declare module "objection" {
findById(idOrIds: IdOrIds): this;

insert: Insert<T>;
insertAndFetch(modelOrObject: ModelOrObject): QueryBuilderSingle<T>;
insertAndFetch(modelsOrObjects?: ModelsOrObjects): QueryBuilder<T>;
insertAndFetch(modelOrObject: Partial<T>): QueryBuilderSingle<T>;
insertAndFetch(modelsOrObjects?: Partial<T>[]): QueryBuilder<T>;

insertGraph: Insert<T>;
insertGraphAndFetch: InsertGraphAndFetch<T>
Expand All @@ -334,23 +330,23 @@ declare module "objection" {
/**
* @return a Promise of the number of updated rows
*/
update(modelOrObject: ModelOrObject): QueryBuilderSingle<number>;
updateAndFetch(modelOrObject: ModelOrObject): QueryBuilderSingle<T>;
updateAndFetchById(id: Id, modelOrObject: ModelOrObject): QueryBuilderSingle<T>;
update(modelOrObject: Partial<T>): QueryBuilderSingle<number>;
updateAndFetch(modelOrObject: Partial<T>): QueryBuilderSingle<T>;
updateAndFetchById(id: Id, modelOrObject: Partial<T>): QueryBuilderSingle<T>;

/**
* @return a Promise of the number of patched rows
*/
patch(modelOrObject: ModelOrObject): QueryBuilderSingle<number>;
patchAndFetchById(id: Id, modelOrObject: ModelOrObject): QueryBuilderSingle<T>;
patchAndFetch(modelOrObject: ModelOrObject): QueryBuilderSingle<T>;
patch(modelOrObject: Partial<T>): QueryBuilderSingle<number>;
patchAndFetchById(id: Id, modelOrObject: Partial<T>): QueryBuilderSingle<T>;
patchAndFetch(modelOrObject: Partial<T>): QueryBuilderSingle<T>;

/**
* @return a Promise of the number of deleted rows
*/
deleteById(idOrIds: IdOrIds): QueryBuilderSingle<number>;

relate(ids: IdOrIds | ModelsOrObjects): this;
relate<M extends Model>(ids: IdOrIds | Partial<M> | Partial<M>[]): this;
unrelate(): this;

forUpdate(): this;
Expand Down
Loading