Skip to content

Commit

Permalink
Merge pull request #329 from Vitallyio/partial-typing
Browse files Browse the repository at this point in the history
Change ModelOrObject to Partial<T>
  • Loading branch information
koskimas authored Mar 16, 2017
2 parents 021051e + 8f4fbc0 commit 88b1058
Show file tree
Hide file tree
Showing 3 changed files with 2,043 additions and 17 deletions.
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 @@ -93,9 +93,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 @@ -304,15 +301,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>;
(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 @@ -321,8 +317,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 @@ -336,23 +332,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

0 comments on commit 88b1058

Please sign in to comment.