marshallswain / feathers-pinia

Connect your Feathers API to the elegant data store for Vue
52 stars 22 forks source link

Feathers-pinia 4.2.X: createInStore() isn't a function #163

Closed boumaza-samir closed 4 months ago

boumaza-samir commented 4 months ago

Hello everyone

I have a strange behaviour since version 4.2.X (last try 4.2.4) If I do a new and then do a createInStore() like this

let service = api.service('reports');
let instance = service.new({id: uuid.v4().replace(/^.{8}/, '00000000')});
instance.createInStore()

I get the following error

Uncaught TypeError: instance.createInStore is not a function

In the return from new, there is no createInStore()

If I do this

let service = api.service('reports');
service.createInStore({id: uuid.v4().replace(/^.{8}/, '00000000')}) 

It works, but it's not the excepted behaviour I've never had a problem until now, I've been updating my project regularly since version 3.

Can't we use the createInStore function after a new in the 4.2.X?

gorango commented 4 months ago

TypeError indicates that you likely misconfigured your service.

Check if you're using ModelInstance in your setupService and replace with ServiceInstance. More in the migration guides.

boumaza-samir commented 4 months ago

TypeError indicates that you likely misconfigured your service.

Check if you're using ModelInstance in your setupService and replace with ServiceInstance. More in the migration guides.

Thank you for your reply ! Yes I used the migration guide and feather-pinia works in my project up to version 4.1.1 if I go to version 4.2.2 or higher I get the error on the createInStore

gorango commented 4 months ago

Then you need to provide a repro or at least more context about your setup

boumaza-samir commented 4 months ago

Ok, I found something interesting In createPiniaClient in setupInstance, we spread the data value as follows

export const api = createPiniaClient(feathersClient, {
  pinia,
  idField: 'id',
  // optional
  ssr: false,
  whitelist: [],
  defaultLimit: 100,
  paramsForServer: [],
  skipGetIfExists: false,
  customSiftOperators: {},
  syncWithStorage: false,
  storage: window.localStorage,

setupInstance(data: AnyData): AnyData {
    return {
      id: undefined,
      pathId: undefined,
      created_at: undefined,
      created_by: undefined,
      updated_at: undefined,
      updated_by: undefined,
      deleted_at: undefined,
      deleted_by: undefined,
      ...data, // <-- data spreaded
    };
  },

  customizeStore(defaultStore: AnyData): AnyData {
    return {};
  },

If I return directly data it work

export const api = createPiniaClient(feathersClient, {
  pinia,
  idField: 'id',
  // optional
  ssr: false,
  whitelist: [],
  defaultLimit: 100,
  paramsForServer: [],
  skipGetIfExists: false,
  customSiftOperators: {},
  syncWithStorage: false,
  storage: window.localStorage,

  setupInstance(data: AnyData): AnyData { return data; },

  customizeStore(defaultStore: AnyData): AnyData {
    return {};
  },

But I still don't know why the problem only appears with version 4.2.X ... If anyone has any idea where this problem might be coming from, it would be appreciated.

gorango commented 4 months ago

Some of the issues that immediately jump out:

  1. Defining setupInstance in the root of your options will run for every single service... this seems unintentional.
  2. By spreading the data in your return, you are only returning the enumerable properties, so you effectively throw away all of the methods (like createInStore).
  3. Regarding types, the data object is of a ServiceInstance type - by casting it to AnyData you are losing all type inference, along with all the helpful error messages that would be warning you when you write bad code.

Based on your snippets, simply removing your entire setupInstance block should resolve the issue...

To customise individual services, add them under the services key in options.

I warmly suggest re-reading the docs...

boumaza-samir commented 4 months ago

Thanks for your reply! This code is historic I discovered it today ah ah it's done on purpose so that we have the created etc in all instances when I saw it I said to myself the same thing that it wasn't the best strategy just like anydata I'm going to delete that When I do a service, I do it in a dedicated file, following the procedure in the documentation if I need to.
The thing I don't understand is that it should never work... But it only crashes in versions 4.2.X

boumaza-samir commented 4 months ago

Anyway I will close the issue thanks @gorango !