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

RFC 794: Simplifying Schema Definition Service type signatures #7867

Merged
merged 1 commit into from
Feb 14, 2022
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 @@ -151,7 +151,7 @@ module('unit/model - Custom Class Model', function (hooks) {
this.owner.register('service:store', CreationStore);
store = this.owner.lookup('service:store');
let schema: SchemaDefinitionService = {
attributesDefinitionFor(modelName: string): AttributesSchema {
attributesDefinitionFor({ type: string }): AttributesSchema {
return {
name: {
type: 'string',
Expand All @@ -161,7 +161,7 @@ module('unit/model - Custom Class Model', function (hooks) {
},
};
},
relationshipsDefinitionFor(modelName: string): RelationshipsSchema {
relationshipsDefinitionFor({ type: string }): RelationshipsSchema {
return {};
},
doesTypeExist() {
Expand Down Expand Up @@ -233,7 +233,7 @@ module('unit/model - Custom Class Model', function (hooks) {
this.owner.register('service:store', CustomStore);
store = this.owner.lookup('service:store');
let schema: SchemaDefinitionService = {
attributesDefinitionFor(identifier: string | RecordIdentifier): AttributesSchema {
attributesDefinitionFor(identifier: RecordIdentifier | { type: string }): AttributesSchema {
if (typeof identifier === 'string') {
assert.strictEqual(identifier, 'person', 'type passed in to the schema hooks');
} else {
Expand All @@ -254,7 +254,7 @@ module('unit/model - Custom Class Model', function (hooks) {
},
};
},
relationshipsDefinitionFor(identifier: string | RecordIdentifier): RelationshipsSchema {
relationshipsDefinitionFor(identifier: RecordIdentifier | { type: string }): RelationshipsSchema {
if (typeof identifier === 'string') {
assert.strictEqual(identifier, 'person', 'type passed in to the schema hooks');
} else {
Expand Down Expand Up @@ -385,7 +385,7 @@ module('unit/model - Custom Class Model', function (hooks) {
this.owner.register('service:store', CustomStore);
store = this.owner.lookup('service:store');
let schema: SchemaDefinitionService = {
attributesDefinitionFor(identifier: string | RecordIdentifier): AttributesSchema {
attributesDefinitionFor(identifier: RecordIdentifier | { type: string }): AttributesSchema {
let modelName = (identifier as RecordIdentifier).type || identifier;
if (modelName === 'person') {
return {
Expand All @@ -409,7 +409,7 @@ module('unit/model - Custom Class Model', function (hooks) {
return {};
}
},
relationshipsDefinitionFor(identifier: string | RecordIdentifier): RelationshipsSchema {
relationshipsDefinitionFor(identifier: RecordIdentifier | { type: string }): RelationshipsSchema {
let modelName = (identifier as RecordIdentifier).type || identifier;
if (modelName === 'person') {
return {
Expand Down Expand Up @@ -469,7 +469,7 @@ module('unit/model - Custom Class Model', function (hooks) {
this.owner.register('service:store', CustomStore);
store = this.owner.lookup('service:store');
let schema: SchemaDefinitionService = {
attributesDefinitionFor(modelName: string): AttributesSchema {
attributesDefinitionFor({ type: modelName }: { type: string }): AttributesSchema {
if (modelName === 'person') {
return {
name: {
Expand All @@ -492,7 +492,7 @@ module('unit/model - Custom Class Model', function (hooks) {
return {};
}
},
relationshipsDefinitionFor(modelName: string): RelationshipsSchema {
relationshipsDefinitionFor({ type: modelName }: { type: string }): RelationshipsSchema {
if (modelName === 'person') {
return {
house: {
Expand Down Expand Up @@ -541,7 +541,7 @@ module('unit/model - Custom Class Model', function (hooks) {
this.owner.register('service:store', CustomStore);
store = this.owner.lookup('service:store');
let schema: SchemaDefinitionService = {
attributesDefinitionFor(modelName: string): AttributesSchema {
attributesDefinitionFor({ type: modelName }: { type: string }): AttributesSchema {
if (modelName === 'person') {
return {
name: {
Expand All @@ -564,7 +564,7 @@ module('unit/model - Custom Class Model', function (hooks) {
return {};
}
},
relationshipsDefinitionFor(modelName: string): RelationshipsSchema {
relationshipsDefinitionFor({ type: modelName }: { type: string }): RelationshipsSchema {
if (modelName === 'person') {
return {
house: {
Expand Down
20 changes: 6 additions & 14 deletions packages/store/addon/-private/system/core-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ abstract class CoreStore extends Service {
}

// convert relationship Records to RecordDatas before passing to RecordData
let defs = this._relationshipsDefinitionFor(modelName);
let defs = this._relationshipsDefinitionFor({ type: modelName });

if (defs !== null) {
let keys = Object.keys(properties);
Expand Down Expand Up @@ -398,20 +398,12 @@ abstract class CoreStore extends Service {
}

// FeatureFlagged in the DSModelStore claas
_attributesDefinitionFor(modelName: string, identifier?: StableRecordIdentifier): AttributesSchema {
if (identifier) {
return this.getSchemaDefinitionService().attributesDefinitionFor(identifier);
} else {
return this.getSchemaDefinitionService().attributesDefinitionFor(modelName);
}
_attributesDefinitionFor(identifier: RecordIdentifier | { type: string }): AttributesSchema {
return this.getSchemaDefinitionService().attributesDefinitionFor(identifier);
}

_relationshipsDefinitionFor(modelName: string, identifier?: StableRecordIdentifier) {
if (identifier) {
return this.getSchemaDefinitionService().relationshipsDefinitionFor(identifier);
} else {
return this.getSchemaDefinitionService().relationshipsDefinitionFor(modelName);
}
_relationshipsDefinitionFor(identifier: RecordIdentifier | { type: string }) {
return this.getSchemaDefinitionService().relationshipsDefinitionFor(identifier);
}

registerSchemaDefinitionService(schema: SchemaDefinitionService) {
Expand All @@ -424,7 +416,7 @@ abstract class CoreStore extends Service {

// TODO Double check this return value is correct
_relationshipMetaFor(modelName: string, id: string | null, key: string) {
return this._relationshipsDefinitionFor(modelName)[key];
return this._relationshipsDefinitionFor({ type: modelName })[key];
}

/**
Expand Down
20 changes: 6 additions & 14 deletions packages/store/addon/-private/system/ds-model-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DEBUG } from '@glimmer/env';
import type DSModelClass from '@ember-data/model';

import type { DSModel } from '../ts-interfaces/ds-model';
import type { StableRecordIdentifier } from '../ts-interfaces/identifier';
import type { RecordIdentifier, StableRecordIdentifier } from '../ts-interfaces/identifier';
import type { RecordDataRecordWrapper } from '../ts-interfaces/record-data-record-wrapper';
import type { RelationshipsSchema } from '../ts-interfaces/record-data-schemas';
import type { SchemaDefinitionService } from '../ts-interfaces/schema-definition-service';
Expand Down Expand Up @@ -105,23 +105,15 @@ class Store extends CoreStore {
}

_relationshipMetaFor(modelName: string, id: string | null, key: string) {
return this._relationshipsDefinitionFor(modelName)[key];
return this._relationshipsDefinitionFor({ type: modelName })[key];
}

_attributesDefinitionFor(modelName: string, identifier?: StableRecordIdentifier) {
if (identifier) {
return this.getSchemaDefinitionService().attributesDefinitionFor(identifier);
} else {
return this.getSchemaDefinitionService().attributesDefinitionFor(modelName);
}
_attributesDefinitionFor(identifier: RecordIdentifier | { type: string }) {
return this.getSchemaDefinitionService().attributesDefinitionFor(identifier);
}

_relationshipsDefinitionFor(modelName: string, identifier?: StableRecordIdentifier): RelationshipsSchema {
if (identifier) {
return this.getSchemaDefinitionService().relationshipsDefinitionFor(identifier);
} else {
return this.getSchemaDefinitionService().relationshipsDefinitionFor(modelName);
}
_relationshipsDefinitionFor(identifier: RecordIdentifier | { type: string }): RelationshipsSchema {
return this.getSchemaDefinitionService().relationshipsDefinitionFor(identifier);
}

getSchemaDefinitionService(): SchemaDefinitionService {
Expand Down
14 changes: 7 additions & 7 deletions packages/store/addon/-private/system/model/shim-model-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,26 @@ export default class ShimModelClass implements ModelSchema {
constructor(private __store: CoreStore, public modelName: string) {}

get fields(): Map<string, 'attribute' | 'belongsTo' | 'hasMany'> {
let attrs = this.__store._attributesDefinitionFor(this.modelName);
let relationships = this.__store._relationshipsDefinitionFor(this.modelName);
let attrs = this.__store._attributesDefinitionFor({ type: this.modelName });
let relationships = this.__store._relationshipsDefinitionFor({ type: this.modelName });
let fields = new Map<string, 'attribute' | 'belongsTo' | 'hasMany'>();
Object.keys(attrs).forEach((key) => fields.set(key, 'attribute'));
Object.keys(relationships).forEach((key) => fields.set(key, relationships[key]!.kind));
return fields;
}

get attributes(): Map<string, AttributeSchema> {
let attrs = this.__store._attributesDefinitionFor(this.modelName);
let attrs = this.__store._attributesDefinitionFor({ type: this.modelName });
return mapFromHash(attrs);
}

get relationshipsByName(): Map<string, RelationshipSchema> {
let relationships = this.__store._relationshipsDefinitionFor(this.modelName);
let relationships = this.__store._relationshipsDefinitionFor({ type: this.modelName });
return mapFromHash(relationships);
}

eachAttribute<T>(callback: (this: T | undefined, key: string, attribute: AttributeSchema) => void, binding?: T) {
let attrDefs = this.__store._attributesDefinitionFor(this.modelName);
let attrDefs = this.__store._attributesDefinitionFor({ type: this.modelName });
Object.keys(attrDefs).forEach((key) => {
callback.call(binding, key, attrDefs[key] as AttributeSchema);
});
Expand All @@ -65,7 +65,7 @@ export default class ShimModelClass implements ModelSchema {
callback: (this: T | undefined, key: string, relationship: RelationshipSchema) => void,
binding?: T
) {
let relationshipDefs = this.__store._relationshipsDefinitionFor(this.modelName);
let relationshipDefs = this.__store._relationshipsDefinitionFor({ type: this.modelName });
Object.keys(relationshipDefs).forEach((key) => {
callback.call(binding, key, relationshipDefs[key] as RelationshipSchema);
});
Expand All @@ -75,7 +75,7 @@ export default class ShimModelClass implements ModelSchema {
callback: (this: T | undefined, key: string, relationship: RelationshipSchema) => void,
binding?: T
) {
let relationshipDefs = this.__store._relationshipsDefinitionFor(this.modelName);
let relationshipDefs = this.__store._relationshipsDefinitionFor({ type: this.modelName });
Object.keys(relationshipDefs).forEach((key) => {
if (relationshipDefs[key]!.type) {
callback.call(binding, key, relationshipDefs[key] as RelationshipSchema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class DSModelSchemaDefinitionService {
constructor(public store: Store) {}

// Following the existing RD implementation
attributesDefinitionFor(identifier: RecordIdentifier | string): AttributesSchema {
attributesDefinitionFor(identifier: RecordIdentifier | { type: string }): AttributesSchema {
let modelName, attributes;
if (typeof identifier === 'string') {
modelName = identifier;
Expand All @@ -55,7 +55,7 @@ export class DSModelSchemaDefinitionService {
}

// Following the existing RD implementation
relationshipsDefinitionFor(identifier: RecordIdentifier | string): RelationshipsSchema {
relationshipsDefinitionFor(identifier: RecordIdentifier | { type: string }): RelationshipsSchema {
let modelName, relationships;
if (typeof identifier === 'string') {
modelName = identifier;
Expand Down
6 changes: 3 additions & 3 deletions packages/store/addon/-private/system/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export default class Snapshot implements Snapshot {
}
let record = this.record;
let attributes = (this.__attributes = Object.create(null));
let attrs = Object.keys(this._store._attributesDefinitionFor(this.modelName, this.identifier));
let attrs = Object.keys(this._store._attributesDefinitionFor(this.identifier));
attrs.forEach((keyName) => {
if (schemaIsDSModel(this.type)) {
// if the schema is for a DSModel then the instance is too
Expand Down Expand Up @@ -478,7 +478,7 @@ export default class Snapshot implements Snapshot {
@public
*/
eachAttribute(callback: (key: string, meta: AttributeSchema) => void, binding?: unknown): void {
let attrDefs = this._store._attributesDefinitionFor(this.modelName, this.identifier);
let attrDefs = this._store._attributesDefinitionFor(this.identifier);
Object.keys(attrDefs).forEach((key) => {
callback.call(binding, key, attrDefs[key] as AttributeSchema);
});
Expand All @@ -502,7 +502,7 @@ export default class Snapshot implements Snapshot {
@public
*/
eachRelationship(callback: (key: string, meta: RelationshipSchema) => void, binding?: unknown): void {
let relationshipDefs = this._store._relationshipsDefinitionFor(this.modelName, this.identifier);
let relationshipDefs = this._store._relationshipsDefinitionFor(this.identifier);
Object.keys(relationshipDefs).forEach((key) => {
callback.call(binding, key, relationshipDefs[key] as RelationshipSchema);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ export default class RecordDataStoreWrapper implements StoreWrapper {
}

attributesDefinitionFor(type: string): AttributesSchema {
return this._store._attributesDefinitionFor(type);
return this._store._attributesDefinitionFor({ type });
}

relationshipsDefinitionFor(type: string): RelationshipsSchema {
return this._store._relationshipsDefinitionFor(type);
return this._store._relationshipsDefinitionFor({ type });
}

inverseForRelationship(type: string, key: string): string | null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import type { AttributesSchema, RelationshipsSchema } from './record-data-schema

export interface SchemaDefinitionService {
doesTypeExist(modelName: string): boolean;
attributesDefinitionFor(identifier: RecordIdentifier | string): AttributesSchema;
relationshipsDefinitionFor(identifier: RecordIdentifier | string): RelationshipsSchema;
attributesDefinitionFor(identifier: RecordIdentifier | { type: string }): AttributesSchema;
relationshipsDefinitionFor(identifier: RecordIdentifier | { type: string }): RelationshipsSchema;
}