From c48cd44e3973a1a4023ea75973f057642ad5869a Mon Sep 17 00:00:00 2001 From: Will Soto Date: Tue, 31 Dec 2019 10:12:19 -0500 Subject: [PATCH] docs(recipes): add recipe for TypeScript plugin Closes #1589 Signed-off-by: Will Soto --- doc/recipes/plugins.md | 71 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 doc/recipes/plugins.md diff --git a/doc/recipes/plugins.md b/doc/recipes/plugins.md new file mode 100644 index 000000000..d696d1d40 --- /dev/null +++ b/doc/recipes/plugins.md @@ -0,0 +1,71 @@ +# Plugins + +## TypeScript Example + +```ts +export function Mixin(options = {}) { + return function(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 extends QueryBuilder { + ArrayQueryBuilderType!: CustomQueryBuilder; + SingleQueryBuilderType!: CustomQueryBuilder; + NumberQueryBuilderType!: CustomQueryBuilder; + PageQueryBuilderType!: CustomQueryBuilder>; + + someCustomMethod(): this { + return this; + } +} + +export function CustomQueryBuilderMixin(options = {}) { + return function(Base: T) { + return class extends Base { + static QueryBuilder = QueryBuilder; + QueryBuilderType: CustomQueryBuilder; + + 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(); +}; +```