Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DEPRECATION] deprecate intimate Reference.internalModel #6889

Merged
merged 6 commits into from
Apr 22, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { attr } from '@ember-data/model';
import JSONAPISerializer from '@ember-data/serializer/json-api';

type RecordIdentifier = import('@ember-data/store/-private/ts-interfaces/identifier').RecordIdentifier;
type NewRecordIdentifier = import('@ember-data/store/-private/ts-interfaces/identifier').NewRecordIdentifier;
type RecordData = import('@ember-data/store/-private/ts-interfaces/record-data').RecordData;
type JsonApiValidationError = import('@ember-data/store/-private/ts-interfaces/record-data-json-api').JsonApiValidationError;

Expand All @@ -25,7 +26,21 @@ class Person extends Model {
lastName;
}

class TestRecordIdentifier implements NewRecordIdentifier {
constructor(public id: string | null, public lid: string, public type: string) {}
}

class TestRecordData implements RecordData {
id: string | null = '1';
clientId: string | null = 'test-record-data-1';
modelName = 'tst';

getResourceIdentifier() {
if (this.clientId !== null) {
return new TestRecordIdentifier(this.id, this.clientId, this.modelName);
}
}

commitWasRejected(recordIdentifier: RecordIdentifier, errors?: JsonApiValidationError[]): void {}

// Use correct interface once imports have been fix
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { attr } from '@ember-data/model';
import JSONAPISerializer from '@ember-data/serializer/json-api';

type RecordData = import('@ember-data/store/-private/ts-interfaces/record-data').RecordData;
type NewRecordIdentifier = import('@ember-data/store/-private/ts-interfaces/identifier').NewRecordIdentifier;

class Person extends Model {
// TODO fix the typing for naked attrs
Expand All @@ -23,7 +24,21 @@ class Person extends Model {
lastName;
}

class TestRecordIdentifier implements NewRecordIdentifier {
constructor(public id: string | null, public lid: string, public type: string) {}
}

class TestRecordData implements RecordData {
id: string | null = '1';
clientId: string | null = 'test-record-data-1';
modelName = 'tst';

getResourceIdentifier() {
if (this.clientId !== null) {
return new TestRecordIdentifier(this.id, this.clientId, this.modelName);
}
}

commitWasRejected(): void {}

// Use correct interface once imports have been fix
Expand Down
1 change: 1 addition & 0 deletions packages/private-build-infra/addon/current-deprecations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ export default {
DEPRECATE_MISMATCHED_INVERSE_RELATIONSHIP_DATA: '3.12',
DEPRECATE_SERIALIZER_QUERY_RECORD_ARRAY_RESPONSE: '3.4',
DEPRECATE_BELONGS_TO_REFERENCE_PUSH: '3.16',
DEPRECATE_REFERENCE_INTERNAL_MODEL: '3.19',
};
2 changes: 2 additions & 0 deletions packages/private-build-infra/addon/deprecations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ export const DEPRECATE_SERIALIZER_QUERY_RECORD_ARRAY_RESPONSE = deprecationState
export const DEPRECATE_MISMATCHED_INVERSE_RELATIONSHIP_DATA = deprecationState(
'DEPRECATE_MISMATCHED_INVERSE_RELATIONSHIP_DATA'
);
export const DEPRECATE_BELONGS_TO_REFERENCE_PUSH = deprecationState('DEPRECATE_BELONGS_TO_REFERENCE_PUSH');
export const DEPRECATE_REFERENCE_INTERNAL_MODEL = deprecationState('DEPRECATE_REFERENCE_INTERNAL_MODEL');
8 changes: 4 additions & 4 deletions packages/store/addon/-private/system/references/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { assertPolymorphicType } from '@ember-data/store/-debug';

import recordDataFor from '../record-data-for';
import { peekRecordIdentifier } from '../store/internal-model-factory';
import Reference from './reference';
import Reference, { INTERNAL_MODELS } from './reference';

/**
@module @ember-data/store
Expand Down Expand Up @@ -73,14 +73,14 @@ export default class BelongsToReference extends Reference {
id() {
let id = null;
let resource = this._resource();
if (resource && resource.data && resource.data.id) {
if (resource && resource.data) {
id = resource.data.id;
}
return id;
}

_resource() {
return this.recordData.getBelongsTo(this.key);
return INTERNAL_MODELS.get(this)?._recordData.getBelongsTo(this.key);
}

/**
Expand Down Expand Up @@ -144,7 +144,7 @@ export default class BelongsToReference extends Reference {
}

assertPolymorphicType(
this.internalModel,
INTERNAL_MODELS.get(this),
this.belongsToRelationship.relationshipMeta,
record._internalModel,
this.store
Expand Down
19 changes: 12 additions & 7 deletions packages/store/addon/-private/system/references/has-many.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { resolve } from 'rsvp';
import { assertPolymorphicType } from '@ember-data/store/-debug';

import recordDataFor from '../record-data-for';
import Reference from './reference';
import Reference, { INTERNAL_MODELS } from './reference';

/**
@module @ember-data/store
Expand All @@ -31,7 +31,7 @@ export default class HasManyReference extends Reference {
}

_resource() {
return this.recordData.getHasMany(this.key);
return INTERNAL_MODELS.get(this)?._recordData.getHasMany(this.key);
}

/**
Expand Down Expand Up @@ -178,19 +178,21 @@ export default class HasManyReference extends Reference {
array = payload.data;
}

let internalModel = INTERNAL_MODELS.get(this);

let internalModels = array.map(obj => {
let record = this.store.push(obj);

if (DEBUG) {
let relationshipMeta = this.hasManyRelationship.relationshipMeta;
assertPolymorphicType(this.internalModel, relationshipMeta, record._internalModel, this.store);
assertPolymorphicType(internalModel, relationshipMeta, record._internalModel, this.store);
}
return recordDataFor(record);
});

this.hasManyRelationship.computeChanges(internalModels);

return this.internalModel.getHasMany(this.hasManyRelationship.key);
return internalModel.getHasMany(this.hasManyRelationship.key);
// TODO IGOR it seems wrong that we were returning the many array here
//return this.hasManyRelationship.manyArray;
});
Expand Down Expand Up @@ -252,8 +254,9 @@ export default class HasManyReference extends Reference {
@return {ManyArray}
*/
value() {
let internalModel = INTERNAL_MODELS.get(this);
if (this._isLoaded()) {
return this.internalModel.getManyArray(this.key);
return internalModel.getManyArray(this.key);
}

return null;
Expand Down Expand Up @@ -322,7 +325,8 @@ export default class HasManyReference extends Reference {
this has-many relationship.
*/
load(options) {
return this.internalModel.getHasMany(this.key, options);
let internalModel = INTERNAL_MODELS.get(this);
return internalModel.getHasMany(this.key, options);
}

/**
Expand Down Expand Up @@ -374,6 +378,7 @@ export default class HasManyReference extends Reference {
@return {Promise} a promise that resolves with the ManyArray in this has-many relationship.
*/
reload(options) {
return this.internalModel.reloadHasMany(this.key, options);
let internalModel = INTERNAL_MODELS.get(this);
return internalModel.reloadHasMany(this.key, options);
}
}
26 changes: 15 additions & 11 deletions packages/store/addon/-private/system/references/record.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import RSVP, { resolve } from 'rsvp';

import Reference from './reference';
import Reference, { INTERNAL_MODELS } from './reference';

type SingleResourceDocument = import('../../ts-interfaces/ember-data-json-api').SingleResourceDocument;
type RecordInstance = import('../../ts-interfaces/record-instance').RecordInstance;
Expand All @@ -17,9 +17,12 @@ type RecordInstance = import('../../ts-interfaces/record-instance').RecordInstan
@extends Reference
*/
export default class RecordReference extends Reference {
public type = this.internalModel.modelName;
public get type() {
return INTERNAL_MODELS.get(this)!.modelName;
}

private get _id() {
return this.internalModel.id;
return INTERNAL_MODELS.get(this)!.id;
}

/**
Expand Down Expand Up @@ -123,9 +126,12 @@ export default class RecordReference extends Reference {
@method value
@return {Model} the record for this RecordReference
*/
value() {
if (this.internalModel.hasRecord) {
return this.internalModel.getRecord();
value(): RecordInstance | null {
if (this._id !== null) {
let internalModel = INTERNAL_MODELS.get(this);
if (internalModel && internalModel.isLoaded()) {
return internalModel.getRecord();
}
}
return null;
}
Expand Down Expand Up @@ -170,11 +176,9 @@ export default class RecordReference extends Reference {
@return {Promise<record>} the record for this RecordReference
*/
reload() {
let record = this.value();
if (record) {
return record.reload();
if (this._id !== null) {
return this.store.findRecord(this.type, this._id, { reload: true });
Gaurav0 marked this conversation as resolved.
Show resolved Hide resolved
}

return this.load();
throw new Error(`Unable to fetch record of type ${this.type} without an id`);
Gaurav0 marked this conversation as resolved.
Show resolved Hide resolved
}
}
24 changes: 20 additions & 4 deletions packages/store/addon/-private/system/references/reference.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FULL_LINKS_ON_RELATIONSHIPS } from '@ember-data/canary-features';
import { deprecate } from '@ember/debug';

import recordDataFor from '../record-data-for';
import { FULL_LINKS_ON_RELATIONSHIPS } from '@ember-data/canary-features';
import { DEPRECATE_REFERENCE_INTERNAL_MODEL } from '@ember-data/private-build-infra/deprecations';

type Dict<T> = import('../../ts-interfaces/utils').Dict<T>;
type JsonApiRelationship = import('../../ts-interfaces/record-data-json-api').JsonApiRelationship;
Expand Down Expand Up @@ -28,6 +29,8 @@ function isResourceIdentiferWithRelatedLinks(
return value && value.links && value.links.related;
}

export const INTERNAL_MODELS = new WeakMap<Reference, InternalModel>();

/**
This is the baseClass for the different References
like RecordReference/HasManyReference/BelongsToReference
Expand All @@ -39,8 +42,8 @@ interface Reference {
}
abstract class Reference {
public recordData: InternalModel['_recordData'];
constructor(public store: CoreStore, public internalModel: InternalModel) {
this.recordData = recordDataFor(this);
constructor(public store: CoreStore, internalModel: InternalModel) {
INTERNAL_MODELS.set(this, internalModel);
}

public _resource(): ResourceIdentifier | JsonApiRelationship | void {}
Expand Down Expand Up @@ -201,4 +204,17 @@ if (FULL_LINKS_ON_RELATIONSHIPS) {
};
}

if (DEPRECATE_REFERENCE_INTERNAL_MODEL) {
Object.defineProperty(Reference.prototype, 'internalModel', {
get() {
deprecate('Accessing the internalModel property of Reference is deprecated', false, {
id: 'ember-data:reference-internal-model',
until: '3.21',
});

return INTERNAL_MODELS.get(this);
},
});
}

export default Reference;
1 change: 1 addition & 0 deletions packages/store/addon/-private/ts-interfaces/record-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface ChangedAttributesHash {
}

export interface RecordData {
getResourceIdentifier(): RecordIdentifier | undefined;
pushData(data: JsonApiResource, calculateChange?: boolean): void;
clientDidCreate(): void;
willCommit(): void;
Expand Down