-
Notifications
You must be signed in to change notification settings - Fork 1
/
README.Rmd
143 lines (96 loc) · 3.61 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# frstore
<img src="inst/figures/logo.png" align="right" height="138" alt="frstore logo" />
<!-- badges: start -->
<!-- badges: end -->
{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:
```{r, eval=FALSE}
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`:
```{r, eval=FALSE}
FIREBASE_PROJECT_ID = "<Firebase-Project-ID>"
```
Furthermore, `frstore` requires an access token to interact with the Cloud Firestore database. [`frbs` package](https://github.com/kennedymwavu/frbs/tree/main) provides useful functions to sign up and sign in:
```{r signin, eval=FALSE}
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](https://cloud.google.com/firestore/docs/reference/rest). 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:
```{r create1, eval=FALSE}
library(frstore)
frstore_create_document("test/firstDoc", foo$idToken)
```
Create a document in a subcollection by providing the `data` argument:
```{r create2, eval=FALSE}
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:
```{r create3, eval=FALSE}
frstore_create_document("test/secondDoc", foo$idToken, data_list)
```
### Read data
Get document(s) with all fields:
```{r read1, eval=FALSE}
frstore_get("test", foo$idToken)
frstore_get("test/firstDoc", foo$idToken)
```
Get a specific field from a document:
```{r read2, eval=FALSE}
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:
```{r update, eval=FALSE}
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:
```{r delete, eval=FALSE}
frstore_delete("test/firstDoc/firstCollection/doc", foo$idToken)
```
### Run query
Three functions exist:
- `frstore_run_query`
- `frstore_run_query_parallel`
- `frstore_run_query_sequential`
## Acknowledgements
The authors of `frstore` are grateful to [Kennedy Mwavu](https://github.com/kennedymwavu) for the [frbs package](https://github.com/kennedymwavu/frbs/tree/main) that provided inspiration and code help in the development of `frstore`.