Discussed in #1434
Originally posted by adm-bome July 3, 2023
Why does the Repository uses the $entity() as a name for the Pinia Store, while the Query uses the $baseEntity(). Is this by design or by mistake?
|
piniaStore<S extends DataStoreState = DataStoreState>() { |
|
return useDataStore<S>(this.model.$entity(), this.model.$piniaOptions(), this.query())(this.pinia) |
|
} |
|
protected commit(name: string, payload?: any) { |
|
const store = useDataStore(this.model.$baseEntity(), this.model.$piniaOptions(), this)(this.pinia) |
|
if (name && typeof store[name] === 'function') |
|
store[name](payload, false) |
|
|
|
if (this.cache && ['get', 'all', 'insert', 'flush', 'delete', 'update', 'destroy'].includes(name)) |
|
this.cache.clear() |
|
|
|
return store.$state.data |
|
} |
When creating a Model extended from an other Model we use baseEntity in the exteded Model.
// Base entity.
class Person extends Model {
static entity = 'person'
static types () {
return {
PERSON: Person,
ADULT: Adult
}
}
static fields () {
return {
id: this.attr(null),
name: this.attr('')
}
}
}
// Derived entity.
class Adult extends Person {
static entity = 'adult'
static baseEntity = 'person'
static fields () {
return {
...super.fields(),
job: this.attr('')
}
}
When we use the following code, a store is created with the name adult
const repo = useRepo(Adult)
const store = repo.piniaStore() // Wrong StoreInstance
But when we use the following code a store is created and data is saved into the piniaStoreInstance with the name person
const repo = useRepo(Adult)
repo.save([{ id: 1 }])
Discussed in #1434
Originally posted by adm-bome July 3, 2023
Why does the Repository uses the
$entity()as a name for the Pinia Store, while the Query uses the$baseEntity(). Is this by design or by mistake?pinia-orm/packages/pinia-orm/src/repository/Repository.ts
Lines 117 to 119 in 43126f6
pinia-orm/packages/pinia-orm/src/query/Query.ts
Lines 163 to 172 in 43126f6
When creating a Model extended from an other Model we use baseEntity in the exteded Model.
When we use the following code, a store is created with the name
adultBut when we use the following code a store is created and data is saved into the piniaStoreInstance with the name
person