Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recipe for TypeScript plugin #1637

Merged
merged 1 commit into from
Jan 9, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
};
```