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

@star__hoshi/tart

v0.11.0

Published

A thin wrapper for Cloud Functions.

Downloads

9

Readme

tart

npm version Build Status Codacy Badge License: MIT

Tart is a thin wrapper for Cloud Functions.

Let's define the model and write Cloud Functions with TypesScript.

Sample

You can write like this.

import * as Tart from '@star__hoshi/tart'

Tart.initialize(admin.firestore())

interface User extends Tart.Timestamps {
  name: string
}

export const updateUser = functions.firestore.document('user/{userId}')
  .onCreate(async event => {
    const user = new Tart.Snapshot<User>(event.data)
    console.log(user.data.name) // => old name
    console.log(user.ref) // => DocumentReference

    await user.update({ name: 'new name'})
    console.log(user.data.name) // => new name

    return undefined
})

Installation

npm install @star__hoshi/tart --save
yarn add @star__hoshi/tart

Usage

Initialize

import * as Tart from '@star__hoshi/tart'
import * as admin from 'firebase-admin'

admin.initializeApp(<admin.AppOptions>functions.config().firebase)

// Tart expects timestampsInSnapshots to be 'true'
admin.firestore().settings({ timestampsInSnapshots: true })

// both admin sdk and cloud functions are same interface.
Tart.initialize(admin.firestore())

Define Interface

You have to extend Tart.Timestamps.

interface User extends Tart.Timestamps {
  name: string
}

interface Game extends Tart.Timestamps {
  price: string
}

Snapshot Type

Snapshot has 2 local variables.

  • ref
    • DocumentReference
  • data
    • T extends Tart.Timestamps
const user = new Tart.Snapshot<User>(snapshot)
console.log(user.data) // => Same as snapshot.data()
console.log(user.ref) // => Same as snapshot.ref

Data Management

Save

const data: User = { name: 'test' }
const user = Tart.Snapshot.makeNotSavedSnapshot('user', data)

// Save a document
await user.save()

// Batched writes
const batch = admin.firestore().batch()
user.saveWithBatch(batch)
await batch.commit()

Save Reference Collection

[Reference|Nedted] Collection's description is here.

const user = Tart.Snapshot.makeNotSavedSnapshot<User>('user', { name: 'test' })
const game = Tart.Snapshot.makeNotSavedSnapshot<Game>('game', { price: 1000 })

const batch = admin.firestore().batch()
user.saveWithBatch(batch)
user.saveReferenceCollectionWithBatch(batch, 'games', game.ref)
user.saveNestedCollectionWithBatch(batch, 'nestedgames', game.ref)
game.saveWithBatch(batch)
await batch.commit()

Get

Pass path or ref as argument.

const savedUser = await Tart.fetch<User>('user', 'id')
const savedUser = await Tart.fetch<User>(userDocumentReference)

Refresh

Reload the data.

const savedUser = await Tart.fetch<User>(userDocumentReference)
await savedUser.refresh()

Update

const savedUser = await Tart.fetch<User>('user', 'id')

// Update a document
await savedUser.update({ name: 'new name' })

// Batched writes
savedUser.saveWithBatch(batch)
savedUser.updateWithBatch(batch, { name: 'new name' })
await savedUser.commit()

Delete

const savedUser = await Tart.fetch<User>('user', 'id')

// Delete a document
await savedUser.delete()

// Batched writes
savedUser.deleteWithBatch(batch)
await savedUser.commit()