Skip to content

Commit

Permalink
Merge pull request #159 from robsontenorio/dev
Browse files Browse the repository at this point in the history
Update documentation
  • Loading branch information
JoaoPedroAS51 authored Dec 5, 2020
2 parents 90fe3f3 + 05e1055 commit 42de647
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/content/en/building-the-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ Just for convenience, it's possible to make Static calls. We are going to use th
</code-block>
</code-group>

<alert type="info">It's also possible to use `$get` method or its alias `$all`,
which handle and unwrap responses within "data".</alert>

## Retrieving a Single Record

To retrieve a single record from the database, we can use two methods:
Expand Down Expand Up @@ -163,6 +166,8 @@ To get the first **Post** of a list:
</code-block>
</code-group>

<alert type="info">It's also possible to use `$first` method, which handle and unwrap responses within "data".</alert>

### Finding a Specific Record

See the [API reference](/api/query-builder-methods#find)
Expand Down Expand Up @@ -205,6 +210,8 @@ To find a specific **Post**:
</code-block>
</code-group>

<alert type="info">It's also possible to use `$find` method, which handle and unwrap responses within "data".</alert>

## Filtering

One of the most important parts when building a query is filtering, so let's get started!
Expand Down
66 changes: 65 additions & 1 deletion docs/content/en/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default class Model extends BaseModel {

## Creating the Domain Models

Now let's create our domain models that extends the base model. We can create as many models as you like.
Now let's create our domain models that extends the base model. We can create as many models as we like.

Each model must implement:
- `resource` - The resource route of the model.
Expand All @@ -59,6 +59,30 @@ export default class User extends Model {

This **User** model will make request to `/users` route as defined in `resource`.

We can also add extra methods and computed properties:

```js{}[~/models/User.js]
import Model from './Model'
export default class User extends Model {
// Set the resource route of the model
resource() {
return 'users'
}
// Computed properties are reactive -> user.fullName
// Make sure to use "get" prefix
get fullName () {
return `${this.firstname} ${this.lastname}`
}
// Method -> user.makeBirthday()
makeBirthday() {
return this.age += 1
}
}
```

## Changing the Primary Key

<alert type="info">By default, the `primaryKey` is set to `id`.</alert>
Expand Down Expand Up @@ -132,6 +156,35 @@ export default class Post extends Model {
Now we can easily access an instance of the **User** model containing the eager loaded data
using the specified key: `post.user`

The `relations` method also support nested keys, by dot notation:

```js{}[~/models/Post.js]
import Model from './Model'
import User from './User'
import Comment from './Comment'
export default class Post extends Model {
// Set the resource route of the model
resource() {
return 'posts'
}
// Define the primary key of the model
primaryKey() {
return 'slug'
}
// Apply model instances to eager loaded relationships
relations() {
return {
'relationships.user': User,
'relationships.comments': Comment
}
}
```

Then we can access using the specified key: `post.relationships.user`

### Lazy Loading Relationships

See the [API reference](/api/model-options#hasmany)
Expand All @@ -154,6 +207,17 @@ export default class User extends Model {
posts() {
return this.hasMany(Post)
}
// Computed properties are reactive -> user.fullName
// Make sure to use "get" prefix
get fullName () {
return `${this.firstname} ${this.lastname}`
}
// Method -> user.makeBirthday()
makeBirthday() {
return this.age += 1
}
}
```

Expand Down

0 comments on commit 42de647

Please sign in to comment.