Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/pinia-orm/.eslintcache

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/pinia-orm/src/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ export class Model {
parentKey = parentKey ?? model.$getLocalKey()
relatedKey = relatedKey ?? instance.$getLocalKey()

this.schemas[related.entity][`pivot_${pivotInstance.$entity()}`] = new HasOne(instance, pivotInstance, relatedPivotKey, relatedKey)
this.schemas[related.entity][`pivot_${relatedPivotKey}_${pivotInstance.$entity()}`] = new HasOne(instance, pivotInstance, relatedPivotKey, relatedKey)

return new BelongsToMany(
model,
Expand Down Expand Up @@ -427,7 +427,7 @@ export class Model {
parentKey = parentKey ?? model.$getLocalKey()
relatedKey = relatedKey ?? instance.$getLocalKey()

this.schemas[related.entity][`pivot_${pivotInstance.$entity()}`] = new MorphOne(instance, pivotInstance, relatedId, model.$entity(), relatedKey)
this.schemas[related.entity][`pivot_${relatedId}_${pivotInstance.$entity()}`] = new MorphOne(instance, pivotInstance, relatedId, model.$entity(), relatedKey)

return new MorphToMany(
model,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class BelongsToMany extends Relation {
const pivot = child.pivot ?? {}
pivot[this.foreignPivotKey] = record[this.parentKey]
pivot[this.relatedPivotKey] = child[this.relatedKey]
child[`pivot_${this.pivot.$entity()}`] = pivot
child[`pivot_${this.relatedPivotKey}_${this.pivot.$entity()}`] = pivot
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class MorphToMany extends Relation {
pivot[this.morphId] = record[this.parentKey]
pivot[this.morphType] = this.parent.$entity()
pivot[this.relatedId] = child[this.relatedKey]
child[`pivot_${this.pivot.$entity()}`] = pivot
child[`pivot_${this.relatedId}_${this.pivot.$entity()}`] = pivot
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe('feature/relations/belongs_to_many_save', () => {
id: this.number(0),
name: this.string(null),
retailers: this.belongsToMany(Client, ClientRetailer, 'supplierId', 'retailerId'),
suppliers: this.belongsToMany(Client, ClientRetailer, 'retailerId', 'supplierId'),
}
}
}
Expand All @@ -130,40 +131,95 @@ describe('feature/relations/belongs_to_many_save', () => {
name: 'Client 1',
retailers: [
{
id: 16,
name: 'Client 16',
id: 4,
pivot: {
retailerCode: '555',
retailerCode: '401',
},
},
{
id: 18,
name: 'Client 18',
id: 5,
pivot: {
retailerCode: '666',
retailerCode: '501',
},
},
],
},
{
id: 2,
name: 'Client 2',
retailers: [
{
id: 3,
pivot: {
retailerCode: '302',
},
},
{
id: 5,
pivot: {
retailerCode: '502',
},
},
],
},
{
id: 3,
name: 'Client 3',
},
{
id: 4,
name: 'Client 4',
retailers: [
{
id: 1,
pivot: {
retailerCode: '104',
},
},
{
id: 5,
pivot: {
retailerCode: '504',
},
},
],
},
{
id: 5,
name: 'Client 5',
},
])

assertState({
client_retailers: {
'[16,1]': { retailerId: 16, supplierId: 1, retailerCode: '555' },
'[18,1]': { retailerId: 18, supplierId: 1, retailerCode: '666' },
'[1,4]': { 'supplierId': 4, 'retailerId': 1, 'retailerCode': '104' },
'[3,2]': { 'supplierId': 2, 'retailerId': 3, 'retailerCode': '302' },
'[5,2]': { 'supplierId': 2, 'retailerId': 5, 'retailerCode': '502' },
'[4,1]': { 'supplierId': 1, 'retailerId': 4, 'retailerCode': '401' },
'[5,4]': { 'supplierId': 4, 'retailerId': 5, 'retailerCode': '504' },
'[5,1]': { 'supplierId': 1, 'retailerId': 5, 'retailerCode': '501' },

},
clients: {
1: {
'id': 1,
'name': 'Client 1',
},
16: {
'id': 16,
'name': 'Client 16',
2: {
'id': 2,
'name': 'Client 2',
},
3: {
'id': 3,
'name': 'Client 3',
},
4: {
'id': 4,
'name': 'Client 4',
},
18: {
'id': 18,
'name': 'Client 18',
5: {
'id': 5,
'name': 'Client 5',
},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ describe('feature/repository/retrieves_order_by', () => {

const expected = [
{ id: 1, name: 'James', roles: [
{ id: 2, users: [], pivot_roleUser: null },
{ id: 1, users: [], pivot_roleUser: null },
{ id: 2, users: [], pivot_role_id_roleUser: null },
{ id: 1, users: [], pivot_role_id_roleUser: null },
],
},
{ id: 2, name: 'Andy', roles: [
{ id: 1, users: [], pivot_roleUser: null },
{ id: 1, users: [], pivot_role_id_roleUser: null },
],
},
{ id: 3, name: 'David', roles: [] },
Expand Down