-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 🐛 retransform data query on updateMany query
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { relationFieldsByModel } from "./helpers" | ||
|
||
export function transformDataToWhereQuery(args: any, model: string) { | ||
|
||
;['connect', 'disconnect'].forEach((action) => { | ||
Object.entries(args.data).forEach(([relation, obj]: [string, any]) => { | ||
if (typeof obj === 'object' && !Array.isArray(obj) && obj[action]) { | ||
// combine args.data.relation[action].AND and args.where.AND | ||
const ANDArgs = { AND: [...(obj[action].AND ?? []), ...(args.where[relation]?.AND ?? [])] } | ||
const relationTo = relationFieldsByModel[model][relation].relationToFields?.[0] | ||
const relationFrom = relationFieldsByModel[model][relation].relationFromFields?.[0] | ||
if (!relationTo || !relationFrom) { | ||
throw new Error('Cannot find correct relations to transform updateMany to casl query.') | ||
} | ||
args.where = { | ||
...(args.where ?? {}), | ||
[relation]: { | ||
...(args.where[relation] ?? {}), | ||
...obj[action], | ||
...(ANDArgs.AND.length > 0 ? ANDArgs : {}) | ||
} | ||
} | ||
args.data = { | ||
...args.data, | ||
[relationFrom]: obj[action][relationTo] | ||
} | ||
delete args.data[relation] | ||
delete args.where[relation][relationTo] | ||
} | ||
}) | ||
}) | ||
return args | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { transformDataToWhereQuery } from '../src/transformDataToWhereQuery' | ||
|
||
describe('transform data to where query', () => { | ||
|
||
it('converts data relations with connect to where query and transforms to relationField data entries', () => { | ||
|
||
const result = transformDataToWhereQuery({ data: { id: 1, thread: { connect: { id: 0, text: 'a' } } }, where: { id: 0 } }, | ||
'Post' | ||
) | ||
expect(result.data).toEqual({ id: 1, threadId: 0 }) | ||
expect(result.where).toEqual({ id: 0, thread: { text: 'a' } }) | ||
}) | ||
|
||
it('converts data relations with disconnect to where query and transforms to relationField data entries', () => { | ||
const result = transformDataToWhereQuery({ data: { id: 1, thread: { disconnect: { id: 0, text: 'a' } } }, where: { id: 0 } }, | ||
'Post' | ||
) | ||
expect(result.data).toEqual({ id: 1, threadId: 0 }) | ||
expect(result.where).toEqual({ id: 0, thread: { text: 'a' } }) | ||
}) | ||
}) |