diff --git a/docs/content/en/building-the-query.md b/docs/content/en/building-the-query.md index 9eae5ab8..355d0ada 100644 --- a/docs/content/en/building-the-query.md +++ b/docs/content/en/building-the-query.md @@ -114,6 +114,9 @@ Just for convenience, it's possible to make Static calls. We are going to use th +It's also possible to use `$get` method or its alias `$all`, +which handle and unwrap responses within "data". + ## Retrieving a Single Record 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: +It's also possible to use `$first` method, which handle and unwrap responses within "data". + ### Finding a Specific Record See the [API reference](/api/query-builder-methods#find) @@ -205,6 +210,8 @@ To find a specific **Post**: +It's also possible to use `$find` method, which handle and unwrap responses within "data". + ## Filtering One of the most important parts when building a query is filtering, so let's get started! diff --git a/docs/content/en/configuration.md b/docs/content/en/configuration.md index 2d62d10a..68742e12 100644 --- a/docs/content/en/configuration.md +++ b/docs/content/en/configuration.md @@ -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. @@ -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 By default, the `primaryKey` is set to `id`. @@ -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) @@ -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 + } } ```