Skip to content

Commit 7c5a23e

Browse files
committed
docs(pinia-orm): adapt STI docs to be more correctly
resolves #1629, resolves #1630, resolves #1631
1 parent 05efbbc commit 7c5a23e

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

docs/content/1.guide/2.model/7.single-table-inheritance.md

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ In order to define inheritance in Pinia ORM, you need to follow some conventions
1515
1. Make sure the sub-entity class extends another Pinia ORM Model class.
1616
2. Add a reference to the base entity name along with the `static entity` reference.
1717
3. Call `super.fields()` in the `static fields` method to make sure to merge the sub-entity fields with the base one.
18+
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)
1819

1920
```js
2021
// Base entity.
@@ -43,13 +44,15 @@ class Adult extends Person {
4344
}
4445
```
4546

47+
Be aware that this example will save `adult` in a seperate store `adult`. If you want to have it saved in `person` continue reading.
48+
4649
::alert{type="info"}
4750
For typescript look at this [discussion](https://github.com/CodeDredd/pinia-orm/discussions/325)
4851
::
4952

5053
## Interacting with Data
5154

52-
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:
55+
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:
5356

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

71-
You can also fetch mixed results using the base entity getter:
72-
73-
```js
74-
const people = useRepo(Person).all()
75-
76-
/*
77-
[
78-
Person { id: 1, name: 'John Doe' },
79-
Adult { id: 2, name: 'Jane Doe', job: 'Software Engineer' }
80-
]
81-
*/
82-
```
83-
84-
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.
85-
8674
## Discriminator Field
8775

8876
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.
@@ -216,6 +204,8 @@ Note that if the `static fields` method doesn't expose the discriminator field (
216204
// Base entity.
217205
class Person extends Model {
218206
static entity = 'person'
207+
208+
// static typeKey = 'person_type' // If your type field is not `type`
219209

220210
static types () {
221211
return {
@@ -229,6 +219,7 @@ class Person extends Model {
229219
id: this.attr(null),
230220
name: this.attr(''),
231221
type: this.attr('PERSON') // Exposing the discriminator field.
222+
// person_type: this.attr('PERSON') // If your type field is not `type`
232223
}
233224
}
234225
}
@@ -243,6 +234,7 @@ class Adult extends Person {
243234
...super.fields(),
244235
job: this.attr(''),
245236
type: this.attr('ADULT') // necessary fallback if you use the childRepo directly without type
237+
// person_type: this.attr('ADULT') // If your type field is not `type`
246238
}
247239
}
248240
}
@@ -266,6 +258,30 @@ const people = useRepo(Person).all()
266258
*/
267259
```
268260

261+
You also need to set the `type` field if you want to save data without its `type` property but by store
262+
263+
````ts
264+
useRepo(Adult).insert([
265+
{ id: 2, name: 'Jane Doe', job: 'Software Engineer' }
266+
])
267+
268+
useRepo(Person).insert([
269+
{ id: 1, name: 'John Doe' }
270+
])
271+
272+
/*
273+
[
274+
Person { id: 1, name: 'John Doe', type: 'PERSON' },
275+
Adult { id: 2, name: 'Jane Doe', job: 'Software Engineer', type: 'ADULT' }
276+
]
277+
*//*
278+
[
279+
Person { id: 1, name: 'John Doe', type: 'PERSON' },
280+
Adult { id: 2, name: 'Jane Doe', job: 'Software Engineer', type: 'ADULT' }
281+
]
282+
*/
283+
````
284+
269285
## Relationship Handling
270286

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

298+
static types () {
299+
return {
300+
PERSON: Person,
301+
ADULT: Adult
302+
}
303+
}
304+
282305
static fields () {
283306
return {
284307
id: this.attr(null),
285308
home_address_id: this.attr(null),
286309
name: this.attr(''),
287310
home_address: this.belongsTo(Address, 'home_address_id'),
311+
type: this.attr('PERSON')
288312
}
289313
}
290314
}
@@ -300,6 +324,7 @@ class Adult extends Person {
300324
work_address_id: this.attr(null),
301325
job: this.attr(''),
302326
work_address: this.belongsTo(Address, 'work_address_id'),
327+
type: this.attr('ADULT')
303328
}
304329
}
305330
}
@@ -443,7 +468,8 @@ export default class Adult extends Person {
443468
static fields() {
444469
return {
445470
...super.fields(),
446-
job: this.attr('')
471+
job: this.attr(''),
472+
type: this.attr('ADULT')
447473
}
448474
}
449475
}
@@ -507,7 +533,8 @@ export class Adult extends Person {
507533
static fields() {
508534
return {
509535
...super.fields(),
510-
job: this.attr('')
536+
job: this.attr(''),
537+
type: this.attr('ADULT')
511538
}
512539
}
513540
}

0 commit comments

Comments
 (0)