vuex-orm icon indicating copy to clipboard operation
vuex-orm copied to clipboard

Elements are overridden after reload with vuex-persist

Open Maqsyo opened this issue 5 years ago • 6 comments

ezgif com-video-to-gif

If you use vuex-orm with vuex-persist you onccur the problem, that, after a reload, vuex-orm starts to count at 1 again. So your persisted elements will be overridden.

Here you have an simple repo to check this out. https://github.com/Maqsyo/vuex-orm-uid-bug

Maqsyo avatar Mar 05 '20 11:03 Maqsyo

Ah... yea. I think this would happen. Hmmm, maybe we should store uid increment numbers to Vuex State as well, or either implement a real UID feature.

kiaking avatar Mar 08 '20 13:03 kiaking

I think storing it in the state could be a really good quick fix. Thats how MySQL is doing it.

Maqsyo avatar Mar 08 '20 16:03 Maqsyo

I tried two implementations yesterday, store method as @Maqsyo suggested and the UID refactoring as @kiaking suggested.

I found there are side-effects to both approaches but the store is more feasible especially since no additional logic is required when using third party persist tools.

UID’s are especially important when generating indexes so a lot of logic is required when refactoring. Where as we can simply use the mutate action to update the UID count to the store when a store is available.

cuebit avatar Mar 08 '20 16:03 cuebit

@cuebit Thank you for trying out! Yea, I think storing inside Vuex State would work. There're lot of UID implementation out there, and if you really need UUID in client side, the work needed for the app would vary from user to user I guess. So keeping Vuex ORM default UID as simple as possible would be the way to go, I think 👍

kiaking avatar Mar 09 '20 12:03 kiaking

My current quick-fix: I'm using the current timestamp and concatenate it with an increment-value to reduce collisions.

import {Model} from '@vuex-orm/core';

let increment = 0;

export default class Post extends Model
{
    static entity = 'posts'

    static fields()
    {
        return {
            id: this.uid(() => `${new Date().getTime()}_${increment++}`),
            [...]
        };
    }
}

Maqsyo avatar Mar 16 '20 06:03 Maqsyo

Ah yea, that’s smart 👍

cuebit avatar Mar 16 '20 12:03 cuebit