-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathdata_context.ts
More file actions
250 lines (200 loc) · 8.07 KB
/
data_context.ts
File metadata and controls
250 lines (200 loc) · 8.07 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import {
HttpClient, MetaData,
MetaEntity, combinePath,
EasyDataTable, EasyDataTableOptions,
DataLoader
} from '@easydata/core';
import { DataFilter } from '../filter/data_filter';
import { TextDataFilter } from '../filter/text_data_filter';
import { EasyDataServerLoader } from './easy_data_server_loader';
type EasyDataEndpointKey =
'GetMetaData' |
'FetchDataset' |
'FetchRecord' |
'CreateRecord' |
'UpdateRecord' |
'DeleteRecord' |
'BulkDeleteRecords';
interface CompoundRecordKey {
[key: string]: string
}
export interface EasyDataContextOptions {
metaDataId?: string;
endpoint?: string;
dataTable?: EasyDataTableOptions,
onProcessStart?: () => void;
onProcessEnd?: () => void;
}
export class DataContext {
private endpoints: Map<string, string> = new Map<string, string>();
private http: HttpClient;
private model: MetaData;
private data: EasyDataTable;
private dataLoader: EasyDataServerLoader;
private activeEntity: MetaEntity;
private options: EasyDataContextOptions;
constructor(options?: EasyDataContextOptions) {
this.options = options || {};
this.http = new HttpClient();
this.model = new MetaData();
this.model.id = options.metaDataId || '__default';
this.dataLoader = new EasyDataServerLoader(this);
const dataTableOptions = {
loader: this.dataLoader,
...options.dataTable
};
this.data = new EasyDataTable(dataTableOptions);
this.setDefaultEndpoints(this.options.endpoint || '/api/easydata');
}
public getActiveEntity() {
return this.activeEntity;
}
public setActiveSource(entityId: string) {
this.activeEntity = this.model.getRootEntity().subEntities
.filter(e => e.id == entityId)[0];
}
public getMetaData(): MetaData {
return this.model;
}
public getData() {
return this.data;
}
public getDataLoader(): DataLoader {
return this.dataLoader;
}
public createFilter(): DataFilter
public createFilter(sourceId: string, data: EasyDataTable, isLookup?: boolean): DataFilter
public createFilter(sourceId?: string, data?: EasyDataTable, isLookup?: boolean): DataFilter {
return new TextDataFilter(
this.dataLoader,
data || this.getData(),
sourceId || this.activeEntity.id,
isLookup);
}
public loadMetaData(): Promise<MetaData> {
const url = this.resolveEndpoint('GetMetaData');
this.startProcess();
return this.http.get(url)
.then(result => {
if (result.model) {
this.model.loadFromData(result.model);
}
return this.model;
})
.catch(error => {
console.error(`Error: ${error.message}. Source: ${error.sourceError}`);
return null;
})
.finally(() => {
this.endProcess();
});
}
public getHttpClient() {
return this.http;
}
public fetchDataset() {
this.data.clear();
return this.dataLoader.loadChunk({offset: 0, limit: this.data.chunkSize, needTotal: true})
.then(result => {
for(const col of result.table.columns.getItems()) {
this.data.columns.add(col);
}
this.data.setTotal(result.total);
for(const row of result.table.getCachedRows()) {
this.data.addRow(row);
}
return this.data;
})
}
public fetchRecord(keys : CompoundRecordKey, sourceId?: string) {
const url = this.resolveEndpoint('FetchRecord', { sourceId: sourceId || this.activeEntity.id });
this.startProcess();
return this.http.get(url, { queryParams: keys})
.finally(() => this.endProcess());
}
public createRecord(obj: any, sourceId?: string) {
const url = this.resolveEndpoint('CreateRecord',
{ sourceId: sourceId || this.activeEntity.id });
this.startProcess();
return this.http.post(url, obj, { dataType: 'json' })
.finally(() => this.endProcess());
}
public updateRecord(obj: any, sourceId?: string) {
const url = this.resolveEndpoint('UpdateRecord', { sourceId: sourceId || this.activeEntity.id });
this.startProcess();
return this.http.post(url, obj, { dataType: 'json' })
.finally(() => this.endProcess());
}
public deleteRecord(obj: any, sourceId?: string) {
const url = this.resolveEndpoint('DeleteRecord', { sourceId: sourceId || this.activeEntity.id });
this.startProcess();
return this.http.post(url, obj, { dataType: 'json'})
.finally(() => this.endProcess());
}
/**
* Delete records in bulk.
* @param obj Instances primary keys.
* @param sourceId Entity Id.
*/
public bulkDeleteRecords(obj: {[key: string]: object[]}, sourceId?: string) {
const url = this.resolveEndpoint('BulkDeleteRecords', { sourceId: sourceId || this.activeEntity.id });
this.startProcess();
return this.http.post(url, obj, { dataType: 'json'})
.finally(() => this.endProcess());
}
public setEndpoint(key: EasyDataEndpointKey, value: string) : void
public setEndpoint(key: EasyDataEndpointKey | string, value: string) : void {
this.endpoints.set(key, value);
}
public setEnpointIfNotExist(key: EasyDataEndpointKey, value: string): void
public setEnpointIfNotExist(key: EasyDataEndpointKey | string, value: string): void {
if (!this.endpoints.has(key))
this.endpoints.set(key, value);
}
private endpointVarsRegex = /\{.*?\}/g;
public resolveEndpoint(endpointKey: EasyDataEndpointKey, options?: any): string
public resolveEndpoint(endpointKey: EasyDataEndpointKey | string, options?: any) : string {
options = options || {};
let result = this.endpoints.get(endpointKey);
if (!result) {
throw endpointKey + ' endpoint is not defined';
}
let matches = result.match(this.endpointVarsRegex);
if (matches) {
for (let match of matches) {
let opt = match.substring(1, match.length - 1);
let optVal = options[opt];
if (!optVal) {
if (opt == 'modelId') {
optVal = this.model.getId();
}
else if (opt == 'sourceId') {
optVal = this.activeEntity.id;
}
else {
throw `Parameter [${opt}] is not defined`;
}
}
result = result.replace(match, optVal);
}
}
return result;
}
public startProcess() {
if (this.options.onProcessStart)
this.options.onProcessStart();
}
public endProcess() {
if (this.options.onProcessEnd)
this.options.onProcessEnd();
}
private setDefaultEndpoints(endpointBase : string) {
this.setEnpointIfNotExist('GetMetaData', combinePath(endpointBase, 'models/{modelId}'));
this.setEnpointIfNotExist('FetchDataset', combinePath(endpointBase, 'models/{modelId}/sources/{sourceId}/fetch'));
this.setEnpointIfNotExist('FetchRecord', combinePath(endpointBase, 'models/{modelId}/sources/{sourceId}/fetch'));
this.setEnpointIfNotExist('CreateRecord', combinePath(endpointBase, 'models/{modelId}/sources/{sourceId}/create'));
this.setEnpointIfNotExist('UpdateRecord', combinePath(endpointBase, 'models/{modelId}/sources/{sourceId}/update'));
this.setEnpointIfNotExist('DeleteRecord', combinePath(endpointBase, 'models/{modelId}/sources/{sourceId}/delete'));
this.setEnpointIfNotExist('BulkDeleteRecords', combinePath(endpointBase, 'models/{modelId}/sources/{sourceId}/bulk-delete'));
}
}