Skip to content

Commit

Permalink
docs(recipes): add recipe for TypeScript plugin
Browse files Browse the repository at this point in the history
Closes #1589

Signed-off-by: Will Soto <willsoto@users.noreply.github.com>
  • Loading branch information
willsoto committed Dec 31, 2019
1 parent 6157410 commit c0cf2bf
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions doc/recipes/plugins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Plugins

## TypeScript Example

```ts
export function Mixin(options = {}) {
return function<T extends typeof Model>(Base: T) {
return class extends Base {
mixinMethod() {}
};
};
}

// Usage

class Person extends Model {}

const MixinPerson = Mixin(Person);

// Or as a decorator:

@Mixin
class Person extends Model {}
```

## TypeScript Example with Custom QueryBuilder

```ts
class CustomQueryBuilder<M extends Model, R = M[]> extends QueryBuilder<M, R> {
ArrayQueryBuilderType!: CustomQueryBuilder<M, M[]>;
SingleQueryBuilderType!: CustomQueryBuilder<M, M>;
NumberQueryBuilderType!: CustomQueryBuilder<M, number>;
PageQueryBuilderType!: CustomQueryBuilder<M, Page<M>>;

someCustomMethod(): this {
return this;
}
}

export function CustomQueryBuilderMixin(options = {}) {
return function<T extends typeof Model>(Base: T) {
return class extends Base {
static QueryBuilder = QueryBuilder;
QueryBuilderType: CustomQueryBuilder<this, this[]>;

mixinMethod() {}
};
};
}

// Usage

class Person extends Model {}

const MixinPerson = CustomQueryBuilderMixin(Person);

// Or as a decorator:

@CustomQueryBuilderMixin
class Person extends Model {}

async () => {
const z = await MixinPerson.query()
.whereIn('id', [1, 2])
.someCustomMethod()
.where('foo', 1)
.someCustomMethod();

z[0].mixinMethod();
};
```

0 comments on commit c0cf2bf

Please sign in to comment.