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(); +}; +```