diff --git a/addon/adapters/cloud-firestore.js b/addon/adapters/cloud-firestore.js index 0286e5dc..8b2129a4 100644 --- a/addon/adapters/cloud-firestore.js +++ b/addon/adapters/cloud-firestore.js @@ -1,6 +1,6 @@ import { Promise } from 'rsvp'; import { getOwner } from '@ember/application'; -import { inject } from '@ember/service'; +import { inject as service } from '@ember/service'; import { run } from '@ember/runloop'; import RESTAdapter from 'ember-data/adapters/rest'; @@ -19,7 +19,7 @@ export default RESTAdapter.extend({ /** * @type {Ember.Service} */ - firestore: inject(), + firebase: service(), /** * @type {string} @@ -32,6 +32,11 @@ export default RESTAdapter.extend({ */ defaultSerializer: 'cloud-firestore', + /** + * @type {Object} + */ + firestoreSettings: { timestampsInSnapshots: true }, + /** * @override */ @@ -42,11 +47,24 @@ export default RESTAdapter.extend({ */ willUnloadRecordOnListenError: true, + /** + * @override + */ + init(...args) { + this._super(...args); + + if (this.get('firestoreSettings')) { + const db = this.get('firebase').firestore(); + + db.settings(this.get('firestoreSettings')); + } + }, + /** * @override */ generateIdForRecord(store, type) { - const db = this.get('firestore.instance'); + const db = this.get('firebase').firestore(); const collectionName = buildCollectionName(type); return db.collection(collectionName).doc().id; @@ -138,7 +156,7 @@ export default RESTAdapter.extend({ } return new Promise((resolve, reject) => { - const db = this.get('firestore.instance'); + const db = this.get('firebase').firestore(); const docRef = db.collection(buildCollectionName(type.modelName)).doc(snapshot.id); const batch = this.buildWriteBatch(type, snapshot, docRef, true); @@ -151,7 +169,7 @@ export default RESTAdapter.extend({ */ findAll(store, type) { return new Promise((resolve, reject) => { - const db = this.get('firestore.instance'); + const db = this.get('firebase').firestore(); const collectionName = buildCollectionName(type.modelName); const collectionRef = db.collection(collectionName); const unsubscribe = collectionRef.onSnapshot((querySnapshot) => { @@ -174,7 +192,7 @@ export default RESTAdapter.extend({ */ findRecord(store, type, id, snapshot = {}) { return new Promise((resolve, reject) => { - const db = this.get('firestore.instance'); + const db = this.get('firebase').firestore(); const collectionRef = this.buildCollectionRef(type.modelName, snapshot.adapterOptions, db); const docRef = collectionRef.doc(id); const unsubscribe = docRef.onSnapshot((docSnapshot) => { @@ -236,7 +254,7 @@ export default RESTAdapter.extend({ */ query(store, type, option = {}) { return new Promise((resolve, reject) => { - const db = this.get('firestore.instance'); + const db = this.get('firebase').firestore(); let collectionRef = this.buildCollectionRef(type.modelName, option, db); collectionRef = this.buildQuery(collectionRef, option); @@ -307,7 +325,7 @@ export default RESTAdapter.extend({ * @private */ buildHasManyCollectionRef(store, snapshot, url, relationship) { - const db = this.get('firestore.instance'); + const db = this.get('firebase').firestore(); const cardinality = snapshot.type.determineRelationshipType(relationship, store); let collectionRef; @@ -345,7 +363,7 @@ export default RESTAdapter.extend({ return this.buildCollectionRef( type.modelName, snapshot.adapterOptions, - this.get('firestore.instance'), + this.get('firebase').firestore(), ).doc(snapshot.id); }, @@ -359,7 +377,7 @@ export default RESTAdapter.extend({ * @private */ buildWriteBatch(type, snapshot, docRef, isDeletingMainDoc) { - const db = this.get('firestore.instance'); + const db = this.get('firebase').firestore(); const payload = this.serialize(snapshot); const batch = db.batch(); diff --git a/addon/serializers/cloud-firestore.js b/addon/serializers/cloud-firestore.js index cbe0d543..1dec0081 100644 --- a/addon/serializers/cloud-firestore.js +++ b/addon/serializers/cloud-firestore.js @@ -17,7 +17,7 @@ export default JSONSerializer.extend({ /** * @type {Ember.Service} */ - firestore: inject(), + firebase: inject(), /** * Overriden to properly get the data of a `Reference` type relationship @@ -97,7 +97,7 @@ export default JSONSerializer.extend({ if (this.getAdapterOptionAttribute(snapshot, 'onServer')) { json[relationship.key] = path; } else { - json[relationship.key] = buildRefFromPath(this.get('firestore.instance'), path); + json[relationship.key] = buildRefFromPath(this.get('firebase').firestore(), path); } } }, @@ -118,7 +118,7 @@ export default JSONSerializer.extend({ if (this.getAdapterOptionAttribute(snapshot, 'onServer')) { references.push(path); } else { - references.push(buildRefFromPath(this.get('firestore.instance'), path)); + references.push(buildRefFromPath(this.get('firebase').firestore(), path)); } }); diff --git a/addon/services/firestore.js b/addon/services/firestore.js deleted file mode 100644 index e071c1f8..00000000 --- a/addon/services/firestore.js +++ /dev/null @@ -1,31 +0,0 @@ -import Service, { inject as service } from '@ember/service'; - -/** - * @class Firestore - * @namespace Service - * @extends Ember.Service - */ -export default Service.extend({ - /** - * @type {Ember.Service} - */ - firebase: service(), - - /** - * @type {Object} - */ - settings: { timestampsInSnapshots: true }, - - /** - * @override - */ - init(...args) { - this._super(...args); - - const firestore = this.get('firebase').firestore(); - - firestore.settings(this.get('settings')); - - this.set('instance', firestore); - }, -}); diff --git a/app/services/firestore.js b/app/services/firestore.js deleted file mode 100644 index cf966e2a..00000000 --- a/app/services/firestore.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from 'ember-cloud-firestore-adapter/services/firestore'; diff --git a/guides/02-configuration.md b/guides/02-configuration.md index 3de65420..2295bf32 100644 --- a/guides/02-configuration.md +++ b/guides/02-configuration.md @@ -41,6 +41,7 @@ export default CloudFirestoreAdapter.extend(); These are the settings currently available +- `firestoreSettings` - Specifies custom configurations for your Cloud Firestore instance. See [here](https://firebase.google.com/docs/reference/js/firebase.firestore.Settings). - `willUnloadRecordOnListenError` - Unloads a record whenever a listener for a document fails (e.g. sudden permission denied). Defaults to `true`. --- diff --git a/tests/unit/adapters/cloud-firestore-test.js b/tests/unit/adapters/cloud-firestore-test.js index bf9bf547..3636d810 100644 --- a/tests/unit/adapters/cloud-firestore-test.js +++ b/tests/unit/adapters/cloud-firestore-test.js @@ -210,8 +210,8 @@ module('Unit | Adapter | cloud firestore', function (hooks) { const snapshot = { id: 'user_a' }; const adapter = this.owner.lookup('adapter:cloud-firestore'); - adapter.set('firestore', { - instance: { + adapter.set('firebase', { + firestore: sinon.stub().returns({ batch() { return { set() {}, @@ -229,7 +229,7 @@ module('Unit | Adapter | cloud firestore', function (hooks) { }, }; }, - }, + }), }); adapter.set('serialize', () => [ { @@ -306,8 +306,8 @@ module('Unit | Adapter | cloud firestore', function (hooks) { const snapshot = { id: 'user_a' }; const adapter = this.owner.lookup('adapter:cloud-firestore'); - adapter.set('firestore', { - instance: { + adapter.set('firebase', { + firestore: sinon.stub().returns({ batch() { return { commit() { @@ -323,7 +323,7 @@ module('Unit | Adapter | cloud firestore', function (hooks) { doc() {}, }; }, - }, + }), }); adapter.set('serialize', () => [ { @@ -391,8 +391,8 @@ module('Unit | Adapter | cloud firestore', function (hooks) { // Arrange const adapter = this.owner.lookup('adapter:cloud-firestore'); - adapter.set('firestore', { - instance: { + adapter.set('firebase', { + firestore: sinon.stub().returns({ collection() { return { onSnapshot(onSuccess, onError) { @@ -400,7 +400,7 @@ module('Unit | Adapter | cloud firestore', function (hooks) { }, }; }, - }, + }), }); try { @@ -469,8 +469,8 @@ module('Unit | Adapter | cloud firestore', function (hooks) { const modelId = 'user_a'; const adapter = this.owner.lookup('adapter:cloud-firestore'); - adapter.set('firestore', { - instance: { + adapter.set('firebase', { + firestore: sinon.stub().returns({ collection() { return { doc() { @@ -482,7 +482,7 @@ module('Unit | Adapter | cloud firestore', function (hooks) { }, }; }, - }, + }), }); try { @@ -797,8 +797,8 @@ module('Unit | Adapter | cloud firestore', function (hooks) { }; const adapter = this.owner.lookup('adapter:cloud-firestore'); - adapter.set('firestore', { - instance: { + adapter.set('firebase', { + firestore: sinon.stub().returns({ collection() { return { where() { @@ -810,7 +810,7 @@ module('Unit | Adapter | cloud firestore', function (hooks) { }, }; }, - }, + }), }); try { diff --git a/tests/unit/services/firestore-test.js b/tests/unit/services/firestore-test.js deleted file mode 100644 index 1b37126f..00000000 --- a/tests/unit/services/firestore-test.js +++ /dev/null @@ -1,13 +0,0 @@ -import { module, test } from 'qunit'; -import { setupTest } from 'ember-qunit'; - -module('Unit | Service | firestore', function (hooks) { - setupTest(hooks); - - // Replace this with your real tests. - test('it exists', function (assert) { - const service = this.owner.lookup('service:firestore'); - assert.ok(service); - }); -}); -