Socket
Socket
Sign inDemoInstall

@google-cloud/firestore

Package Overview
Dependencies
Maintainers
1
Versions
145
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@google-cloud/firestore

Firestore Client Library for Node.js


Version published
Weekly downloads
1.1M
decreased by-36.89%
Maintainers
1
Weekly downloads
 
Created

What is @google-cloud/firestore?

The @google-cloud/firestore npm package is a client library for accessing Google Cloud Firestore, a NoSQL document database built for automatic scaling, high performance, and ease of application development. It provides functionality to perform various operations such as adding, updating, deleting documents, querying collections, and managing transactions.

What are @google-cloud/firestore's main functionalities?

Adding Documents

This code sample demonstrates how to add a document to a Firestore collection. It creates a new document with a specified ID ('alovelace') and sets the data for the document.

const { Firestore } = require('@google-cloud/firestore');
const firestore = new Firestore();
const document = firestore.collection('users').doc('alovelace');
document.set({
  first: 'Ada',
  last: 'Lovelace',
  born: 1815
});

Updating Documents

This code sample shows how to update an existing document in a Firestore collection. It updates the 'born' field of the document with the ID 'alovelace'.

const { Firestore } = require('@google-cloud/firestore');
const firestore = new Firestore();
const document = firestore.collection('users').doc('alovelace');
document.update({
  born: 1816
});

Deleting Documents

This code sample illustrates how to delete a document from a Firestore collection. It deletes the document with the ID 'alovelace'.

const { Firestore } = require('@google-cloud/firestore');
const firestore = new Firestore();
const document = firestore.collection('users').doc('alovelace');
document.delete();

Querying Collections

This code sample demonstrates how to query documents in a Firestore collection that meet certain criteria. It retrieves all documents where the 'born' field is greater than 1800.

const { Firestore } = require('@google-cloud/firestore');
const firestore = new Firestore();
firestore.collection('users').where('born', '>', 1800).get()
  .then(snapshot => {
    snapshot.forEach(doc => console.log(doc.id, '=>', doc.data()));
  });

Managing Transactions

This code sample shows how to use transactions to read and write data atomically. It reads a document and then updates it within a transaction, ensuring that the operation is atomic.

const { Firestore } = require('@google-cloud/firestore');
const firestore = new Firestore();
let documentRef = firestore.collection('users').doc('alovelace');
firestore.runTransaction(transaction => {
  return transaction.get(documentRef).then(doc => {
    if (!doc.exists) {
      throw 'Document does not exist!';
    }
    let newBorn = doc.data().born + 1;
    transaction.update(documentRef, { born: newBorn });
  });
}).then(() => console.log('Transaction successfully committed!'))
.catch(error => console.log('Transaction failed: ', error));

Other packages similar to @google-cloud/firestore

Keywords

FAQs

Package last updated on 25 Mar 2024

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc