コンテンツへスキップ

useFirestore

カテゴリ
エクスポートサイズ
637 B
パッケージ
@vueuse/firebase
最終更新日
先週

リアクティブなFirestoreバインディング。ローカルデータをリモートデータベースと**常に同期**させることを容易にします。@vueuse/firebaseアドオンで利用可能です。

使用方法

js
import { useFirestore } from '@vueuse/firebase/useFirestore'
import { initializeApp } from 'firebase/app'
import { collection, doc, getFirestore, limit, orderBy, query } from 'firebase/firestore'
import { computed, ref } from 'vue'

const app = initializeApp({ projectId: 'MY PROJECT ID' })
const db = getFirestore(app)

const todos = useFirestore(collection(db, 'todos'))

// or for doc reference
const user = useFirestore(doc(db, 'users', 'my-user-id'))

// you can also use ref value for reactive query
const postsLimit = ref(10)
const postsQuery = computed(() => query(collection(db, 'posts'), orderBy('createdAt', 'desc'), limit(postsLimit.value)))
const posts = useFirestore(postsQuery)

// you can use the boolean value to tell a query when it is ready to run
// when it gets falsy value, return the initial value
const userId = ref('')
const userQuery = computed(() => userId.value && doc(db, 'users', userId.value))
const userData = useFirestore(userQuery, null)

インスタンス間での共有

autoDispose: falseを渡すことで、db参照を再利用できます。db参照を自動破棄するまでのミリ秒数を設定することもできます。

注:破棄されていないdb参照を再度取得しても、Firestoreの読み取りは発生しません。

ts
const todos = useFirestore(collection(db, 'todos'), undefined, { autoDispose: false })

または、コアパッケージのcreateGlobalStateを使用します

js
// store.js
import { createGlobalState } from '@vueuse/core'
import { useFirestore } from '@vueuse/firebase/useFirestore'

export const useTodos = createGlobalState(
  () => useFirestore(collection(db, 'todos')),
)
js
// app.js
import { useTodos } from './store'

export default {
  setup() {
    const todos = useTodos()
    return { todos }
  },
}

型定義

typescript
export interface UseFirestoreOptions {
  errorHandler?: (err: Error) => void
  autoDispose?: boolean | number
}
export type FirebaseDocRef<T> = Query<T> | DocumentReference<T>
type Falsy = false | 0 | "" | null | undefined
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<DocumentReference<T> | Falsy>,
  initialValue: T,
  options?: UseFirestoreOptions,
): Ref<T | null>
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<Query<T> | Falsy>,
  initialValue: T[],
  options?: UseFirestoreOptions,
): Ref<T[]>
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<DocumentReference<T> | Falsy>,
  initialValue?: T | undefined | null,
  options?: UseFirestoreOptions,
): Ref<T | undefined | null>
export declare function useFirestore<T extends DocumentData>(
  maybeDocRef: MaybeRef<Query<T> | Falsy>,
  initialValue?: T[],
  options?: UseFirestoreOptions,
): Ref<T[] | undefined>

ソースコード

ソースコードドキュメント

コントリビューター

Anthony Fu
平間清彦
Anthony Fu
Zehir
Antério Vieira
azaleta
sun0day
Alex Kozack
ロドルフォ・ロマン
マティアス・スティラー
フィル・リー

変更ログ

v12.0.0-beta.1 2024/11/21
0a9ed - feat!: Vue 2サポートの削除、バンドルの最適化、クリーンアップ (#4349)
v10.0.0-beta.3 2023/4/12
05781 - feat: autoDisposeの遅延サポート、#2252の修正 (#2276)
v9.6.0 2022/11/22
6886e - fix: 偽りの型エラーの修正 (#2431)
v9.3.1 2022/10/17
66f56 - fix: ドキュメントの自動破棄 (#2318)
v9.3.0 2022/9/26
2188b - 機能追加: 依存クエリへの対応 (#2103)

MITライセンスの下でリリースされています。