Skip to content

Commit 42de647

Browse files
Merge pull request #159 from robsontenorio/dev
Update documentation
2 parents 90fe3f3 + 05e1055 commit 42de647

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

docs/content/en/building-the-query.md

+7
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ Just for convenience, it's possible to make Static calls. We are going to use th
114114
</code-block>
115115
</code-group>
116116

117+
<alert type="info">It's also possible to use `$get` method or its alias `$all`,
118+
which handle and unwrap responses within "data".</alert>
119+
117120
## Retrieving a Single Record
118121

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

169+
<alert type="info">It's also possible to use `$first` method, which handle and unwrap responses within "data".</alert>
170+
166171
### Finding a Specific Record
167172

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

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

210217
One of the most important parts when building a query is filtering, so let's get started!

docs/content/en/configuration.md

+65-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class Model extends BaseModel {
4040

4141
## Creating the Domain Models
4242

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

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

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

62+
We can also add extra methods and computed properties:
63+
64+
```js{}[~/models/User.js]
65+
import Model from './Model'
66+
67+
export default class User extends Model {
68+
// Set the resource route of the model
69+
resource() {
70+
return 'users'
71+
}
72+
73+
// Computed properties are reactive -> user.fullName
74+
// Make sure to use "get" prefix
75+
get fullName () {
76+
return `${this.firstname} ${this.lastname}`
77+
}
78+
79+
// Method -> user.makeBirthday()
80+
makeBirthday() {
81+
return this.age += 1
82+
}
83+
}
84+
```
85+
6286
## Changing the Primary Key
6387

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

159+
The `relations` method also support nested keys, by dot notation:
160+
161+
```js{}[~/models/Post.js]
162+
import Model from './Model'
163+
import User from './User'
164+
import Comment from './Comment'
165+
166+
export default class Post extends Model {
167+
// Set the resource route of the model
168+
resource() {
169+
return 'posts'
170+
}
171+
172+
// Define the primary key of the model
173+
primaryKey() {
174+
return 'slug'
175+
}
176+
177+
// Apply model instances to eager loaded relationships
178+
relations() {
179+
return {
180+
'relationships.user': User,
181+
'relationships.comments': Comment
182+
}
183+
}
184+
```
185+
186+
Then we can access using the specified key: `post.relationships.user`
187+
135188
### Lazy Loading Relationships
136189

137190
See the [API reference](/api/model-options#hasmany)
@@ -154,6 +207,17 @@ export default class User extends Model {
154207
posts() {
155208
return this.hasMany(Post)
156209
}
210+
211+
// Computed properties are reactive -> user.fullName
212+
// Make sure to use "get" prefix
213+
get fullName () {
214+
return `${this.firstname} ${this.lastname}`
215+
}
216+
217+
// Method -> user.makeBirthday()
218+
makeBirthday() {
219+
return this.age += 1
220+
}
157221
}
158222
```
159223

0 commit comments

Comments
 (0)