Skip to content

Commit

Permalink
feat: full-relationship definitions when scaffolding with blueprints (#…
Browse files Browse the repository at this point in the history
…8542)

* docs: show correct relationship definitions

* fix tests
  • Loading branch information
runspired authored Apr 5, 2023
1 parent 3b3da8a commit 038e1ce
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 41 deletions.
2 changes: 1 addition & 1 deletion packages/graph/src/-private/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ export class Graph {
export default class Post extends Model {
@attr title;
@hasMany('comment') comments;
@hasMany('comment', { async: true, inverse: null }) comments;
}
```
Expand Down
4 changes: 2 additions & 2 deletions packages/model/blueprints/model/HELP.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ would result in the following model:
import Model, { belongsTo, hasMany, attr } from '@ember-data/model';

export default class TacoModel extends Model {
@belongsTo('protein') filling;
@hasMany('topping') toppings;
@belongsTo('protein', { async: false, inverse: null }) filling;
@hasMany('topping', { async: false, inverse: null }) toppings;
@attr('string') name;
@attr('number') price;
@attr misc;
Expand Down
18 changes: 5 additions & 13 deletions packages/model/blueprints/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,11 @@ function nativeAttr(attr) {
result;

if (type === 'belongs-to') {
if (name === propertyName) {
result = '@belongsTo';
} else {
result = "@belongsTo('" + name + "')";
}
result = "@belongsTo('" + name + "', { async: false, inverse: null })";
} else if (type === 'has-many') {
if (inflection.pluralize(name) === propertyName) {
result = '@hasMany';
} else {
result = "@hasMany('" + name + "')";
}
result = "@hasMany('" + name + "', { async: false, inverse: null })";
} else if (type === '') {
result = '@attr()';
result = '@attr';
} else {
result = "@attr('" + type + "')";
}
Expand All @@ -144,9 +136,9 @@ function classicAttr(attr) {
result;

if (type === 'belongs-to') {
result = "belongsTo('" + name + "')";
result = "belongsTo('" + name + "', { async: false, inverse: null })";
} else if (type === 'has-many') {
result = "hasMany('" + name + "')";
result = "hasMany('" + name + "', { async: false, inverse: null })";
} else if (type === '') {
//"If you don't specify the type of the attribute, it will be whatever was provided by the server"
//https://emberjs.com/guides/models/defining-models/
Expand Down
21 changes: 5 additions & 16 deletions packages/model/src/-private/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,38 +59,26 @@ function normalizeType(type) {
```
#### One-To-Many
To declare a one-to-many relationship between two models, use
`belongsTo` in combination with `hasMany`, like this:
```app/models/post.js
import Model, { hasMany } from '@ember-data/model';
export default class PostModel extends Model {
@hasMany('comment') comments;
}
```
```app/models/comment.js
import Model, { belongsTo } from '@ember-data/model';
export default class CommentModel extends Model {
@belongsTo('post') post;
@hasMany('comment', { async: false, inverse: 'post' }) comments;
}
```
You can avoid passing a string as the first parameter. In that case Ember Data
will infer the type from the key name.
```app/models/comment.js
import Model, { belongsTo } from '@ember-data/model';
export default class CommentModel extends Model {
@belongsTo post;
@belongsTo('post', { async: false, inverse: 'comments' }) post;
}
```
will lookup for a Post type.
#### Sync relationships
Ember Data resolves sync relationships with the related resources
Expand All @@ -102,7 +90,8 @@ function normalizeType(type) {
export default class CommentModel extends Model {
@belongsTo('post', {
async: false
async: false,
inverse: null
})
post;
}
Expand Down
18 changes: 9 additions & 9 deletions tests/blueprints/tests/model-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ describe('Acceptance: generate and destroy model blueprints', function () {
expect(_file('app/models/comment.js'))
.to.contain(`import Model, { belongsTo } from '@ember-data/model';`)
.to.contain('export default Model.extend(')
.to.contain(" post: belongsTo('post'),")
.to.contain(" author: belongsTo('user')");
.to.contain(" post: belongsTo('post', { async: false, inverse: null }),")
.to.contain(" author: belongsTo('user', { async: false, inverse: null })");

expect(_file('tests/unit/models/comment-test.js')).to.equal(
fixture(__dirname, 'model-test/comment-default.js')
Expand All @@ -91,8 +91,8 @@ describe('Acceptance: generate and destroy model blueprints', function () {
expect(_file('app/models/post.js'))
.to.contain(`import Model, { hasMany } from '@ember-data/model';`)
.to.contain('export default Model.extend(')
.to.contain(" comments: hasMany('comment')")
.to.contain(" otherComments: hasMany('comment')");
.to.contain(" comments: hasMany('comment', { async: false, inverse: null })")
.to.contain(" otherComments: hasMany('comment', { async: false, inverse: null })");

expect(_file('tests/unit/models/post-test.js')).to.equal(fixture(__dirname, 'model-test/post-default.js'));
});
Expand Down Expand Up @@ -197,7 +197,7 @@ describe('Acceptance: generate and destroy model blueprints', function () {
expect(_file('app/models/foo.js'))
.to.contain(`import Model, { attr } from '@ember-data/model';`)
.to.contain('export default class FooModel extends Model {')
.to.contain(' @attr() misc;')
.to.contain(' @attr misc;')
.to.contain(" @attr('array') skills;")
.to.contain(" @attr('boolean') isActive;")
.to.contain(" @attr('date') birthday;")
Expand All @@ -217,8 +217,8 @@ describe('Acceptance: generate and destroy model blueprints', function () {
expect(_file('app/models/comment.js'))
.to.contain(`import Model, { belongsTo } from '@ember-data/model';`)
.to.contain('export default class CommentModel extends Model {')
.to.contain(' @belongsTo post;')
.to.contain(" @belongsTo('user') author;");
.to.contain(` @belongsTo('post', { async: false, inverse: null }) post;`)
.to.contain(" @belongsTo('user', { async: false, inverse: null }) author;");

expect(_file('tests/unit/models/comment-test.js')).to.equal(
fixture(__dirname, 'model-test/comment-default.js')
Expand All @@ -233,8 +233,8 @@ describe('Acceptance: generate and destroy model blueprints', function () {
expect(_file('app/models/post.js'))
.to.contain(`import Model, { hasMany } from '@ember-data/model';`)
.to.contain('export default class PostModel extends Model {')
.to.contain(' @hasMany comments;')
.to.contain(" @hasMany('comment') otherComments;");
.to.contain(` @hasMany('comment', { async: false, inverse: null }) comments;`)
.to.contain(" @hasMany('comment', { async: false, inverse: null }) otherComments;");

expect(_file('tests/unit/models/post-test.js')).to.equal(fixture(__dirname, 'model-test/post-default.js'));
});
Expand Down

0 comments on commit 038e1ce

Please sign in to comment.