Skip to content

Commit dd9299f

Browse files
committed
Start adding docs
1 parent 0e7ff28 commit dd9299f

File tree

9 files changed

+342
-23
lines changed

9 files changed

+342
-23
lines changed

Diff for: src/add/index.ts

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ import { unwrapData } from '../data'
44
import { doc } from '../doc'
55
import { ref } from '../ref'
66

7+
/**
8+
* Adds a new document with a random id to a collection.
9+
*
10+
* @param collection - the collection to add to
11+
* @param data - the data to add to
12+
* @returns a promise to the document
13+
*
14+
* @example
15+
* import { add, collection } from 'typesaurus'
16+
*
17+
* type User = { name: string }
18+
* const users = collection<User>('users')
19+
*
20+
* add(users, { name: 'Sasha' }).then(sasha => {
21+
* console.log(sasha.ref.id)
22+
* //=> '00sHm46UWKObv2W7XK9e'
23+
* console.log(sasha.data)
24+
* //=> { name: 'Sasha' }
25+
* })
26+
*/
727
export default async function add<Model>(
828
collection: Collection<Model>,
929
data: Model

Diff for: src/all/index.ts

+21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ import { doc, Doc } from '../doc'
44
import { ref } from '../ref'
55
import { wrapData } from '../data'
66

7+
/**
8+
* Returns all documents in a collection.
9+
*
10+
* @param collection - the collection to get all documents from
11+
* @returns a promise to all documents
12+
*
13+
* @example
14+
* import { all, collection } from 'typesaurus'
15+
*
16+
* type User = { name: string }
17+
* const users = collection<User>('users')
18+
*
19+
* all(users).then(allUsers => {
20+
* console.log(allUsers.length)
21+
* //=> 420
22+
* console.log(allUsers[0].ref.id)
23+
* //=> '00sHm46UWKObv2W7XK9e'
24+
* console.log(allUsers[0].data)
25+
* //=> { name: 'Sasha' }
26+
* })
27+
*/
728
export default async function all<Model>(
829
collection: Collection<Model>
930
): Promise<Doc<Model>[]> {

Diff for: src/batch/index.ts

+120-21
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,65 @@ import { Collection } from '../collection'
33
import { Ref, ref } from '../ref'
44
import { Doc, doc } from '../doc'
55
import { unwrapData } from '../data'
6-
import set from '../set'
7-
import update, { ModelUpdate } from '../update'
6+
import { ModelUpdate } from '../update'
87
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-
}
178

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+
*/
1826
export function batch() {
19-
const b = firestore().batch()
20-
21-
// set
27+
const firestoreBatch = firestore().batch()
2228

29+
/**
30+
* @param ref - the reference to the document to set
31+
* @param data - the document data
32+
*/
2333
function set<Model>(ref: Ref<Model>, data: Model): Doc<Model>
2434

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+
*/
2540
function set<Model>(
2641
collection: Collection<Model>,
2742
id: string,
2843
data: Model
2944
): Doc<Model>
3045

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+
*/
3165
function set<Model>(
3266
collectionOrRef: Collection<Model> | Ref<Model>,
3367
idOrData: string | Model,
@@ -53,29 +87,67 @@ export function batch() {
5387
.doc(id)
5488
// ^ above
5589
// 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))
5791
// v below
5892
return doc(ref(collection, id), data)
5993
}
6094

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+
*/
63100
function update<Model>(
64101
collection: Collection<Model>,
65102
id: string,
66103
data: Field<Model>[]
67104
): void
68105

106+
/**
107+
* @param ref - the reference to the document to set
108+
* @param data - the document data to update
109+
*/
69110
function update<Model>(ref: Ref<Model>, data: Field<Model>[]): void
70111

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+
*/
71117
function update<Model>(
72118
collection: Collection<Model>,
73119
id: string,
74120
data: ModelUpdate<Model>
75121
): void
76122

123+
/**
124+
* @param ref - the reference to the document to set
125+
* @param data - the document data to update
126+
*/
77127
function update<Model>(ref: Ref<Model>, data: ModelUpdate<Model>): void
78128

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+
*/
79151
function update<Model>(
80152
collectionOrRef: Collection<Model> | Ref<Model>,
81153
idOrData: string | Field<Model>[] | ModelUpdate<Model>,
@@ -110,15 +182,37 @@ export function batch() {
110182
: data
111183
// ^ above
112184
// 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))
114186
}
115187

116-
// clear
117-
188+
/**
189+
* @param collection - the collection to remove document in
190+
* @param id - the id of the documented to remove
191+
*/
118192
function clear<Model>(collection: Collection<Model>, id: string): void
119193

194+
/**
195+
* @param ref - the reference to the document to remove
196+
*/
120197
function clear<Model>(ref: Ref<Model>): void
121198

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+
*/
122216
function clear<Model>(
123217
collectionOrRef: Collection<Model> | Ref<Model>,
124218
maybeId?: string
@@ -140,11 +234,16 @@ export function batch() {
140234
.doc(id)
141235
// ^ above
142236
// TODO: Refactor code above because is all the same as in the regular update function
143-
b.delete(firebaseDoc)
237+
firestoreBatch.delete(firebaseDoc)
144238
}
145239

240+
/**
241+
* Starts the execution of the operations in the batch.
242+
*
243+
* @returns a promise that resolves when the operations are finished
244+
*/
146245
async function commit() {
147-
await b.commit()
246+
await firestoreBatch.commit()
148247
}
149248

150249
return {

Diff for: src/clear/index.ts

+18
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,31 @@ import firestore from '../adaptor'
22
import { Collection } from '../collection'
33
import { Ref } from '../ref'
44

5+
/**
6+
* @param collection - the collection to remove document in
7+
* @param id - the id of the documented to remove
8+
*/
59
async function clear<Model>(
610
collection: Collection<Model>,
711
id: string
812
): Promise<void>
913

14+
/**
15+
* @param ref - the reference to the document to remove
16+
*/
1017
async function clear<Model>(ref: Ref<Model>): Promise<void>
1118

19+
/**
20+
* Removes a document.
21+
*
22+
* @example
23+
* import { clear } from 'typesaurus'
24+
*
25+
* type User = { name: string }
26+
* const users = collection<User>('users')
27+
*
28+
* clear(users, '00sHm46UWKObv2W7XK9e').then(() => console.log('Done!'))
29+
*/
1230
async function clear<Model>(
1331
collectionOrRef: Collection<Model> | Ref<Model>,
1432
maybeId?: string

Diff for: src/collection/index.ts

+15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,21 @@ export interface Collection<_Model> {
33
path: string
44
}
55

6+
/**
7+
* Creates collection object.
8+
*
9+
* @param path - the collection path
10+
* @returns collection object
11+
*
12+
* @example
13+
* import { add, collection } from 'typesaurus'
14+
*
15+
* type User = { name: string }
16+
* const users = collection<User>('users')
17+
* // { __type__: 'collection', path: 'users' }
18+
*
19+
* add(users, { name: 'Sasha' })
20+
*/
621
export function collection<Model>(path: string): Collection<Model> {
722
return { __type__: 'collection', path }
823
}

0 commit comments

Comments
 (0)