Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bluespark

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bluespark

> Firestore library for TypeScript

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
increased by50%
Maintainers
1
Weekly downloads
 
Created
Source

BlueSpark

Firestore library for TypeScript

Installation

yarn add firebase firebase-admin bluespark

Initialization

import firebase, { firestore } from 'firebase/app'
import 'firebase/firestore'
import { Blue, Spark } from 'bluespark'

const app = firebase.initializeApp({
    apiKey: '### FIREBASE API KEY ###',
    authDomain: '### FIREBASE AUTH DOMAIN ###',
    projectId: '### CLOUD FIRESTORE PROJECT ID ###',
})

const dbInstance = app.firestore()

Define collections

const createCollections = <F extends Blue.Firestore>(instance: F) => {
    type C = ReturnType<F['collection']>
    type Q = ReturnType<F['collectionGroup']>

    return {
        posts: () => instance.collection('posts') as C,
    }
}

const collection = createCollections(dbInstance)
// const collectionAdmin = createCollections(dbInstanceAdmin)

Define models

type IPost = Blue.Interface<{
    number: number
    date: Blue.IO<Blue.Timestamp, Date | Blue.FieldValue>
    text: string
    tags: string[]
}>

const Post = Spark<IPost>()

Usage

Get document

const post = await Post.get(collection.posts().doc('doc-id'))

// with React Hooks
const { data: post, loading, error } = useSDoc(
    Post,
    collection.posts().doc('doc-id'),
)

// passes
expectType<{
    _createdAt: Blue.Timestamp
    _updatedAt: Blue.Timestamp
    _id: string
    _ref: Blue.DocRef
    number: number
    date: Blue.Timestamp
    text: string
    tags: string[]
}>(post!)

Get collection/query

const { array, map } = await Post.getCollection(collection.posts())

// with React Hooks
const { array, map, query, loading, error } = useSCollection(
    Post,
    collection.posts(),
)

// passes
expectType<{
    _createdAt: Blue.Timestamp
    _updatedAt: Blue.Timestamp
    _id: string
    _ref: Blue.DocRef
    number: number
    date: Blue.Timestamp
    text: string
    tags: string[]
}>(array[0])

// passes
expectType<{
    _createdAt: Blue.Timestamp
    _updatedAt: Blue.Timestamp
    _id: string
    _ref: Blue.DocRef
    number: number
    date: Blue.Timestamp
    text: string
    tags: string[]
}>(map.get('doc-id')!)

Create document

await Post.create(collection.posts().doc('doc-id'), {
    number: 17,
    date: firestore.FieldValue.serverTimestamp(), // Date | Blue.FieldValue
    text: 'text',
    tags: ['a', 'b'],
})

Update document

await Post.update(collection.posts().doc('doc-id'), {
    text: 'new-text',
})

FAQs

Package last updated on 21 Oct 2019

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