firestore-wrapper
v0.1.8
Published
Typing your firestore database and avoid access errors on development
Downloads
90
Maintainers
Readme
Purpose
typescript helps to avoid numerous errors at development time and for sure firebase is one of the most used tools among developers, be it for mobile, web projects and etc.
The biggest problem is that we have to force data types when we access them through the firebase and this can cause numerous errors.
The firestore-wrapper has the purpose of facilitating and speeding up the life of the developer, being necessary to type your database only once.
How to use
1) Describe your database
In some file, describe your database using a typescript, look like this:
export interface Database {
User: {[user_id: string]: User};
Locations: {[location_name: string]: Locations};
}
interface User {
name: string,
email: string,
nick: string
}
interface Locations {
latitude: number;
longitude: number;
single: string;
}
2) Initialize your firebase-admin and associate with wapper
import * as firebase from "firebase-admin";
import Credentials from "../Credentials.json";
import { root } from "firestore-wrapper";
import databaseSchema from './database';
const firebaseSdk = firebase.initializeApp({
credential: firebase.credential.cert(Credentials as firebase.ServiceAccount),
databaseURL: "https://any-project.firebaseio.com"
});
const db = root<databaseSchema>(firebaseSdk.firestore() as any);
3) Now, all firestore functions is typing with your database schema
While you access your database, the wrapper indicates if the path is right
When you retrieve data, he cames with right type
Even inside transactions, you keep the types