forked from EvolutionAPI/evolution-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprismaExtensionPgpathToMysql.js
More file actions
61 lines (54 loc) · 1.69 KB
/
prismaExtensionPgpathToMysql.js
File metadata and controls
61 lines (54 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { Prisma } from '@prisma/client'
import { Logger } from '@config/logger.config';
const logger = new Logger('PGPATH2MYSQL');
function convertPgPathToMysql (path) {
if (!Array.isArray(path)) return path
let result = '$'
for (const item of path) {
if (/^\d+$/.test(item)) {
result += `[${item}]`
} else {
result += `.${item}`
}
}
return result
}
function processWhere (obj) {
if (obj && typeof obj === 'object') {
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (key === 'path') {
obj[key] = convertPgPathToMysql(obj[key]);
} else {
processWhere(obj[key]);
}
}
}
}
}
// https://www.prisma.io/docs/orm/prisma-client/client-extensions/query#modify-all-operations-in-all-models-of-your-schema
// https://www.prisma.io/docs/orm/prisma-client/client-extensions/query#modify-a-specific-operation-in-a-specific-model
const overriddenOperation = async ({ model, operation, args, query }) => {
if (args?.where) {
processWhere(args.where)
}
const result = await query(args)
logger.debug({ model, operation, args: JSON.stringify(args), result })
return result
}
export default Prisma.defineExtension({
name: 'prisma-extension-pgpath-to-mysql',
query: {
$allModels: {
findFirst: overriddenOperation,
findMany: overriddenOperation,
updateMany: overriddenOperation,
count: overriddenOperation,
deleteMany: overriddenOperation,
delete: overriddenOperation,
findUnique: overriddenOperation,
update: overriddenOperation,
upsert: overriddenOperation,
}
}
})