Introducing FirelordJS, Extremely High Precision Typescript Wrapper for Firestore Web V9
Tags: firebase, typescript, opensource, webdev
Published: 2022-03-27 21:49:03

![FirelordJS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ysco4ybzx1aaqp2ll2nw.png)
 
Introducing FirelordJS, Typescript Wrapper for Firestore Web V9 to help you write truly scalable Firestore code

[FirelordJS V1 Documentation](https://firelordjs.com/)

[Github](https://github.com/tylim88/Firelordjs)

And I believe this is the only proper wrapper for V9 available now.

But FirelordJS is more than just a proper wrapper, it is a powerful wrapper with a deep type-safe logic, to show you what I mean, here is a sneak peek of the source code:

![Where clause type logic](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z77ooturc6539yv2g2o4.png)
  
and this is the logic piece that governs only the Where clause, imagine the rest of the logic

Why do we need these many type checks?

Well, this is unavoidable in order to create something that is both powerful and easy to use, and most importantly the nature of the database is complicated, simple typing is not enough.

There are a lot of rules to accommodate in Firestore and FirelordJS try its best to deal with every known rules.

Enough talk, let's get started on how it works.

First defined your type, I call this Meta Type (naming is hard)

![Define MetaType](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vg6njb4nhip8mhbsv1wo.png)

You can define a deeply nested type, the maximum depth depends on Typescript and Firestore limitation.

then create Firelord reference (yup, naming is hard)

![create Firelord ref](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1mok4yqfn8lk2eo7sq2e.png)

## Basic operations

![basic operations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r03qqlvk3289ec12vpma.png)

## Query And OnSnapshot

![Query And onSnapshot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jlusl7z3hf85bh5xe0yj.png)
 

## Batch

![Batch](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3pbi0b9qgr05pt1j26af.png)
 

## Transaction

![Transaction](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ll1y53jl22bb4bpnh7qa.png)
 
It looks familiar right?

Yup, it is intended to look and feel like Firestore API. This reduces the learning curve.

FirelordJS type safe every single input in the code above, every single one, starting from collection path to query clauses.

And the images above contain 95% of FirelordJS manual, you have learned almost everything about FirelordJS in just one post.

**FirelordJS offer powerful type safe while remain easy to learn and use.**

Not only does FirelordJS safeguard the value type, but it also stops you from violating Firestore rules.

Rules are basically values that need to fulfil certain conditions and exception is thrown on runtime if you do not meet the conditions, for example, [Firestore Query Limitation](https://firebase.google.com/docs/firestore/query-data/queries#query_limitations)

let's take a look at one rule that is convoluted and seemly impossible to type: the field path sentinel documentId

>*When querying a collection group by documentId(), the value provided must result in a valid document path, and should has an odd number of segments.*

(this exceptions is not something that is properly documented)

FirelordJS make this possible:

![FirelordJS counts and compares the number of slashes on type level and displays it in hint.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uf9qk39w89mprf5x54j0.png)

Not only FirelordJS make sure the number of slashes is correct, but it also checks whether your document path is correct, based on what you defined in Meta Type.

And this is just one of the many rules that FirelordJS takes care of. You can find out more by reading the doc.

Whether you are a developer who is ok with safe enough type or a developer that wants to write truly scalable Firestore code, FirelordJS can help you deliver it with minimum effort on your side.

Actually, you need more than just type-safe to write scalable code, but you get what I mean.

It is not easy to create high precision Typescript wrapper, and FirelordJS took me hundreds of hours to make, it is a chaotic journey.

I hope you find it useful, enjoy!