Skip to content

R Wrapper for Google Cloud Firestore REST API

License

Notifications You must be signed in to change notification settings

durraniu/frstore

 
 

Repository files navigation

frstore

frstore logo

{frstore} is an R interface to perform the create, read, update, and delete (CRUD) operations on the Cloud Firestore database via REST API.

Installation

You can install the development version of frstore like so:

remotes::install_github("Presage-Group/frstore")

Usage

frstore requires the Firebase project ID that you can obtain from the project settings page. Put the Firebase project ID in your .Renviron as FIREBASE_PROJECT_ID:

FIREBASE_PROJECT_ID = "<Firebase-Project-ID>"

Furthermore, frstore requires an access token to interact with the Cloud Firestore database. frbs package provides useful functions to sign up and sign in:

library(frbs)
# Sign up via Firebase authentication:
frbs_sign_up(email = "<EMAIL>", password = "<PASSWORD>")
# Sign in:
foo <- frbs_sign_in(email = "<EMAIL>", password = "<PASSWORD>")

foo$idToken provides the access token.

Functions in this package are named similar to the methods described in the REST resource v1beta1.projects.databases.documents in the Cloud Firestore REST API docs. All functions have the prefix frstore_. Currently, only these methods are implemented as functions:

  • createDocument
  • delete
  • get
  • patch
  • runQuery

Examples

Create document(s)

Create a document without specifying data:

library(frstore)
frstore_create_document("test/firstDoc", foo$idToken)

Create a document in a subcollection by providing the data argument:

data_list <- list(
  fields = list(
    age = list("integerValue" = 36),
    name = list("stringValue" = "merry")
 )
)
frstore_create_document("test/firstDoc/firstCollection/doc", foo$idToken, data_list)

Create a document in the main collection:

frstore_create_document("test/secondDoc", foo$idToken, data_list)

Read data

Get document(s) with all fields:

frstore_get("test", foo$idToken)
frstore_get("test/firstDoc", foo$idToken)

Get a specific field from a document:

frstore_get("test/firstDoc", foo$idToken, fields = c("age"))

Update data

Suppose there is an existing document at test/firstDoc/firstCollection/doc and we want to update it with new data:

data_list <- list(
   fields = list(
     age = list("integerValue" = 3600),
     name = list("stringValue" = "merryyyy")
   )
)
frstore_patch("test/firstDoc/firstCollection/doc", foo$idToken, data_list)

Delete data

Suppose there is an existing document at test/firstDoc/firstCollection/doc and we want to delete it:

frstore_delete("test/firstDoc/firstCollection/doc", foo$idToken)

Run query

Suppose there is an existing subcollection at test/firstDoc/firstCollection and we want to get all documents where the name field matches "merry":

frstore_run_query(
  "test/firstDoc/firstCollection",
  foo$idToken,
  field = "name",
  operation = "EQUAL",
  value_type = "stringValue",
  value = "merry"
)

Acknowledgements

The authors of frstore are grateful to Kennedy Mwavu for the frbs package that provided inspiration and code help in the development of frstore.

About

R Wrapper for Google Cloud Firestore REST API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • R 100.0%