Skip to content

posva/pinia-colada

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Pinia Colada logo


npm package build status


Pinia Colada

The missing data fetching library for Pinia

This is a more complete and production-ready (not yet!) version of the exercises from Mastering Pinia.

Mastering Pinia banner

Note

Pinia Colada is in active development not ready for production. New versions might introduce breaking changes. Feedback regarding new and existing options and features is welcome! Documentation is a work in progress and contributions are welcome.

Pinia Colada is an opinionated yet flexible data fetching layer on top of Pinia. It's built as a set of pinia plugins, stores and composables to benefit from Pinia's features and ecosystem. Pinia Colada has:

  • โšก๏ธ Automatic caching: Smart client-side caching with request deduplication
  • ๐Ÿ—„๏ธ Async State: Handle any async state
  • ๐Ÿ“š Typescript Support: Fully typed with Typescript
  • ๐Ÿ’จ Bundle Size: Small bundle size (<2kb) and fully tree-shakeable
  • ๐Ÿ“ฆ Zero Dependencies: No dependencies other than Pinia
  • โš™๏ธ SSR: Server-side rendering support
  • ๐Ÿ”Œ Plugins: Powerful plugin system

Installation

npm install pinia @pinia/colada

Install the plugins for the features you need:

import { createPinia } from 'pinia'
import { PiniaColada } from '@pinia/colada'

app.use(createPinia())
// install after pinia
app.use(PiniaColada, {
  // optional options
})

Usage

<script lang="ts" setup>
import { useRoute } from 'vue-router'
import { useMutation, useQuery, useQueryCache } from '@pinia/colada'
import { updateContact as _updateContact, getContactById } from '~/api/contacts'

const route = useRoute()
const caches = useQueryCache()

const { data: contact, isLoading } = useQuery({
  // recognizes this query as ['contacts', id]
  key: () => ['contacts', route.params.id],
  query: () => getContactById(route.params.id),
})

const { mutate: updateContact } = useMutation({
  mutation: _updateContact,
  onSettled({ id }) {
    caches.invalidateQueries({ key: ['contacts', id], exact: true })
  },
})
</script>

<template>
  <section>
    <ContactCard
      :key="contact.id"
      :contact="contact"
      :is-updating="isLoading"
      @update:contact="updateContact"
    />
  </section>
</template>

License

MIT