Welcome to your journey of integrating a strongly typed Firestore framework using TypeScript! Fireschema simplifies data modeling and security rules management, allowing developers to convey their intent without compromising on type safety.
Features of Fireschema
- Strong Type Safety for Firestore: Automatically provides type information to nested documents with no need for unsafe type assertions.
- Security Rules Generation: Generate
firestore.rules
files to include data type validation and access control directly from your schema. - React Hooks: Benefit from real-time updates using React Hooks.
- Type Safety for Cloud Functions: Automatically provides type information to snapshot data on Firestore Trigger Functions and guards request/response data types for HTTPS callable functions.
Requirements
To start using Fireschema, you’ll need TypeScript version 4.4 or higher.
Installation
Simply execute the following commands in your terminal:
yarn add fireschema firebase firebase-admin firebase-functions zodyarn add -D typescript ts-node
Setup
Since Fireschema version 5, you no longer need to compile codes via custom transformers, streamlining your setup process considerably.
Usage
Creating Your Firestore Schema
Defining your schema is the first step, which must be the default export. Here’s a simple analogy to understand the schema transformation:
Imagine you are a chef creating a recipe book. Each recipe requires specific ingredients and preparation steps. Similarly, in Fireschema, your data model defines the essential components (like ingredients) required for your Firestore database. It ensures that each document has the same structure, preventing chaos in your data kitchen.
import z from "zod"
import { DataModel, FirestoreModel } from "fireschema"
export const UserType = z.object({
name: z.string(),
displayName: z.union([z.string(), z.null()]),
age: z.number().int(),
timestamp: timestampType(),
options: z.object({ a: z.boolean() }).optional(),
})
Generating Firestore Rules
After defining your schema, generate the firestore.rules
file using:
yarn fireschema rules path-to-schema.ts
Reading and Writing Collections and Documents
The Fireschema interface supports both the Web SDK and Admin SDK, making it easy to communicate with your Firestore data.
const usersRef = firestore.collection("users")
const userRef = usersRef.doc(userId)
await userRef.set({ name: "test", age: 27 })
React Hooks for Real-Time Updates
Get live updates with React Hooks, keeping your components in sync automatically!
const posts = useTypedCollection(postsRef)
return (
{posts.data.map((post, i) => (
- {post.text}
))}
)
Troubleshooting
While Fireschema aims to simplify your workflow, you may face some issues along the way. Here are some troubleshooting tips:
- Cannot Read from Firestore: Ensure your Firestore security rules allow read access for your defined paths.
- Type Errors: Confirm that your data model matches the structure of the data in Firestore.
- Cloud Function Not Invoked: Verify that your function trigger is set correctly and deployed.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Final Thoughts
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.