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

Makes firestore a singleton and sets correct timestamp setting #48

Merged
merged 5 commits into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
18 changes: 9 additions & 9 deletions addon/adapters/cloud-firestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default RESTAdapter.extend({
/**
* @type {Ember.Service}
*/
firebase: inject(),
firestore: inject(),

/**
* @type {string}
Expand All @@ -46,7 +46,7 @@ export default RESTAdapter.extend({
* @override
*/
generateIdForRecord(store, type) {
const db = this.get('firebase').firestore();
const db = this.get('firestore.instance');
const collectionName = buildCollectionName(type);

return db.collection(collectionName).doc().id;
Expand Down Expand Up @@ -139,7 +139,7 @@ export default RESTAdapter.extend({
return this._super(store, type, snapshot);
} else {
return new Promise((resolve, reject) => {
const db = this.get('firebase').firestore();
const db = this.get('firestore.instance');
const docRef = db
.collection(buildCollectionName(type.modelName))
.doc(snapshot.id);
Expand All @@ -159,7 +159,7 @@ export default RESTAdapter.extend({
*/
findAll(store, type) {
return new Promise((resolve, reject) => {
const db = this.get('firebase').firestore();
const db = this.get('firestore.instance');
const collectionName = buildCollectionName(type.modelName);
const collectionRef = db.collection(collectionName);
const unsubscribe = collectionRef.onSnapshot((querySnapshot) => {
Expand Down Expand Up @@ -196,7 +196,7 @@ export default RESTAdapter.extend({
*/
findRecord(store, type, id, snapshot = {}) {
return new Promise((resolve, reject) => {
const db = this.get('firebase').firestore();
const db = this.get('firestore.instance');
const collectionRef = this.buildCollectionRef(
type.modelName,
snapshot.adapterOptions,
Expand Down Expand Up @@ -295,7 +295,7 @@ export default RESTAdapter.extend({
*/
query(store, type, query = {}) {
return new Promise((resolve, reject) => {
const db = this.get('firebase').firestore();
const db = this.get('firestore.instance');
let collectionRef = this.buildCollectionRef(type.modelName, query, db);

collectionRef = this.buildQuery(collectionRef, query);
Expand Down Expand Up @@ -375,7 +375,7 @@ export default RESTAdapter.extend({
* @return {firebase.firestore.CollectionReference|firebase.firestore.Query} Reference
*/
buildHasManyCollectionRef(store, snapshot, url, relationship) {
const db = this.get('firebase').firestore();
const db = this.get('firestore.instance');
const cardinality = snapshot.type.determineRelationshipType(
relationship,
store,
Expand Down Expand Up @@ -426,7 +426,7 @@ export default RESTAdapter.extend({
return this.buildCollectionRef(
type.modelName,
snapshot.adapterOptions,
this.get('firebase').firestore(),
this.get('firestore.instance'),
).doc(snapshot.id);
},

Expand All @@ -441,7 +441,7 @@ export default RESTAdapter.extend({
* @private
*/
buildWriteBatch(type, snapshot, docRef, isDeletingMainDoc) {
const db = this.get('firebase').firestore();
const db = this.get('firestore.instance');
const payload = this.serialize(snapshot);
const batch = db.batch();

Expand Down
6 changes: 3 additions & 3 deletions addon/serializers/cloud-firestore.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default JSONSerializer.extend({
/**
* @type {Ember.Service}
*/
firebase: inject(),
firestore: inject(),

/**
* Overriden to properly get the data of a `Reference` type relationship
Expand Down Expand Up @@ -101,7 +101,7 @@ export default JSONSerializer.extend({
json[relationship.key] = path;
} else {
json[relationship.key] = buildRefFromPath(
this.get('firebase').firestore(),
this.get('firestore.instance'),
path,
);
}
Expand All @@ -125,7 +125,7 @@ export default JSONSerializer.extend({
references.push(path);
} else {
references.push(buildRefFromPath(
this.get('firebase').firestore(),
this.get('firestore.instance'),
path,
));
}
Expand Down
15 changes: 15 additions & 0 deletions addon/services/firestore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Service from '@ember/service';
import { inject as service } from '@ember/service';
import { computed } from '@ember/object';

export default Service.extend({
firebase: service(),
instance: computed('firebase', function() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really appreciate for your time on taking a stab at this.

I'm not really liking the firestore.instance but I could see why you did it this way. I could probably refactor in the future should there be a cleaner solution that we can omit the .instance part. For now, this will do.

My only suggestion is maybe we can make this as not a computed property. We can assign the instance property in the init() hook. The settings will also be a standalone property so that other devs can override the service and provide their own settings if needed.

e.g.

export default Service.extend({
  ...

  settings: { timestampInSnapshots: true },

  init(...args) {
    this._super(...args);

    const firestore = this.get('firebase').firestore();

    firestore.settings(this.get('settings'));

    this.set('instance', firestore);
  }
});

Copy link
Contributor Author

@douweh douweh May 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree on both items:

  • as a service needs to be an extension of Service, I could not see a way around this (the instance part) for now.
  • I changed the code so the instance gets set on init (and settings are overridable)

I pushed a new commit.

Changes I think could be added but should not prevent this PR from merging:
-> get settings from ENV
-> Update your mock-cloud-firestore so it implements a settings method (tests were failing because of that, so I added the if-guard)

const firestore = this.get('firebase').firestore();
if (firestore.settings) {
const settings = { timestampsInSnapshots: true };
firestore.settings(settings);
}
return firestore;
}),
});
1 change: 1 addition & 0 deletions app/services/firestore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-cloud-firestore-adapter/services/firestore';
152 changes: 71 additions & 81 deletions tests/unit/adapters/cloud-firestore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,27 +265,25 @@ module('Unit | Adapter | cloud firestore', function(hooks) {
const snapshot = { id: 'user_a' };
const adapter = this.owner.lookup('adapter:cloud-firestore');

adapter.set('firebase', {
firestore() {
return {
batch() {
return {
set() {},

commit() {
return Promise.reject('error');
},
};
},

collection() {
return {
doc() {
return {};
},
};
},
};
adapter.set('firestore', {
instance: {
batch() {
return {
set() {},

commit() {
return Promise.reject('error');
},
};
},

collection() {
return {
doc() {
return {};
},
};
},
},
});
adapter.set('serialize', () => {
Expand Down Expand Up @@ -369,25 +367,23 @@ module('Unit | Adapter | cloud firestore', function(hooks) {
const snapshot = { id: 'user_a' };
const adapter = this.owner.lookup('adapter:cloud-firestore');

adapter.set('firebase', {
firestore() {
return {
batch() {
return {
commit() {
return Promise.reject('error');
},

delete() {},
};
},

collection() {
return {
doc() {},
};
},
};
adapter.set('firestore', {
instance: {
batch() {
return {
commit() {
return Promise.reject('error');
},

delete() {},
};
},

collection() {
return {
doc() {},
};
},
},
});
adapter.set('serialize', () => {
Expand Down Expand Up @@ -450,17 +446,15 @@ module('Unit | Adapter | cloud firestore', function(hooks) {
// Arrange
const adapter = this.owner.lookup('adapter:cloud-firestore');

adapter.set('firebase', {
firestore() {
return {
collection() {
return {
onSnapshot(onSuccess, onError) {
onError();
},
};
},
};
adapter.set('firestore', {
instance: {
collection() {
return {
onSnapshot(onSuccess, onError) {
onError();
},
};
},
},
});

Expand Down Expand Up @@ -544,21 +538,19 @@ module('Unit | Adapter | cloud firestore', function(hooks) {
const modelId = 'user_a';
const adapter = this.owner.lookup('adapter:cloud-firestore');

adapter.set('firebase', {
firestore() {
return {
collection() {
return {
doc() {
return {
onSnapshot(onSuccess, onError) {
onError();
},
};
},
};
},
};
adapter.set('firestore', {
instance: {
collection() {
return {
doc() {
return {
onSnapshot(onSuccess, onError) {
onError();
},
};
},
};
},
},
});

Expand Down Expand Up @@ -916,21 +908,19 @@ module('Unit | Adapter | cloud firestore', function(hooks) {
};
const adapter = this.owner.lookup('adapter:cloud-firestore');

adapter.set('firebase', {
firestore() {
return {
collection() {
return {
where() {
return {
onSnapshot(onSuccess, onError) {
onError();
},
};
},
};
},
};
adapter.set('firestore', {
instance: {
collection() {
return {
where() {
return {
onSnapshot(onSuccess, onError) {
onError();
},
};
},
};
},
},
});

Expand Down
13 changes: 13 additions & 0 deletions tests/unit/services/firestore-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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);
});
});