Skip to content

Commit

Permalink
Merge pull request #111 from ampatspell/issue-108/nested-doc-references
Browse files Browse the repository at this point in the history
Nested reference parents
  • Loading branch information
ampatspell authored Aug 24, 2018
2 parents d354bc4 + 2995fa2 commit 897aeb7
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 2 deletions.
46 changes: 44 additions & 2 deletions addon/-private/store/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ import queue from '../queue/computed';
import settle from '../util/settle';
import destroyCached from '../util/destroy-cached';
import instantiateFirebase from '../firebase/instantiate';
import { isFirestoreDocumentReference, isFirestoreCollectionReference } from '../util/firestore-types';

const splitReference = (ref, parentRef) => {
let refs = [];
while(ref) {
if(ref instanceof parentRef.constructor && parentRef.isEqual(ref)) {
return refs.reverse();
} else {
refs.push(ref);
}
ref = ref.parent;
}
}

export default Internal.extend({

Expand Down Expand Up @@ -72,14 +85,43 @@ export default Internal.extend({
return this.factory.create({ _internal: this });
},

createInternalDocumentReferenceForReference(ref, _parent) {
_createInternalReferenceForNestedReference(ref, internal) {
assert(`ref is required`, !!ref);
assert(`parent is required`, !!internal);
let refs = splitReference(ref, internal.ref);
assert(`ref parent must be ${internal}`, !!refs);
return refs.reduce((parent, ref) => {
if(isFirestoreDocumentReference(ref)) {
return this._createInternalDocumentReferenceForReference(ref, parent);
} else if(isFirestoreCollectionReference(ref)) {
return this._createInternalCollectionReferenceForReference(ref, parent);
}
assert(`ref must be a document or collection reference`, false);
}, internal);
},

_createInternalDocumentReferenceForReference(ref, _parent) {
return this.factoryFor('zuglet:reference/document/internal').create({ store: this, ref, _parent });
},

createInternalCollectionReferenceForReference(ref, _parent) {
createInternalDocumentReferenceForReference(ref, parent) {
if(parent) {
return this._createInternalReferenceForNestedReference(ref, parent);
}
return this._createInternalDocumentReferenceForReference(ref);
},

_createInternalCollectionReferenceForReference(ref, _parent) {
return this.factoryFor('zuglet:reference/collection/internal').create({ store: this, ref, _parent });
},

createInternalCollectionReferenceForReference(ref, parent) {
if(parent) {
return this._createInternalReferenceForNestedReference(ref, parent);
}
return this._createInternalCollectionReferenceForReference(ref);
},

createInternalQueryReferenceForReference(ref, _parent, info) {
return this.factoryFor('zuglet:reference/query/internal').create({ store: this, ref, _parent, info });
},
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/references-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,29 @@ module('references', function(hooks) {
}
});

test('parents for nested collection doc references', function(assert) {
let doc = this.store.collection('ducks').doc('yellow/feathers/white');
assert.equal(doc.get('id'), 'white');
assert.equal(doc.get('parent.id'), 'feathers');
assert.equal(doc.get('parent.parent.id'), 'yellow');
assert.equal(doc.get('parent.parent.parent.id'), 'ducks');
});

test('parent for doc nested collection references', function(assert) {
let doc = this.store.doc('ducks/yellow').collection('orders/first/products');
assert.equal(doc.get('id'), 'products');
assert.equal(doc.get('parent.id'), 'first');
assert.equal(doc.get('parent.parent.id'), 'orders');
assert.equal(doc.get('parent.parent.parent.id'), 'yellow');
assert.equal(doc.get('parent.parent.parent.parent.id'), 'ducks');
});

test('parent for doc nested doc references', function(assert) {
let doc = this.store.doc('ducks/yellow').doc('orders/first');
assert.equal(doc.get('id'), 'first');
assert.equal(doc.get('parent.id'), 'orders');
assert.equal(doc.get('parent.parent.id'), 'yellow');
assert.equal(doc.get('parent.parent.parent.id'), 'ducks');
});

});

0 comments on commit 897aeb7

Please sign in to comment.