-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathoperationalResource.ts
More file actions
115 lines (99 loc) · 4.09 KB
/
Copy pathoperationalResource.ts
File metadata and controls
115 lines (99 loc) · 4.09 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
import { IAdminForthSingleFilter, IAdminForthAndOrFilter, IAdminForthSort, IOperationalResource, IAdminForthDataSourceConnectorBase, AdminForthResource, IAggregationRule, IGroupByRule } from '../types/Back.js';
import { AdminForthFilterOperators } from '../types/Common.js';
import { normalizeRecordValues } from './columnValueNormalizer.js';
function sortsIfSort(sort: IAdminForthSort | IAdminForthSort[]): IAdminForthSort[] {
return (Array.isArray(sort) ? sort : [sort]) as IAdminForthSort[];
}
export default class OperationalResource implements IOperationalResource {
dataConnector: IAdminForthDataSourceConnectorBase;
resourceConfig: AdminForthResource;
constructor(dataConnector: IAdminForthDataSourceConnectorBase, resourceConfig: AdminForthResource) {
this.dataConnector = dataConnector;
this.resourceConfig = resourceConfig;
}
async get(filter: IAdminForthSingleFilter | IAdminForthAndOrFilter | Array<IAdminForthSingleFilter | IAdminForthAndOrFilter>): Promise<any | null> {
return (
await this.dataConnector.getData({
resource: this.resourceConfig,
filters: this.dataConnector.validateAndNormalizeInputFilters(filter),
limit: 1,
offset: 0,
sort: [],
})
).data[0] || null;
}
async list(
filter: IAdminForthSingleFilter | IAdminForthAndOrFilter | Array<IAdminForthSingleFilter | IAdminForthAndOrFilter>,
limit: number | null = null,
offset: number | null = null,
sort: IAdminForthSort | IAdminForthSort[] = []
): Promise<any[]> {
// check if type of limit and offset is number
if (limit !== null && typeof limit !== 'number') {
throw new Error('Limit must be a number');
}
if (offset !== null && typeof offset !== 'number') {
throw new Error('Offset must be a number');
}
let appliedLimit = limit;
if (limit === null) {
appliedLimit = 1000000000;
}
let appliedOffset = offset;
if (offset === null) {
appliedOffset = 0;
}
const { data } = await this.dataConnector.getData({
resource: this.resourceConfig,
filters: this.dataConnector.validateAndNormalizeInputFilters(filter),
limit: appliedLimit,
offset: appliedOffset,
sort: sortsIfSort(sort),
getTotals: false,
});
return data;
}
async aggregate(
filter: IAdminForthSingleFilter | IAdminForthAndOrFilter | Array<IAdminForthSingleFilter | IAdminForthAndOrFilter>,
aggregations: { [alias: string]: IAggregationRule },
groupBy?: IGroupByRule | IGroupByRule[]
): Promise<Array<{ group?: string, [key: string]: any }>> {
return this.dataConnector.aggregate({
resource: this.resourceConfig,
filters: this.dataConnector.validateAndNormalizeInputFilters(filter),
aggregations,
groupBy,
});
}
async count(filter?: IAdminForthSingleFilter | IAdminForthAndOrFilter | Array<IAdminForthSingleFilter | IAdminForthAndOrFilter> | undefined): Promise<number> {
return await this.dataConnector.getCount({
resource: this.resourceConfig,
filters: this.dataConnector.validateAndNormalizeInputFilters(filter),
});
}
async create(recordValues: any): Promise<{ ok: boolean; createdRecord: any; error?: string; }> {
const normalizedRecord = { ...recordValues };
normalizeRecordValues(this.resourceConfig, normalizedRecord);
const { ok, createdRecord, error } = await this.dataConnector.createRecord({
resource: this.resourceConfig,
record: normalizedRecord,
adminUser: null
});
return { ok, createdRecord, error };
}
async update(primaryKey: any, record: any): Promise<any> {
if (Object.keys(record).length === 0) {
return { ok: true };
}
const normalizedRecord = { ...record };
normalizeRecordValues(this.resourceConfig, normalizedRecord);
return await this.dataConnector.updateRecord({
resource: this.resourceConfig,
recordId: primaryKey,
newValues: normalizedRecord
});
}
async delete(primaryKey: any): Promise<boolean> {
return await this.dataConnector.deleteRecord({ resource: this.resourceConfig, recordId: primaryKey });
}
}