Skip to content

Commit 28c25e3

Browse files
committed
fixed add/update operation serialization
1 parent bb48320 commit 28c25e3

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/processors/knex-processor.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,16 @@ export default class KnexProcessor<ResourceT extends Resource> extends Operation
130130
const { id } = op.ref;
131131
const primaryKeyName = this.resourceClass.schema.primaryKeyName || DEFAULT_PRIMARY_KEY;
132132

133+
const dataToUpdate = Object.keys(op.data.attributes)
134+
.map(attribute => ({
135+
[this.appInstance.app.serializer.attributeToColumn(attribute)]: op.data.attributes[attribute]
136+
}))
137+
.reduce((keyValues, keyValue) => ({ ...keyValues, ...keyValue }), {});
138+
133139
const updated = await this.getQuery()
134140
.where({ [primaryKeyName]: id })
135141
.first()
136-
.update(op.data.attributes);
142+
.update(dataToUpdate);
137143

138144
if (!updated) {
139145
throw JsonApiErrors.RecordNotExists();
@@ -147,7 +153,14 @@ export default class KnexProcessor<ResourceT extends Resource> extends Operation
147153

148154
async add(op: Operation): Promise<HasId> {
149155
const primaryKeyName = this.resourceClass.schema.primaryKeyName || DEFAULT_PRIMARY_KEY;
150-
const ids = await this.getQuery().insert(op.data.attributes, primaryKeyName);
156+
157+
const dataToInsert = Object.keys(op.data.attributes)
158+
.map(attribute => ({
159+
[this.appInstance.app.serializer.attributeToColumn(attribute)]: op.data.attributes[attribute]
160+
}))
161+
.reduce((keyValues, keyValue) => ({ ...keyValues, ...keyValue }), {});
162+
163+
const ids = await this.getQuery().insert(dataToInsert, primaryKeyName);
151164

152165
return await this.getQuery()
153166
.whereIn(primaryKeyName, ids)

0 commit comments

Comments
 (0)