🤖 Merge PR #50044 [ember-data] [@ember-data/model] Add relationship convenience types by @jamescdavis

* Add convenience types for ember-data relationships

* Re-export relationship convenience types from @ember-data/model

* Add tests for ember-data relationship convenience types
This commit is contained in:
James C. Davis
2020-12-10 17:48:59 -05:00
committed by GitHub
parent bdae685427
commit 2038b75722
3 changed files with 14 additions and 8 deletions

View File

@@ -77,6 +77,7 @@ export namespace DS {
async?: true;
}
type AsyncBelongsTo<T extends Model> = T & PromiseObject<T>;
/**
* `DS.belongsTo` is used to define One-To-One and One-To-Many
* relationships on a [DS.Model](/api/data/classes/DS.Model.html).
@@ -89,9 +90,11 @@ export namespace DS {
modelName: K,
options?: RelationshipOptions<ModelRegistry[K]> & Async
): Ember.ComputedProperty<
ModelRegistry[K] & PromiseObject<ModelRegistry[K]>,
AsyncBelongsTo<ModelRegistry[K]>,
ModelRegistry[K]
>;
type AsyncHasMany<T extends Model> = PromiseManyArray<T>;
type SyncHasMany<T extends Model> = ManyArray<T>;
/**
* `DS.hasMany` is used to define One-To-Many and Many-To-Many
* relationships on a [DS.Model](/api/data/classes/DS.Model.html).
@@ -99,12 +102,12 @@ export namespace DS {
function hasMany<K extends keyof ModelRegistry>(
type: K,
options: RelationshipOptions<ModelRegistry[K]> & Sync
): Ember.ComputedProperty<ManyArray<ModelRegistry[K]>>;
): Ember.ComputedProperty<SyncHasMany<ModelRegistry[K]>>;
function hasMany<K extends keyof ModelRegistry>(
type: K,
options?: RelationshipOptions<ModelRegistry[K]> & Async
): Ember.ComputedProperty<
PromiseManyArray<ModelRegistry[K]>,
AsyncHasMany<ModelRegistry[K]>,
Ember.Array<ModelRegistry[K]>
>;
/**

View File

@@ -13,3 +13,6 @@ export default DS.Model;
export import attr = DS.attr;
export import belongsTo = DS.belongsTo;
export import hasMany = DS.hasMany;
export import AsyncBelongsTo = DS.AsyncBelongsTo;
export import AsyncHasMany = DS.AsyncHasMany;
export import SyncHasMany = DS.SyncHasMany;

View File

@@ -1,5 +1,5 @@
import { computed } from '@ember/object';
import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
import Model, { attr, belongsTo, AsyncBelongsTo, hasMany, AsyncHasMany, SyncHasMany } from '@ember-data/model';
import DS, { ChangedAttributes } from 'ember-data';
import RSVP from 'rsvp';
@@ -39,10 +39,10 @@ const User = Model.extend({
class Human extends Model {
@attr age: number;
@belongsTo('human') mother: Human;
// We should remove the direct use of `DS.PromiseManyArray` by creating and
// exporting a type which represents `HasMany<Person>`.
@hasMany('person') children: DS.PromiseManyArray<Person>;
@belongsTo('human') mother: AsyncBelongsTo<Human>;
@belongsTo('human', { async: false }) motherSync: Human;
@hasMany('person') children: AsyncHasMany<Person>;
@hasMany('person', { async: false }) childrenSync: SyncHasMany<Person>;
}
const user = User.create({ username: 'dwickern' });