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

chore: update EmberData types packages, use keyof ModelRegistry #293

Merged
merged 1 commit into from
Apr 22, 2024
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
33 changes: 16 additions & 17 deletions addon/adapters/cloud-firestore-modular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import { getOwner } from '@ember/application';
import { inject as service } from '@ember/service';
import Adapter from '@ember-data/adapter';
import DS from 'ember-data';
import DS, { ModelSchema } from 'ember-data';
import ModelRegistry from 'ember-data/types/registries/model';
import RSVP from 'rsvp';
import Store from '@ember-data/store';

Expand All @@ -34,10 +35,6 @@ import FirestoreDataManager from 'ember-cloud-firestore-adapter/services/-firest
import buildCollectionName from 'ember-cloud-firestore-adapter/-private/build-collection-name';
import flattenDocSnapshot from 'ember-cloud-firestore-adapter/-private/flatten-doc-snapshot';

interface ModelClass {
modelName: string;
}

interface AdapterOption {
isRealtime?: boolean;
queryId?: string;
Expand All @@ -53,12 +50,12 @@ interface Snapshot extends DS.Snapshot {
adapterOptions: AdapterOption;
}

interface SnapshotRecordArray extends DS.SnapshotRecordArray<string | number> {
interface SnapshotRecordArray extends DS.SnapshotRecordArray<keyof ModelRegistry> {
adapterOptions: AdapterOption;
}

interface BelongsToRelationshipMeta {
type: string;
type: keyof ModelRegistry;
options: { isRealtime?: boolean };
}

Expand All @@ -85,6 +82,7 @@ export default class CloudFirestoreModularAdapter extends Adapter {
return fastboot && fastboot.isFastBoot;
}

// @ts-expect-error EmberData types incorrect
public generateIdForRecord(_store: Store, type: string): string {
const db = getFirestore();
const collectionName = buildCollectionName(type);
Expand All @@ -94,15 +92,15 @@ export default class CloudFirestoreModularAdapter extends Adapter {

public createRecord(
store: Store,
type: ModelClass,
type: ModelSchema,
snapshot: Snapshot,
): RSVP.Promise<unknown> {
return this.updateRecord(store, type, snapshot);
}

public updateRecord(
_store: Store,
type: ModelClass,
type: ModelSchema,
snapshot: Snapshot,
): RSVP.Promise<unknown> {
return new RSVP.Promise((resolve, reject) => {
Expand All @@ -127,7 +125,7 @@ export default class CloudFirestoreModularAdapter extends Adapter {

public deleteRecord(
_store: Store,
type: ModelClass,
type: ModelSchema,
snapshot: Snapshot,
): RSVP.Promise<unknown> {
return new RSVP.Promise((resolve, reject) => {
Expand All @@ -149,7 +147,7 @@ export default class CloudFirestoreModularAdapter extends Adapter {

public findRecord(
_store: Store,
type: ModelClass,
type: ModelSchema,
id: string,
snapshot: Snapshot,
): RSVP.Promise<unknown> {
Expand All @@ -174,14 +172,14 @@ export default class CloudFirestoreModularAdapter extends Adapter {

public findAll(
_store: Store,
type: ModelClass,
type: ModelSchema,
_sinceToken: string,
snapshotRecordArray?: SnapshotRecordArray,
): RSVP.Promise<unknown> {
return new RSVP.Promise(async (resolve, reject) => {
try {
const db = getFirestore();
const colRef = collection(db, buildCollectionName(type.modelName));
const colRef = collection(db, buildCollectionName(type.modelName as string));
const querySnapshot = snapshotRecordArray?.adapterOptions?.isRealtime && !this.isFastBoot
? await this.firestoreDataManager.findAllRealtime(type.modelName, colRef)
: await getDocs(colRef);
Expand All @@ -197,7 +195,7 @@ export default class CloudFirestoreModularAdapter extends Adapter {

public query(
_store: Store,
type: ModelClass,
type: ModelSchema,
queryOption: AdapterOption,
recordArray: DS.AdapterPopulatedRecordArray<unknown>,
): RSVP.Promise<unknown> {
Expand Down Expand Up @@ -267,7 +265,7 @@ export default class CloudFirestoreModularAdapter extends Adapter {
const queryRef = this.buildHasManyCollectionRef(store, snapshot, url, relationship);
const config = {
queryRef,
modelName: snapshot.modelName as string,
modelName: snapshot.modelName,
id: snapshot.id,
field: relationship.key,
referenceKeyName: this.referenceKeyName,
Expand All @@ -286,12 +284,13 @@ export default class CloudFirestoreModularAdapter extends Adapter {
}

private buildCollectionRef(
modelName: string,
modelName: keyof ModelRegistry,
adapterOptions?: AdapterOption,
): CollectionReference {
const db = getFirestore();

return adapterOptions?.buildReference?.(db) || collection(db, buildCollectionName(modelName));
return adapterOptions?.buildReference?.(db)
|| collection(db, buildCollectionName(modelName as string));
}

private addDocRefToWriteBatch(
Expand Down
29 changes: 17 additions & 12 deletions addon/services/-firestore-data-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/*
eslint
ember/use-ember-data-rfc-395-imports: off
*/

import { next } from '@ember/runloop';
// eslint-disable-next-line ember/use-ember-data-rfc-395-imports
import DS from 'ember-data';
import ModelRegistry from 'ember-data/types/registries/model';
import Service, { inject as service } from '@ember/service';
import StoreService from '@ember-data/store';

Expand Down Expand Up @@ -38,15 +43,15 @@ interface QueryListeners {
}

interface QueryFetchConfig {
modelName: string;
modelName: keyof ModelRegistry;
referenceKeyName: string;
recordArray: DS.AdapterPopulatedRecordArray<unknown>;
queryRef: Query,
queryId?: string,
}

interface HasManyFetchConfig {
modelName: string;
modelName: keyof ModelRegistry;
id: string;
field: string;
referenceKeyName: string;
Expand Down Expand Up @@ -75,7 +80,7 @@ export default class FirestoreDataManager extends Service {
}

public async findRecordRealtime(
modelName: string,
modelName: keyof ModelRegistry,
docRef: DocumentReference,
): Promise<DocumentSnapshot> {
const { path: listenerKey } = docRef;
Expand All @@ -88,7 +93,7 @@ export default class FirestoreDataManager extends Service {
}

public async findAllRealtime(
modelName: string,
modelName: keyof ModelRegistry,
colRef: CollectionReference,
): Promise<QuerySnapshot> {
const { path: listenerKey } = colRef;
Expand Down Expand Up @@ -150,7 +155,7 @@ export default class FirestoreDataManager extends Service {
}

private setupDocRealtimeUpdates(
modelName: string,
modelName: keyof ModelRegistry,
docRef: DocumentReference,
): Promise<void> {
return new Promise((resolve, reject) => {
Expand All @@ -171,7 +176,7 @@ export default class FirestoreDataManager extends Service {
}

private setupColRealtimeUpdates(
modelName: string,
modelName: keyof ModelRegistry,
colRef: CollectionReference,
): Promise<void> {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -251,7 +256,7 @@ export default class FirestoreDataManager extends Service {

private handleSubsequentDocRealtimeUpdates(
docSnapshot: DocumentSnapshot,
modelName: string,
modelName: keyof ModelRegistry,
listenerKey: string,
): void {
if (docSnapshot.exists()) {
Expand All @@ -263,7 +268,7 @@ export default class FirestoreDataManager extends Service {
}

private handleSubsequentColRealtimeUpdates(
modelName: string,
modelName: keyof ModelRegistry,
listenerKey: string,
querySnapshot: QuerySnapshot,
): void {
Expand Down Expand Up @@ -347,7 +352,7 @@ export default class FirestoreDataManager extends Service {

public async getReferenceToDoc(
docSnapshot: DocumentSnapshot,
modelName: string,
modelName: keyof ModelRegistry,
referenceKeyName: string,
isRealtime = false,
): Promise<DocumentSnapshot> {
Expand All @@ -360,7 +365,7 @@ export default class FirestoreDataManager extends Service {
return docSnapshot;
}

private pushRecord(modelName: string, snapshot: DocumentSnapshot): void {
private pushRecord(modelName: keyof ModelRegistry, snapshot: DocumentSnapshot): void {
const flatRecord = flattenDocSnapshot(snapshot);
const normalizedRecord = this.store.normalize(modelName, flatRecord);

Expand All @@ -373,7 +378,7 @@ export default class FirestoreDataManager extends Service {
}
}

private unloadRecord(modelName: string, id: string, path?: string): void {
private unloadRecord(modelName: keyof ModelRegistry, id: string, path?: string): void {
const record = this.store.peekRecord(modelName, id);

if (record !== null) {
Expand Down
Loading
Loading