Skip to content

Commit

Permalink
relationship refactor part-1
Browse files Browse the repository at this point in the history
flatten belongs-to relationship

fix: implicit inverse key gen needs to utilize the related type for uniqueness
  • Loading branch information
runspired committed Apr 30, 2021
1 parent bfcb8ae commit 66f5e79
Show file tree
Hide file tree
Showing 36 changed files with 1,658 additions and 1,242 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { setupRenderingTest } from 'ember-qunit';
import { ServerError } from '@ember-data/adapter/error';
import JSONAPIAdapter from '@ember-data/adapter/json-api';
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import { implicitRelationshipsFor } from '@ember-data/record-data/-private';
import JSONAPISerializer from '@ember-data/serializer/json-api';
import Store from '@ember-data/store';

import { implicitRelationshipsFor } from '../../helpers/accessors';

class Person extends Model {
@attr()
name;
Expand Down Expand Up @@ -511,19 +512,20 @@ module('async belongs-to rendering tests', function (hooks) {

assert.equal(this.element.textContent.trim(), '', 'we have no parent');

let relationshipState = sedona.belongsTo('parent').belongsToRelationship;
const relationship = sedona.belongsTo('parent').belongsToRelationship;
const { state, definition } = relationship;
let RelationshipPromiseCache = sedona._internalModel._relationshipPromisesCache;
let RelationshipProxyCache = sedona._internalModel._relationshipProxyCache;

assert.true(relationshipState.isAsync, 'The relationship is async');
assert.false(relationshipState.relationshipIsEmpty, 'The relationship is not empty');
assert.true(relationshipState.hasDematerializedInverse, 'The relationship inverse is dematerialized');
assert.true(relationshipState.hasAnyRelationshipData, 'The relationship knows which record it needs');
assert.true(definition.isAsync, 'The relationship is async');
assert.false(state.isEmpty, 'The relationship is not empty');
assert.true(state.hasDematerializedInverse, 'The relationship inverse is dematerialized');
assert.true(state.hasReceivedData, 'The relationship knows which record it needs');
assert.false(!!RelationshipPromiseCache['parent'], 'The relationship has no fetch promise');
assert.true(relationshipState.hasFailedLoadAttempt === true, 'The relationship has attempted a load');
assert.true(relationshipState.shouldForceReload === false, 'The relationship will not force a reload');
assert.true(state.hasFailedLoadAttempt === true, 'The relationship has attempted a load');
assert.true(state.shouldForceReload === false, 'The relationship will not force a reload');
assert.true(!!RelationshipProxyCache['parent'], 'The relationship has a promise proxy');
assert.false(!!relationshipState.link, 'The relationship does not have a link');
assert.false(!!relationship.link, 'The relationship does not have a link');

try {
let result = await sedona.get('parent.content');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,12 @@ module('async has-many rendering tests', function (hooks) {
let RelationshipPromiseCache = parent._internalModel._relationshipPromisesCache;
let RelationshipProxyCache = parent._internalModel._relationshipProxyCache;

assert.true(relationshipState.isAsync, 'The relationship is async');
assert.false(relationshipState.relationshipIsEmpty, 'The relationship is not empty');
assert.true(relationshipState.hasDematerializedInverse, 'The relationship has a dematerialized inverse');
assert.true(relationshipState.hasAnyRelationshipData, 'The relationship knows which record it needs');
assert.true(relationshipState.definition.isAsync, 'The relationship is async');
assert.false(relationshipState.state.isEmpty, 'The relationship is not empty');
assert.true(relationshipState.state.hasDematerializedInverse, 'The relationship has a dematerialized inverse');
assert.true(relationshipState.state.hasReceivedData, 'The relationship knows which record it needs');
assert.false(!!RelationshipPromiseCache['children'], 'The relationship has no fetch promise');
assert.true(relationshipState.hasFailedLoadAttempt === true, 'The relationship has attempted a load');
assert.true(relationshipState.state.hasFailedLoadAttempt === true, 'The relationship has attempted a load');
assert.true(!!RelationshipProxyCache['children'], 'The relationship has a promise proxy');
assert.false(!!relationshipState.link, 'The relationship does not have a link');

Expand Down Expand Up @@ -432,16 +432,16 @@ module('async has-many rendering tests', function (hooks) {
let RelationshipPromiseCache = parent._internalModel._relationshipPromisesCache;
let RelationshipProxyCache = parent._internalModel._relationshipProxyCache;

assert.true(relationshipState.isAsync, 'The relationship is async');
assert.true(relationshipState.definition.isAsync, 'The relationship is async');
assert.true(
relationshipState.relationshipIsEmpty,
relationshipState.state.isEmpty,
'The relationship is empty because no signal has been received as to true state'
);
assert.true(relationshipState.relationshipIsStale, 'The relationship is still stale');
assert.false(relationshipState.hasAnyRelationshipData, 'The relationship knows which record it needs');
assert.true(relationshipState.state.isStale, 'The relationship is still stale');
assert.false(relationshipState.state.hasReceivedData, 'The relationship knows which record it needs');
assert.false(!!RelationshipPromiseCache['children'], 'The relationship has no fetch promise');
assert.true(!!RelationshipProxyCache['children'], 'The relationship has a promise proxy');
assert.true(relationshipState.hasFailedLoadAttempt === true, 'The relationship has attempted a load');
assert.true(relationshipState.state.hasFailedLoadAttempt === true, 'The relationship has attempted a load');
assert.true(!!(relationshipState.links && relationshipState.links.related), 'The relationship has a link');

Ember.onerror = originalOnError;
Expand Down
47 changes: 47 additions & 0 deletions packages/-ember-data/tests/helpers/accessors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { graphFor } from '@ember-data/record-data/-private';
import { recordIdentifierFor } from '@ember-data/store';

type CoreStore = import('@ember-data/store/-private/system/core-store').default;
type BelongsToRelationship = import('@ember-data/record-data/-private').BelongsToRelationship;
type ManyRelationship = import('@ember-data/record-data/-private').ManyRelationship;
type Relationship = import('@ember-data/record-data/-private').Relationship;
type RecordDataStoreWrapper = import('@ember-data/store/-private').RecordDataStoreWrapper;
type StableRecordIdentifier = import('@ember-data/store/-private/ts-interfaces/identifier').StableRecordIdentifier;
type RelationshipDict = import('@ember-data/store/-private/ts-interfaces/utils').ConfidentDict<Relationship>;

export function getRelationshipStateForRecord(
record: { store: CoreStore },
propertyName: string
): BelongsToRelationship | ManyRelationship | Relationship {
const identifier = recordIdentifierFor(record);
return graphFor(record.store._storeWrapper).get(identifier, propertyName);
}

export function hasRelationshipForRecord(
record: {
store: CoreStore;
},
propertyName: string
): boolean {
const identifier = recordIdentifierFor(record);
const relationships = graphFor(record.store._storeWrapper).identifiers.get(identifier);
return relationships ? propertyName in relationships : false;
}

export function implicitRelationshipsFor(
storeWrapper: RecordDataStoreWrapper,
identifier: StableRecordIdentifier
): RelationshipDict {
const rels = graphFor(storeWrapper).identifiers.get(identifier);
if (!rels) {
throw new Error(`Expected at least one relationship to be populated`);
}
let implicits = Object.create(null);
Object.keys(rels).forEach((key) => {
let rel = rels[key]!;
if (rel.definition.isImplicit) {
implicits[key] = rel;
}
});
return implicits;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module('integration/polymorphic-belongs-to - Polymorphic BelongsTo', function (h
@attr()
title;

@belongsTo('person', { polymorphic: true, async: false })
@belongsTo('author', { polymorphic: true, async: false })
author;
}

Expand All @@ -26,7 +26,7 @@ module('integration/polymorphic-belongs-to - Polymorphic BelongsTo', function (h
class Person extends Model {}

class AsyncBook extends Model {
@belongsTo('person', { polymorphic: true })
@belongsTo('author', { polymorphic: true })
author;
}
owner.register('model:book', Book);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ module('integration/references/belongs-to', function (hooks) {
run(function () {
person.belongsTo('unknown-relationship');
});
}, "There is no belongsTo relationship named 'unknown-relationship' on a model of modelClass 'person'");
}, 'Expected to find a relationship definition for person.unknown-relationship but none was found');
});

testInDebug(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module('integration/references/has-many', function (hooks) {
run(function () {
family.hasMany('unknown-relationship');
});
}, "There is no hasMany relationship named 'unknown-relationship' on a model of modelClass 'family'");
}, 'Expected to find a relationship definition for family.unknown-relationship but none was found');
});

testInDebug(
Expand Down
Loading

0 comments on commit 66f5e79

Please sign in to comment.