Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to complete Firebase Modular SDK #252

Merged
merged 3 commits into from
Nov 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions addon/-private/flatten-doc-snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { DocumentSnapshot } from 'firebase/firestore';
import firebase from 'firebase/compat/app';

export default function flattenDocSnapshot(
docSnapshot: firebase.firestore.DocumentSnapshot | DocumentSnapshot,
): {
export default function flattenDocSnapshot(docSnapshot: DocumentSnapshot): {
id: string,
[key: string]: unknown,
} {
Expand Down
206 changes: 0 additions & 206 deletions addon/-private/realtime-tracker.ts

This file was deleted.

29 changes: 13 additions & 16 deletions addon/adapters/cloud-firestore-modular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ import Store from '@ember-data/store';
import {
CollectionReference,
DocumentReference,
Firestore,
Query,
WriteBatch,
} from 'firebase/firestore';
import firebase from 'firebase/compat/app';

import {
collection,
doc,
getDoc,
getDocs,
getFirestore,
query,
where,
writeBatch,
} from 'ember-cloud-firestore-adapter/firebase/firestore';
import { AdapterRecordNotFoundError } from 'ember-cloud-firestore-adapter/utils/custom-errors';
import FirebaseService from 'ember-cloud-firestore-adapter/services/-firebase';
import FirestoreDataManager from 'ember-cloud-firestore-adapter/services/-firestore-data-manager';
import buildCollectionName from 'ember-cloud-firestore-adapter/-private/build-collection-name';
import flattenDocSnapshot from 'ember-cloud-firestore-adapter/-private/flatten-doc-snapshot';
Expand All @@ -42,9 +42,9 @@ interface AdapterOption {
isRealtime?: boolean;
queryId?: string;

buildReference?(db: firebase.firestore.Firestore): CollectionReference;
buildReference?(db: Firestore): CollectionReference;
filter?(db: CollectionReference): Query;
include?(batch: WriteBatch, db: firebase.firestore.Firestore): void;
include?(batch: WriteBatch, db: Firestore): void;

[key: string]: unknown;
}
Expand All @@ -68,15 +68,12 @@ interface HasManyRelationshipMeta {
options: {
isRealtime?: boolean,

buildReference?(db: firebase.firestore.Firestore, record: unknown): CollectionReference,
buildReference?(db: Firestore, record: unknown): CollectionReference,
filter?(db: CollectionReference | Query, record: unknown): Query,
};
}

export default class CloudFirestoreModularAdapter extends Adapter {
@service('-firebase')
protected declare firebase: FirebaseService;

@service('-firestore-data-manager')
private declare firestoreDataManager: FirestoreDataManager;

Expand All @@ -89,7 +86,7 @@ export default class CloudFirestoreModularAdapter extends Adapter {
}

public generateIdForRecord(_store: Store, type: string): string {
const db = this.firebase.firestore();
const db = getFirestore();
const collectionName = buildCollectionName(type);

return doc(collection(db, collectionName)).id;
Expand Down Expand Up @@ -134,7 +131,7 @@ export default class CloudFirestoreModularAdapter extends Adapter {
snapshot: Snapshot,
): RSVP.Promise<unknown> {
return new RSVP.Promise((resolve, reject) => {
const db = this.firebase.firestore();
const db = getFirestore();
const collectionRef = this.buildCollectionRef(type.modelName, snapshot.adapterOptions);
const docRef = doc(collectionRef, snapshot.id);
const batch = writeBatch(db);
Expand Down Expand Up @@ -183,7 +180,7 @@ export default class CloudFirestoreModularAdapter extends Adapter {
): RSVP.Promise<unknown> {
return new RSVP.Promise(async (resolve, reject) => {
try {
const db = this.firebase.firestore();
const db = getFirestore();
const colRef = collection(db, buildCollectionName(type.modelName));
const querySnapshot = snapshotRecordArray?.adapterOptions?.isRealtime && !this.isFastBoot
? await this.firestoreDataManager.findAllRealtime(type.modelName, colRef)
Expand Down Expand Up @@ -241,7 +238,7 @@ export default class CloudFirestoreModularAdapter extends Adapter {

urlNodes.pop();

const db = this.firebase.firestore();
const db = getFirestore();
const docRef = doc(db, urlNodes.join('/'), id);
const modelName = relationship.type;
const docSnapshot = relationship.options.isRealtime && !this.isFastBoot
Expand Down Expand Up @@ -292,7 +289,7 @@ export default class CloudFirestoreModularAdapter extends Adapter {
modelName: string,
adapterOptions?: AdapterOption,
): CollectionReference {
const db = this.firebase.firestore();
const db = getFirestore();

return adapterOptions?.buildReference?.(db) || collection(db, buildCollectionName(modelName));
}
Expand All @@ -308,13 +305,13 @@ export default class CloudFirestoreModularAdapter extends Adapter {
}

private addIncludeToWriteBatch(batch: WriteBatch, adapterOptions?: AdapterOption): void {
const db = this.firebase.firestore();
const db = getFirestore();

adapterOptions?.include?.(batch, db);
}

private buildWriteBatch(docRef: DocumentReference, snapshot: Snapshot): WriteBatch {
const db = this.firebase.firestore();
const db = getFirestore();
const batch = writeBatch(db);

this.addDocRefToWriteBatch(batch, docRef, snapshot);
Expand All @@ -329,7 +326,7 @@ export default class CloudFirestoreModularAdapter extends Adapter {
url: string,
relationship: HasManyRelationshipMeta,
): CollectionReference | Query {
const db = this.firebase.firestore();
const db = getFirestore();

if (relationship.options.buildReference) {
const collectionRef = relationship.options.buildReference(db, snapshot.record);
Expand Down
Loading