Skip to content

Commit

Permalink
better Modifier types
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed Oct 5, 2019
1 parent 7f33b6b commit b353a67
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "objection",
"version": "2.0.0-alpha.8",
"version": "2.0.0-alpha.9",
"description": "An SQL-friendly ORM for Node.js",
"main": "lib/objection.js",
"license": "MIT",
Expand Down
42 changes: 35 additions & 7 deletions tests/ts/examples.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import * as ajv from 'ajv';
import * as knex from 'knex';
import * as objection from '../../';
import { DBError, fn, JSONSchema, lit, raw, ref, RelationMappings } from '../../';
import {
DBError,
fn,
JSONSchema,
lit,
raw,
ref,
RelationMappings,
RelationMapping,
QueryBuilder
} from '../../';

// This file exercises the Objection.js typings.

Expand Down Expand Up @@ -112,13 +122,31 @@ class Person extends objection.Model {
return new CustomValidationError('my custom error: ' + message + ' ' + itemMessage);
}

static get modifiers() {
return {
myFilter(builder: objection.QueryBuilder<Person>) {
return builder.orderBy('date');
static modifiers = {
myFilter(query: QueryBuilder<Person>) {
query.where('something', 'something');
}
};

static relationMappings = () => ({
fancyPets: {
modelClass: Animal,
relation: objection.Model.HasManyRelation,

beforeInsert(pet) {
pet.name = pet.name + ' Fancy Pants';
},

modify(query) {
query.where('name', 'like', '% Fancy Pants');
},

join: {
from: 'person.ownerId',
to: 'pet.id'
}
};
}
} as RelationMapping<Animal>
});
}

function takesModelSubclass<M extends objection.Model>(m: M) {}
Expand Down
22 changes: 13 additions & 9 deletions typings/objection/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ declare namespace Objection {
type Modifier<QB extends AnyQueryBuilder = AnyQueryBuilder> =
| ((qb: QB, ...args: any[]) => void)
| string
| object;
| string[]
| Record<string, Expression<PrimitiveValue>>;
type OrderByDirection = 'asc' | 'desc' | 'ASC' | 'DESC';

interface Modifiers<QB extends AnyQueryBuilder = AnyQueryBuilder> {
Expand Down Expand Up @@ -1252,35 +1253,38 @@ declare namespace Objection {
export type Transaction = knex.Transaction;

export interface RelationMappings {
[relationName: string]: RelationMapping;
[relationName: string]: RelationMapping<any>;
}

type ModelClassFactory = () => AnyModelClass;
type ModelClassSpecifier = ModelClassFactory | AnyModelClass | string;
type RelationMappingHook = (model: Model, context: QueryContext) => Promise<void> | void;
type RelationMappingHook<M extends Model> = (
model: M,
context: QueryContext
) => Promise<void> | void;
type RelationMappingColumnRef = string | ReferenceBuilder | (string | ReferenceBuilder)[];

export interface RelationMapping<M extends Model = Model> {
export interface RelationMapping<M extends Model> {
relation: RelationType;
modelClass: ModelClassSpecifier;
join: RelationJoin;
modify?: Modifier<M['QueryBuilderType']>;
filter?: Modifier<M['QueryBuilderType']>;
beforeInsert?: RelationMappingHook;
beforeInsert?: RelationMappingHook<M>;
}

export interface RelationJoin {
from: RelationMappingColumnRef;
to: RelationMappingColumnRef;
through?: RelationThrough;
through?: RelationThrough<any>;
}

export interface RelationThrough {
export interface RelationThrough<M extends Model> {
from: RelationMappingColumnRef;
to: RelationMappingColumnRef;
extra?: string[] | object;
extra?: string[] | Record<string, string>;
modelClass?: ModelClassSpecifier;
beforeInsert?: RelationMappingHook;
beforeInsert?: RelationMappingHook<M>;
}

export interface RelationType extends Constructor<Relation> {}
Expand Down

0 comments on commit b353a67

Please sign in to comment.