Firebase
QvikChat provides support for using Firebase Firestore as a chat history store, cache store and API key store.
To use Firebase, you will need to initialize a Firebase app instance. You can use the getFirebaseApp
function provided by QvikChat to initialize a Firebase app.
Initialize Firebase App
To initialize a Firebase app, you can use the getFirebaseApp
function provided by QvikChat.
import { getFirebaseApp } from "@oconva/qvikchat/firebase";
// Initialize Firebase App
const firebaseApp = getFirebaseApp();
When calling the getFirebaseApp
function without passing any value for the credential
option, it will automatically try to use the GOOGLE_APPLICATION_CREDENTIALS
environment variable to locate the service account credentials file. So, please make sure to set the GOOGLE_APPLICATION_CREDENTIALS
environment variable before calling the getFirebaseApp
function without explicitly specifying the location of the service account credentials file.
GOOGLE_APPLICATION_CREDENTIALS=<path-to-your-firebase-service-account-key-file>
You can provide additional configurations to the getFirebaseApp
function. For example, you can specify the path to the service account credentials file and the provide project ID.
For information on how to create a Firebase service account key file, refer to the Firebase documentation (opens in a new tab).
import { getFirebaseApp, credential } from "@oconva/qvikchat/firebase";
// Initialize Firebase App with additional configurations
getFirebaseApp({
credential: credential.cert("./service-account.json"),
projectId: "your-project-id",
});
Using firebase-admin
You may choose to directly use the firebase-admin (opens in a new tab) package to initialize the Firebase app.
import { initializeApp, credential } from "firebase-admin";
// Initialize Firebase App (GOOGLE_APPLICATION_CREDENTIALS env var specified)
const firebaseAppDefault = initializeApp();
// Initialize Firebase App (provide configurations)
const firebaseAppWithParams = initializeApp({
credential: credential.cert("./service-account.json"),
projectId: "your-project-id",
});
For more information on how to initialize a Firebase app when using firebase-admin
, refer to the Firebase documentation (opens in a new tab).
Firestore Chat History Store
You can use Firestore as a chat history store. To do this, you can provide an instance of the FirestoreChatHistoryStore
class as the chatHistoryStore
when configuring the chat endpoint using defineChatEndpoint
function.
When creating an instance of the FirestoreChatHistoryStore
class, you need to provide the following options:
firebaseApp
: The Firebase app instance.collectionName
: The name of the collection that will store the chat history.
The collection storing the chat history will contain records that follow the structure of the ChatHistoryRecord
type. For more information, check Chat History Record.
import { getFirebaseApp } from "@oconva/qvikchat/firebase";
import { FirestoreChatHistoryStore } from "@oconva/qvikchat/history";
import { defineChatEndpoint } from "@oconva/qvikchat/endpoints";
// Initialize Firebase App
const firebaseApp = getFirebaseApp();
// Firestore chat history store
const firestoreChatHistoryStore = new FirestoreChatHistoryStore({
firebaseApp,
collectionName: "chat-history", // collection that will store chat history
});
// Chat Endpoint with Firestore chat history store
defineChatEndpoint({
endpoint: "chat",
enableChatHistory: true,
chatHistoryStore: firestoreChatHistoryStore,
});
Firestore Cache Store
You can use Firestore as a cache store. To do this, you need to provide the Firestore cache store as the cacheStore
to the endpoint configurations when defining the chat endpoint.
When creating an instance of the FirestoreCacheStore
class, you need to provide the following options:
firebaseApp
: The Firebase app instance.collectionName
: The name of the collection that will store the cache.
The collection storing the cache will contain records that follow the structure of the CacheRecord
interface. For more information, check Cache Store Interface.
import { getFirebaseApp } from "@oconva/qvikchat/firebase";
import { FirestoreCacheStore } from "@oconva/qvikchat/cache";
import { defineChatEndpoint } from "@oconva/qvikchat/endpoints";
// Initialize Firebase App
const firebaseApp = getFirebaseApp();
// Firestore cache store
const firestoreCacheStore = new FirestoreCacheStore({
firebaseApp,
collectionName: "cache", // collection that will store cache
});
// Chat Endpoint with Firestore cache store
defineChatEndpoint({
endpoint: "chat",
enableCache: true,
cacheStore: firestoreCacheStore,
});
Firestore API Key Store
You can use Firestore as an API key store. To do this, you need to provide the Firestore API key store as the apiKeyStore
to the endpoint configurations.
When creating an instance of the FirestoreAPIKeyStore
class, you need to provide the following options:
firebaseApp
: The Firebase app instance.collectionName
: The name of the collection that will store the API keys.
The collection storing the API keys should contain records that follow the structure of the APIKeyRecord
interface. For more information, check API Key Store Interface.
import { getFirebaseApp } from "@oconva/qvikchat/firebase";
import { FirestoreAPIKeyStore } from "@oconva/qvikchat/auth";
import { defineChatEndpoint } from "@oconva/qvikchat/endpoints";
// Initialize Firebase App
const firebaseApp = getFirebaseApp();
// Firestore API Key store
const firestoreCacheStore = new FirestoreAPIKeyStore({
firebaseApp,
collectionName: "api-keys", // collection where API keys are stored
});
// Chat Endpoint with Firestore API Key store
defineChatEndpoint({
endpoint: "chat",
enableAuth: true,
apiKeyStore: firestoreCacheStore,
});