Skip to content

Commit

Permalink
fix(@aws-amplify/datastore): update mutation input - use diff with DB…
Browse files Browse the repository at this point in the history
… instead of patches
  • Loading branch information
iartemiev committed Mar 14, 2021
1 parent 0218cf5 commit 075a95e
Show file tree
Hide file tree
Showing 9 changed files with 477 additions and 306 deletions.
37 changes: 6 additions & 31 deletions packages/datastore/__tests__/DataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,12 @@ describe('DataStore tests', () => {
const result = await DataStore.save(model);

const [settingsSave, modelCall] = <any>save.mock.calls;
const [_model, _condition, _mutator, patches] = modelCall;
const [_model, _condition, _mutator] = modelCall;

expect(result).toMatchObject(model);
expect(patches).toBeUndefined();
});

test('Save returns the updated model and patches', async () => {
test('Save returns the updated model', async () => {
let model: Model;
const save = jest.fn(() => [model]);
const query = jest.fn(() => [model]);
Expand Down Expand Up @@ -438,17 +437,12 @@ describe('DataStore tests', () => {
const result = await DataStore.save(model);

const [settingsSave, modelSave, modelUpdate] = <any>save.mock.calls;
const [_model, _condition, _mutator, patches] = modelUpdate;

const expectedPatches = [
{ op: 'replace', path: ['field1'], value: 'edited' },
];
const [_model, _condition, _mutator] = modelUpdate;

expect(result).toMatchObject(model);
expect(patches).toMatchObject(expectedPatches);
});

test('Save returns the updated model and patches - list field', async () => {
test('Save returns the updated model - list field', async () => {
let model: Model;
const save = jest.fn(() => [model]);
const query = jest.fn(() => [model]);
Expand Down Expand Up @@ -505,27 +499,8 @@ describe('DataStore tests', () => {
save.mock.calls
);

const [_model, _condition, _mutator, patches] = modelUpdate;
const [_model2, _condition2, _mutator2, patches2] = modelUpdate2;

const expectedPatches = [
{
op: 'replace',
path: ['emails'],
value: ['john@doe.com', 'jane@doe.com', 'joe@doe.com'],
},
];

const expectedPatches2 = [
{
op: 'add',
path: ['emails', 3],
value: 'joe@doe.com',
},
];

expect(patches).toMatchObject(expectedPatches);
expect(patches2).toMatchObject(expectedPatches2);
const [_model, _condition, _mutator] = modelUpdate;
const [_model2, _condition2, _mutator2] = modelUpdate2;
});

test('Instantiation validations', async () => {
Expand Down
100 changes: 99 additions & 1 deletion packages/datastore/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export declare class Model {
mutator: (draft: MutableModel<Model>) => void | Model
): Model;
}

export declare class Metadata {
readonly author: string;
readonly tags?: string[];
Expand All @@ -27,6 +26,17 @@ export declare class Metadata {
constructor(init: Metadata);
}

export declare class Post {
public readonly id: string;
public readonly title: string;
}

export declare class Comment {
public readonly id: string;
public readonly content: string;
public readonly post: Post;
}

export function testSchema(): Schema {
return {
enums: {},
Expand Down Expand Up @@ -88,6 +98,94 @@ export function testSchema(): Schema {
},
},
},
Post: {
name: 'Post',
fields: {
id: {
name: 'id',
isArray: false,
type: 'ID',
isRequired: true,
attributes: [],
},
title: {
name: 'title',
isArray: false,
type: 'String',
isRequired: true,
attributes: [],
},
comments: {
name: 'comments',
isArray: true,
type: {
model: 'Comment',
},
isRequired: true,
attributes: [],
isArrayNullable: true,
association: {
connectionType: 'HAS_MANY',
associatedWith: 'postId',
},
},
},
syncable: true,
pluralName: 'Posts',
attributes: [
{
type: 'model',
properties: {},
},
],
},
Comment: {
name: 'Comment',
fields: {
id: {
name: 'id',
isArray: false,
type: 'ID',
isRequired: true,
attributes: [],
},
content: {
name: 'content',
isArray: false,
type: 'String',
isRequired: true,
attributes: [],
},
post: {
name: 'post',
isArray: false,
type: {
model: 'Post',
},
isRequired: false,
attributes: [],
association: {
connectionType: 'BELONGS_TO',
targetName: 'postId',
},
},
},
syncable: true,
pluralName: 'Comments',
attributes: [
{
type: 'model',
properties: {},
},
{
type: 'key',
properties: {
name: 'byPost',
fields: ['postId'],
},
},
],
},
LocalModel: {
name: 'LocalModel',
pluralName: 'LocalModels',
Expand Down
Loading

0 comments on commit 075a95e

Please sign in to comment.