Skip to content

Commit

Permalink
fix: camel cased relationships do not resolve correctly (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharygolba authored Aug 10, 2016
1 parent c903770 commit a04c0fd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/packages/database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Model from './model';

import initialize from './initialize';

import normalizeModelName from './utils/normalize-model-name';

import type Logger from '../logger';
import type { Database$opts } from './interfaces';

Expand Down Expand Up @@ -43,7 +45,7 @@ class Database {
}

modelFor(type: string): Class<Model> {
const model = this.models.get(type);
const model = this.models.get(normalizeModelName(type));

if (!model) {
throw new ModelMissingError(type);
Expand Down
6 changes: 3 additions & 3 deletions src/packages/database/model/initialize-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ export default async function initializeClass<T: Class<Model>>({

Object.defineProperties(relationship, {
model: {
value: store.modelFor(relatedModel || singularize(relatedName)),
value: store.modelFor(relatedModel || relatedName),
writable: false,
enumerable: true,
configurable: false
Expand Down Expand Up @@ -269,7 +269,7 @@ export default async function initializeClass<T: Class<Model>>({

Object.defineProperties(relationship, {
model: {
value: store.modelFor(relatedModel || singularize(relatedName)),
value: store.modelFor(relatedModel || relatedName),
writable: false,
enumerable: true,
configurable: false
Expand Down Expand Up @@ -314,7 +314,7 @@ export default async function initializeClass<T: Class<Model>>({
if (typeof relatedModel === 'string') {
relatedModel = store.modelFor(relatedModel);
} else {
relatedModel = store.modelFor(singularize(relatedName));
relatedModel = store.modelFor(relatedName);
}

if (typeof through === 'string') {
Expand Down
11 changes: 11 additions & 0 deletions src/packages/database/utils/normalize-model-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @flow
import { dasherize, singularize } from 'inflection';

import underscore from '../../../utils/underscore';

/**
* @private
*/
export default function normalizeModelName(modelName: string) {
return singularize(dasherize(underscore(modelName)));
}
5 changes: 4 additions & 1 deletion src/packages/serializer/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// @flow
import { dasherize } from 'inflection';

import { VERSION } from '../jsonapi';

import uniq from '../../utils/uniq';
import insert from '../../utils/insert';
import underscore from '../../utils/underscore';
import promiseHash from '../../utils/promise-hash';
import { dasherizeKeys } from '../../utils/transform-keys';

Expand Down Expand Up @@ -386,7 +389,7 @@ class Serializer {
[...this.hasOne, ...this.hasMany].reduce((hash, name) => ({
...hash,

[name]: (async () => {
[dasherize(underscore(name))]: (async () => {
const related = await Reflect.get(item, name);

if (Array.isArray(related)) {
Expand Down

0 comments on commit a04c0fd

Please sign in to comment.