Skip to content
This repository was archived by the owner on Apr 30, 2026. It is now read-only.

Latest commit

 

History

History
77 lines (56 loc) · 2.17 KB

File metadata and controls

77 lines (56 loc) · 2.17 KB

firetype

NPM Version Release Test JavaScript Style Guide

ORM for TypeScript and Google Cloud Firestore.

Usage

Firetype requires TypeScript to work as intended.

npm install -D typescript

Install the firetype and @google-cloud/firestore packages.

npm install firetype @google-cloud/firestore

Create an interface that describes your document and extends the Entity type.

import { Entity } from 'firetype'

interface User extends Entity {
  firstName: string
  lastName: string
}

The Entity type adds an id property with type string. This is used as the Document ID in Cloud Firestore and inserted into the document as id.

Use the base repository with built-in methods.

import { Repository } from 'firetype'
import { User } from './entity/User'

const userRepository = new Repository<User>('root_collection_name')

await userRepository.find('123')

await userRepository.createOrUpdate({
  id: uuidv4(),
  firstName: 'Jack',
  lastName: 'Cuthbert'
})

Custom repository

Use a customm repository to add additional methods to the ORM. this.firestore and this.collection are available for creating custom queries and operations.

import { Firestore } from '@google-cloud/firestore'
import { Repository } from 'firetype'
import { User } from './entity/User'

class TeamRepository extends Repository<User> {
  constructor (firestore?: Firestore) {
    super('root_collection_name', firestore) {
  }

  async setPremiumStatus(user: User, isPremium: boolean): Promise<User> {
    const ref = this.firestore.doc(this.createDocRef(user.id))
    const update: Partial<User> = { isPremium }
    await ref.set(update, { merge: true })
    return { ...user, ...update }
  }
}