npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

firebase-decorators

v1.0.3

Published

This module provides a set of decorators for your firebase project.

Downloads

180

Readme

Firebase Decorators

contributions welcome HitCount Coverage Status

Write cleaner code using decorators in your next firebase project.

(Work in progress)

Usage

Installation

Important Typescript > 5.0.0 This package uses the latest typescript decorator APIs and thus requires typescript version > 5.0

$ npm install --save firebase-decorators

Initialize Firebase app

import  { initializeApp }  from  'firebase/app';  
  
const firebaseConfig =  {  //...  
};  
  
const app = initializeApp(firebaseConfig);

Admin SDK

import {initializeApp} from  "firebase-admin/app";
    
const firebaseConfig =  {  //...  
};  
  
const app = initializeApp(firebaseConfig);

Create a model

Create a model which reflects the data on the database.

Let's create a user model which we will use to store user data in Firestore. We will extend the Class with BaseFirebaseModel.

Class User extends BaseFirebaseModel {

	userId!:  string;

	role!:  "USER"  |  "ADMIN";

	joinDate!:  number;

	constructor(userId:string) {
		super();
		this.userId  =  userId;
		// initialize other fields as required.
	}
}

@PrimaryKey Decorator

The document id to read the document from can be marked as the primary key in the model.

This field should be initialized for all the functionalities to work, Otherwise an error will be thrown during runtime. The field can be initialized either using the constructor or directly.

Constructor

Class User extends BaseFirebaseModel {

	@PrimaryKey()
	private  userId!:  string;

	role!:  "USER"  |  "ADMIN";

	joinDate!:  number;

	constructor(userId:string) {
		super();
		this.userId  =  userId;
		// initialize other fields as required.
	}
}

Direct initialization

Class User extends BaseFirebaseModel {

	@PrimaryKey()
	userId!:  string = "documentId";

	role!:  "USER"  |  "ADMIN";

	joinDate!:  number;
}

@DataKey Decorator

Mark the fields of the model using DataKey decorator.

Class User extends BaseFirebaseModel {

	@PrimaryKey()
	private  userId!:  string;
	
	@DataKey()
	role!:  "USER"  |  "ADMIN";
	
	@DataKey("join_date")
	joinDate!:  number;

	// since this field is not marked,
	// it will be ignored during any read/write operation.
	thisFieldWillBeIgnored! : string;

	constructor(userId:string) {
		super();
		this.userId  =  userId;
		// initialize other fields as required.
	}
}

The Firestore model fields will be mapped to the model fields join_date -> joinDate (since we explicitly pass the remote key name) role -> role (since no value for the remote key was passed) thisFieldWillBeIgnored -> this field will be ignored since it is not marked as a data field.

@FirestoreModel Decorator

Finally mark the model with @FirestoreModel and provide the collection name and whether to use the admin SDK or not. The value for useAdminSdk should be set depending on whether the admin app was initialized or not. Firebase app Initialization

@FirestoreModel("user",false)
Class User extends BaseFirebaseModel {

	@PrimaryKey()
	private userId!:  string;
	
	@DataKey()
	role!:  "USER"  |  "ADMIN";
	
	@DataKey("join_date")
	joinDate!:  number;
	
	// since this field is not marked,
	// it will be ignored during any read/write operation.
	thisFieldWillBeIgnored! : string;

	constructor(userId:string) {
		super();
		this.userId  =  userId;
		// initialize other fields as required.
	}
}

Use the model

Simply use the model to read, write and sync data to the database.

const user = new User("this_is_the_user_id");

// wait for model to get initialized.
// this value resolves when the model has synced
// its data with firestore document
await user.ready;

// set values into the model;;
user.role = "USER"
user.joinDate = 1234

// write the data to the database
// this will create a new document if it doesnt exist
// or overwrtire old document
await user.write()

// sync data from the remote
await user.sync()

// change any value
user.role = "ADMIN"

// update the model
await user.update()
 

New features to come very soon!!

Contributers and feature requests are welcome!!