@@ -4,6 +4,13 @@ var _ = require('lodash')
44var statusCodes = require ( 'http-status-codes' )
55const 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 */
815module . exports = {
916 getIncludesArray : getIncludesArray ,
@@ -170,17 +177,17 @@ function updateHasManyThrough (
170177
171178function 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
179186function 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 )
0 commit comments