Skip to content

Commit

Permalink
Merge pull request #74 from mikkopaderes/allow-sub-collection-fetch
Browse files Browse the repository at this point in the history
Allow sub-collection fetching
  • Loading branch information
mikkopaderes authored Dec 9, 2018
2 parents 0ce6e0c + 5431f17 commit 8067561
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 39 deletions.
20 changes: 15 additions & 5 deletions addon/adapters/cloud-firestore.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assign } from '@ember/polyfills';
import { inject as service } from '@ember/service';
import Adapter from 'ember-data/adapter';

Expand Down Expand Up @@ -197,7 +198,7 @@ export default Adapter.extend({
const collectionRef = this.buildCollectionRef(type, query);
const firestoreQuery = this.buildQuery(collectionRef, query);
const unsubscribe = firestoreQuery.onSnapshot(async (querySnapshot) => {
if (this.getAdapterOptionConfig(query, 'isRealTime')) {
if (this.getAdapterOptionConfig({ adapterOptions: query }, 'isRealTime')) {
this.realtimeTracker.trackQueryChanges(firestoreQuery, recordArray, query.queryId);
}

Expand All @@ -224,7 +225,6 @@ export default Adapter.extend({
*/
buildCollectionRef(type, adapterOptions = {}) {
const db = this.firebase.firestore();

if (Object.prototype.hasOwnProperty.call(adapterOptions, 'buildReference')) {
return adapterOptions.buildReference(db);
}
Expand Down Expand Up @@ -346,9 +346,13 @@ export default Adapter.extend({
});
}

return this.findRecord(store, type, docSnapshot.id, {
adapterOptions: { isRealTime: relationship.options.isRealTime },
const adapterOptions = assign({}, relationship.options, {
buildReference() {
return docSnapshot.ref.parent;
},
});

return this.findRecord(store, type, docSnapshot.id, { adapterOptions });
});
},

Expand Down Expand Up @@ -379,7 +383,13 @@ export default Adapter.extend({
return request;
}

return this.findRecord(store, type, docSnapshot.id, { adapterOptions: option });
const adapterOptions = assign({}, option, {
buildReference() {
return docSnapshot.ref.parent;
},
});

return this.findRecord(store, type, docSnapshot.id, { adapterOptions });
});
},

Expand Down
29 changes: 8 additions & 21 deletions addon/utils/realtime-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,16 @@ export default class RealtimeTracker {
* @function
*/
trackFindHasManyChanges(modelName, id, relationship, collectionRef, store) {
const { type, key: field } = relationship;
const queryId = Math.random().toString(32).slice(2).substr(0, 5);

collectionRef.onSnapshot(async (querySnapshot) => {
const requests = [];
const records = [];

querySnapshot.forEach((docSnapshot) => {
const request = store.findRecord(type, docSnapshot.id, {
adapterOptions: { isRealTime: true },
});

requests.push(request);
records.push({
data: { type, id: docSnapshot.id },
});
});
const { key: field } = relationship;
const queryId = `${modelName}_${id}_${field}`;

await Promise.all(requests);
store.peekRecord(modelName, id).hasMany(field).push(records);
}, () => delete this.query[queryId]);
if (!this.isQueryTracked(queryId)) {
collectionRef.onSnapshot(() => (
store.peekRecord(modelName, id).hasMany(field).reload()
), () => delete this.query[queryId]);

this.query[queryId] = true;
this.query[queryId] = true;
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions guides/03-finding-records.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ this.store.query('post', {
});
```

If the document contains a field that matches your [`referenceKeyName`](02-configuration.md#settings), it'll fetch that one instead.

### `isRealTime`

Indicates if the record will update in realtime
Expand Down
6 changes: 5 additions & 1 deletion guides/06-relationships.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ export default Model.extend({
});
```

If the document contains a field that matches your [`referenceKeyName`](02-configuration.md#settings), it'll fetch that one instead.

### `buildReference`

Hook for providing a custom collection reference
Hook for providing a custom collection reference.

This is ignored when the relationship is a many-to-one type.

**Type:** `function`

Expand Down
4 changes: 1 addition & 3 deletions tests/dummy/app/adapters/application.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import CloudFirestoreAdapter from 'ember-cloud-firestore-adapter/adapters/cloud-firestore';

export default CloudFirestoreAdapter.extend({
namespace: 'api',
});
export default CloudFirestoreAdapter.extend();
8 changes: 4 additions & 4 deletions tests/helpers/fixture-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ export default function getFixtureData() {
friends: {
__doc__: {
user_b: {
cloudFirestoreReference: '__ref__:users/user_b',
referenceTo: '__ref__:users/user_b',
},

user_c: {
cloudFirestoreReference: '__ref__:users/user_c',
referenceTo: '__ref__:users/user_c',
},
},
},
Expand Down Expand Up @@ -89,7 +89,7 @@ export default function getFixtureData() {
friends: {
__doc__: {
user_a: {
cloudFirestoreReference: '__ref__:users/user_a',
referenceTo: '__ref__:users/user_a',
},
},
},
Expand Down Expand Up @@ -118,7 +118,7 @@ export default function getFixtureData() {
friends: {
__doc__: {
user_a: {
cloudFirestoreReference: '__ref__:users/user_a',
referenceTo: '__ref__:users/user_a',
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/adapters/cloud-firestore-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ module('Unit | Adapter | cloud firestore', function (hooks) {
const determineRelationshipTypeStub = sinon.stub().returns('manyToNone');
const snapshot = {
record: EmberObject.create({
cloudFirestoreReference: db.collection('users').doc('user_a'),
referenceTo: db.collection('users').doc('user_a'),
}),
type: {
determineRelationshipType: determineRelationshipTypeStub,
Expand Down
5 changes: 1 addition & 4 deletions tests/unit/utils/realtime-tracker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,9 @@ module('Unit | Utility | realtime-tracker', function () {
const relationship = { type: 'post', key: 'posts' };
const collectionRef = db.collection('users');
const store = {
findRecord: sinon.stub(),
normalize: sinon.stub(),
peekRecord: sinon.stub().returns({
hasMany: sinon.stub().returns({ push: sinon.stub() }),
hasMany: sinon.stub().returns({ reload: sinon.stub() }),
}),
push: sinon.stub(),
};
const realtimeTracker = new RealtimeTracker();

Expand Down

0 comments on commit 8067561

Please sign in to comment.