firestore-timestamp-converter
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

firestore-timestamp-converter

NPM version CircleCI Dependency Status

🔥 ➡️

Easily convert between Firestore Timestamps and JS Dates

What problem does this solve?

Firestore stores all its dates/times with a custom Timestamp data type, which isn't understood by the majority of Javascript libraries etc.

This library makes it easy to convert between and 'think' in terms of JS Dates without having to keep remembering to call .toDate() on the Firestore outputs.

This works with all the normal methods for fetching data (get and onSnapshot) and doesn't interfere with the process of saving back to the Firestore document with set or add

Usage

interface TypeWithDates {
  dateValue: Date;
}

// Before TimestampConverter
// =========================
const snapshot = await db
  .collection("firestore-timestamp-converter")
  .doc(id)
  .get();

const data = snapshot.data();
data.dateValue instanceof Timestamp; // true
myFunc(data); // This function now has to remember that dateValue is a Timestamp not a JS date!

// With TimestampConverter
// =======================
const snapshot = await db
  .collection("firestore-timestamp-converter")
  .doc(id)
  .withConverter(new TimestampConverter<TypeWithDates>())
  .get(); // Now I will return a TypeWithDates instead of a DocumentData!

const data = snapshot.data();
data.dateValue instanceof Date; // true
myFunc(data); // This function receives a JS date!

Installation

Use your favourite package manager to add the package to your package.json

yarn add firestore-timestamp-converter

Readme

Keywords

none

Package Sidebar

Install

npm i firestore-timestamp-converter

Weekly Downloads

13

Version

1.0.7

License

MIT

Unpacked Size

9.45 kB

Total Files

8

Last publish

Collaborators

  • alexhayton