Skip to content

Commit d06dc94

Browse files
committed
refactor(relationships): Use contants instead of magic strings
1 parent 483e31e commit d06dc94

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

lib/utilities/relationship-utils.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ var _ = require('lodash')
44
var statusCodes = require('http-status-codes')
55
const utils = require('../utils')
66

7+
const RELATION_TYPES = Object.freeze({
8+
HAS_MANY_THROUGH: 0,
9+
HAS_MANY: 1,
10+
HAS_ONE: 2,
11+
BELONGS_TO: 3
12+
})
13+
714
/* global module */
815
module.exports = {
916
getIncludesArray: getIncludesArray,
@@ -170,17 +177,17 @@ function updateHasManyThrough (
170177

171178
function detectUpdateStrategy (Model, relationName) {
172179
const relationDefn = Model.relations[relationName]
173-
if (relationDefn.modelThrough) return 'updateHasManyThrough'
174-
if (relationDefn.type === 'hasMany') return 'updateHasMany'
175-
if (relationDefn.type === 'hasOne') return 'updateHasOne'
176-
if (relationDefn.type === 'belongsTo') return 'updateBelongsTo'
180+
if (relationDefn.modelThrough) return RELATION_TYPES.HAS_MANY_THROUGH
181+
if (relationDefn.type === 'hasMany') return RELATION_TYPES.HAS_MANY
182+
if (relationDefn.type === 'hasOne') return RELATION_TYPES.HAS_ONE
183+
if (relationDefn.type === 'belongsTo') return RELATION_TYPES.BELONGS_TO
177184
}
178185

179186
function linkRelatedModels (relationName, LeftModel, id, RightModel, data) {
180187
const relationDefn = LeftModel.relations[relationName]
181188
const strategy = detectUpdateStrategy(LeftModel, relationName)
182189

183-
if (strategy === 'updateHasManyThrough') {
190+
if (strategy === RELATION_TYPES.HAS_MANY_THROUGH) {
184191
const leftPrimaryKey = utils.primaryKeyForModel(LeftModel)
185192
const rightPrimaryKey = utils.primaryKeyForModel(RightModel)
186193
const PivotModel = relationDefn.modelThrough
@@ -197,19 +204,19 @@ function linkRelatedModels (relationName, LeftModel, id, RightModel, data) {
197204
)
198205
}
199206

200-
if (strategy === 'updateHasMany') {
207+
if (strategy === RELATION_TYPES.HAS_MANY) {
201208
const leftPrimaryKey = utils.primaryKeyForModel(LeftModel)
202209
const rightForeignKey = relationDefn.keyTo
203210
return updateHasMany(leftPrimaryKey, id, RightModel, rightForeignKey, data)
204211
}
205212

206-
if (strategy === 'updateHasOne') {
213+
if (strategy === RELATION_TYPES.HAS_ONE) {
207214
const rightPrimaryKey = utils.primaryKeyForModel(RightModel)
208215
const rightForeignKey = relationDefn.keyTo
209216
return updateHasOne(rightPrimaryKey, id, RightModel, rightForeignKey, data)
210217
}
211218

212-
if (strategy === 'updateBelongsTo') {
219+
if (strategy === RELATION_TYPES.BELONGS_TO) {
213220
const leftPrimaryKey = utils.primaryKeyForModel(LeftModel)
214221
const leftForeignKey = relationDefn.keyFrom
215222
return updateBelongsTo(LeftModel, leftPrimaryKey, id, leftForeignKey, data)

test/config.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict'
2+
13
var request = require('supertest')
24
var loopback = require('loopback')
35
var expect = require('chai').expect

test/relationship-utils.test.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -241,23 +241,21 @@ describe('relationship utils', () => {
241241
})
242242

243243
describe('detectUpdateStrategy()', function () {
244+
it('hasManyThrough', () => {
245+
setupHasManyThrough()
246+
expect(detectUpdateStrategy(Post, 'comments')).to.equal(0)
247+
})
244248
it('hasMany', () => {
245249
setupHasMany()
246-
expect(detectUpdateStrategy(Post, 'comments')).to.equal('updateHasMany')
247-
})
248-
it('belongsTo', () => {
249-
setupBelongsTo()
250-
expect(detectUpdateStrategy(Comment, 'post')).to.equal('updateBelongsTo')
250+
expect(detectUpdateStrategy(Post, 'comments')).to.equal(1)
251251
})
252252
it('hasOne', () => {
253253
setupHasOne()
254-
expect(detectUpdateStrategy(Post, 'comment')).to.equal('updateHasOne')
254+
expect(detectUpdateStrategy(Post, 'comment')).to.equal(2)
255255
})
256-
it('hasManyThrough', () => {
257-
setupHasManyThrough()
258-
expect(detectUpdateStrategy(Post, 'comments')).to.equal(
259-
'updateHasManyThrough'
260-
)
256+
it('belongsTo', () => {
257+
setupBelongsTo()
258+
expect(detectUpdateStrategy(Comment, 'post')).to.equal(3)
261259
})
262260
})
263261
})

0 commit comments

Comments
 (0)