Skip to content

Commit

Permalink
Merge pull request #52 from rmmmp/revert-firestore-service
Browse files Browse the repository at this point in the history
Revert Firestore service
  • Loading branch information
mikkopaderes authored May 12, 2018
2 parents 7c2ff24 + c2685e3 commit d350896
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 73 deletions.
38 changes: 28 additions & 10 deletions addon/adapters/cloud-firestore.js
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -19,7 +19,7 @@ export default RESTAdapter.extend({
/**
* @type {Ember.Service}
*/
firestore: inject(),
firebase: service(),

/**
* @type {string}
Expand All @@ -32,6 +32,11 @@ export default RESTAdapter.extend({
*/
defaultSerializer: 'cloud-firestore',

/**
* @type {Object}
*/
firestoreSettings: { timestampsInSnapshots: true },

/**
* @override
*/
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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) => {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
},

Expand All @@ -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();

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}
*/
firestore: inject(),
firebase: inject(),

/**
* Overriden to properly get the data of a `Reference` type relationship
Expand Down Expand Up @@ -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);
}
}
},
Expand All @@ -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));
}
});

Expand Down
31 changes: 0 additions & 31 deletions addon/services/firestore.js

This file was deleted.

1 change: 0 additions & 1 deletion app/services/firestore.js

This file was deleted.

1 change: 1 addition & 0 deletions guides/02-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.

---
Expand Down
30 changes: 15 additions & 15 deletions tests/unit/adapters/cloud-firestore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {},
Expand All @@ -229,7 +229,7 @@ module('Unit | Adapter | cloud firestore', function (hooks) {
},
};
},
},
}),
});
adapter.set('serialize', () => [
{
Expand Down Expand Up @@ -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() {
Expand All @@ -323,7 +323,7 @@ module('Unit | Adapter | cloud firestore', function (hooks) {
doc() {},
};
},
},
}),
});
adapter.set('serialize', () => [
{
Expand Down Expand Up @@ -391,16 +391,16 @@ 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) {
onError();
},
};
},
},
}),
});

try {
Expand Down Expand Up @@ -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() {
Expand All @@ -482,7 +482,7 @@ module('Unit | Adapter | cloud firestore', function (hooks) {
},
};
},
},
}),
});

try {
Expand Down Expand Up @@ -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() {
Expand All @@ -810,7 +810,7 @@ module('Unit | Adapter | cloud firestore', function (hooks) {
},
};
},
},
}),
});

try {
Expand Down
13 changes: 0 additions & 13 deletions tests/unit/services/firestore-test.js

This file was deleted.

0 comments on commit d350896

Please sign in to comment.