Skip to content

Commit

Permalink
Makes firestore a singleton and sets correct timestamp setting (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
douweh authored and mikkopaderes committed May 10, 2018
1 parent f5b3997 commit 2bc7863
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 93 deletions.
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
20 changes: 20 additions & 0 deletions addon/services/firestore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Service from '@ember/service';
import { inject as service } from '@ember/service';

export default Service.extend({
firebase: service(),
instance: null,
settings: { timestampsInSnapshots: true },

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

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

if (firestore.settings) {
firestore.settings(this.settings);
}

this.set('instance', 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);
});
});

0 comments on commit 2bc7863

Please sign in to comment.