forked from adonisjs/v5-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improvement: small improvements to the writing structure
- Loading branch information
1 parent
99f9864
commit a6700e4
Showing
3 changed files
with
31 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,9 @@ Lucid models make it very easy to perform CRUD operations and also define lifecy | |
This guide covers 80% of the use cases. However, do make sure to check the [Model API docs](../../reference/orm/base-model.md) docs for all the available methods. | ||
|
||
## Create | ||
You can create and persist new records to the database by assigning values to the model instance properties and calling the `model.save` method. | ||
You can create and persist new records to the database by first assigning values to the model instance and then calling the `save` method. | ||
|
||
The `save` method performs the **INSERT** query when persisting the model instance for the first time and performs the **UPDATE** query when model has already been persisted. | ||
|
||
```ts | ||
import User from 'App/Models/User' | ||
|
@@ -23,13 +25,20 @@ await user.save() | |
console.log(user.$isPersisted) // true | ||
``` | ||
|
||
- The `user.save` method will perform the insert query. | ||
- The `user.$isPersisted` flag returns `true` when the values are persisted to the database. | ||
Also, you can make use of the `fill` method to define all the attributes as once and then call the `save` method. | ||
|
||
```ts | ||
await user | ||
.fill({ username: 'virk', email: '[email protected]' }) | ||
.save() | ||
|
||
console.log(user.$isPersisted) // true | ||
``` | ||
|
||
--- | ||
|
||
### create | ||
Another option is to make use of the `static create` method on the Model class itself. | ||
The `static create` method creates the model instance and persists it to database in one go. | ||
|
||
```ts | ||
import User from 'App/Models/User' | ||
|
@@ -156,9 +165,14 @@ const user = await User.findOrFail(1) | |
user.last_login_at = DateTime.local() // Luxon dateTime is used | ||
|
||
await user.save() | ||
``` | ||
|
||
// OR | ||
// await user.merge({ last_login_at: DateTime.local() }).save() | ||
Also, you can make use of the `merge` method to define all the attributes as once and then call the `save` method. | ||
|
||
```ts | ||
await user | ||
.merge({ last_login_at: DateTime.local() }) | ||
.save() | ||
``` | ||
|
||
#### Why not use the update query directly? | ||
|