pinia-firestore

Pinia with Firestore
2,410 9
Install
npm install pinia-firestore
Latest Version:0.2.1
Vue:3
License:MIT
Last Updated:Oct 31, 2021
Links: GitHub  ·  npm
Maintainer: s-agena

pinia-firestore

Pinia with Firestore

pinia-firestore is a firestore helper library for pinia inspired by vuex fire.

Policy

  • Please note that this is an experimental code for commercial use.
  • When vuex5 is released, I plan to switch to it.

Concept

  • Using pinia, firebase(firestore) v9 and vue3
  • Typescript support
  • Use of differential update in real-time update (document unit)

Sample Code

import { initializeApp } from "firebase/app";
import {
  getFirestore,
  collection,
  query,
  where,
  addDoc,
  updateDoc,
  deleteDoc,
  doc,
  serverTimestamp,
  FieldValue,
} from "firebase/firestore";
import { defineStore } from "pinia";
import { bind, DocumentProperties } from "pinia-firestore";

const app = initializeApp({
  projectId: "agena-repo",
});

const db = getFirestore(app);
const colRefTodos = collection(db, "todos");
const unFinishedTodos = query(colRefTodos, where("finished", "==", false));
const finishedTodos = query(colRefTodos, where("finished", "==", true));
let currentTodos = unFinishedTodos;

export type TypeTodo = {
  finished: boolean;
  text: string;
  create: FieldValue;
};

export const useTodosStore = defineStore({
  id: "todos",
  state: () => ({
    todos: [] as (TypeTodo & DocumentProperties)[],
    isLoading: false,
  }),
  getters: {
    count: (state) => {
      return state.todos.length;
    },
  },
  actions: {
    async toggleBind() {
      currentTodos =
        currentTodos === finishedTodos ? unFinishedTodos : finishedTodos;
      this.isLoading = true;
      await bind(this, "todos", currentTodos);
      this.isLoading = false;
    },
    async init() {
      this.isLoading = true;
      await bind(this, "todos", currentTodos);
      this.isLoading = false;
    },
    addTodo(text: string) {
      const addData: TypeTodo = {
        finished: false,
        text,
        create: serverTimestamp(),
      };
      addDoc(colRefTodos, addData);
    },
    updateTodo(id: string, text: string) {
      updateDoc(doc(db, "todos/" + id), { text });
    },
    removeTodo(id: string) {
      deleteDoc(doc(db, "todos/" + id));
    },
  },
});

Caution

Use v9 for firebase library.

License

MIT

Related Packages

pinia-plugin-firestore-sync

Sync your pinia state and firestore document at ease!

442
@volverjs/query-vue

Repository pattern applied to a pinia store.

6,325 3
taro-plugin-pinia

Taro&vue3 支持使用 Pinia 进行状态管理的插件

3,934 10
pinia-scope

Scoped Pinia Stores for Vue

3,115 0
pinia-persisted-plugin

将 Pinia 的状态持久化到本地存储(如LocalStorage或SessionStorage),并在页面刷新后...

1,387 0

Version History

Version Released Vue License
0.2.1 ^3.2.20 MIT
0.2.0 ^3.2.20 MIT
0.1.1 ^3.2.20 MIT
0.1.0 ^3.2.20 MIT
0.0.12 ^3.2.20 MIT