CodeDredd / pinia-orm

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

method .all() retrieve the relationships too #1237

Closed Michel-HaiRUn closed 1 year ago

Michel-HaiRUn commented 1 year ago

Environment

Reproduction

User Model

import { Model } from "pinia-orm";
import Post from '@/models/post'

export default class User extends Model {
  static entity = 'users';

  static fields () {
    return {
      id: this.uid(),
      name: this.string(''),
      posts: this.hasMany(Post, "userId"),
    }
  }

  declare id: string
  declare name: string
}

Posts Model

import { Model } from 'pinia-orm'
import User from '@/models/user'
export default class Post extends Model {
  static entity = 'posts'

  static fields () {
    return {
      id: this.uid(),
      userId: this.uid(),
      title: this.string(''),
      done: this.boolean(false),
      author: this.belongsTo(User, 'userId')
    }
  }

  declare id: string
  declare userId: string | null
  declare title: string
  declare done: boolean
  declare author: User | null
}

data object used

{
  users: [
    {
      name: "John Doe",
      posts: [
        {
          title: "Create awesome application!",
          done: false,
        },
        {
          title: "Read the documentation",
          done: false,
        },
      ],
    },
    {
      name: "Johnny Doe",
      posts: [
        {
          title: "Star Vuex ORM repository",
          done: false,
        },
      ],
    },
  ]
}

user Store

import User from '@/models/user';
import { getAllData } from '@/utils/api';
import { defineStore } from 'pinia'
import { useRepo } from 'pinia-orm';

export const userRepo = useRepo(User);

export const useUserStore = defineStore('user', {
  state: () => ({
    users: []
  }),

  getters: {
    userList: () => userRepo.all()
  },

  actions: {
    async getUsers(){
      try{
        const users = await getAllData()
        userRepo.save(users)
      }catch(e){
        console.error('error get user', e)
      }
    },
    createUser(user: User){
      if(!user) return;
      userRepo.save(user)
    }
  }
})

Describe the bug

After the data is inserted with save; my new users, posts pinia store created look like this:

users

{
  "kmJqjh_RyaAfg90ykmfri": {
    "id": "kmJqjh_RyaAfg90ykmfri",
    "name": "John Doe"
  },
  "ekJ_Xx88MjNDCU0hy5kyc": {
    "id": "ekJ_Xx88MjNDCU0hy5kyc",
    "name": "Johnny Doe"
  }
}

posts

{
  "RJ-DcwF2tJ7HNp4f6Rgx0": {
    "id": "RJ-DcwF2tJ7HNp4f6Rgx0",
    "userId": "kmJqjh_RyaAfg90ykmfri",
    "title": "Create awesome application!",
    "done": false
  },
  "LZJZao63i-GnCvXP9uixK": {
    "id": "LZJZao63i-GnCvXP9uixK",
    "userId": "kmJqjh_RyaAfg90ykmfri",
    "title": "Read the documentation",
    "done": false
  },
  "EdrL0u-o1IaxovpDjus43": {
    "id": "EdrL0u-o1IaxovpDjus43",
    "userId": "ekJ_Xx88MjNDCU0hy5kyc",
    "title": "Star Vuex ORM repository",
    "done": false
  }
}

and from my getters in my user store the result of the userRepo.all() is:

[
  {
    "id": "kmJqjh_RyaAfg90ykmfri",
    "name": "John Doe",
    "posts": [
      {
        "id": "RJ-DcwF2tJ7HNp4f6Rgx0",
        "userId": "kmJqjh_RyaAfg90ykmfri",
        "title": "Create awesome application!",
        "done": false
      },
      {
        "id": "LZJZao63i-GnCvXP9uixK",
        "userId": "kmJqjh_RyaAfg90ykmfri",
        "title": "Read the documentation",
        "done": false
      }
    ]
  },
  {
    "id": "ekJ_Xx88MjNDCU0hy5kyc",
    "name": "Johnny Doe",
    "posts": [
      {
        "id": "EdrL0u-o1IaxovpDjus43",
        "userId": "ekJ_Xx88MjNDCU0hy5kyc",
        "title": "Star Vuex ORM repository",
        "done": false
      }
    ]
  }
]

The posts is retrieved to when in the docs .all() retrieve only the model without relationships

Additional context

No response

Logs

There is no error log in the console
CodeDredd commented 1 year ago

Please read the note in the docs: https://pinia-orm.codedredd.de/guide/repository/retrieving-data#retrieving-data 😉

Michel-HaiRUn commented 1 year ago

Sorry i don't know if don't know how to read the docs but i didn't find any instruction of how to retrieve all model without the relationships :/

i tried userRepo.get() ``Error .get() is not a function trieduserRepo.all() its retrieve all models with relationships