Skip to content

Commit d57a63e

Browse files
committed
refactor(relationships): refactor arguments
1 parent d06dc94 commit d57a63e

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

lib/relationships.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module.exports = function (app, options) {
2626
data = options.data
2727
model = utils.getModelFromContext(ctx, app)
2828

29-
relationships(id, data, model).then(() => next()).catch(err => next(err))
29+
relationships(model, id, data).then(() => next()).catch(err => next(err))
3030
})
3131

3232
// for create
@@ -41,7 +41,7 @@ module.exports = function (app, options) {
4141
id = ctx.result.data.id
4242
data = options.data
4343
model = utils.getModelFromContext(ctx, app)
44-
return relationships(id, data, model)
44+
return relationships(model, id, data)
4545
.then(() => next())
4646
.catch(err => next(err))
4747
}
@@ -57,24 +57,26 @@ function extractIdsFromResource (resource) {
5757
return _.get(resource, 'id', null)
5858
}
5959

60-
function relationships (id, payload, ModelFrom) {
61-
if (!id || !ModelFrom) return
60+
function relationships (model, id, payload) {
61+
if (!id || !model) return
6262
const relationships = _.get(payload, 'data.relationships', {})
6363

6464
return Promise.all(
65-
Object.keys(relationships).map(name => {
66-
const relationship = relationships[name]
67-
const relationDefn = ModelFrom.relations[name]
65+
Object.keys(relationships).map(relationName => {
66+
const relationship = relationships[relationName]
67+
const relationDefn = model.relations[relationName]
6868
if (!relationDefn) return
6969

7070
const type = relationDefn.type
71-
const ModelTo = relationDefn.modelTo
71+
const modelTo = relationDefn.modelTo
7272

7373
// don't handle belongsTo in relationships function
74-
if (!ModelTo || type === 'belongsTo') return
74+
if (!modelTo || type === 'belongsTo') return
7575

7676
const data = extractIdsFromResource(relationship.data)
77-
return linkRelatedModels(name, ModelFrom, id, ModelTo, data)
77+
const from = { model, id }
78+
const to = { model: modelTo, data }
79+
return linkRelatedModels(relationName, from, to)
7880
})
7981
)
8082
}

lib/utilities/relationship-utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ function detectUpdateStrategy (Model, relationName) {
183183
if (relationDefn.type === 'belongsTo') return RELATION_TYPES.BELONGS_TO
184184
}
185185

186-
function linkRelatedModels (relationName, LeftModel, id, RightModel, data) {
186+
function linkRelatedModels (relationName, from, to) {
187+
const LeftModel = from.model
188+
const id = from.id
189+
const RightModel = to.model
190+
const data = to.data
187191
const relationDefn = LeftModel.relations[relationName]
188192
const strategy = detectUpdateStrategy(LeftModel, relationName)
189193

test/relationship-utils.test.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,10 @@ describe('relationship utils', () => {
178178
describe('linkRelatedModels()', function () {
179179
it('belongsTo', () => {
180180
setupBelongsTo()
181+
const from = { model: Comment, id: 1 }
182+
const to = { model: Post, data: 1 }
181183
return Comment.create({}, {})
182-
.then(() => linkRelatedModels('post', Comment, 1, Post, 1))
184+
.then(() => linkRelatedModels('post', from, to))
183185
.then(() => Comment.find({ where: { postId: 1 } }))
184186
.then(comments => {
185187
expect(comments.length).to.equal(1)
@@ -188,8 +190,10 @@ describe('relationship utils', () => {
188190
})
189191
it('hasMany', () => {
190192
setupHasMany()
193+
const from = { model: Post, id: 1 }
194+
const to = { model: Comment, data: [2, 3, 4] }
191195
return Comment.create([{ postId: 1 }, { postId: 1 }, { postId: 1 }, {}])
192-
.then(() => linkRelatedModels('comments', Post, 1, Comment, [2, 3, 4]))
196+
.then(() => linkRelatedModels('comments', from, to))
193197
.then(() => Comment.find({ where: { postId: 1 } }))
194198
.then(comments => {
195199
expect(comments.length).to.equal(3)
@@ -200,6 +204,8 @@ describe('relationship utils', () => {
200204
})
201205
it('hasManyThrough', () => {
202206
setupHasManyThrough()
207+
const from = { model: Post, id: 1 }
208+
const to = { model: Comment, data: [2, 3, 4] }
203209
return Promise.all([
204210
Post.create({}),
205211
Comment.create([{}, {}, {}, {}]),
@@ -209,7 +215,7 @@ describe('relationship utils', () => {
209215
{ postId: 1, commentId: 3 }
210216
])
211217
])
212-
.then(() => linkRelatedModels('comments', Post, 1, Comment, [2, 3, 4]))
218+
.then(() => linkRelatedModels('comments', from, to))
213219
.then(() => PostComment.find({ where: { postId: 1 } }))
214220
.then(postComments => {
215221
expect(postComments.length).to.equal(3)
@@ -220,8 +226,10 @@ describe('relationship utils', () => {
220226
})
221227
it('hasOne', () => {
222228
setupHasOne()
229+
const from = { model: Post, id: 1 }
230+
const to = { model: Comment, data: 1 }
223231
return Comment.create([{}, {}])
224-
.then(() => linkRelatedModels('comment', Post, 1, Comment, 1))
232+
.then(() => linkRelatedModels('comment', from, to))
225233
.then(() => Comment.find({ where: { postId: 1 } }))
226234
.then(comments => {
227235
expect(comments.length).to.equal(1)
@@ -230,8 +238,10 @@ describe('relationship utils', () => {
230238
})
231239
it('hasOne null', () => {
232240
setupHasOne()
241+
const from = { model: Post, id: 1 }
242+
const to = { model: Comment, data: null }
233243
return Comment.create([{ postId: 1 }])
234-
.then(() => linkRelatedModels('comment', Post, 1, Comment, null))
244+
.then(() => linkRelatedModels('comment', from, to))
235245
.then(() => Comment.find({ where: { postId: null } }))
236246
.then(comments => {
237247
expect(comments.length).to.equal(1)

0 commit comments

Comments
 (0)