Skip to content

Commit

Permalink
docs(pinia-orm): adapt STI docs to be more correctly
Browse files Browse the repository at this point in the history
resolves #1629, resolves #1630, resolves #1631
  • Loading branch information
CodeDredd committed Sep 17, 2023
1 parent 05efbbc commit 7c5a23e
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions docs/content/1.guide/2.model/7.single-table-inheritance.md
Expand Up @@ -15,6 +15,7 @@ In order to define inheritance in Pinia ORM, you need to follow some conventions
1. Make sure the sub-entity class extends another Pinia ORM Model class.
2. Add a reference to the base entity name along with the `static entity` reference.
3. Call `super.fields()` in the `static fields` method to make sure to merge the sub-entity fields with the base one.
4. If you don't want your model to be saved in a different store also define `static types` and set a `type` property if your data passed to the instance are not having it (its recommended always to set it)

```js
// Base entity.
Expand Down Expand Up @@ -43,13 +44,15 @@ class Adult extends Person {
}
```

Be aware that this example will save `adult` in a seperate store `adult`. If you want to have it saved in `person` continue reading.

::alert{type="info"}
For typescript look at this [discussion](https://github.com/CodeDredd/pinia-orm/discussions/325)
::

## Interacting with Data

Once you defined a sub-class, you can `insert` / `create` / `update` / `get` / `delete` entities using the Model static methods. For instance, to create or insert data:
Once you defined a sub-class, you can `insert` / `create` / `update` / `get` / `delete` entities using the Repository static methods. For instance, to create or insert data:

```js
useRepo(Adult).insert({id: 1, name: 'John Doe', job: 'Software Engineer' })
Expand All @@ -68,21 +71,6 @@ const adults = useRepo(Adult).all()
*/
```

You can also fetch mixed results using the base entity getter:

```js
const people = useRepo(Person).all()

/*
[
Person { id: 1, name: 'John Doe' },
Adult { id: 2, name: 'Jane Doe', job: 'Software Engineer' }
]
*/
```

However, using only these, you need to use the sub-entity methods (like `Adult.insert` in our example) if you want to insert sub-entity. If you want to deal with mixed data from the same hierarchy, you may use a "Discriminator Field" to dispatch entity using the base entity methods.

## Discriminator Field

When defining an inheritance model, one can use a discriminator field to dispatch entities based on this field value when inserting data using the base entity `insert` or `create` method.
Expand Down Expand Up @@ -216,6 +204,8 @@ Note that if the `static fields` method doesn't expose the discriminator field (
// Base entity.
class Person extends Model {
static entity = 'person'

// static typeKey = 'person_type' // If your type field is not `type`

static types () {
return {
Expand All @@ -229,6 +219,7 @@ class Person extends Model {
id: this.attr(null),
name: this.attr(''),
type: this.attr('PERSON') // Exposing the discriminator field.
// person_type: this.attr('PERSON') // If your type field is not `type`
}
}
}
Expand All @@ -243,6 +234,7 @@ class Adult extends Person {
...super.fields(),
job: this.attr(''),
type: this.attr('ADULT') // necessary fallback if you use the childRepo directly without type
// person_type: this.attr('ADULT') // If your type field is not `type`
}
}
}
Expand All @@ -266,6 +258,30 @@ const people = useRepo(Person).all()
*/
```

You also need to set the `type` field if you want to save data without its `type` property but by store

````ts
useRepo(Adult).insert([
{ id: 2, name: 'Jane Doe', job: 'Software Engineer' }
])

useRepo(Person).insert([
{ id: 1, name: 'John Doe' }
])

/*
[
Person { id: 1, name: 'John Doe', type: 'PERSON' },
Adult { id: 2, name: 'Jane Doe', job: 'Software Engineer', type: 'ADULT' }
]
*//*
[
Person { id: 1, name: 'John Doe', type: 'PERSON' },
Adult { id: 2, name: 'Jane Doe', job: 'Software Engineer', type: 'ADULT' }
]
*/
````

## Relationship Handling

Inheritance handles relation as any field:
Expand All @@ -279,12 +295,20 @@ Querying related data using the `with` keyword (see [this page](/guide/relations
class Person extends Model {
static entity = 'person'

static types () {
return {
PERSON: Person,
ADULT: Adult
}
}

static fields () {
return {
id: this.attr(null),
home_address_id: this.attr(null),
name: this.attr(''),
home_address: this.belongsTo(Address, 'home_address_id'),
type: this.attr('PERSON')
}
}
}
Expand All @@ -300,6 +324,7 @@ class Adult extends Person {
work_address_id: this.attr(null),
job: this.attr(''),
work_address: this.belongsTo(Address, 'work_address_id'),
type: this.attr('ADULT')
}
}
}
Expand Down Expand Up @@ -443,7 +468,8 @@ export default class Adult extends Person {
static fields() {
return {
...super.fields(),
job: this.attr('')
job: this.attr(''),
type: this.attr('ADULT')
}
}
}
Expand Down Expand Up @@ -507,7 +533,8 @@ export class Adult extends Person {
static fields() {
return {
...super.fields(),
job: this.attr('')
job: this.attr(''),
type: this.attr('ADULT')
}
}
}
Expand Down

0 comments on commit 7c5a23e

Please sign in to comment.