Skip to content

Commit 6a28f57

Browse files
committed
- fix incorrect id, path, and parent types of collection reference
- narrow the `id` types of document reference - add more tests and more code for doc - remove offset prop of collection reference
1 parent 82ac7e9 commit 6a28f57

18 files changed

+148
-59
lines changed

codeForDoc/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
This folder consist of code for documentation.
44

5-
It also serve as end to end test.
5+
It also serves as end to end type test and some functionality test.
66

7-
run `npm run link` to use this folder properly.
7+
Run `npm run d-link` in root directory to use this folder properly.

codeForDoc/src/quick_start/operations.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ import {
3939
await deleteDoc(example.doc('abc'))
4040

4141
await getDoc(example.doc('abc')).then(docSnapshot => {
42-
const data = docSnapshot.data()
42+
console.log(docSnapshot.data())
43+
console.log(docSnapshot.get('b.c'))
44+
console.log(docSnapshot.id)
45+
46+
console.log(docSnapshot.ref.firestore)
47+
console.log(docSnapshot.ref.id)
48+
console.log(docSnapshot.ref.listCollections())
49+
console.log(docSnapshot.ref.path)
50+
51+
console.log(docSnapshot.ref.parent)
52+
console.log(docSnapshot.ref.parent.firestore)
53+
console.log(docSnapshot.ref.parent.id)
54+
console.log(docSnapshot.ref.parent.listDocuments())
55+
console.log(docSnapshot.ref.parent.parent)
4356
})
4457
}

codeForDoc/src/quick_start/queryAndOnSnapshot.ts

+32-23
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,37 @@ import {
99
limit,
1010
} from 'firelord'
1111
//
12-
;async () => {
13-
await getDocs(
14-
query(
15-
example.collection(),
16-
where('f.h', '>', 1010 as const),
17-
orderBy('f.h'),
18-
limit(10)
19-
)
12+
getDocs(
13+
query(
14+
example.collection(),
15+
where('f.h', '>', 1010 as const),
16+
orderBy('f.h'),
17+
limit(10)
2018
)
19+
).then(querySnapshot => {
20+
querySnapshot.docChanges().forEach(docChange => {
21+
console.log(docChange.doc)
22+
console.log(docChange.type)
23+
console.log(docChange.oldIndex)
24+
console.log(docChange.newIndex)
25+
console.log(docChange.isEqual(docChange))
26+
})
27+
querySnapshot.forEach(docSnapshot => {})
2128

22-
const unsub = onSnapshot(
23-
query(
24-
example.collectionGroup(),
25-
where('b.d', 'array-contains', { e: 'hello' }),
26-
orderBy('f.g'),
27-
startAfter(new Date())
28-
),
29-
querySnapshot => {
30-
querySnapshot.forEach(docSnapshot => {
31-
const data = docSnapshot.data()
32-
})
33-
},
34-
error => {}
35-
)
36-
}
29+
querySnapshot.docs.forEach(docSnapshot => {})
30+
})
31+
32+
const unsub = onSnapshot(
33+
query(
34+
example.collectionGroup(),
35+
where('b.d', 'array-contains', { e: 'hello' }),
36+
orderBy('f.g'),
37+
startAfter(new Date())
38+
),
39+
querySnapshot => {
40+
querySnapshot.forEach(docSnapshot => {
41+
const data = docSnapshot.data()
42+
})
43+
},
44+
error => {}
45+
)

codeForDoc/src/utilForTests.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export const readThenCompareWithWriteData = async (
169169
compareWriteDataWithDocSnapData(cloneDeep(writeData), docSnap)
170170
}
171171

172-
export const writeThenReadTest = async (
172+
export const writeThenCompareWithRead = async (
173173
writeCallback: (
174174
data: ReturnType<typeof generateRandomData>
175175
) => Promise<DocumentReference<User>>

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "firelord",
3-
"version": "2.0.1",
3+
"version": "2.1.0",
44
"description": "🔥 Write V9 like Firestore Admin code with extreme type safety.",
55
"source": "src/index.ts",
66
"main": "dist/index.js",

src/operations/addDoc.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { removeFieldValueInhomogeneousProps } from '../fieldValue'
1212
*/
1313
// @ts-expect-error
1414
export const addDoc: AddDoc = (reference, data) => {
15+
// @ts-expect-error
1516
return (reference as OriCollectionReference).add(
1617
removeFieldValueInhomogeneousProps(data)
1718
)

src/operations/createDoc.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createDoc } from './createDoc'
33
import {
44
userRefCreator,
55
initializeApp,
6-
writeThenReadTest,
6+
writeThenCompareWithRead,
77
generateRandomData,
88
} from '../utilForTests'
99
import { increment, arrayUnion, serverTimestamp } from '../fieldValue'
@@ -114,7 +114,7 @@ describe('test createDoc', () => {
114114
})
115115
const ref = userRef.doc('FirelordTest', 'createDocTestCase')
116116
it('test functionality', async () => {
117-
await writeThenReadTest(async data => {
117+
await writeThenCompareWithRead(async data => {
118118
await createDoc(ref, data)
119119
return ref
120120
})

src/operations/deleteDoc.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ import { Delete, OriDocumentReference } from '../types'
77
* @return A Promise resolved with the write time of this delete.
88
*/
99
export const deleteDoc: Delete = (reference, precondition?) => {
10+
// @ts-expect-error
1011
return (reference as OriDocumentReference).delete(precondition || {})
1112
}

src/operations/getDocs.test.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from '../types'
1818
import { query } from '../refs'
1919
import { where } from '../queryClauses'
20-
import { snapshotEqual } from '../equal'
20+
import { snapshotEqual, queryEqual, refEqual } from '../equal'
2121

2222
initializeApp()
2323
const userRef = userRefCreator()
@@ -43,8 +43,16 @@ const queryTest = async (
4343
IsTrue<IsSame<X, Y>>()
4444
await compareWriteDataWithDocSnapData(data, queryDocumentSnapshot)
4545
}
46-
46+
const incorrectDocRef = userRefCreator().doc('FirelordTest', 'abc')
4747
expect(snapshotEqual(querySnapshot, querySnapshot)).toBe(true)
48+
expect(refEqual(queryDocumentSnapshot!.ref, docRef)).toBe(true)
49+
expect(refEqual(queryDocumentSnapshot!.ref, incorrectDocRef)).toBe(false)
50+
expect(queryEqual(queryDocumentSnapshot!.ref.parent, docRef.parent)).toBe(
51+
true
52+
)
53+
expect(
54+
queryEqual(queryDocumentSnapshot!.ref.parent, incorrectDocRef.parent)
55+
).toBe(true)
4856
}
4957

5058
describe('test getDocs', () => {

src/operations/setDoc.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { setDoc } from './setDoc'
33
import {
44
userRefCreator,
55
initializeApp,
6-
writeThenReadTest,
6+
writeThenCompareWithRead,
77
generateRandomData,
88
readThenCompareWithWriteData,
99
} from '../utilForTests'
@@ -249,15 +249,15 @@ describe('test setDoc', () => {
249249
)
250250
})
251251
it('test functionality', async () => {
252-
await writeThenReadTest(async data => {
252+
await writeThenCompareWithRead(async data => {
253253
const ref = userRefCreator().doc('FirelordTest', 'setDocTestCase')
254254
await setDoc(ref, data)
255255

256256
return ref
257257
})
258258
})
259259
it('test merge false functionality', async () => {
260-
await writeThenReadTest(async data => {
260+
await writeThenCompareWithRead(async data => {
261261
const ref = userRefCreator().doc(
262262
'FirelordTest',
263263
'setDocTestCaseMergeFalse'

src/operations/setDoc.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { removeFieldValueInhomogeneousProps } from '../fieldValue'
1717
* @return A Promise resolved with the write time of this set.
1818
*/
1919
export const setDoc: Set = (reference, data, options?) => {
20+
// @ts-expect-error
2021
return (reference as OriDocumentReference).set(
2122
removeFieldValueInhomogeneousProps(data),
2223
options || {}

src/operations/updateDoc.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getDoc } from './getDoc'
44
import {
55
userRefCreator,
66
initializeApp,
7-
writeThenReadTest,
7+
writeThenCompareWithRead,
88
generateRandomData,
99
readThenCompareWithWriteData,
1010
} from '../utilForTests'
@@ -199,15 +199,15 @@ describe('test updateDoc', () => {
199199
)
200200
})
201201
it('test functionality', async () => {
202-
await writeThenReadTest(async data => {
202+
await writeThenCompareWithRead(async data => {
203203
const ref = userRefCreator().doc('FirelordTest', 'updateDocTestCase')
204204
await setDoc(ref, generateRandomData())
205205
await updateDoc(ref, data)
206206
return ref
207207
})
208208
})
209209
it('test functionality with overload', async () => {
210-
await writeThenReadTest(async data => {
210+
await writeThenCompareWithRead(async data => {
211211
const ref = userRefCreator().doc('FirelordTest', 'updateDocTestCase')
212212
await setDoc(ref, generateRandomData())
213213
await updateDoc(ref, data)

src/operations/updateDoc.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export const updateDoc: Update = (reference, data, precondition?) => {
1919
const data_ = flatten(removeFieldValueInhomogeneousProps(data))
2020

2121
return Object.keys(data_).length > 0
22-
? (reference as OriDocumentReference).update(
22+
? // @ts-expect-error
23+
(reference as OriDocumentReference).update(
2324
flatten(removeFieldValueInhomogeneousProps(data)),
2425
precondition || {}
2526
)

src/refs/collection.test.ts

+34-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
import { initializeApp, grandChildRefCreator } from '../utilForTests'
1+
import {
2+
initializeApp,
3+
grandChildRefCreator,
4+
userRefCreator,
5+
User,
6+
GrandChild,
7+
} from '../utilForTests'
8+
import { refEqual } from '../equal'
9+
import { IsSame, IsTrue, DocumentReference } from '../types'
210

311
initializeApp()
412

513
const grandChildRef = grandChildRefCreator()
614

715
describe('simple collection type test', () => {
8-
it('test invalid doc ID, negative test', () => {
16+
it('test invalid doc ID, positive test', () => {
917
grandChildRef.collection('FirelordTest', 'ab')
1018

1119
grandChildRef.collection('FirelordTest', 'a.b')
@@ -40,4 +48,28 @@ describe('simple collection type test', () => {
4048
'__ab__'
4149
)
4250
})
51+
52+
it('test props value and type', async () => {
53+
const id = 'abc'
54+
const ref = grandChildRef.collection('FirelordTest', id)
55+
const parentRef = userRefCreator().doc('FirelordTest', id)
56+
const documents = await ref.listDocuments()
57+
58+
expect(ref.id).toBe('GrandChild')
59+
expect(ref.path).toBe(`topLevel/FirelordTest/Users/${id}/GrandChild`)
60+
61+
expect(refEqual(ref.parent, parentRef)).toBe(true)
62+
expect(Array.isArray(documents)).toBe(true)
63+
64+
IsTrue<IsSame<typeof ref.id, 'GrandChild'>>()
65+
IsTrue<
66+
IsSame<
67+
typeof ref.path,
68+
| `topLevel/FirelordTest/Users/${string}/GrandChild`
69+
| `topLevel/ForCursorTest/Users/${string}/GrandChild`
70+
>
71+
>()
72+
IsTrue<IsSame<typeof ref.parent, DocumentReference<User>>>()
73+
IsTrue<IsSame<typeof documents, DocumentReference<GrandChild>[]>>()
74+
})
4375
})

src/refs/doc.test.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { initializeApp, userRefCreator } from '../utilForTests'
1+
import {
2+
initializeApp,
3+
grandChildRefCreator,
4+
userRefCreator,
5+
GrandChild,
6+
} from '../utilForTests'
7+
import { refEqual } from '../equal'
8+
import { IsSame, IsTrue, CollectionReference } from '../types'
29

310
initializeApp()
411

@@ -26,4 +33,28 @@ describe('simple collection type test', () => {
2633
'a..b'
2734
)
2835
})
36+
37+
it('test props value and type', async () => {
38+
const id = 'abc'
39+
const id2 = 'xyz'
40+
const ref = grandChildRefCreator().doc('FirelordTest', id, id2)
41+
const parentRef = grandChildRefCreator().collection('FirelordTest', id)
42+
const documents = await ref.listCollections()
43+
expect(ref.id).toBe('xyz')
44+
expect(ref.path).toBe(`topLevel/FirelordTest/Users/${id}/GrandChild/${id2}`)
45+
46+
expect(refEqual(ref.parent, parentRef)).toBe(true)
47+
expect(Array.isArray(documents)).toBe(true)
48+
49+
IsTrue<IsSame<typeof ref.id, string>>()
50+
IsTrue<
51+
IsSame<
52+
typeof ref.path,
53+
| `topLevel/FirelordTest/Users/${string}/GrandChild/${string}`
54+
| `topLevel/ForCursorTest/Users/${string}/GrandChild/${string}`
55+
>
56+
>()
57+
IsTrue<IsSame<typeof ref.parent, CollectionReference<GrandChild>>>()
58+
IsTrue<IsSame<typeof documents, CollectionReference<any>[]>>()
59+
})
2960
})

src/types/refs.ts

+7-15
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,24 @@ export interface DocumentReference<T extends MetaType> {
2626
*
2727
* @returns A Promise that resolves with an array of CollectionReferences.
2828
*/
29-
listCollections(): Promise<Array<CollectionReference<T>>> // ! revisit and fix type
29+
listCollections(): Promise<CollectionReference<any>[]> // ! revisit and fix type
3030
}
3131

3232
export interface CollectionReference<T extends MetaType> extends Query<T> {
3333
/** The collection's identifier. */
34-
readonly id: T['docID']
34+
readonly id: T['collectionID']
3535
/**
3636
* A reference to the containing `DocumentReference` if this is a
3737
* subcollection. If this isn't a subcollection, the reference is null.
3838
*/
39-
readonly parent: T['parent']
39+
readonly parent: T['parent'] extends MetaType
40+
? DocumentReference<T['parent']>
41+
: null
4042
/**
4143
* A string representing the path of the referenced collection (relative
4244
* to the root of the database).
4345
*/
44-
readonly path: T['docPath']
46+
readonly path: T['collectionPath']
4547
/**
4648
* Retrieves the list of documents in this collection.
4749
*
@@ -54,7 +56,7 @@ export interface CollectionReference<T extends MetaType> extends Query<T> {
5456
* @return {Promise<DocumentReference[]>} The list of documents in this
5557
* collection.
5658
*/
57-
listDocuments(): Promise<Array<DocumentReference<T>>> // ! revisit
59+
listDocuments(): Promise<DocumentReference<T>[]> // ! revisit
5860
}
5961

6062
export interface Query<T extends MetaType> {
@@ -85,14 +87,4 @@ export interface Query<T extends MetaType> {
8587
* @return The created Query.
8688
*/
8789
select(...field: (keyof T['writeFlatten'])[]): Query<T> // ! revisit
88-
/**
89-
* Specifies the offset of the returned results.
90-
*
91-
* This function returns a new (immutable) instance of the Query (rather
92-
* than modify the existing instance) to impose the offset.
93-
*
94-
* @param offset The offset to apply to the Query results.
95-
* @return The created Query.
96-
*/
97-
offset(offset: number): Query<T> // ! revisit
9890
}

src/types/snapshot.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface DocumentSnapshot<T extends MetaType> {
1616
/**
1717
* The ID of the document for which this `DocumentSnapshot` contains data.
1818
*/
19-
readonly id: string
19+
readonly id: T['docID']
2020

2121
/**
2222
* The time the document was created. Not set for documents that don't

0 commit comments

Comments
 (0)