You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/content/1.guide/2.model/7.single-table-inheritance.md
+45-18Lines changed: 45 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,6 +15,7 @@ In order to define inheritance in Pinia ORM, you need to follow some conventions
15
15
1. Make sure the sub-entity class extends another Pinia ORM Model class.
16
16
2. Add a reference to the base entity name along with the `static entity` reference.
17
17
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)
18
19
19
20
```js
20
21
// Base entity.
@@ -43,13 +44,15 @@ class Adult extends Person {
43
44
}
44
45
```
45
46
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
+
46
49
::alert{type="info"}
47
50
For typescript look at this [discussion](https://github.com/CodeDredd/pinia-orm/discussions/325)
48
51
::
49
52
50
53
## Interacting with Data
51
54
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:
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
-
86
74
## Discriminator Field
87
75
88
76
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 (
216
204
// Base entity.
217
205
classPersonextendsModel {
218
206
static entity ='person'
207
+
208
+
// static typeKey = 'person_type' // If your type field is not `type`
219
209
220
210
statictypes () {
221
211
return {
@@ -229,6 +219,7 @@ class Person extends Model {
229
219
id:this.attr(null),
230
220
name:this.attr(''),
231
221
type:this.attr('PERSON') // Exposing the discriminator field.
222
+
// person_type: this.attr('PERSON') // If your type field is not `type`
232
223
}
233
224
}
234
225
}
@@ -243,6 +234,7 @@ class Adult extends Person {
243
234
...super.fields(),
244
235
job:this.attr(''),
245
236
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`
246
238
}
247
239
}
248
240
}
@@ -266,6 +258,30 @@ const people = useRepo(Person).all()
266
258
*/
267
259
```
268
260
261
+
You also need to set the `type` field if you want to save data without its `type` property but by store
0 commit comments