Skip to content

Commit a0fc6d8

Browse files
fix: allow to have different than id fields as PK
1 parent 6d5bf46 commit a0fc6d8

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/property.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Property extends BaseProperty {
3636
}
3737

3838
isEditable() {
39-
return !this.sequelizePath._autoGenerated
39+
return !this.sequelizePath._autoGenerated && !this.sequelizePath.autoIncrement
4040
}
4141

4242
isVisible() {

src/resource.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class Resource extends BaseResource {
101101
async findMany(ids) {
102102
const sequelizeObjects = await this.SequelizeModel.findAll({
103103
where: {
104-
id: { [Op.in]: ids },
104+
[this.SequelizeModel.primaryKeyField]: { [Op.in]: ids },
105105
},
106106
})
107107
return sequelizeObjects.map(sequelizeObject => new BaseRecord(sequelizeObject.toJSON(), this))
@@ -130,7 +130,9 @@ class Resource extends BaseResource {
130130
const parsedParams = this.parseParams(params)
131131
try {
132132
await this.SequelizeModel.update(parsedParams, {
133-
where: { id },
133+
where: {
134+
[this.SequelizeModel.primaryKeyField]: id,
135+
},
134136
})
135137
const record = await this.findById(id)
136138
return record.toJSON()
@@ -158,24 +160,27 @@ class Resource extends BaseResource {
158160
/**
159161
* Check all params against values they hold. In case of wrong value it corrects it.
160162
*
161-
* What it does esactly:
163+
* What it does exactly:
162164
* - removes keys with empty strings for the `number`, `float` and 'reference' properties.
163165
*
164166
* @param {Object} params received from AdminBro form
165167
*
166168
* @return {Object} converted params
167169
*/
168170
parseParams(params) {
169-
const parasedParams = { ...params }
171+
const parsedParams = { ...params }
170172
this.properties().forEach((property) => {
171-
const value = parasedParams[property.name()]
173+
const value = parsedParams[property.name()]
172174
if (['number', 'float', 'reference'].includes(property.type())) {
173175
if (value === '') {
174-
delete parasedParams[property.name()]
176+
delete parsedParams[property.name()]
175177
}
176178
}
179+
if (!property.isEditable()) {
180+
delete parsedParams[property.name()]
181+
}
177182
})
178-
return parasedParams
183+
return parsedParams
179184
}
180185
}
181186

0 commit comments

Comments
 (0)