Skip to content

ORM for TypeScript and Google Cloud Firestore (Native mode)

License

Notifications You must be signed in to change notification settings

JackCuthbert/firetype

Repository files navigation

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 }
  }
}