Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: add upgrade information for Pinia migration #1677

Merged
merged 2 commits into from
Jan 31, 2025
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: 2 additions & 0 deletions .wordlist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ ManyToManyIdField
ManyToOne
ManyToOneAssociation
ManyToOneAssociationField
MapState
MediaDataSelection
MediaDataSet
MediaFolderDataSet
Expand Down Expand Up @@ -1386,6 +1387,7 @@ mandatorily
manufacturerId
manytomanyid
mapErrors
mapGetters
martinfowler
masternode
matchers
Expand Down
265 changes: 265 additions & 0 deletions guides/plugins/plugins/administration/system-updates/pinia.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
---
nav:
title: Upgrading to Pinia
position: 261
---

# Migration from Vuex in Shopware to Pinia

## Introduction

With the release of Shopware 6.7, we will replace Vuex with [Pinia](https://pinia.vuejs.org/) as the state management library for the administration.

## Why Pinia?

Migrating to Pinia simplifies state management with an intuitive API, no need for mutations, better TypeScript support, and seamless integration with Vue 3 Composition API. It’s lightweight, modular, and offers modern features like devtools support, making it a more efficient alternative to Vuex.

## Migration Guide

To migrate a Vuex store to Pinia, you need to make some changes to the store definition and how you access it in components.

- First, register it with `Shopware.Store.register` and define the store with `state`, `getters`, and `actions` properties:

Check warning on line 21 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L21

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` Store` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:21:34: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` Store`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY

**Before (Vuex):**

```javascript
export default {
namespaced: true,

state: {
// Initial state
...
},
mutations: {
...
},
getters: {
...
},
actions: {
...
},
}
```

**After (Pinia):**

```javascript
const store = Shopware.Store.register('<storeName>', {

Check warning on line 48 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L48

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` Store` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:48:21: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` Store`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY
state: () => ({
// Initial state
...
}),
getters: {
...
},
actions: {
...
},
});
export default store;
```

- You can also register the store with an `id` property in the definition object, for example:

```javascript
const store = Shopware.Store.register({

Check warning on line 66 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L66

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` Store` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:66:21: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` Store`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY
id: '<storeName>',

Check warning on line 67 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L67

This abbreviation for “identification” is spelled all-uppercase. (ID_CASING[2]) Suggestions: `ID` Rule: https://community.languagetool.org/rule/show/ID_CASING?lang=en-US&subId=2 Category: CASING
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:67:2: This abbreviation for “identification” is spelled all-uppercase. (ID_CASING[2])
 Suggestions: `ID`
 Rule: https://community.languagetool.org/rule/show/ID_CASING?lang=en-US&subId=2
 Category: CASING
state: () => ({
// Initial state
}),
getters: {
// ...
},
actions: {
// ...
},
});
```

- If you register a store that already exists, it will be overwritten. You can also unregister a store:

```javascript
Shopware.Store.unregister('<storeName>');

Check warning on line 83 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L83

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` Store` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:83:7: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` Store`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY
```

- To register a store from a component or index file, simply import the store file.

**Before (Vuex):**

```javascript
import productsStore from './state/products.state';

Shopware.State.registerModule('product', productsStore);

Check warning on line 93 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L93

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` State` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:93:7: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` State`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY
```

**After (Pinia):**

```javascript
import './state/products.state';
```

### Key Changes

#### State

In Pinia, `state` must be a function returning the initial state instead of a static object.

```javascript
state: () => ({
productName: '',
})
```

#### Mutations

Vuex `mutations` are no longer needed in Pinia, since you can modify state directly in actions or compute it dynamically.

```javascript
actions: {
updateProductName(newName) {
this.productName = newName; // Directly update state
},
},
```

#### Getters

- There cannot be getters with the same name as a property in the state, as both are exposed at the same level in the store.
- Getters should be used to compute and return information based on state, without modifying it.

#### TypeScript

We recommend migrating JavaScript stores to TypeScript for stricter typing, better autocompletion, and fewer errors during development.

```typescript
const store = Shopware.Store.register({

Check warning on line 136 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L136

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` Store` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:136:21: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` Store`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY

Check warning on line 136 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L136

This abbreviation for “identification” is spelled all-uppercase. (ID_CASING[2]) Suggestions: `ID` Rule: https://community.languagetool.org/rule/show/ID_CASING?lang=en-US&subId=2 Category: CASING
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:136:40: This abbreviation for “identification” is spelled all-uppercase. (ID_CASING[2])
 Suggestions: `ID`
 Rule: https://community.languagetool.org/rule/show/ID_CASING?lang=en-US&subId=2
 Category: CASING
id: 'myStore',
...
});

export type StoreType = ReturnType<typeof store>;
```

Then, you can use this type to extend `PiniaRootState`:

```typescript
import type { StoreType } from './store/myStore';

declare global {
interface PiniaRootState {
myStore: StoreType;
}
}
```

### Composables as a Store

With Pinia, you can use reactive properties inside a store and define it like a composable. Keep in mind that only variables and functions returned from the store will be tracked by Pinia in devtools.

```typescript
const store = Shopware.Store.register('<storeName>', function() {

Check warning on line 161 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L161

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` Store` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:161:21: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` Store`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY
const count = ref(0);

const doubled = computed(() => count.value * 2);

Check warning on line 164 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L164

If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1]) Suggestions: ` Value`, ` value` Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:164:37: If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1])
 Suggestions: ` Value`, ` value`
 Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1
 Category: CASING

function increment() {
count.value++;

Check warning on line 167 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L167

If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1]) Suggestions: ` Value`, ` value` Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:167:8: If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1])
 Suggestions: ` Value`, ` value`
 Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1
 Category: CASING
}

function decrement() {
count.value--;

Check warning on line 171 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L171

If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1]) Suggestions: ` Value`, ` value` Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:171:8: If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1])
 Suggestions: ` Value`, ` value`
 Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1
 Category: CASING
}

return { count, doubled, increment, decrement };
});
```

You can also use a composable function defined outside the store. This allows you to encapsulate and reuse logic across different stores or components, promoting better code organization and modularity:

```typescript
// composables/myComposable.ts
export function useMyComposable() {
const count = ref(0);

const doubled = computed(() => count.value * 2);

Check warning on line 185 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L185

If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1]) Suggestions: ` Value`, ` value` Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:185:37: If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1])
 Suggestions: ` Value`, ` value`
 Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1
 Category: CASING

function increment() {
count.value++;

Check warning on line 188 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L188

If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1]) Suggestions: ` Value`, ` value` Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:188:8: If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1])
 Suggestions: ` Value`, ` value`
 Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1
 Category: CASING
}

function decrement() {
count.value--;

Check warning on line 192 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L192

If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1]) Suggestions: ` Value`, ` value` Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1 Category: CASING
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:192:8: If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1])
 Suggestions: ` Value`, ` value`
 Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1
 Category: CASING
}

return { count, doubled, increment, decrement };
}
```

```typescript
// store/myStore.ts
import { useMyComposable } from '../composables/myComposable';

const store = Shopware.Store.register('myStore', useMyComposable);

Check warning on line 203 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L203

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` Store` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:203:21: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` Store`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY
```

### Accessing the Store

To access the store in Vuex, you would typically do:

```javascript
Shopware.State.get('<storeName>');

Check warning on line 211 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L211

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` State` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:211:7: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` State`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY
```

When migrating to Pinia, it changes to:

```javascript
Shopware.Store.get('<storeName>');

Check warning on line 217 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L217

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` Store` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:217:7: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` Store`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY
```

### Testing

To test your store, just import it so it's registered. You can use `$reset()` to reset the store before each test:

```javascript
import './store/my.store';

describe('my store', () => {
const store = Shopware.Store.get('myStore');

Check warning on line 228 in guides/plugins/plugins/administration/system-updates/pinia.md

View workflow job for this annotation

GitHub Actions / LanguageTool

[LanguageTool] guides/plugins/plugins/administration/system-updates/pinia.md#L228

Add a space between sentences. (SENTENCE_WHITESPACE) Suggestions: ` Store` Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US Category: TYPOGRAPHY
Raw output
guides/plugins/plugins/administration/system-updates/pinia.md:228:23: Add a space between sentences. (SENTENCE_WHITESPACE)
 Suggestions: ` Store`
 Rule: https://community.languagetool.org/rule/show/SENTENCE_WHITESPACE?lang=en-US
 Category: TYPOGRAPHY

beforeEach(() => {
store.$reset();
});

it('has initial state', () => {
expect(store.count).toBe(0);
});
});
```

When testing components that use Pinia stores, register Pinia as a plugin and reset it before each test:

```javascript
import { createPinia, setActivePinia } from 'pinia';

const pinia = createPinia();

describe('my component', () => {
beforeEach(() => {
setActivePinia(pinia);
});

it('is a component', async () => {
const wrapper = mount(await wrapTestComponent('myComponent', { sync: true }), {
global: {
plugins: [pinia],
stubs: {
// ...
},
},
});

expect(wrapper.exists()).toBe(true);
});
});
```
Loading