0.0.13 • Published 1 year ago

@tixel/pinia-entity v0.0.13

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

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));
}
0.0.13

1 year ago

0.0.12

1 year ago

0.0.11

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago