Skip to content

Repository piniaStore() returns different piniaStoreInstance, when using a derivedModel #1437

@adm-bome

Description

@adm-bome

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 }])

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions