vuex-pathify

repository·master·Indexed 23 days ago

https://github.com/davestewart/vuex-pathify

A tool for simplifying Vuex development using a declarative, path-based syntax for accessing and mutating state. It reduces boilerplate by providing helpers like get(), sync(), and call() to replace standard Vuex map* helpers, and includes a make.mutations() utility to automatically generate mutations for state objects. Version 1.5.1.

Tokens
14.6K
Snippets
50
Records
93
Agent score
78%

What's inside vuex-pathify

  1. What is Vuex Pathify

    master

    Vuex Pathify provides a declarative, state-based, path interface to your Vuex store. Instead of traditional Vuex patterns, you access the store using path strings that can reference any module, property, or sub-property using the @ syntax.

    Path Syntax Example: 'foo/bar@a.b.c'

    This allows you to get or set data without worrying about the underlying implementation details or complex syntax juggling.

  2. Understand Pathify path syntax

    master

    Pathify uses a declarative path syntax to access Vuex stores. This syntax supports module and property access, variable expansion, and wildcard expansion. It also provides direct access syntaxes to bypass standard get/set mapping for custom Vuex member names.

    Paths are used in global store methods and component helpers:

    // Global store usage
    store.get('items')
    store.set('products/items', items)
    
    // Component helper usage
    computed: {
      search: sync('products/filters@search')
    }
    // global
    store.get('items')
    store.set('products/items', items)
    
    // components
    computed: {
      search: sync('products/filters@search')
    }
  3. How vuex-pathify solves Vuex pain points

    master

    vuex-pathify is designed to simplify the Vuex experience by addressing common developer friction points. It focuses on reducing boilerplate and unifying the fragmented syntax required to interact with a Vuex store.

    Key improvements include:

    • Unified Syntax: Replaces the need to juggle between state, getters, commit, and dispatch with a single path-based syntax.
    • Boilerplate Reduction: Eliminates the need to explicitly create mutations, getters, and actions for every state property using Store Helpers.
    • Simplified Component Wiring: Reduces the complexity of mapping state and mutations/actions in components using Component Helpers.
    • Transparent Sub-property Access: Allows for direct read/write access to nested object properties without manual compound computed properties.
    • Operational Simplification: Reduces the 4 standard Vuex operations (get state, get getter, commit mutation, dispatch action) into 2 core Pathify operations.
  4. How Pathify handles accessor priority

    master

    Pathify uses a concept called Accessor Priority to automatically determine whether to use state/getters for reading data or actions/mutations for writing data. This simplifies the Vuex API by allowing you to request a property without specifying the implementation detail.

    Priority Logic

    • For Reads (get): A mapped getter is prioritized over a mapped state property. This allows you to use getters as
    // don't care about the implementation, just get/set the value
    store.get('items')
    store.set('items', data)
  5. When to access the Vuex store directly

    master

    While Pathify handles most wiring, there are specific scenarios where you should bypass Pathify helpers:

    • Global access/Syncing: Use Pathify get(), set(), and sync().
    • Non-standard mutations/actions: Use Pathify direct syntax.
    • Explicit intent or Store-internal logic: Use standard Vuex dispatch() or commit() if you want to be highly explicit, or when writing logic within the store itself.
    • Getter priority conflicts: Call state directly if a getter with the same name is taking priority over the state property you want to access.
  6. When to use Pathify

    master

    Pathify is best suited for getting, setting, and syncing properties 1:1 with the Vuex store. It excels at mapping state names to Vuex members in a get/set manner.

    Use cases:

    • Setting and getting data.
    • Syncing component properties with the store.

    Limitations:

    • It is less intuitive for calling non-get/set named actions or mutations (e.g., updateItems or loadItems). For these, use direct syntax or standard Vuex commit/dispatch.
    • Do not use sub-property access as a crutch to avoid proper state structuring; keep application logic clear by not nesting everything under a single property.
  7. Understand Pathify naming schemes

    master

    Pathify maps paths to store members (state, getters, mutations, and actions). To generate the correct code, you must choose a naming scheme. If you use the standard scheme, no additional configuration is required.

    schemepathstategettermutationactionnotes
    standard/foofoofooSET_FOOsetFooUsed by most Vue developers
    simple/foofoofoofoosetFooSimpler, unified format
    custom/foo????User must supply custom mapping function
  8. How Pathify simplifies Vuex development

    master

    Pathify simplifies the Vuex development experience by replacing multiple operations, helpers, and naming formats with a single unified path syntax and four core methods.

    Instead of managing different syntaxes for state, getters, mutations, and actions, Pathify uses a custom path syntax (e.g., 'products/items@filters.search') that maps to store members via a configurable naming scheme. This allows you to interact with the store using a consistent set of methods regardless of whether you are reading from a getter or writing via an action.

  9. How Pathify's path syntax works

    master

    Pathify uses a declarative, state-based path syntax to access Vuex state. This allows you to reference any module, property, or sub-property using a specific string format. This abstraction removes the need for manually-written getters, setters, and mutations for every piece of state.

    // Accessing a top-level property
    store.get('loaded')
    
    // Reaching into sub-properties and arrays using '@' and '.'
    store.get('products@items.0.name')
    store.set('products@items.1.name', 'Vuex Pathify')
  10. Understand Pathify's member mapping and prioritization

    master

    Pathify maps paths to Vuex members using a configurable naming scheme. When a path is provided, Pathify determines which Vuex member to target based on the operation and the member type.

    Prioritization Logic

    To reduce complexity, Pathify prioritizes certain Vuex members over others:

    • Read operations: Prioritizes getters over state.
    • Write operations: Prioritizes actions over mutations.

    Default Naming Schemes

    OperationMemberNameScheme
    readstatefoobase name
    readgettersfoono prefix, no case conversion
    writemutationsSET_FOO"set" prefix, constant case
    writeactionssetFoo"set" prefix, camel case

    Mapping Examples

    • store.get('products/items') maps to store.getters['products/items'] (if available), otherwise store.state.products.items.
    • store.set('products/items', items) maps to dispatch('products/setItems', items) (if available), otherwise commit('products/SET_ITEMS', items).
  11. Use vuex-pathify store helpers to eliminate boilerplate

    master

    The make helper object provides functions to automatically generate Vuex mutations, actions, and getters based on a provided state object. This eliminates the need to write redundant 1:1 wiring functions for every state property.

    Core Concepts

    • Automatic Generation: Helpers create functions that map directly to your state structure.
    • Mixing and Matching: You can use helpers to generate a subset of store members and mix them with your own manual declarations (e.g., custom actions or complex getters).
    • Payload Support: Mutations generated by make.mutations() support transparent sub-property writes using the Payload class.

    While you can generate all three (mutations, actions, and getters), the Pathify-recommended approach is to eschew redundant getters and actions that simply proxy work to state and mutations. Instead, focus on creating mutations and let Pathify handle the heavy lifting for state access.

    import { make } from 'vuex-pathify'
    
    const state = {
      items: [],
      status: '',
      filters: {
        search: '',
        sort: {}
      }
    }
    
    // Generate mutations, actions, and getters
    const mutations = make.mutations(state)
    const actions = { ...make.actions(state) }
    const getters = { ...make.getters(state) }
    
    export default {
      state,
      mutations,
      actions,
      getters,
    }