|
1
|
/** |
|
2
|
* Convert a string fields array to a specific object such as { select, relation } |
|
3
|
* @param fields |
|
4
|
* |
|
5
|
* @example |
|
6
|
* const fields = [ |
|
7
|
* "id", |
|
8
|
* "created_at", |
|
9
|
* "updated_at", |
|
10
|
* "deleted_at", |
|
11
|
* "url", |
|
12
|
* "metadata", |
|
13
|
* "tags.id", |
|
14
|
* "tags.created_at", |
|
15
|
* "tags.updated_at", |
|
16
|
* "tags.deleted_at", |
|
17
|
* "tags.value", |
|
18
|
* "options.id", |
|
19
|
* "options.created_at", |
|
20
|
* "options.updated_at", |
|
21
|
* "options.deleted_at", |
|
22
|
* "options.title", |
|
23
|
* "options.product_id", |
|
24
|
* "options.metadata", |
|
25
|
* "options.values.id", |
|
26
|
* "options.values.created_at", |
|
27
|
* "options.values.updated_at", |
|
28
|
* "options.values.deleted_at", |
|
29
|
* "options.values.value", |
|
30
|
* "options.values.option_id", |
|
31
|
* "options.values.variant_id", |
|
32
|
* "options.values.metadata", |
|
33
|
* ] |
|
34
|
* |
|
35
|
* const remoteQueryObject = stringToSelectRelationObject(fields) |
|
36
|
* |
|
37
|
* console.log(remoteQueryObject) |
|
38
|
* // { |
|
39
|
* // select: [ |
|
40
|
* // "id", |
|
41
|
* // "created_at", |
|
42
|
* // "updated_at", |
|
43
|
* // "deleted_at", |
|
44
|
* // "url", |
|
45
|
* // "metadata", |
|
46
|
* // "tags.id", |
|
47
|
* // "tags.created_at", |
|
48
|
* // "tags.updated_at", |
|
49
|
* // "tags.deleted_at", |
|
50
|
* // "tags.value", |
|
51
|
* // "options.id", |
|
52
|
* // "options.created_at", |
|
53
|
* // "options.updated_at", |
|
54
|
* // "options.deleted_at", |
|
55
|
* // "options.title", |
|
56
|
* // "options.product_id", |
|
57
|
* // "options.metadata", |
|
58
|
* // "options.values.id", |
|
59
|
* // "options.values.created_at", |
|
60
|
* // "options.values.updated_at", |
|
61
|
* // "options.values.deleted_at", |
|
62
|
* // "options.values.value", |
|
63
|
* // "options.values.option_id", |
|
64
|
* // "options.values.variant_id", |
|
65
|
* // "options.values.metadata", |
|
66
|
* // ], |
|
67
|
* // relations: ["tags", "options", "options.values"], |
|
68
|
* // } |
|
69
|
*/ |
|
70
|
export function stringToSelectRelationObject(fields: string[]): { |
|
71
|
select: string[] |
|
72
|
relations: string[] |
|
73
|
} { |
|
74
|
const tempResult = { |
|
75
|
select: new Set<string>(), |
|
76
|
relations: new Set<string>(), |
|
77
|
} |
|
78
|
|
|
79
|
for (const field of fields) { |
|
80
|
tempResult.select.add(field) |
|
81
|
|
|
82
|
if (!field.includes(".")) { |
|
83
|
continue |
|
84
|
} |
|
85
|
|
|
86
|
const segments = field.split(".") |
|
87
|
segments.pop() |
|
88
|
const relationPath = segments.join(".") |
|
89
|
|
|
90
|
tempResult.relations.add(relationPath) |
|
91
|
} |
|
92
|
|
|
93
|
return { |
|
94
|
select: Array.from(tempResult.select), |
|
95
|
relations: Array.from(tempResult.relations), |
|
96
|
} |
|
97
|
} |