Skip to content

Commit 9f1e32d

Browse files
committed
Finish documenting the source code
1 parent dd9299f commit 9f1e32d

File tree

25 files changed

+644
-101
lines changed

25 files changed

+644
-101
lines changed

Diff for: .clocignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
secrets
3+
lib

Diff for: src/add/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { ref } from '../ref'
77
/**
88
* Adds a new document with a random id to a collection.
99
*
10-
* @param collection - the collection to add to
11-
* @param data - the data to add to
12-
* @returns a promise to the document
10+
* @param collection - The collection to add to
11+
* @param data - The data to add to
12+
* @returns A promise to the document
1313
*
1414
* @example
1515
* import { add, collection } from 'typesaurus'

Diff for: src/all/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { wrapData } from '../data'
77
/**
88
* Returns all documents in a collection.
99
*
10-
* @param collection - the collection to get all documents from
11-
* @returns a promise to all documents
10+
* @param collection - The collection to get all documents from
11+
* @returns A promise to all documents
1212
*
1313
* @example
1414
* import { all, collection } from 'typesaurus'

Diff for: src/batch/index.ts

+23-21
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import { ModelUpdate } from '../update'
77
import { Field } from '../field'
88

99
/**
10-
* @returns batch API (set, update, clear, commit)
10+
* Creates batch.
11+
*
12+
* @returns Batch API (set, update, clear, commit)
1113
*
1214
* @example
1315
* import { batch, collection } from 'typesaurus'
@@ -27,15 +29,15 @@ export function batch() {
2729
const firestoreBatch = firestore().batch()
2830

2931
/**
30-
* @param ref - the reference to the document to set
31-
* @param data - the document data
32+
* @param ref - The reference to the document to set
33+
* @param data - The document data
3234
*/
3335
function set<Model>(ref: Ref<Model>, data: Model): Doc<Model>
3436

3537
/**
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
38+
* @param collection - The collection to set document in
39+
* @param id - The id of the document to set
40+
* @param data - The document data
3941
*/
4042
function set<Model>(
4143
collection: Collection<Model>,
@@ -46,7 +48,7 @@ export function batch() {
4648
/**
4749
* Sets a document to the given data.
4850
*
49-
* @returns the document
51+
* @returns The document
5052
*
5153
* @example
5254
* import { batch, collection } from 'typesaurus'
@@ -93,9 +95,9 @@ export function batch() {
9395
}
9496

9597
/**
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
98+
* @param collection - The collection to update document in
99+
* @param id - The id of the document to update
100+
* @param data - The document data to update
99101
*/
100102
function update<Model>(
101103
collection: Collection<Model>,
@@ -104,15 +106,15 @@ export function batch() {
104106
): void
105107

106108
/**
107-
* @param ref - the reference to the document to set
108-
* @param data - the document data to update
109+
* @param ref - The reference to the document to set
110+
* @param data - The document data to update
109111
*/
110112
function update<Model>(ref: Ref<Model>, data: Field<Model>[]): void
111113

112114
/**
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
115+
* @param collection - The collection to update document in
116+
* @param id - The id of the document to update
117+
* @param data - The document data to update
116118
*/
117119
function update<Model>(
118120
collection: Collection<Model>,
@@ -121,8 +123,8 @@ export function batch() {
121123
): void
122124

123125
/**
124-
* @param ref - the reference to the document to set
125-
* @param data - the document data to update
126+
* @param ref - The reference to the document to set
127+
* @param data - The document data to update
126128
*/
127129
function update<Model>(ref: Ref<Model>, data: ModelUpdate<Model>): void
128130

@@ -186,13 +188,13 @@ export function batch() {
186188
}
187189

188190
/**
189-
* @param collection - the collection to remove document in
190-
* @param id - the id of the documented to remove
191+
* @param collection - The collection to remove document in
192+
* @param id - The id of the documented to remove
191193
*/
192194
function clear<Model>(collection: Collection<Model>, id: string): void
193195

194196
/**
195-
* @param ref - the reference to the document to remove
197+
* @param ref - The reference to the document to remove
196198
*/
197199
function clear<Model>(ref: Ref<Model>): void
198200

@@ -240,7 +242,7 @@ export function batch() {
240242
/**
241243
* Starts the execution of the operations in the batch.
242244
*
243-
* @returns a promise that resolves when the operations are finished
245+
* @returns A promise that resolves when the operations are finished
244246
*/
245247
async function commit() {
246248
await firestoreBatch.commit()

Diff for: src/clear/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import { Collection } from '../collection'
33
import { Ref } from '../ref'
44

55
/**
6-
* @param collection - the collection to remove document in
7-
* @param id - the id of the documented to remove
6+
* @param collection - The collection to remove document in
7+
* @param id - The id of the documented to remove
88
*/
99
async function clear<Model>(
1010
collection: Collection<Model>,
1111
id: string
1212
): Promise<void>
1313

1414
/**
15-
* @param ref - the reference to the document to remove
15+
* @param ref - The reference to the document to remove
1616
*/
1717
async function clear<Model>(ref: Ref<Model>): Promise<void>
1818

Diff for: src/collection/index.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1+
/**
2+
* The collection type. It contains the path in Firestore.
3+
*/
14
export interface Collection<_Model> {
25
__type__: 'collection'
36
path: string
47
}
58

69
/**
7-
* Creates collection object.
10+
* Creates a collection object.
811
*
9-
* @param path - the collection path
10-
* @returns collection object
12+
* @param path - The collection path
13+
* @returns The collection object
1114
*
1215
* @example
1316
* import { add, collection } from 'typesaurus'
1417
*
1518
* type User = { name: string }
1619
* const users = collection<User>('users')
17-
* // { __type__: 'collection', path: 'users' }
20+
* //=> { __type__: 'collection', path: 'users' }
1821
*
1922
* add(users, { name: 'Sasha' })
2023
*/

Diff for: src/cursor/index.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export interface Cursor<Model, Key extends keyof Model> {
1414
/**
1515
* Start the query results after the given value.
1616
*
17-
* @param value - the value to end the query results after
17+
* @param value - The value to end the query results after
18+
* @returns The cursor object
1819
*
1920
* @example
2021
* import { startAfter, order, query, collection } from 'typesaurus'
@@ -40,7 +41,8 @@ export function startAfter<Model, Key extends keyof Model>(
4041
/**
4142
* Start the query results on the given value.
4243
*
43-
* @param value - the value to start the query results at
44+
* @param value - The value to start the query results at
45+
* @returns The cursor object
4446
*
4547
* @example
4648
* import { startAt, order, query, collection } from 'typesaurus'
@@ -66,7 +68,8 @@ export function startAt<Model, Key extends keyof Model>(
6668
/**
6769
* Ends the query results before the given value.
6870
*
69-
* @param value - the value to end the query results before
71+
* @param value - The value to end the query results before
72+
* @returns The cursor object
7073
*
7174
* @example
7275
* import { endBefore, order, query, collection } from 'typesaurus'
@@ -92,7 +95,8 @@ export function endBefore<Model, Key extends keyof Model>(
9295
/**
9396
* Ends the query results on the given value.
9497
*
95-
* @param value - the value to end the query results at
98+
* @param value - The value to end the query results at
99+
* @returns The cursor object
96100
*
97101
* @example
98102
* import { endAt, order, query, collection } from 'typesaurus'

Diff for: src/data/index.ts

+22-14
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,23 @@ import {
33
FirestoreFieldValue,
44
FirestoreTimestamp
55
} from '../adaptor'
6-
import { pathToRef, Ref, refToFirebaseDocument } from '../ref'
6+
import { pathToRef, Ref, refToFirestoreDocument } from '../ref'
77
import { UpdateValue } from '../value'
88

99
/**
10+
* Converts Typesaurus data to Firestore format. It deeply traverse all the data and
11+
* converts values to compatible format.
1012
*
11-
* @param value - the value to convert
13+
* @param data - the data to convert
1214
*/
13-
export function unwrapData(value: any) {
14-
if (value instanceof Date) {
15-
return FirestoreTimestamp.fromDate(value)
16-
} else if (value && typeof value === 'object') {
17-
if (value.__type__ === 'ref') {
18-
return refToFirebaseDocument(value as Ref<any>)
19-
} else if (value.__type__ === 'value') {
20-
const fieldValue = value as UpdateValue<any>
15+
export function unwrapData(data: any) {
16+
if (data instanceof Date) {
17+
return FirestoreTimestamp.fromDate(data)
18+
} else if (data && typeof data === 'object') {
19+
if (data.__type__ === 'ref') {
20+
return refToFirestoreDocument(data as Ref<any>)
21+
} else if (data.__type__ === 'value') {
22+
const fieldValue = data as UpdateValue<any>
2123
switch (fieldValue.kind) {
2224
case 'clear':
2325
return FirestoreFieldValue.delete()
@@ -33,20 +35,26 @@ export function unwrapData(value: any) {
3335
}
3436

3537
const unwrappedObject: { [key: string]: any } = Object.assign(
36-
Array.isArray(value) ? [] : {},
37-
value
38+
Array.isArray(data) ? [] : {},
39+
data
3840
)
3941
Object.keys(unwrappedObject).forEach(key => {
4042
unwrappedObject[key] = unwrapData(unwrappedObject[key])
4143
})
4244
return unwrappedObject
43-
} else if (value === undefined) {
45+
} else if (data === undefined) {
4446
return null
4547
} else {
46-
return value
48+
return data
4749
}
4850
}
4951

52+
/**
53+
* Converts Firestore data to Typesaurus format. It deeply traverse all the
54+
* data and converts values to compatible format.
55+
*
56+
* @param data - the data to convert
57+
*/
5058
export function wrapData(data: unknown) {
5159
if (data instanceof FirestoreDocumentReference) {
5260
return pathToRef(data.path)

Diff for: src/doc/index.ts

+27
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
import { Ref } from '../ref'
22

3+
/**
4+
* The document type. It contains the reference in the DB and the model data.
5+
*/
36
export interface Doc<Model> {
47
__type__: 'doc'
58
data: Model
69
ref: Ref<Model>
710
}
811

12+
/**
13+
* Creates a document object.
14+
*
15+
* @param ref - The document reference
16+
* @param data - The model data
17+
* @returns The document object
18+
*
19+
* @example
20+
* import { doc, ref, collection } from 'typesaurus'
21+
*
22+
* type User = { name: string }
23+
* const users = collection<User>('users')
24+
*
25+
* doc(ref(users, '00sHm46UWKObv2W7XK9e'), { name: 'Sasha' })
26+
* //=> {
27+
* //=> __type__: 'doc',
28+
* //=> data: { name: 'Sasha' },
29+
* //=> ref: {,
30+
* //=> __type__: 'ref'
31+
* //=> collection: { __type__: 'collection', path: 'users' },
32+
* //=> id: '00sHm46UWKObv2W7XK9e'
33+
* //=> }
34+
* //=> }
35+
*/
936
export function doc<Model>(ref: Ref<Model>, data: Model): Doc<Model> {
1037
return { __type__: 'doc', ref, data }
1138
}

Diff for: src/field/index.ts

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* The field type. It contains path to the property and property value.
3+
*/
14
export interface Field<_Model> {
25
key: string | string[]
36
value: any
@@ -118,6 +121,24 @@ function field<
118121
value: Model[Key1][Key2][Key3][Key4][Key5][Key6][Key7][Key8][Key9][Key10]
119122
): Field<Model>
120123

124+
/**
125+
* Creates a field object.
126+
*
127+
* @param key - The field key or key path
128+
* @param value - The value
129+
* @returns The field object
130+
*
131+
* @example
132+
* import { field, update, collection } from 'typesaurus'
133+
*
134+
* type User = { name: string }
135+
* const users = collection<User>('users')
136+
* update(users, '00sHm46UWKObv2W7XK9e', [
137+
* field('name', 'Sasha Koss'),
138+
* field(['address', 'city'], 'Dimitrovgrad')
139+
* ])
140+
* //=> Promise<void>
141+
*/
121142
function field<Model>(key: string | string[], value: any): Field<Model> {
122143
return { key, value }
123144
}

0 commit comments

Comments
 (0)