https://github.com/narumincho/typed-admin-firestore
stronger type declaration for firebase admin firestore.
https://github.com/narumincho/typed-admin-firestore
firebase firebase-admin firestore type-declarations
Last synced: about 2 months ago
JSON representation
stronger type declaration for firebase admin firestore.
- Host: GitHub
- URL: https://github.com/narumincho/typed-admin-firestore
- Owner: narumincho
- License: apache-2.0
- Created: 2019-12-10T06:41:35.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2023-07-20T05:02:48.000Z (over 2 years ago)
- Last Synced: 2025-11-27T12:00:59.781Z (3 months ago)
- Topics: firebase, firebase-admin, firestore, type-declarations
- Language: TypeScript
- Homepage:
- Size: 1.56 MB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# typed-admin-firestore
[](https://badge.fury.io/js/typed-admin-firestore)
[](https://gitpod.io#https://github.com/narumincho/typed-admin-firestore)
stronger type declaration for firebase admin firestore.
[type-firestore](https://github.com/narumincho/typed-firestore)
## admin
```ts
import * as admin from "firebase-admin";
import type * as typedAdminFirestore from "typed-admin-firestore";
const app = admin.initializeApp();
const firestoreInstance = (app.firestore() as unknown) as typedAdminFirestore.Firestore<{
user: { key: UserId; value: User; subCollections: {} };
music: { key: MusicId; value: Music; subCollections: {} };
project: {
key: ProjectId;
value: Project;
subCollections: {
data:
| {
key: "Body";
value: { text: string };
subCollections: {};
}
| {
key: "Comments";
value: Comments;
subCollections: {};
};
};
};
}>;
type UserId = string & { _userId: never };
type User = {
name: string;
age: number;
openIdConnect: {
providerName: string;
idInProvider: string;
};
likedMusics: Array;
createdAt: admin.firestore.Timestamp;
};
type MusicId = string & { _musicId: never };
type Music = {
title: string;
artist: UserId;
};
type ProjectId = string & { _projectId: never };
type Project = {
name: string;
createdBy: UserId;
};
type Comments = {
comments: ReadonlyArray<{
body: string;
createdBy: UserId;
createdAt: admin.firestore.Timestamp;
}>;
};
(async () => {
const userQuerySnapshotArray = await firestoreInstance
.collection("user")
.where("age", "<=", 20)
.get();
for (const userQueryDocumentSnapshot of userQuerySnapshotArray.docs) {
const data = userQueryDocumentSnapshot.data();
console.log("name", data.name); // Type hint !!!!!
console.log("providerName", data.openIdConnect.providerName); // Type hint !!!!!
for (const likedMusicId of data.likedMusics) {
firestoreInstance.collection("music").doc(likedMusicId); // no error !!!
firestoreInstance.collection("user").doc(likedMusicId); // error !!!
}
}
const commentDoc = await firestoreInstance
.collection("project") // autocomplete
.doc("6b9495528e9a12186b9c210448bdc90b" as ProjectId)
.collection("data") // autocomplete
.doc("Comments") // autocomplete
.get(); // returns DocumentSnapshot of Comments
})();
```