firestore-wrapper
Version:
Typing your firestore database and avoid access errors on development
62 lines (42 loc) • 2.11 kB
Markdown
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.
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;
}
```
```
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);
```
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
