From e216c0ffe1ec8c252b9f953b59f9731528019f0a Mon Sep 17 00:00:00 2001 From: Charles Fries Date: Mon, 22 Apr 2024 14:28:06 -0700 Subject: [PATCH] Update --- addon/adapters/cloud-firestore-modular.ts | 33 ++++---- addon/services/-firestore-data-manager.ts | 29 ++++--- package-lock.json | 96 ++++++++++++----------- package.json | 10 +-- 4 files changed, 87 insertions(+), 81 deletions(-) diff --git a/addon/adapters/cloud-firestore-modular.ts b/addon/adapters/cloud-firestore-modular.ts index 022e44d..2def1cc 100644 --- a/addon/adapters/cloud-firestore-modular.ts +++ b/addon/adapters/cloud-firestore-modular.ts @@ -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'; @@ -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; @@ -53,12 +50,12 @@ interface Snapshot extends DS.Snapshot { adapterOptions: AdapterOption; } -interface SnapshotRecordArray extends DS.SnapshotRecordArray { +interface SnapshotRecordArray extends DS.SnapshotRecordArray { adapterOptions: AdapterOption; } interface BelongsToRelationshipMeta { - type: string; + type: keyof ModelRegistry; options: { isRealtime?: boolean }; } @@ -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); @@ -94,7 +92,7 @@ export default class CloudFirestoreModularAdapter extends Adapter { public createRecord( store: Store, - type: ModelClass, + type: ModelSchema, snapshot: Snapshot, ): RSVP.Promise { return this.updateRecord(store, type, snapshot); @@ -102,7 +100,7 @@ export default class CloudFirestoreModularAdapter extends Adapter { public updateRecord( _store: Store, - type: ModelClass, + type: ModelSchema, snapshot: Snapshot, ): RSVP.Promise { return new RSVP.Promise((resolve, reject) => { @@ -127,7 +125,7 @@ export default class CloudFirestoreModularAdapter extends Adapter { public deleteRecord( _store: Store, - type: ModelClass, + type: ModelSchema, snapshot: Snapshot, ): RSVP.Promise { return new RSVP.Promise((resolve, reject) => { @@ -149,7 +147,7 @@ export default class CloudFirestoreModularAdapter extends Adapter { public findRecord( _store: Store, - type: ModelClass, + type: ModelSchema, id: string, snapshot: Snapshot, ): RSVP.Promise { @@ -174,14 +172,14 @@ export default class CloudFirestoreModularAdapter extends Adapter { public findAll( _store: Store, - type: ModelClass, + type: ModelSchema, _sinceToken: string, snapshotRecordArray?: SnapshotRecordArray, ): RSVP.Promise { 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); @@ -197,7 +195,7 @@ export default class CloudFirestoreModularAdapter extends Adapter { public query( _store: Store, - type: ModelClass, + type: ModelSchema, queryOption: AdapterOption, recordArray: DS.AdapterPopulatedRecordArray, ): RSVP.Promise { @@ -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, @@ -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( diff --git a/addon/services/-firestore-data-manager.ts b/addon/services/-firestore-data-manager.ts index effda9e..2c722fc 100644 --- a/addon/services/-firestore-data-manager.ts +++ b/addon/services/-firestore-data-manager.ts @@ -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'; @@ -38,7 +43,7 @@ interface QueryListeners { } interface QueryFetchConfig { - modelName: string; + modelName: keyof ModelRegistry; referenceKeyName: string; recordArray: DS.AdapterPopulatedRecordArray; queryRef: Query, @@ -46,7 +51,7 @@ interface QueryFetchConfig { } interface HasManyFetchConfig { - modelName: string; + modelName: keyof ModelRegistry; id: string; field: string; referenceKeyName: string; @@ -75,7 +80,7 @@ export default class FirestoreDataManager extends Service { } public async findRecordRealtime( - modelName: string, + modelName: keyof ModelRegistry, docRef: DocumentReference, ): Promise { const { path: listenerKey } = docRef; @@ -88,7 +93,7 @@ export default class FirestoreDataManager extends Service { } public async findAllRealtime( - modelName: string, + modelName: keyof ModelRegistry, colRef: CollectionReference, ): Promise { const { path: listenerKey } = colRef; @@ -150,7 +155,7 @@ export default class FirestoreDataManager extends Service { } private setupDocRealtimeUpdates( - modelName: string, + modelName: keyof ModelRegistry, docRef: DocumentReference, ): Promise { return new Promise((resolve, reject) => { @@ -171,7 +176,7 @@ export default class FirestoreDataManager extends Service { } private setupColRealtimeUpdates( - modelName: string, + modelName: keyof ModelRegistry, colRef: CollectionReference, ): Promise { return new Promise((resolve, reject) => { @@ -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()) { @@ -263,7 +268,7 @@ export default class FirestoreDataManager extends Service { } private handleSubsequentColRealtimeUpdates( - modelName: string, + modelName: keyof ModelRegistry, listenerKey: string, querySnapshot: QuerySnapshot, ): void { @@ -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 { @@ -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); @@ -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) { diff --git a/package-lock.json b/package-lock.json index c5fc15e..e252aaa 100644 --- a/package-lock.json +++ b/package-lock.json @@ -37,11 +37,11 @@ "@types/ember__template": "^3.16.1", "@types/ember__test": "^3.16.1", "@types/ember__utils": "^3.16.2", - "@types/ember-data": "^3.16.15", - "@types/ember-data__adapter": "^3.16.1", - "@types/ember-data__model": "^3.16.2", - "@types/ember-data__serializer": "^3.16.1", - "@types/ember-data__store": "^3.16.1", + "@types/ember-data": "^4.4.16", + "@types/ember-data__adapter": "^4.0.6", + "@types/ember-data__model": "^4.0.5", + "@types/ember-data__serializer": "^4.0.6", + "@types/ember-data__store": "^4.0.7", "@types/ember-qunit": "^3.4.14", "@types/ember-resolver": "^5.0.10", "@types/htmlbars-inline-precompile": "^1.0.1", @@ -5397,50 +5397,51 @@ "dev": true }, "node_modules/@types/ember-data": { - "version": "3.16.18", - "resolved": "https://registry.npmjs.org/@types/ember-data/-/ember-data-3.16.18.tgz", - "integrity": "sha512-7Dh6cexENDulsR2mEkyEO7dCUm3BDUbn5p0nrnnefNYw1OBQL1etHA4WKUbjKeHhFVLvVel2zTnyNHheCrcF6g==", + "version": "4.4.16", + "resolved": "https://registry.npmjs.org/@types/ember-data/-/ember-data-4.4.16.tgz", + "integrity": "sha512-plFqPkgw7n4YlkzvApkQAIhvvYTERlx8PeI2J5gSFtMtsKuoaIo8fXm4w22ZdBQtTzeh/kwvpO2WY8R/d5Ttfg==", "dev": true, "dependencies": { - "@types/ember": "^3", - "@types/ember__object": "^3", + "@types/ember": "*", + "@types/ember__error": "*", + "@types/ember__object": "*", "@types/rsvp": "*" } }, "node_modules/@types/ember-data__adapter": { - "version": "3.16.2", - "resolved": "https://registry.npmjs.org/@types/ember-data__adapter/-/ember-data__adapter-3.16.2.tgz", - "integrity": "sha512-VvCmA4A47AKx0w8kehGXj84w+QVoYGiyHHnQkvrGUVuU2Z6oi9eBXMF7W7ROj0jjTneiYXLWlk/d5UIBFoglBw==", + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/@types/ember-data__adapter/-/ember-data__adapter-4.0.6.tgz", + "integrity": "sha512-vSrx1cqz8jO0cBYwu98SrwFk5s722C+BGpw++NregwQio3g9VbNoymx+HsYeXvsWvGUsoRewxu9K7TwAOJSsvw==", "dev": true, "dependencies": { - "@types/ember-data": "^3" + "@types/ember-data": "*" } }, "node_modules/@types/ember-data__model": { - "version": "3.16.3", - "resolved": "https://registry.npmjs.org/@types/ember-data__model/-/ember-data__model-3.16.3.tgz", - "integrity": "sha512-tBJePqVH2XQMIxAPuZUqxV2tbQ7CK2ViL28hlvsEZwdNQbHLFd0cP4gOWD9Gy87mXTRlM9vBsK40hlZHa/4kWg==", + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/@types/ember-data__model/-/ember-data__model-4.0.5.tgz", + "integrity": "sha512-zyfHh3tQiMdpbPZ1/RFxi4m9TdwsZxDmVY0e7V1UYp7pWvm/DeqEXqoy3WS9xa/O01xoJFpQtJHyasdXvVTfbg==", "dev": true, "dependencies": { - "@types/ember-data": "^3" + "@types/ember-data": "*" } }, "node_modules/@types/ember-data__serializer": { - "version": "3.16.2", - "resolved": "https://registry.npmjs.org/@types/ember-data__serializer/-/ember-data__serializer-3.16.2.tgz", - "integrity": "sha512-aEg2rUMb8yGHEzcvMDJXZdPTC+1RAxWEmnCUXDZPVPsjFcOvRgkWFJtq6UfSpRuDylA+e5yY9EHZVkNaQJ6RKQ==", + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/@types/ember-data__serializer/-/ember-data__serializer-4.0.6.tgz", + "integrity": "sha512-ebxxySSvTiay1/hgN4fElahSuL0MX+1sMMwx/RQyy7z98ifV8wafW62BwlYtbH44aeTIdcaSBE2pm3R1aP2yUA==", "dev": true, "dependencies": { - "@types/ember-data": "^3" + "@types/ember-data": "*" } }, "node_modules/@types/ember-data__store": { - "version": "3.16.2", - "resolved": "https://registry.npmjs.org/@types/ember-data__store/-/ember-data__store-3.16.2.tgz", - "integrity": "sha512-KWNpUHSHru1hDArK/TEqW610caCw3RqG3opn+MqadzRuSaSUVsOEK84A6C6vBXKh4JTukfCzh0mHlfJsiG7D8Q==", + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/@types/ember-data__store/-/ember-data__store-4.0.7.tgz", + "integrity": "sha512-0S3Etr32i4aGvhXhZM6CXaosvX8E6c+RCx8pCxUXVdn+SmICm8jqFXsDC6Gnqq4bJeTkf9cglqz46prOVR1Tcg==", "dev": true, "dependencies": { - "@types/ember-data": "^3" + "@types/ember-data": "*" } }, "node_modules/@types/ember-qunit": { @@ -46863,50 +46864,51 @@ "dev": true }, "@types/ember-data": { - "version": "3.16.18", - "resolved": "https://registry.npmjs.org/@types/ember-data/-/ember-data-3.16.18.tgz", - "integrity": "sha512-7Dh6cexENDulsR2mEkyEO7dCUm3BDUbn5p0nrnnefNYw1OBQL1etHA4WKUbjKeHhFVLvVel2zTnyNHheCrcF6g==", + "version": "4.4.16", + "resolved": "https://registry.npmjs.org/@types/ember-data/-/ember-data-4.4.16.tgz", + "integrity": "sha512-plFqPkgw7n4YlkzvApkQAIhvvYTERlx8PeI2J5gSFtMtsKuoaIo8fXm4w22ZdBQtTzeh/kwvpO2WY8R/d5Ttfg==", "dev": true, "requires": { - "@types/ember": "^3", - "@types/ember__object": "^3", + "@types/ember": "*", + "@types/ember__error": "*", + "@types/ember__object": "*", "@types/rsvp": "*" } }, "@types/ember-data__adapter": { - "version": "3.16.2", - "resolved": "https://registry.npmjs.org/@types/ember-data__adapter/-/ember-data__adapter-3.16.2.tgz", - "integrity": "sha512-VvCmA4A47AKx0w8kehGXj84w+QVoYGiyHHnQkvrGUVuU2Z6oi9eBXMF7W7ROj0jjTneiYXLWlk/d5UIBFoglBw==", + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/@types/ember-data__adapter/-/ember-data__adapter-4.0.6.tgz", + "integrity": "sha512-vSrx1cqz8jO0cBYwu98SrwFk5s722C+BGpw++NregwQio3g9VbNoymx+HsYeXvsWvGUsoRewxu9K7TwAOJSsvw==", "dev": true, "requires": { - "@types/ember-data": "^3" + "@types/ember-data": "*" } }, "@types/ember-data__model": { - "version": "3.16.3", - "resolved": "https://registry.npmjs.org/@types/ember-data__model/-/ember-data__model-3.16.3.tgz", - "integrity": "sha512-tBJePqVH2XQMIxAPuZUqxV2tbQ7CK2ViL28hlvsEZwdNQbHLFd0cP4gOWD9Gy87mXTRlM9vBsK40hlZHa/4kWg==", + "version": "4.0.5", + "resolved": "https://registry.npmjs.org/@types/ember-data__model/-/ember-data__model-4.0.5.tgz", + "integrity": "sha512-zyfHh3tQiMdpbPZ1/RFxi4m9TdwsZxDmVY0e7V1UYp7pWvm/DeqEXqoy3WS9xa/O01xoJFpQtJHyasdXvVTfbg==", "dev": true, "requires": { - "@types/ember-data": "^3" + "@types/ember-data": "*" } }, "@types/ember-data__serializer": { - "version": "3.16.2", - "resolved": "https://registry.npmjs.org/@types/ember-data__serializer/-/ember-data__serializer-3.16.2.tgz", - "integrity": "sha512-aEg2rUMb8yGHEzcvMDJXZdPTC+1RAxWEmnCUXDZPVPsjFcOvRgkWFJtq6UfSpRuDylA+e5yY9EHZVkNaQJ6RKQ==", + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/@types/ember-data__serializer/-/ember-data__serializer-4.0.6.tgz", + "integrity": "sha512-ebxxySSvTiay1/hgN4fElahSuL0MX+1sMMwx/RQyy7z98ifV8wafW62BwlYtbH44aeTIdcaSBE2pm3R1aP2yUA==", "dev": true, "requires": { - "@types/ember-data": "^3" + "@types/ember-data": "*" } }, "@types/ember-data__store": { - "version": "3.16.2", - "resolved": "https://registry.npmjs.org/@types/ember-data__store/-/ember-data__store-3.16.2.tgz", - "integrity": "sha512-KWNpUHSHru1hDArK/TEqW610caCw3RqG3opn+MqadzRuSaSUVsOEK84A6C6vBXKh4JTukfCzh0mHlfJsiG7D8Q==", + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/@types/ember-data__store/-/ember-data__store-4.0.7.tgz", + "integrity": "sha512-0S3Etr32i4aGvhXhZM6CXaosvX8E6c+RCx8pCxUXVdn+SmICm8jqFXsDC6Gnqq4bJeTkf9cglqz46prOVR1Tcg==", "dev": true, "requires": { - "@types/ember-data": "^3" + "@types/ember-data": "*" } }, "@types/ember-qunit": { diff --git a/package.json b/package.json index 8e14206..fa5a643 100644 --- a/package.json +++ b/package.json @@ -60,11 +60,11 @@ "@types/ember__template": "^3.16.1", "@types/ember__test": "^3.16.1", "@types/ember__utils": "^3.16.2", - "@types/ember-data": "^3.16.15", - "@types/ember-data__adapter": "^3.16.1", - "@types/ember-data__model": "^3.16.2", - "@types/ember-data__serializer": "^3.16.1", - "@types/ember-data__store": "^3.16.1", + "@types/ember-data": "^4.4.16", + "@types/ember-data__adapter": "^4.0.6", + "@types/ember-data__model": "^4.0.5", + "@types/ember-data__serializer": "^4.0.6", + "@types/ember-data__store": "^4.0.7", "@types/ember-qunit": "^3.4.14", "@types/ember-resolver": "^5.0.10", "@types/htmlbars-inline-precompile": "^1.0.1",