Skip to content

Commit a0f24a9

Browse files
committed
Enable prettier plugin
1 parent 50c29e1 commit a0f24a9

34 files changed

+808
-383
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
'airbnb-base',
1212
'eslint:recommended',
1313
'plugin:ember/recommended',
14+
'plugin:prettier/recommended',
1415
],
1516
env: {
1617
browser: true,

addon/-private/flatten-doc-snapshot.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { DocumentSnapshot } from 'firebase/firestore';
22

33
export default function flattenDocSnapshot(docSnapshot: DocumentSnapshot): {
4-
id: string,
5-
[key: string]: unknown,
4+
id: string;
5+
[key: string]: unknown;
66
} {
77
const { id } = docSnapshot;
88
const data = docSnapshot.data() || {};

addon/adapters/cloud-firestore-modular.ts

+154-63
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ interface Snapshot extends DS.Snapshot {
4444
adapterOptions: AdapterOption;
4545
}
4646

47-
interface SnapshotRecordArray extends DS.SnapshotRecordArray<keyof ModelRegistry> {
47+
interface SnapshotRecordArray
48+
extends DS.SnapshotRecordArray<keyof ModelRegistry> {
4849
adapterOptions: AdapterOption;
4950
}
5051

@@ -57,10 +58,10 @@ interface HasManyRelationshipMeta {
5758
key: string;
5859
type: string;
5960
options: {
60-
isRealtime?: boolean,
61+
isRealtime?: boolean;
6162

62-
buildReference?(db: Firestore, record: unknown): CollectionReference,
63-
filter?(db: CollectionReference | Query, record: unknown): Query,
63+
buildReference?(db: Firestore, record: unknown): CollectionReference;
64+
filter?(db: CollectionReference | Query, record: unknown): Query;
6465
};
6566
}
6667

@@ -98,22 +99,31 @@ export default class CloudFirestoreAdapter extends Adapter {
9899
snapshot: Snapshot,
99100
): RSVP.Promise<unknown> {
100101
return new RSVP.Promise((resolve, reject) => {
101-
const collectionRef = this.buildCollectionRef(type.modelName, snapshot.adapterOptions);
102+
const collectionRef = this.buildCollectionRef(
103+
type.modelName,
104+
snapshot.adapterOptions,
105+
);
102106
const docRef = doc(collectionRef, snapshot.id);
103107
const batch = this.buildWriteBatch(docRef, snapshot);
104108

105-
batch.commit().then(() => {
106-
const data = this.serialize(snapshot, { includeId: true });
107-
108-
resolve(data);
109-
110-
if (snapshot.adapterOptions?.isRealtime && !this.isFastBoot) {
111-
// Setup realtime listener for record
112-
this.firestoreDataManager.findRecordRealtime(type.modelName, docRef);
113-
}
114-
}).catch((e) => {
115-
reject(e);
116-
});
109+
batch
110+
.commit()
111+
.then(() => {
112+
const data = this.serialize(snapshot, { includeId: true });
113+
114+
resolve(data);
115+
116+
if (snapshot.adapterOptions?.isRealtime && !this.isFastBoot) {
117+
// Setup realtime listener for record
118+
this.firestoreDataManager.findRecordRealtime(
119+
type.modelName,
120+
docRef,
121+
);
122+
}
123+
})
124+
.catch((e) => {
125+
reject(e);
126+
});
117127
});
118128
}
119129

@@ -124,18 +134,24 @@ export default class CloudFirestoreAdapter extends Adapter {
124134
): RSVP.Promise<unknown> {
125135
return new RSVP.Promise((resolve, reject) => {
126136
const db = getFirestore();
127-
const collectionRef = this.buildCollectionRef(type.modelName, snapshot.adapterOptions);
137+
const collectionRef = this.buildCollectionRef(
138+
type.modelName,
139+
snapshot.adapterOptions,
140+
);
128141
const docRef = doc(collectionRef, snapshot.id);
129142
const batch = writeBatch(db);
130143

131144
batch.delete(docRef);
132145
this.addIncludeToWriteBatch(batch, snapshot.adapterOptions);
133146

134-
batch.commit().then(() => {
135-
resolve();
136-
}).catch((e) => {
137-
reject(e);
138-
});
147+
batch
148+
.commit()
149+
.then(() => {
150+
resolve();
151+
})
152+
.catch((e) => {
153+
reject(e);
154+
});
139155
});
140156
}
141157

@@ -147,16 +163,27 @@ export default class CloudFirestoreAdapter extends Adapter {
147163
): RSVP.Promise<unknown> {
148164
return new RSVP.Promise(async (resolve, reject) => {
149165
try {
150-
const colRef = this.buildCollectionRef(type.modelName, snapshot.adapterOptions);
166+
const colRef = this.buildCollectionRef(
167+
type.modelName,
168+
snapshot.adapterOptions,
169+
);
151170
const docRef = doc(colRef, id);
152-
const docSnapshot = snapshot.adapterOptions?.isRealtime && !this.isFastBoot
153-
? await this.firestoreDataManager.findRecordRealtime(type.modelName, docRef)
154-
: await getDoc(docRef);
171+
const docSnapshot =
172+
snapshot.adapterOptions?.isRealtime && !this.isFastBoot
173+
? await this.firestoreDataManager.findRecordRealtime(
174+
type.modelName,
175+
docRef,
176+
)
177+
: await getDoc(docRef);
155178

156179
if (docSnapshot.exists()) {
157180
resolve(flattenDocSnapshot(docSnapshot));
158181
} else {
159-
reject(new AdapterRecordNotFoundError(`Record ${id} for model type ${type.modelName} doesn't exist`));
182+
reject(
183+
new AdapterRecordNotFoundError(
184+
`Record ${id} for model type ${type.modelName} doesn't exist`,
185+
),
186+
);
160187
}
161188
} catch (error) {
162189
reject(error);
@@ -173,12 +200,21 @@ export default class CloudFirestoreAdapter extends Adapter {
173200
return new RSVP.Promise(async (resolve, reject) => {
174201
try {
175202
const db = getFirestore();
176-
const colRef = collection(db, buildCollectionName(type.modelName as string));
177-
const querySnapshot = snapshotRecordArray?.adapterOptions?.isRealtime && !this.isFastBoot
178-
? await this.firestoreDataManager.findAllRealtime(type.modelName, colRef)
179-
: await getDocs(colRef);
180-
181-
const result = querySnapshot.docs.map((docSnapshot) => flattenDocSnapshot(docSnapshot));
203+
const colRef = collection(
204+
db,
205+
buildCollectionName(type.modelName as string),
206+
);
207+
const querySnapshot =
208+
snapshotRecordArray?.adapterOptions?.isRealtime && !this.isFastBoot
209+
? await this.firestoreDataManager.findAllRealtime(
210+
type.modelName,
211+
colRef,
212+
)
213+
: await getDocs(colRef);
214+
215+
const result = querySnapshot.docs.map((docSnapshot) =>
216+
flattenDocSnapshot(docSnapshot),
217+
);
182218

183219
resolve(result);
184220
} catch (error) {
@@ -204,11 +240,17 @@ export default class CloudFirestoreAdapter extends Adapter {
204240
referenceKeyName: this.referenceKeyName,
205241
queryId: queryOption.queryId,
206242
};
207-
const docSnapshots = queryOption.isRealtime && !this.isFastBoot
208-
? await this.firestoreDataManager.queryRealtime(config)
209-
: await this.firestoreDataManager.queryWithReferenceTo(queryRef, this.referenceKeyName);
210-
211-
const result = docSnapshots.map((docSnapshot) => (flattenDocSnapshot(docSnapshot)));
243+
const docSnapshots =
244+
queryOption.isRealtime && !this.isFastBoot
245+
? await this.firestoreDataManager.queryRealtime(config)
246+
: await this.firestoreDataManager.queryWithReferenceTo(
247+
queryRef,
248+
this.referenceKeyName,
249+
);
250+
251+
const result = docSnapshots.map((docSnapshot) =>
252+
flattenDocSnapshot(docSnapshot),
253+
);
212254

213255
resolve(result);
214256
} catch (error) {
@@ -233,14 +275,22 @@ export default class CloudFirestoreAdapter extends Adapter {
233275
const db = getFirestore();
234276
const docRef = doc(db, urlNodes.join('/'), id);
235277
const modelName = relationship.type;
236-
const docSnapshot = relationship.options.isRealtime && !this.isFastBoot
237-
? await this.firestoreDataManager.findRecordRealtime(modelName, docRef)
238-
: await getDoc(docRef);
278+
const docSnapshot =
279+
relationship.options.isRealtime && !this.isFastBoot
280+
? await this.firestoreDataManager.findRecordRealtime(
281+
modelName,
282+
docRef,
283+
)
284+
: await getDoc(docRef);
239285

240286
if (docSnapshot.exists()) {
241287
resolve(flattenDocSnapshot(docSnapshot));
242288
} else {
243-
reject(new AdapterRecordNotFoundError(`Record ${id} for model type ${modelName} doesn't exist`));
289+
reject(
290+
new AdapterRecordNotFoundError(
291+
`Record ${id} for model type ${modelName} doesn't exist`,
292+
),
293+
);
244294
}
245295
} catch (error) {
246296
reject(error);
@@ -256,19 +306,30 @@ export default class CloudFirestoreAdapter extends Adapter {
256306
): RSVP.Promise<unknown> {
257307
return new RSVP.Promise(async (resolve, reject) => {
258308
try {
259-
const queryRef = this.buildHasManyCollectionRef(store, snapshot, url, relationship);
309+
const queryRef = this.buildHasManyCollectionRef(
310+
store,
311+
snapshot,
312+
url,
313+
relationship,
314+
);
260315
const config = {
261316
queryRef,
262317
modelName: snapshot.modelName,
263318
id: snapshot.id,
264319
field: relationship.key,
265320
referenceKeyName: this.referenceKeyName,
266321
};
267-
const documentSnapshots = relationship.options.isRealtime && !this.isFastBoot
268-
? await this.firestoreDataManager.findHasManyRealtime(config)
269-
: await this.firestoreDataManager.queryWithReferenceTo(queryRef, this.referenceKeyName);
270-
271-
const result = documentSnapshots.map((docSnapshot) => (flattenDocSnapshot(docSnapshot)));
322+
const documentSnapshots =
323+
relationship.options.isRealtime && !this.isFastBoot
324+
? await this.firestoreDataManager.findHasManyRealtime(config)
325+
: await this.firestoreDataManager.queryWithReferenceTo(
326+
queryRef,
327+
this.referenceKeyName,
328+
);
329+
330+
const result = documentSnapshots.map((docSnapshot) =>
331+
flattenDocSnapshot(docSnapshot),
332+
);
272333

273334
resolve(result);
274335
} catch (error) {
@@ -283,8 +344,10 @@ export default class CloudFirestoreAdapter extends Adapter {
283344
): CollectionReference {
284345
const db = getFirestore();
285346

286-
return adapterOptions?.buildReference?.(db)
287-
|| collection(db, buildCollectionName(modelName as string));
347+
return (
348+
adapterOptions?.buildReference?.(db) ||
349+
collection(db, buildCollectionName(modelName as string))
350+
);
288351
}
289352

290353
private addDocRefToWriteBatch(
@@ -297,13 +360,19 @@ export default class CloudFirestoreAdapter extends Adapter {
297360
batch.set(docRef, data, { merge: true });
298361
}
299362

300-
private addIncludeToWriteBatch(batch: WriteBatch, adapterOptions?: AdapterOption): void {
363+
private addIncludeToWriteBatch(
364+
batch: WriteBatch,
365+
adapterOptions?: AdapterOption,
366+
): void {
301367
const db = getFirestore();
302368

303369
adapterOptions?.include?.(batch, db);
304370
}
305371

306-
private buildWriteBatch(docRef: DocumentReference, snapshot: Snapshot): WriteBatch {
372+
private buildWriteBatch(
373+
docRef: DocumentReference,
374+
snapshot: Snapshot,
375+
): WriteBatch {
307376
const db = getFirestore();
308377
const batch = writeBatch(db);
309378

@@ -322,27 +391,49 @@ export default class CloudFirestoreAdapter extends Adapter {
322391
const db = getFirestore();
323392

324393
if (relationship.options.buildReference) {
325-
const collectionRef = relationship.options.buildReference(db, snapshot.record);
326-
327-
return relationship.options.filter?.(collectionRef, snapshot.record) || collectionRef;
394+
const collectionRef = relationship.options.buildReference(
395+
db,
396+
snapshot.record,
397+
);
398+
399+
return (
400+
relationship.options.filter?.(collectionRef, snapshot.record) ||
401+
collectionRef
402+
);
328403
}
329404

330405
const modelClass = store.modelFor(snapshot.modelName);
331-
const cardinality = modelClass.determineRelationshipType(relationship, store);
406+
const cardinality = modelClass.determineRelationshipType(
407+
relationship,
408+
store,
409+
);
332410

333411
if (cardinality === 'manyToOne') {
334412
const inverse = modelClass.inverseFor(relationship.key, store);
335-
const snapshotCollectionName = buildCollectionName(snapshot.modelName.toString());
336-
const snapshotDocRef = doc(db, `${snapshotCollectionName}/${snapshot.id}`);
413+
const snapshotCollectionName = buildCollectionName(
414+
snapshot.modelName.toString(),
415+
);
416+
const snapshotDocRef = doc(
417+
db,
418+
`${snapshotCollectionName}/${snapshot.id}`,
419+
);
337420
const collectionRef = collection(db, url);
338-
const queryRef = query(collectionRef, where(inverse.name, '==', snapshotDocRef));
339-
340-
return relationship.options.filter?.(queryRef, snapshot.record) || queryRef;
421+
const queryRef = query(
422+
collectionRef,
423+
where(inverse.name, '==', snapshotDocRef),
424+
);
425+
426+
return (
427+
relationship.options.filter?.(queryRef, snapshot.record) || queryRef
428+
);
341429
}
342430

343431
const collectionRef = collection(db, url);
344432

345-
return relationship.options.filter?.(collectionRef, snapshot.record) || collectionRef;
433+
return (
434+
relationship.options.filter?.(collectionRef, snapshot.record) ||
435+
collectionRef
436+
);
346437
}
347438
}
348439

0 commit comments

Comments
 (0)