Skip to content

Commit

Permalink
feat: document scopes within scopes use case
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed May 14, 2021
1 parent 3f5c47c commit f3ed396
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions content/guides/models/query-scopes.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,44 @@ Project
.query()
.withScopes((scopes) => scopes.visibleTo(auth.user))
```

## Calling scopes within the scopes
Since the scope method receives an instance of the [Model query builder](../../reference/database/orm/query-builder.md), you can also reference other model scopes within the scope callback. For example:

```ts
import {
scope,
column,
BaseModel,
ModelQueryBuilderContract,
} from '@ioc:Adonis/Lucid/Orm'

type Builder = ModelQueryBuilderContract<typeof User>

export default class Post extends BaseModel {
public static firstScope = scope((query: Builder) => {
query.withScopes((scopes) => scopes.secondScope())
})

public static secondScope = scope((query) => {
query.whereNull('deletedAt')
})
}
```

#### Noticed the `Builder` type we created above?

The `scope` method is not aware of the Model it is used inside (a TypeScript limitation) and hence it cannot infer the Query builder type for the model as well. Therefore, we need to type hint the `builder` property as follow:

```ts
// highlight-start
type Builder = ModelQueryBuilderContract<typeof User>
// highlight-end

public static firstScope = scope(
// highlight-start
(query: Builder) => {
// highlight-end
}
)
```

0 comments on commit f3ed396

Please sign in to comment.