Skip to content

Commit

Permalink
Merge pull request #45 from lalalilo/fix-typing
Browse files Browse the repository at this point in the history
Fix typing
  • Loading branch information
nicgirault authored Feb 17, 2021
2 parents 8b9e404 + 492fcd6 commit 178cc7a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/getList/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export type GetList<R> = (conf: {

export type Search<R> = (
q: string,
limit: number
limit: number,
filter: Record<string, any>
) => Promise<{ rows: R[]; count: number }>

export const getMany = <R>(
Expand Down Expand Up @@ -41,7 +42,7 @@ export const getMany = <R>(
error: 'Search has not been implemented yet for this resource',
})
}
const { rows, count } = await doGetSearchList(q, limit)
const { rows, count } = await doGetSearchList(q, limit, filter)
setGetListHeaders(res, offset, count, rows.length)
res.json(rows)
}
Expand Down
14 changes: 8 additions & 6 deletions src/sequelize/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { Actions } from '..'

export const sequelizeCrud = <I extends string | number, R>(
model: any // TODO: fix any but this create sequelize dependency
model: R
): Omit<Actions<I, R>, 'search'> => {
// TODO: fix any but this create sequelize dependency
const _model: any = model
return {
create: async body => model.create(body),
create: async body => _model.create(body),
update: async (id, body) => {
const record = await model.findByPk(id)
const record = await _model.findByPk(id)
if (!record) {
throw new Error('Record not found')
}
return record.update(body)
},
getOne: async id => model.findByPk(id),
getOne: async id => _model.findByPk(id),
getList: async ({ filter, limit, offset, order }) => {
return model.findAndCountAll({
return _model.findAndCountAll({
limit,
offset,
order,
Expand All @@ -23,7 +25,7 @@ export const sequelizeCrud = <I extends string | number, R>(
})
},
destroy: async id => {
const record = await model.findByPk(id)
const record = await _model.findByPk(id)
if (!record) {
throw new Error('Record not found')
}
Expand Down
6 changes: 4 additions & 2 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,13 @@ describe('crud', () => {
const response = await dataProvider('GET_LIST', 'users', {
pagination: { page: 0, perPage: 25 },
sort: { field: 'id', order: 'DESC' },
filter: { q: 'some search' },
filter: { q: 'some search', language: 'en' },
})
expect(response.data).toEqual(rows)
expect(response.total).toEqual(totalCount)
expect(search).toHaveBeenCalledWith('some search', 25)
expect(search).toHaveBeenCalledWith('some search', 25, {
language: 'en',
})
})
})

Expand Down

0 comments on commit 178cc7a

Please sign in to comment.