npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nuxt3-pinia

v1.0.14

Published

Nuxt3 module with autoimport and secure persist(localstorage, sessionstoage, cookie), expire option for pinia

Downloads

49

Readme

nuxt3 pinia

A nuxt3 module that helps you easily and powerfully use the Pinia state repository. Provides state value preservation through web storage(localstorage, sessionstorage), and additional options for specifying expiration times and versions of state values for better utilization.

Easy to use!

  1. When you create a store folder and create a status store module, the Pinia store is automatically created globally.
  2. Provides auto-imported global functions to easy access to the store without any setup/init
  3. Multilayer directories are also automatically recognized, allowing store access through automatically generated distinguished names
  4. Auto-imported Nuxt3 global APIs such as useState can be utilized

** Use Case Screenshot Alt Text

** Example of Persistent State Values through WebStorage Alt Text

Readme Translation

한국어 링크: https://github.com/rubystarashe/nuxt3-pinia/blob/master/README-kor.md

Example

https://github.com/rubystarashe/nuxt3-pinia-playground

Installation

npm i nuxt3-pinia

Basic Usage

// nuxt.config.js
import { defineNuxtConfig } from 'nuxt'

export default defineNuxtConfig({
  modules: ['nuxt3-pinia']
})
// store/index.js
export const store1 = {
  state: () => {
    return {
      foo: 'bar'
    }
  }
}
export const store2 = () => {
  const count = ref(0)
  const doubleCount = computed(() => count.value * 2)
  function increment() {
    count.value++
  }
  return { count, doubleCount, increment }
}
// app.vue
<script setup>
const { foo } = getStoreRefs('store1')

const store2 = getStore('store2')
const { count, doubleCount } = store2.toRefs()
const { increment } = store2
</script>

Directory based store module Auto-Import

// If you set the store module to store/index.js as follows:
export default {
  state: () => {
    return {
      count: 0
    }
  }
}
// default can be called as getStore('default')
export const test = {
  state: () => {
    return {
      foo: 'bar'
    }
  }
}
// test can be called as getStore('test')
// If you set the store module to store/othername.js as follows:
export default {
  state: () => {
    return {
      count: 0
    }
  }
}
// default is omitted name and can be called as getStore('othername')
export const nextname = {
  state: () => {
    return {
      foo: 'bar'
    }
  }
}
// nextname can be called as getStore('othername/nextname')
// If you set the store module to store/depth1/depth2.js as follows:
export default {
  state: () => {
    return {
      count: 0
    }
  }
}
// default is omitted name and can be called as getStore('depth1/depth2')
export const depth3 = {
  state: () => {
    return {
      foo: 'bar'
    }
  }
}
// depth3 can be called as getStore('depth1/depth2/depth3')

Autoimport Directory Option

You can specify the name of the directory path from which you want to read the store modules

//  nuxt.config.js
import { defineNuxtConfig } from "nuxt"

export default defineNuxtConfig({
  modules: ['nuxt3-pinia'],
  pinia: {
    autoImport: 'store' // Default value is 'store'. If it is false, it does not read automatically any folder
  }
})

AutoImported API

Get Store

// get store api
const store1 = getStore('store1')
const store2 = store('store2')

// Gets the states of the store as reactive objects
const store1_refs = store1.toRefs()
const store2_refs = storeToRefs(store2)
const store3_refs = getStoreRefs('store3')
const store4_refs = storeRefs('store4')

Set Store programmically

In addition to creating module files in the specified folder, you can create stores within the vue instance

const newStore = defineStore('storename', {
  state: () => {
    return {
      foo: 'bar'
    }
  }
})
// defineStore creates a store object and registers the created store globally
// Therefore, it can be retrieved from other components through get store api without additional settings
const store_by_get = getStore('storename')

Global Pinia instance and list of stores

You can access the Nuxt app to get global pinia instance. You can also refer to the list of stores registered as modules by referring to pinia.stores or $pinia

const { pinia, $pinia } = useNuxtApp()
const stores = pinia.stores // === $pinia
const store1 = $pinia['store1']()
const store2 = pinia.stores['store1']()

Store Options

Persist Option

When you create a store, you can specify that the status is stored in web storage by granting the persist option

export default {
  persist: 'localStorage' // local, localStorage, session, sessionStorage.
  state: () => {
    return {
      foo: 'bar'
    }
  }
}

Stores with persist option can see persist point in time through the $persist property of the store

<template>
<div v-if="store.$setup.$persist">
{{store.foo}}
</div>
</template>

<script setup>
const store = getStore('default')
</script>

Or you can use isStoreLoaded function instead of $persist to match the hydration time

<template>
<div v-if="isLoaded">
{{store.foo}}
</div>
</template>

<script setup>
const store = getStore('default')
const isLoaded = isStoreLoaded()
</script>

Expire Option

When you create a store, you can set how long the state is stored on the web storage by granting the expire option

export default {
  persist: true,  // === localStorage
  expire: 1000 * 60 * 60 // expire or expirein key. ms.
  state: () => {
    return {
      foo: 'bar'
    }
  }
}

Version Option

If the version of the saved state changes, the default value of the new version is saved to web storage without recalling the store state that was previously saved

export default {
  persist: true,
  version: 'some version', // any
  state: () => {
    return {
      foo: 'bar'
    }
  }
}

Next

  1. Secure mode for persist option
  2. Persist state with Cookie mode