@tixel/pinia-entity
TypeScript icon, indicating that this package has built-in type declarations

0.0.12 • Public • Published

Installation

npm install @tixel/pinia-entity -D

Usage

In the example below, the name of the Entity is Entity. This could be replaced for User, Post, Comment, etc.

EntityStore.ts

import { EntityState, createAdapter } from '@tixel/pinia-entity';
import { acceptHMRUpdate, defineStore } from 'pinia';
import { YourEntity } from '~/types/YourEntity';

export type EntityStoreState = EntityState<YourEntity>;
const adapter = createAdapter<YourEntity>('id');
const { getAll, getById } = adapter.getSelectors();

export const useEntityStore = defineStore('EntityStore', {
  state(): EntityStoreState {
    return {
      ids: [],
      entities: {},
    };
  },
  actions: {
    async fetch(id: string): Promise<YourEntity> {
      const yourEntity = useFetch(); // get your entity

      adapter.addOne(this.$state, yourEntity);
      return yourEntity;
    },
  },
  getters: {
    getAll,
    getById
  },
});
if (import.meta.hot) {
  import.meta.hot.accept(acceptHMRUpdate(useEntityStore, import.meta.hot));
}

Readme

Keywords

none

Package Sidebar

Install

npm i @tixel/pinia-entity

Weekly Downloads

118

Version

0.0.12

License

ISC

Unpacked Size

5.82 kB

Total Files

4

Last publish

Collaborators

  • jecantixel
  • jryd
  • flipjms
  • ianjamieson
  • zoltanflamis
  • dusterio