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

Nested reference parents #111

Merged
merged 3 commits into from
Aug 24, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
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');
});

});