Skip to content

Commit bc0635d

Browse files
committed
feat(fire): update implementation
1 parent 7f2f02a commit bc0635d

4 files changed

Lines changed: 107 additions & 53 deletions

File tree

packages/fire/src/firestore.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import {
2+
DocumentData,
3+
FirestoreDataConverter,
4+
PartialWithFieldValue,
5+
} from '@angular/fire/firestore';
6+
7+
export interface SimpleFirestoreDataConverterConfig<
8+
Model extends DocumentData,
9+
Record extends DocumentData,
10+
> {
11+
toRecord: (
12+
model: PartialWithFieldValue<Model>,
13+
) => PartialWithFieldValue<Record>;
14+
toModel(record: object): Model;
15+
}
16+
17+
export function createSimpleFirestoreDataConverter<
18+
Model extends DocumentData,
19+
Record extends DocumentData,
20+
>(
21+
config: SimpleFirestoreDataConverterConfig<Model, Record>,
22+
): FirestoreDataConverter<Model, Record> {
23+
return {
24+
fromFirestore: (snapshot, options) =>
25+
config.toModel(snapshot.data(options)),
26+
toFirestore: (model) => config.toRecord(model) as Record,
27+
};
28+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import {
2+
collection,
3+
collectionData,
4+
CollectionReference,
5+
doc,
6+
docData,
7+
DocumentData,
8+
DocumentReference,
9+
Firestore,
10+
FirestoreDataConverter,
11+
Query,
12+
} from '@angular/fire/firestore';
13+
import { Observable } from 'rxjs';
14+
15+
export interface FirestoreResourceConfig<
16+
ModelWithId extends DocumentData,
17+
Record extends DocumentData,
18+
IdField extends keyof ModelWithId,
19+
> {
20+
readonly path: string;
21+
readonly idField: IdField;
22+
readonly converter: FirestoreDataConverter<
23+
Omit<ModelWithId, IdField>,
24+
Record
25+
>;
26+
}
27+
28+
/**
29+
* A class that provides type-safe access to the Firestore collection and
30+
* documents of a specific model.
31+
*/
32+
export class FirestoreResource<
33+
ModelWithId extends DocumentData,
34+
Record extends DocumentData,
35+
IdField extends keyof ModelWithId,
36+
> {
37+
constructor(
38+
protected firestore: Firestore,
39+
protected config: FirestoreResourceConfig<ModelWithId, Record, IdField>,
40+
) {}
41+
42+
readonly path = this.config.path;
43+
readonly idField = this.config.idField;
44+
readonly converter = this.config.converter;
45+
46+
refDoc(id: string): DocumentReference<Omit<ModelWithId, IdField>, Record> {
47+
const { path, converter } = this;
48+
return doc(this.firestore, path, id).withConverter(converter);
49+
}
50+
51+
refNewDoc(): DocumentReference<Omit<ModelWithId, IdField>, Record> {
52+
const { converter } = this;
53+
const collectionRef = this.refCollection();
54+
return doc(collectionRef).withConverter(converter);
55+
}
56+
57+
refCollection(): CollectionReference<Omit<ModelWithId, IdField>, Record> {
58+
const { path, converter } = this;
59+
return collection(this.firestore, path).withConverter(converter);
60+
}
61+
62+
resolveQuery(
63+
query: Query<Omit<ModelWithId, IdField>>,
64+
): Observable<ModelWithId[]> {
65+
const { idField } = this;
66+
// Type-assert required because of the incorrect typing of the `idField` option
67+
return collectionData(query as Query<ModelWithId>, { idField });
68+
}
69+
70+
resolveDoc(
71+
ref: DocumentReference<Omit<ModelWithId, IdField>>,
72+
): Observable<ModelWithId | undefined> {
73+
const { idField } = this;
74+
// Type-assert required because of the incorrect typing of the `idField` option
75+
return docData(ref as DocumentReference<ModelWithId>, { idField });
76+
}
77+
}

packages/fire/src/public-api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from './firestore';
1+
export * from './firestore/converter';
2+
export * from './firestore/resource';

0 commit comments

Comments
 (0)