New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@adviise/types

Package Overview
Dependencies
Maintainers
2
Versions
71
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@adviise/types

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
2
Weekly downloads
 
Created
Source

Type Safety When Writing and Reading From Firestore

We want to ensure that certain metadata is included in every single document that is created, and updated, so a series of base interfaces have been created for use when creating a new firestore datatype. Adherance to this template will also help ensure data consistency when reading and writing to documents.

Creating A New Firestore Document Type

// import the base interfaces
import { DocData, Metadata, CreateMetadata, UpdateMetadata } from './base';

// Create a generic interface that extends DocData.
// This is where the standard document fields will go.
interface MyDocBase<meta> extends DocData<meta> {
    myField1: string;
    myField2: number;
    myField3: {
        mySubfield: string,
    };
}

// Create a type a new type from this interface that
// passes "Metadata" into the generic slot. This is the type
// you will recieve when fetching from Firestore.
export type MyDocData = MyDocBase<Metadata>;

// create another type but pass in the "CreateMetadata" instead.
// this is the type that you will use when creating a new record
export type MyDocCreateData = MyDocBase<CreateMetadata>;

// Lastly create an interface that extends a Partial
// version of the MyDocBase<UpdateMetadata> and add _metadata: UpdateMetadata
// as a required field. This is the interface that will be used when sending
// update requests
export interface MyDocUpdateData extends Partial<MyDocBase<UpdateMetadata>> {
    _metadata: UpdateMetadata;
}

Using Your New Type

// fetching from firestore
db.collection('my-new-collection').doc('my-id').get().then(doc => {
    // cast doc.data() as MyDocData
    // now you will get autocomplete for doc fields
    const data = doc.data() as MyDocData;
})

// creating a doc
const data: MyDocCreateData = {
    // you will be forced to add all the fields
    // including the _metadata fields here
}
db.collection('my-new-collection').doc('my-id').add(add)

// updating a doc
const updateData: MyDocUpdateData = {
    // you will be forced to add the relevant update _metadata
    // and prevented from adding non-existent fields
    _metadata: {
        updated_at: 'some-timestamp',
        updated_by: 'some-user-id'
    }
    myField1: 'test', // this works
    myNonExistentField: 'test' // this will throw a ts error
}
db.collection('my-new-collection').doc('my-id').update(updateData)

FAQs

Package last updated on 31 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc