Skip to content

Commit

Permalink
fix(@aws-amplify/datastore): belongsTo bug
Browse files Browse the repository at this point in the history
  • Loading branch information
iartemiev committed Nov 22, 2021
1 parent d54a5c0 commit fd09178
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions packages/datastore/src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ import {
import { Adapter } from './adapter';
import getDefaultAdapter from './adapter/getDefaultAdapter';

export type StorageSubscriptionMessage<
T extends PersistentModel
> = SubscriptionMessage<T> & {
mutator?: Symbol;
};
export type StorageSubscriptionMessage<T extends PersistentModel> =
SubscriptionMessage<T> & {
mutator?: Symbol;
};

export type StorageFacade = Omit<Adapter, 'setUp'>;
export type Storage = InstanceType<typeof StorageClass>;
Expand Down Expand Up @@ -135,9 +134,9 @@ class StorageClass implements StorageFacade {

const element = updateMutationInput || originalElement;

const modelConstructor = (Object.getPrototypeOf(
originalElement
) as Object).constructor as PersistentModelConstructor<T>;
const modelConstructor = (
Object.getPrototypeOf(originalElement) as Object
).constructor as PersistentModelConstructor<T>;

this.pushStream.next({
model: modelConstructor,
Expand Down Expand Up @@ -318,17 +317,15 @@ class StorageClass implements StorageFacade {
const modelConstructor = Object.getPrototypeOf(model)
.constructor as PersistentModelConstructor<T>;
const namespace = this.namespaceResolver(modelConstructor);
const { fields } = this.schema.namespaces[namespace].models[
modelConstructor.name
];
const { primaryKey, compositeKeys = [] } = this.schema.namespaces[
namespace
].keys[modelConstructor.name];
const { fields } =
this.schema.namespaces[namespace].models[modelConstructor.name];
const { primaryKey, compositeKeys = [] } =
this.schema.namespaces[namespace].keys[modelConstructor.name];

// set original values for these fields
updatedFields.forEach((field: string) => {
const targetName: any = isTargetNameAssociation(
fields[field].association
fields[field]?.association
);

// if field refers to a belongsTo relation, use the target field instead
Expand Down

0 comments on commit fd09178

Please sign in to comment.