CodeDredd / pinia-orm

The Pinia plugin to enable Object-Relational Mapping access to the Pinia Store.
https://pinia-orm.codedredd.de/
MIT License
452 stars 39 forks source link

`pivot` helper fields are not deleted or removed. #1861

Closed adm-bome closed 3 months ago

adm-bome commented 5 months ago

Question: Is there a reason all (behind the scene) pivot fields are not deleted or removed.

// Setup
import {Model, useRepo} from "pinia-orm";

class Client extends Model {
  static entity= 'clients';

  static fields () {
    return {
      id: this.number(0),
      name: this.string(null),
      retailers: this.belongsToMany(Client, ClientRetailer,  'supplierId', 'retailerId'),
      suppliers: this.belongsToMany(Client, ClientRetailer,  'retailerId', 'supplierId'),
    }
  }
}

class ClientRetailer extends Model {
  static entity = 'client_retailers'

  static primaryKey = ['supplierId','retailerId']

  static fields () {
    return {
      supplierId: this.number(null),
      retailerId: this.number(null),
      retailerCode: this.string(null)
    }
  }
}

const rawData = [
  {
    id: 1,
    name: "Client 1",
    retailers: [
      {
        id: 4,
        pivot: {
          retailerCode: '401'
        }
      },
      {
        id: 5,
        pivot: {
          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",
    retailers: [
      {
        id: 4,
        pivot: {
          retailerCode: '405'
        }
      },
      {
        id: 2,
        pivot: {
          retailerCode: '205'
        }
      }
    ]
  },
]

When I query pinia with

const clientRepo = useRepo(Client)
clientRepo.save(rawData)
clientRepo.whereId(5).with('suppliers').first()

I get this result:

{
    id: 5,
    name: "Client 5",
    pivot: undefined,
    suppliers: [
        {
            pivot: {
                supplierId: 1,
                retailerId: 5,
                retailerCode: "501"
            },
            pivot_retailerId_client_retailers: null,
            pivot_supplierId_client_retailers: null,
            id: 1,
            name: "Client 1",
            retailers: [],
            suppliers: []
        },
        {
            pivot: {
                supplierId: 2,
                retailerId: 5,
                retailerCode: "502"
            },
            pivot_retailerId_client_retailers: null,
            pivot_supplierId_client_retailers: null,
            id: 2,
            name: "Client 2",
            retailers: [],
            suppliers: []
        },
        {
            pivot: {
                supplierId: 4,
                retailerId: 5,
                retailerCode: "504"
            },
            pivot_retailerId_client_retailers: null,
            pivot_supplierId_client_retailers: null,
            id: 4,
            name": "Client 4",
            retailers: [],
            suppliers: []
        }
    ]
}

See the top level pivot: undefined and all pivot_*: null properties on relations

Expected result:

{
    id: 5,
    name: "Client 5",
    suppliers: [
        {
            pivot: {
                supplierId: 1,
                retailerId: 5,
                retailerCode: "501"
            },
            id: 1,
            name: "Client 1",
            retailers: [],
            suppliers: []
        },
        {
            pivot: {
                supplierId: 2,
                retailerId: 5,
                retailerCode: "502"
            },
            id: 2,
            name: "Client 2",
            retailers: [],
            suppliers: []
        },
        {
            pivot: {
                supplierId: 4,
                retailerId: 5,
                retailerCode: "504"
            },
            id: 4,
            name": "Client 4",
            retailers: [],
            suppliers: []
        }
    ]
}

Originally posted by @adm-bome in https://github.com/CodeDredd/pinia-orm/issues/1857#issuecomment-2129429012