Skip to content

Commit

Permalink
fix: make sure sequelizeCrud update triggers update sequelize hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
nicgirault committed Sep 8, 2020
1 parent 78112d5 commit 47b076d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/sequelize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ export const sequelizeCrud = <I extends string | number, R>(
): Omit<Actions<I, R>, 'search'> => {
return {
create: async body => model.create(body),
update: async (id, body) => model.update(body, { where: { id } }),
update: async (id, body) => {
const record = await model.findByPk(id)
if (!record) {
throw new Error('Record not found')
}
return record.update(body)
},
getOne: async id => model.findByPk(id),
getList: async ({ filter, limit, offset, order }) => {
return model.findAndCountAll({
Expand All @@ -18,6 +24,9 @@ export const sequelizeCrud = <I extends string | number, R>(
},
destroy: async id => {
const record = await model.findByPk(id)
if (!record) {
throw new Error('Record not found')
}
await record.destroy()
return { id }
},
Expand Down

0 comments on commit 47b076d

Please sign in to comment.