Skip to content
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
6 changes: 5 additions & 1 deletion src/util/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ const camelize = function(str) {
return str.replace(/(\_[a-z])/g, function($1){return $1.toUpperCase().replace('_','');});
}

export { underscore, camelize };
const decamelize = function(str) {
return str.replace(/([A-Z])/g, function($1){return $1.replace($1,'_' + $1).toLowerCase();});
}

export { underscore, camelize, decamelize };
3 changes: 2 additions & 1 deletion src/util/write-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Model from '../model';
import IncludeDirective from './include-directive';
import * as _snakeCase from './snakecase';
import tempId from './temp-id';
import { decamelize } from './string';
let snakeCase: any = (<any>_snakeCase).default || _snakeCase;
snakeCase = snakeCase['default'] || snakeCase;

Expand Down Expand Up @@ -87,7 +88,7 @@ export default class WritePayload {
}

if (data) {
_relationships[key] = { data }
_relationships[decamelize(key)] = { data }
}
}
});
Expand Down
29 changes: 28 additions & 1 deletion test/integration/nested-persistence-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ let expectedCreatePayload = {
method: 'create'
}
]
},
special_books: {
data: [
{
['temp-id']: 'abc3',
type: 'books',
method: 'create'
}
]
}
}
},
Expand All @@ -68,6 +77,13 @@ let expectedCreatePayload = {
attributes: {
name: 'Horror'
}
},
{
['temp-id']: 'abc3',
type: 'books',
attributes: {
title: 'The Stand'
}
}
]
};
Expand Down Expand Up @@ -122,8 +138,11 @@ const seedPersistedData = function() {
genre.isPersisted(true);
let book = new Book({ id: '10', title: 'The Shining', genre: genre });
book.isPersisted(true);
let specialBook = new Book({ id: '30', title: 'The Stand' });
specialBook.isPersisted(true);
instance.id = '1';
instance.books = [book];
instance.specialBooks = [specialBook];
instance.isPersisted(true);
genre.name = 'Updated Genre Name';
book.title = 'Updated Book Title';
Expand Down Expand Up @@ -169,6 +188,12 @@ describe('nested persistence', function() {
id: '20',
type: 'genres',
attributes: { name: 'name from server' }
},
{
['temp-id']: 'abc3',
id: '30',
type: 'books',
attributes: { title: 'another title from server' }
}
]
}
Expand Down Expand Up @@ -199,14 +224,16 @@ describe('nested persistence', function() {
beforeEach(function() {
let genre = new Genre({ name: 'Horror' });
let book = new Book({ title: 'The Shining', genre: genre });
let specialBook = new Book({ title: 'The Stand' });
instance.books = [book];
instance.specialBooks = [specialBook];
});

// todo test on the way back - id set, attrs updated, isPersisted
// todo remove #destroy? and just save when markwithpersisted? combo? for ombined payload
// todo test unique includes/circular relationshio
it('sends the correct payload', function(done) {
instance.save({ with: { books: 'genre' } }).then((response) => {
instance.save({ with: { books: 'genre', specialBooks: {} } }).then((response) => {
expect(payloads[0]).to.deep.equal(expectedCreatePayload);
done();
});
Expand Down