@@ -3,31 +3,65 @@ import { Collection } from '../collection'
3
3
import { Ref , ref } from '../ref'
4
4
import { Doc , doc } from '../doc'
5
5
import { unwrapData } from '../data'
6
- import set from '../set'
7
- import update , { ModelUpdate } from '../update'
6
+ import { ModelUpdate } from '../update'
8
7
import { Field } from '../field'
9
- import clear from '../clear'
10
-
11
- export type BatchAPI = {
12
- set : typeof set
13
- update : typeof update
14
- clear : typeof clear
15
- commit : ( ) => Promise < void >
16
- }
17
8
9
+ /**
10
+ * @returns batch API (set, update, clear, commit)
11
+ *
12
+ * @example
13
+ * import { batch, collection } from 'typesaurus'
14
+ *
15
+ * type Counter = { count: number }
16
+ * const counters = collection<Counter>('counters')
17
+ *
18
+ * const { set, update, clear, commit } = batch()
19
+ *
20
+ * for (let count = 0; count < 500; count++) {
21
+ * set(counters, count.toString(), { count })
22
+ * }
23
+ *
24
+ * commit().then(() => console.log('Done!'))
25
+ */
18
26
export function batch ( ) {
19
- const b = firestore ( ) . batch ( )
20
-
21
- // set
27
+ const firestoreBatch = firestore ( ) . batch ( )
22
28
29
+ /**
30
+ * @param ref - the reference to the document to set
31
+ * @param data - the document data
32
+ */
23
33
function set < Model > ( ref : Ref < Model > , data : Model ) : Doc < Model >
24
34
35
+ /**
36
+ * @param collection - the collection to set document in
37
+ * @param id - the id of the document to set
38
+ * @param data - the document data
39
+ */
25
40
function set < Model > (
26
41
collection : Collection < Model > ,
27
42
id : string ,
28
43
data : Model
29
44
) : Doc < Model >
30
45
46
+ /**
47
+ * Sets a document to the given data.
48
+ *
49
+ * @returns the document
50
+ *
51
+ * @example
52
+ * import { batch, collection } from 'typesaurus'
53
+ *
54
+ * type Counter = { count: number }
55
+ * const counters = collection<Counter>('counters')
56
+ *
57
+ * const { set, commit } = batch()
58
+ *
59
+ * for (let count = 0; count < 500; count++) {
60
+ * set(counters, count.toString(), { count })
61
+ * }
62
+ *
63
+ * commit()
64
+ */
31
65
function set < Model > (
32
66
collectionOrRef : Collection < Model > | Ref < Model > ,
33
67
idOrData : string | Model ,
@@ -53,29 +87,67 @@ export function batch() {
53
87
. doc ( id )
54
88
// ^ above
55
89
// TODO: Refactor code above and below because is all the same as in the regular set function
56
- b . set ( firestoreDoc , unwrapData ( data ) )
90
+ firestoreBatch . set ( firestoreDoc , unwrapData ( data ) )
57
91
// v below
58
92
return doc ( ref ( collection , id ) , data )
59
93
}
60
94
61
- // update
62
-
95
+ /**
96
+ * @param collection - the collection to update document in
97
+ * @param id - the id of the document to update
98
+ * @param data - the document data to update
99
+ */
63
100
function update < Model > (
64
101
collection : Collection < Model > ,
65
102
id : string ,
66
103
data : Field < Model > [ ]
67
104
) : void
68
105
106
+ /**
107
+ * @param ref - the reference to the document to set
108
+ * @param data - the document data to update
109
+ */
69
110
function update < Model > ( ref : Ref < Model > , data : Field < Model > [ ] ) : void
70
111
112
+ /**
113
+ * @param collection - the collection to update document in
114
+ * @param id - the id of the document to update
115
+ * @param data - the document data to update
116
+ */
71
117
function update < Model > (
72
118
collection : Collection < Model > ,
73
119
id : string ,
74
120
data : ModelUpdate < Model >
75
121
) : void
76
122
123
+ /**
124
+ * @param ref - the reference to the document to set
125
+ * @param data - the document data to update
126
+ */
77
127
function update < Model > ( ref : Ref < Model > , data : ModelUpdate < Model > ) : void
78
128
129
+ /**
130
+ * @returns void
131
+ *
132
+ * @example
133
+ * import { batch, collection } from 'typesaurus'
134
+ *
135
+ * type Counter = { count: number, meta: { updatedAt: number } }
136
+ * const counters = collection<Counter>('counters')
137
+ *
138
+ * const { update, commit } = batch()
139
+ *
140
+ * for (let count = 0; count < 500; count++) {
141
+ * update(counters, count.toString(), { count: count + 1 })
142
+ * // or using key paths:
143
+ * update(counters, count.toString(), [
144
+ * ['count', count + 1],
145
+ * [['meta', 'updatedAt'], Date.now()]
146
+ * ])
147
+ * }
148
+ *
149
+ * commit()
150
+ */
79
151
function update < Model > (
80
152
collectionOrRef : Collection < Model > | Ref < Model > ,
81
153
idOrData : string | Field < Model > [ ] | ModelUpdate < Model > ,
@@ -110,15 +182,37 @@ export function batch() {
110
182
: data
111
183
// ^ above
112
184
// TODO: Refactor code above because is all the same as in the regular update function
113
- b . update ( firebaseDoc , unwrapData ( updateData ) )
185
+ firestoreBatch . update ( firebaseDoc , unwrapData ( updateData ) )
114
186
}
115
187
116
- // clear
117
-
188
+ /**
189
+ * @param collection - the collection to remove document in
190
+ * @param id - the id of the documented to remove
191
+ */
118
192
function clear < Model > ( collection : Collection < Model > , id : string ) : void
119
193
194
+ /**
195
+ * @param ref - the reference to the document to remove
196
+ */
120
197
function clear < Model > ( ref : Ref < Model > ) : void
121
198
199
+ /**
200
+ * Removes a document.
201
+ *
202
+ * @example
203
+ * import { batch, collection } from 'typesaurus'
204
+ *
205
+ * type Counter = { count: number }
206
+ * const counters = collection<Counter>('counters')
207
+ *
208
+ * const { clear, commit } = batch()
209
+ *
210
+ * for (let count = 0; count < 500; count++) {
211
+ * clear(counters, count.toString())
212
+ * }
213
+ *
214
+ * commit()
215
+ */
122
216
function clear < Model > (
123
217
collectionOrRef : Collection < Model > | Ref < Model > ,
124
218
maybeId ?: string
@@ -140,11 +234,16 @@ export function batch() {
140
234
. doc ( id )
141
235
// ^ above
142
236
// TODO: Refactor code above because is all the same as in the regular update function
143
- b . delete ( firebaseDoc )
237
+ firestoreBatch . delete ( firebaseDoc )
144
238
}
145
239
240
+ /**
241
+ * Starts the execution of the operations in the batch.
242
+ *
243
+ * @returns a promise that resolves when the operations are finished
244
+ */
146
245
async function commit ( ) {
147
- await b . commit ( )
246
+ await firestoreBatch . commit ( )
148
247
}
149
248
150
249
return {
0 commit comments