Skip to content

Commit 06b6247

Browse files
committed
refactor(relationships): improve argument names
1 parent d57a63e commit 06b6247

File tree

1 file changed

+57
-57
lines changed

1 file changed

+57
-57
lines changed

lib/utilities/relationship-utils.js

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -91,84 +91,84 @@ function shouldIncludeRelationships (method) {
9191
}
9292

9393
function updateHasMany (
94-
leftPrimaryKey,
95-
leftId,
94+
leftPKName,
95+
leftPKValue,
9696
RightModel,
97-
rightForeignKey,
98-
rightIds
97+
rightFKName,
98+
rightFKValues
9999
) {
100-
return RightModel.updateAll({ [leftPrimaryKey]: { inq: rightIds } }, {
101-
[rightForeignKey]: leftId
100+
return RightModel.updateAll({ [leftPKName]: { inq: rightFKValues } }, {
101+
[rightFKName]: leftPKValue
102102
})
103-
.then(() => RightModel.find({ where: { [rightForeignKey]: leftId } }))
103+
.then(() => RightModel.find({ where: { [rightFKName]: leftPKValue } }))
104104
.then(models => {
105-
const idsToUnset = _.difference(_.map(models, 'id'), rightIds)
105+
const idsToUnset = _.difference(_.map(models, 'id'), rightFKValues)
106106
return RightModel.updateAll({ id: { inq: idsToUnset } }, {
107-
[rightForeignKey]: null
107+
[rightFKName]: null
108108
})
109109
})
110110
}
111111

112112
function updateHasOne (
113-
rightPrimaryKey,
114-
leftId,
113+
rightPKName,
114+
leftPKValue,
115115
RightModel,
116-
rightForeignKey,
117-
rightId
116+
rightFKName,
117+
rightPKId
118118
) {
119-
return RightModel.updateAll({ [rightForeignKey]: leftId }, {
120-
[rightForeignKey]: null
119+
return RightModel.updateAll({ [rightFKName]: leftPKValue }, {
120+
[rightFKName]: null
121121
})
122122
.then(() => {
123-
if (rightId) {
124-
return RightModel.updateAll({ [rightPrimaryKey]: rightId }, {
125-
[rightForeignKey]: leftId
123+
if (rightPKId) {
124+
return RightModel.updateAll({ [rightPKName]: rightPKId }, {
125+
[rightFKName]: leftPKValue
126126
})
127127
}
128128
})
129129
}
130130

131131
function updateBelongsTo (
132132
LeftModel,
133-
leftPrimaryKey,
134-
leftId,
135-
leftForeignKey,
136-
rightId
133+
leftPKName,
134+
leftPKValue,
135+
leftFKName,
136+
rightPKId
137137
) {
138-
if (rightId === null) {
139-
return LeftModel.updateAll({ [leftPrimaryKey]: leftId }, {
140-
[leftForeignKey]: null
138+
if (rightPKId === null) {
139+
return LeftModel.updateAll({ [leftPKName]: leftPKValue }, {
140+
[leftFKName]: null
141141
})
142142
}
143-
return LeftModel.updateAll({ [leftPrimaryKey]: leftId }, {
144-
[leftForeignKey]: rightId
143+
return LeftModel.updateAll({ [leftPKName]: leftPKValue }, {
144+
[leftFKName]: rightPKId
145145
})
146146
}
147147

148148
function updateHasManyThrough (
149-
leftPrimaryKey,
150-
leftId,
149+
leftPKName,
150+
leftPKValue,
151151
PivotModel,
152-
leftForeignKey,
153-
rightForeignKey,
154-
rightPrimaryKey,
155-
rightIds
152+
leftFKName,
153+
rightFKName,
154+
rightPKName,
155+
rightFKValues
156156
) {
157-
return PivotModel.find({ where: { [leftForeignKey]: Number(leftId) } })
157+
return PivotModel.find({ where: { [leftFKName]: Number(leftPKValue) } })
158158
.then(models => {
159-
const existingIds = models.map(model => Number(model[rightForeignKey]))
160-
const newIds = rightIds.map(id => Number(id))
159+
const existingIds = models.map(model => Number(model[rightFKName]))
160+
const newIds = rightFKValues.map(id => Number(id))
161161
const idsToDelete = _.difference(existingIds, newIds)
162162
return PivotModel.destroyAll({
163-
[leftForeignKey]: Number(leftId),
164-
[rightForeignKey]: { inq: idsToDelete }
163+
[leftFKName]: Number(leftPKValue),
164+
[rightFKName]: { inq: idsToDelete }
165165
})
166166
.then(() => {
167167
const idsToAdd = _.difference(newIds, existingIds)
168168
return PivotModel.create(
169169
idsToAdd.map(id => ({
170-
[leftForeignKey]: Number(leftId),
171-
[rightForeignKey]: id
170+
[leftFKName]: Number(leftPKValue),
171+
[rightFKName]: id
172172
}))
173173
)
174174
})
@@ -192,37 +192,37 @@ function linkRelatedModels (relationName, from, to) {
192192
const strategy = detectUpdateStrategy(LeftModel, relationName)
193193

194194
if (strategy === RELATION_TYPES.HAS_MANY_THROUGH) {
195-
const leftPrimaryKey = utils.primaryKeyForModel(LeftModel)
196-
const rightPrimaryKey = utils.primaryKeyForModel(RightModel)
195+
const leftPKName = utils.primaryKeyForModel(LeftModel)
196+
const rightPKName = utils.primaryKeyForModel(RightModel)
197197
const PivotModel = relationDefn.modelThrough
198-
const leftForeignKey = relationDefn.keyTo
199-
const rightForeignKey = relationDefn.keyThrough
198+
const leftFKName = relationDefn.keyTo
199+
const rightFKName = relationDefn.keyThrough
200200
return updateHasManyThrough(
201-
leftPrimaryKey,
201+
leftPKName,
202202
id,
203203
PivotModel,
204-
leftForeignKey,
205-
rightForeignKey,
206-
rightPrimaryKey,
204+
leftFKName,
205+
rightFKName,
206+
rightPKName,
207207
data
208208
)
209209
}
210210

211211
if (strategy === RELATION_TYPES.HAS_MANY) {
212-
const leftPrimaryKey = utils.primaryKeyForModel(LeftModel)
213-
const rightForeignKey = relationDefn.keyTo
214-
return updateHasMany(leftPrimaryKey, id, RightModel, rightForeignKey, data)
212+
const leftPKName = utils.primaryKeyForModel(LeftModel)
213+
const rightFKName = relationDefn.keyTo
214+
return updateHasMany(leftPKName, id, RightModel, rightFKName, data)
215215
}
216216

217217
if (strategy === RELATION_TYPES.HAS_ONE) {
218-
const rightPrimaryKey = utils.primaryKeyForModel(RightModel)
219-
const rightForeignKey = relationDefn.keyTo
220-
return updateHasOne(rightPrimaryKey, id, RightModel, rightForeignKey, data)
218+
const rightPKName = utils.primaryKeyForModel(RightModel)
219+
const rightFKName = relationDefn.keyTo
220+
return updateHasOne(rightPKName, id, RightModel, rightFKName, data)
221221
}
222222

223223
if (strategy === RELATION_TYPES.BELONGS_TO) {
224-
const leftPrimaryKey = utils.primaryKeyForModel(LeftModel)
225-
const leftForeignKey = relationDefn.keyFrom
226-
return updateBelongsTo(LeftModel, leftPrimaryKey, id, leftForeignKey, data)
224+
const leftPKName = utils.primaryKeyForModel(LeftModel)
225+
const leftFKName = relationDefn.keyFrom
226+
return updateBelongsTo(LeftModel, leftPKName, id, leftFKName, data)
227227
}
228228
}

0 commit comments

Comments
 (0)