Skip to content

Commit 6bbcf83

Browse files
authored
feat(pinia-orm): STI Models can only be retrieved through parent (#1835)
closes #1735
1 parent 807fa36 commit 6bbcf83

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

packages/pinia-orm/.eslintcache

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

packages/pinia-orm/src/query/Query.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,10 @@ export class Query<M extends Model = Model> {
505505
}
506506

507507
private internalGet (triggerHook: boolean): Collection<M> | GroupedCollection<M> {
508-
if (this.model.$entity() !== this.model.$baseEntity()) { this.where(this.model.$typeKey(), this.model.$fields()[this.model.$typeKey()].make()) }
508+
if (this.model.$entity() !== this.model.$baseEntity() || this.model.$namespace() !== this.model.$baseNamespace()) {
509+
const typeKeyValue = this.model.$fields()[this.model.$typeKey()].make() ?? this.model.$entity()
510+
this.where(this.model.$typeKey(), typeKeyValue)
511+
}
509512

510513
let models = this.select()
511514

packages/pinia-orm/tests/unit/model/Model_STI.spec.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,4 +311,82 @@ describe('unit/model/Model_STI', () => {
311311
expect(persons[1]).toBeInstanceOf(Adult)
312312
expect(persons[1]).not.toHaveProperty('type')
313313
})
314+
315+
it('retrieves Data from child model without default type prop', () => {
316+
class Personne extends Model {
317+
static entity = 'Personne'
318+
static primaryKey = '@id'
319+
static typeKey = '@type'
320+
321+
static types () {
322+
return {
323+
Personne,
324+
PersonneMorale,
325+
PersonnePhysique,
326+
}
327+
}
328+
329+
static fields () {
330+
return {
331+
'@id': this.string(),
332+
'@type': this.string(),
333+
'id': this.number(),
334+
}
335+
}
336+
}
337+
338+
class PersonnePhysique extends Personne {
339+
static entity = 'PersonnePhysique'
340+
static baseEntity = 'Personne'
341+
342+
static fields () {
343+
return {
344+
...super.fields(),
345+
prenom: this.string(),
346+
nom: this.string(),
347+
}
348+
}
349+
}
350+
351+
class PersonneMorale extends Personne {
352+
static entity = 'PersonneMorale'
353+
static baseEntity = 'Personne'
354+
355+
static fields () {
356+
return {
357+
...super.fields(),
358+
enseigne: this.string(),
359+
raisonSociale: this.string(),
360+
libelle: this.string(),
361+
}
362+
}
363+
}
364+
365+
const personneMorale = {
366+
'@id': '/api/personne_morales/1',
367+
'@type': 'PersonneMorale',
368+
'id': 1,
369+
'enseigne': 'Business',
370+
'raisonSociale': 'Super',
371+
}
372+
373+
const personnePhysique = {
374+
'@id': '/api/personne_physiques/1',
375+
'@type': 'PersonnePhysique',
376+
'id': 2,
377+
'prenom': 'John',
378+
'nom': 'Doe',
379+
}
380+
381+
const personneMoraleRepo = useRepo(PersonneMorale)
382+
const personnePhysiqueRepo = useRepo(PersonnePhysique)
383+
const personneRepo = useRepo(Personne)
384+
385+
personneMoraleRepo.save(personneMorale)
386+
personnePhysiqueRepo.save(personnePhysique)
387+
388+
expect(personnePhysiqueRepo.all().length).toBe(1)
389+
expect(personneMoraleRepo.all().length).toBe(1)
390+
expect(personneRepo.all().length).toBe(2)
391+
})
314392
})

0 commit comments

Comments
 (0)