-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathread-builder-state.ts
More file actions
86 lines (77 loc) · 2.63 KB
/
read-builder-state.ts
File metadata and controls
86 lines (77 loc) · 2.63 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import type { QueryOptions } from "odata-query";
import type { FilterExpression } from "../../orm/operators";
import type { SystemColumnsOption } from "../query/types";
import type { NavigationConfig } from "../query/url-builder";
import type { ExpandConfig } from "./shared-types";
export interface QueryReadBuilderState<TSchema> {
queryOptions: Partial<QueryOptions<TSchema>>;
filterExpression?: FilterExpression;
expandConfigs: ExpandConfig[];
singleMode: "exact" | "maybe" | false;
isCountMode: boolean;
includeCountMode: boolean;
fieldMapping?: Record<string, string>;
systemColumns?: SystemColumnsOption;
navigation?: NavigationConfig;
}
export function createInitialQueryReadBuilderState<TSchema>(): QueryReadBuilderState<TSchema> {
return {
queryOptions: {},
expandConfigs: [],
singleMode: false,
isCountMode: false,
includeCountMode: false,
};
}
export function cloneQueryReadBuilderState<TSchema>(
state: QueryReadBuilderState<TSchema>,
changes?: Partial<QueryReadBuilderState<TSchema>> & {
queryOptions?: Partial<QueryOptions<TSchema>>;
},
): QueryReadBuilderState<TSchema> {
let fieldMapping = state.fieldMapping ? { ...state.fieldMapping } : undefined;
if ("fieldMapping" in (changes ?? {})) {
fieldMapping = changes?.fieldMapping ? { ...changes.fieldMapping } : undefined;
}
return {
...state,
...changes,
queryOptions: {
...state.queryOptions,
...(changes?.queryOptions ?? {}),
},
expandConfigs: changes?.expandConfigs ? [...changes.expandConfigs] : [...state.expandConfigs],
fieldMapping,
};
}
export interface RecordReadBuilderState {
selectedFields?: string[];
expandConfigs: ExpandConfig[];
fieldMapping?: Record<string, string>;
systemColumns?: SystemColumnsOption;
}
export function createInitialRecordReadBuilderState(): RecordReadBuilderState {
return {
expandConfigs: [],
};
}
export function cloneRecordReadBuilderState(
state: RecordReadBuilderState,
changes?: Partial<RecordReadBuilderState>,
): RecordReadBuilderState {
let selectedFields = state.selectedFields ? [...state.selectedFields] : undefined;
if ("selectedFields" in (changes ?? {})) {
selectedFields = changes?.selectedFields ? [...changes.selectedFields] : undefined;
}
let fieldMapping = state.fieldMapping ? { ...state.fieldMapping } : undefined;
if ("fieldMapping" in (changes ?? {})) {
fieldMapping = changes?.fieldMapping ? { ...changes.fieldMapping } : undefined;
}
return {
...state,
...changes,
selectedFields,
expandConfigs: changes?.expandConfigs ? [...changes.expandConfigs] : [...state.expandConfigs],
fieldMapping,
};
}