跳转到正文

快速入门

GitHub

你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:

终端窗口
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

然后使用以下 prompt:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-firebase-firestore` plugin in my project.

如果你更想手动设置,请运行以下命令安装插件,并按照下方各平台的说明操作:

终端窗口
bun add @capgo/capacitor-firebase-firestore
npx cap sync
import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
import { plugin } from '@capgo/capacitor-firebase-firestore';
const result = await plugin.addDocument({
reference: 'users',
data: {},
});
console.log(result);

示例:addCollectionGroupSnapshotListener

章节标题:“示例:addCollectionGroupSnapshotListener”
import { plugin } from '@capgo/capacitor-firebase-firestore';
const result = await plugin.addCollectionGroupSnapshotListener({ reference: 'reference' }, (event, error) => {
console.log(event);
});
console.log(result);
import { plugin } from '@capgo/capacitor-firebase-firestore';
const result = await plugin.addCollectionSnapshotListener({ reference: 'reference' }, (event, error) => {
console.log(event);
});
console.log(result);
  • 原生同步:运行 npx cap sync 在插件升级后。
  • 唯一可信来源:信任 src/definitions.ts 在 GitHub 仓库中。

向集合中添加包含给定数据的新文档。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.addDocument({} as AddDocumentOptions);

写入指定引用所指向的文档。 如果该文档尚不存在,则会自动创建。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.setDocument({} as SetDocumentOptions);

读取指定引用所指向的文档。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.getDocument({} as GetDocumentOptions);

更新指定引用所指向文档中的字段。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.updateDocument({} as UpdateDocumentOptions);

删除指定引用所指向的文档。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.deleteDocument({} as DeleteDocumentOptions);

将多个写入操作作为单个批次执行。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.writeBatch({} as WriteBatchOptions);

读取指定引用所指向的集合。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.getCollection({} as GetCollectionOptions);

读取指定引用所指向的集合组。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.getCollectionGroup({} as GetCollectionGroupOptions);

获取集合中的文档数量。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.getCountFromServer({} as GetCountFromServerOptions);

清除持久化存储。包括待处理的写入和缓存的文档。

必须在应用关闭后或应用首次初始化时调用。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.clearPersistence();

重新启用网络使用。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.enableNetwork();

禁用网络使用。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.disableNetwork();

将你的 App 配置为与 Firestore emulator 通信。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.useEmulator({} as UseEmulatorOptions);

为文档快照事件添加监听器。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.addDocumentSnapshotListener({} as AddDocumentSnapshotListenerOptions, {} as AddDocumentSnapshotListenerCallback<T>);

为集合快照事件添加监听器。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.addCollectionSnapshotListener({} as AddCollectionSnapshotListenerOptions, {} as AddCollectionSnapshotListenerCallback<T>);

为集合组快照事件添加监听器。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.addCollectionGroupSnapshotListener({} as AddCollectionGroupSnapshotListenerOptions, {} as AddCollectionGroupSnapshotListenerCallback<T>);

移除文档或集合快照事件的监听器。

import { FirebaseFirestore } from '@capgo/capacitor-firebase-firestore';
await FirebaseFirestore.removeSnapshotListener({} as RemoveSnapshotListenerOptions);
export interface AddDocumentOptions {
/**
* The reference as a string, with path components separated by a forward slash (`/`).
*
* @since 5.2.0
* @example 'users'
*/
reference: string;
/**
* An object containing the data for the new document.
*
* @since 5.2.0
* @example { first: 'Alan', last: 'Turing', born: 1912 }
*/
data: DocumentData;
}
export interface AddDocumentResult {
/**
* The reference of the newly added document.
*
* @since 5.2.0
*/
reference: DocumentReference;
}
export interface SetDocumentOptions {
/**
* The reference as a string, with path components separated by a forward slash (`/`).
*
* @since 5.2.0
* @example 'users/Aorq09lkt1ynbR7xhTUx'
*/
reference: string;
/**
* An object containing the data for the new document.
*
* @since 5.2.0
* @example { first: 'Alan', last: 'Turing', born: 1912 }
*/
data: DocumentData;
/**
* Whether to merge the provided data with an existing document.
*
* @since 5.2.0
* @example true
* @default false
*/
merge?: boolean;
}
export interface DocumentData {
/**
* A mapping between a field and its value.
*
* @since 5.2.0
*/
[field: string]: any;
}
export interface GetDocumentOptions {
/**
* The reference as a string, with path components separated by a forward slash (`/`).
*
* @since 5.2.0
*/
reference: string;
}
export interface GetDocumentResult<T> {
/**
* The current document contents.
*
* @since 5.2.0
*/
snapshot: DocumentSnapshot<T>;
}
export interface UpdateDocumentOptions {
/**
* The reference as a string, with path components separated by a forward slash (`/`).
*
* @since 5.2.0
*/
reference: string;
/**
* An object containing the data for the new document.
*
* @since 5.2.0
* @example { first: 'Alan', last: 'Turing', born: 1912 }
*/
data: DocumentData;
}
export interface DeleteDocumentOptions {
/**
* The reference as a string, with path components separated by a forward slash (`/`).
*
* @since 5.2.0
*/
reference: string;
}
export interface WriteBatchOptions {
/**
* The operations to execute in the batch.
*
* @since 6.1.0
*/
operations: WriteBatchOperation[];
}
export interface GetCollectionOptions {
/**
* The reference as a string, with path components separated by a forward slash (`/`).
*
* @since 5.2.0
*/
reference: string;
/**
* The filter to apply.
*
* @since 5.2.0
*/
compositeFilter?: QueryCompositeFilterConstraint;
/**
* Narrow or order the set of documents to retrieve, but do not explicitly filter for document fields.
*
* @since 5.2.0
*/
queryConstraints?: QueryNonFilterConstraint[];
}
export interface GetCollectionResult<T> {
/**
* The documents in the collection.
*
* @since 5.2.0
*/
snapshots: DocumentSnapshot<T>[];
}
export interface GetCollectionGroupOptions {
/**
* The reference as a string, with path components separated by a forward slash (`/`).
*
* @since 5.2.0
*/
reference: string;
/**
* The filter to apply.
*
* @since 5.2.0
*/
compositeFilter?: QueryCompositeFilterConstraint;
/**
* Narrow or order the set of documents to retrieve, but do not explicitly filter for document fields.
*
* @since 5.2.0
*/
queryConstraints?: QueryNonFilterConstraint[];
}

本页面由插件的 src/definitions.ts生成。当上游的公共 API 发生变化时,请重新运行 sync。

如果你正在使用 快速入门 要规划 dashboard 和 API 操作,请将其连接到 API 概览 关于 API Overview 中的实现细节, 简介 关于 Introduction 中的实现细节, API 密钥 有关 API Keys 的实现细节, 设备 有关 Devices 的实现细节,以及 Bundle 有关 Bundles 中的实现细节。