Skip to content

Commit

Permalink
Get include: 'somerel' working (#618)
Browse files Browse the repository at this point in the history
* Include relationships fix attempt
* Dont pluralize included key
* fix rel model name for serializer
  • Loading branch information
knownasilya committed Dec 10, 2020
1 parent 0c7b67f commit 329aac3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
7 changes: 5 additions & 2 deletions addon/adapters/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,12 @@ const includeCollectionRelationships = (collection: firestore.QuerySnapshot, sto
if (relationship.kind == 'belongsTo') {
const result = includes.find((r:any) => r.id == doc.data()[key]);
if (result) {
if (!(doc as any)._document) { (doc as any)._document = {} }
if (!(doc as any)._document._included) { (doc as any)._document._included = {} }
(doc as any)._document._included[key] = result;
}
} else {
if (!(doc as any)._document) { (doc as any)._document = {} }
if (!(doc as any)._document._included) { (doc as any)._document._included = {} }
(doc as any)._document._included[pluralKey] = includes;
}
Expand All @@ -358,8 +360,9 @@ const includeRelationships = <T=any>(promise: Promise<T>, store: DS.Store, adapt
promise,
...hasManyRelationships.map(r => adapter.findHasMany(store, snapshot, '', r))
]).then(([doc, ...includes]) => {
if (!(doc as any)._document) { (doc as any)._document = {} }
doc._document._included = hasManyRelationships.reduce((c, e, i) => {
c[pluralize(e.key)] = includes[i];
c[e.key] = includes[i];
return c;
}, {});
return Promise.all([
Expand All @@ -384,4 +387,4 @@ declare module 'ember-data' {
interface AdapterRegistry {
'firestore': FirestoreAdapter;
}
}
}
4 changes: 2 additions & 2 deletions addon/serializers/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const normalizeRelationships = (store: DS.Store, modelClass: DS.Model, attribute
const attribute = attributes.data()[key];
const payload = attributes._document && attributes._document._included && attributes._document._included[key];
if (payload) {
const modelName = singularize(relationship.key) as never;
const modelName = relationship.meta.type as never;
const modelClass = store.modelFor(modelName);
const serializer = store.serializerFor(modelName) as any;
const { data } = relationship.kind === 'belongsTo' ? serializer.normalizeSingleResponse(store, modelClass, payload) : serializer.normalizeArrayResponse(store, modelClass, payload);
Expand Down Expand Up @@ -142,4 +142,4 @@ export const normalize = (store: DS.Store, modelClass: DS.Model, snapshot: Docum
const { relationships, included } = normalizeRelationships(store, modelClass, snapshot);
const data = { id, type, attributes, relationships };
return { data, included };
}
}

0 comments on commit 329aac3

Please sign in to comment.