Skip to content

Commit

Permalink
feat(orm): finish base model methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Jan 6, 2024
1 parent cb1df50 commit 042e496
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions docs/orm/query-builder.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,23 @@ flight.name = 'Brazil to Ukraine'
await flight.save()
```

:::tip

To validate if a model is persisted in database you can use the
`isPersisted()` method:

```typescript
const flight = new Flight()

flight.name = 'Brazil to Ukraine'

if (!flight.isPersisted()) {
await flight.save()
}
```

:::

In this example, we assign the `name` field to the name attribute of
the `#app/models/Flight` model instance. When we call the `save()`
method, a record will be inserted into the database. The model's
Expand Down Expand Up @@ -285,6 +302,38 @@ flight.name = 'Paris to London'
await flight.save()
```

:::tip

To validate if some change has been done in the model after it was
retrieved from database you may use the `isDirty()` method:

```typescript
const flight = await Flight.query()
.where({ id: 1 })
.find()

if (!flight.isDirty()) {
flight.name = 'Paris to London'

await flight.save()
}
```

Also to get only the values that were modified you may use the `dirty()`
method:

```typescript
const flight = await Flight.query()
.where({ id: 1 })
.find()

flight.name = 'Paris to London'

const { name } = flight.dirty()
```

:::

#### Mass updates

Updates can also be performed against models that match a given query.
Expand Down Expand Up @@ -419,6 +468,33 @@ await Flight.query()
})
```

## Refreshing models

If you already have an instance of an model that was retrieved from the
database, you can "refresh" the model using the `fresh()` and `refresh()`
methods. The `fresh()` method will re-retrieve the model from the database.
The existing model instance will not be affected:

```typescript
const flight = await Flight.find({ number: 'FR 900' })

const freshFlight = await flight.fresh()
```

The `refresh()` method will re-hydrate the existing model using fresh data
from the database. In addition, all of its loaded relationships will be
refreshed as well:

```typescript
const flight = await Flight.find({ number: 'FR 900' })

flight.number = 'FR 456'

await flight.refresh()

console.log(flight.number) // "FR 900" 👈
```

## Deleting models

To delete a model, you may call the `delete()` method on the model
Expand Down

0 comments on commit 042e496

Please sign in to comment.