-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataAccessor.tsx
237 lines (203 loc) · 5.99 KB
/
DataAccessor.tsx
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
import axiosCustom from "../api/AxiosCustom";
export class DataAccessor {
data_url: string;
params?: { [key: string]: string };
headers: { [key: string]: string };
values?: Row;
constructor(
data_url: string,
headers: { [key: string]: string },
params?: { [key: string]: string },
values?: Row
) {
this.data_url = data_url;
this.headers = headers;
this.params = params;
this.values = values;
}
// Method to fetch rows from a table
// return type: Row[]
async fetchRows(signal?: AbortSignal | undefined) {
try {
const rows: Row[] = [];
const response = await axiosCustom.get(this.data_url, {
signal: signal,
headers: this.headers,
params: this.params
});
response.data.forEach((row: Row) => {
rows.push(row);
});
return rows;
} catch (error) {
console.error("Could not fetch data:", error);
}
}
// Method to fetch rows from a table or view by column values
// return type: Row[]
async fetchRowsByColumnValues(column_name: string, column_value: string, order_by_column: string | undefined, signal?: AbortSignal | undefined) {
try {
const rows: Row[] = [];
this.data_url = this.data_url + `?${column_name}=eq.${column_value}`;
if (order_by_column) {
this.data_url = this.data_url + `&order=${order_by_column}.asc`;
}
const response = await axiosCustom.get(this.data_url, {
signal: signal,
headers: this.headers,
});
response.data.forEach((row: Row) => {
rows.push(row);
});
return rows;
} catch (error) {
console.error("Could not fetch data:", error);
}
}
// Method to add a row to a table
// return type: AxiosResponse
async addRow() {
try {
const response = await axiosCustom.post(this.data_url, this.values, {
headers: this.headers,
});
return response;
} catch (error) {
console.error("Could not add row:", error);
}
}
// Method to update a row in a table
// return type: AxiosResponse
async updateRow() {
try {
const response = await axiosCustom.patch(this.data_url, this.values, {
headers: this.headers,
});
return response;
} catch (error) {
console.error("Could not update row:", error);
}
}
// Method to upsert a set of rows in a table
// return type: AxiosResponse
async upsert() {
try {
const response = await axiosCustom.post(this.data_url, this.values, {
params: this.params,
headers: this.headers,
});
return response;
} catch (error) {
console.error("Could not update or insert row:", error);
throw error;
}
}
/**Method to upsert a SINGLE ROW in a table
* @returns AxiosResponse
*/
async put(){
try {
const response = await axiosCustom.put(this.data_url, this.values, {
params: this.params,
headers: this.headers,
});
return response;
} catch (error) {
console.error("Could not update or insert row:", error);
throw error;
}
}
// Method to delete a row from a table
// return type: AxiosResponse
async deleteRow() {
try {
const response = await axiosCustom.delete(this.data_url, {
headers: this.headers,
params: this.params
});
return response;
} catch (error) {
console.error("Could not delete row:", error);
throw error;
}
}
async updateTableData(
new_table_data: Row[],
table: ITable,
schema_name: string
) {
let old_row: Row | undefined = {} as Row;
let old_table_data = await this.fetchRows();
const primary_key = table.getPrimaryKey()?.column_name;
// const schema_name = vmd.getTableSchema(table.table_name)?.schema_name;
// After fetching rows, change the header from Accept-Profile to Content-Profile
this.headers = { "Content-Profile": schema_name as string };
if (!primary_key) return false;
try {
if (!(typeof new_table_data[Symbol.iterator] === "function")) {
throw new Error("new_table_data is not iterable");
}
for (const new_row of new_table_data) {
old_row = old_table_data?.find(
(r) => r[primary_key] === new_row[primary_key]
);
if (old_row) {
for (const key in new_row) {
if (old_row[key] !== new_row[key]) {
console.log(this);
this.values = new_row;
this.data_url = `${table.getURL()}?${primary_key}=eq.${
new_row[primary_key]
}`;
await this.updateRow();
}
}
} else {
this.values = new_row;
this.data_url = table.getURL();
await this.addRow();
}
}
} catch (error) {
console.error("Error fetching table data:", error);
throw error;
}
// If the new data does not contain a row that exists in the old data, delete the row from the table
old_table_data = old_table_data?.filter((old_row) => {
const existsInNewData = new_table_data.some(
(new_row) => new_row[primary_key] === old_row[primary_key]
);
if (!existsInNewData) {
this.data_url = `${table.getURL()}?${primary_key}=eq.${
old_row[primary_key]
}`;
try {
this.deleteRow();
} catch (error) {
console.error("Error updating row:", error);
throw error;
}
}
return existsInNewData;
});
}
toggleAuthentication(value: boolean) {
axiosCustom.defaults.withCredentials = value;
}
}
export class Row {
row?: { [key: string]: string | number | boolean };
constructor(row?: { [key: string]: string | number | boolean }) {
this.row = row;
}
// Adding index signature to Row class to allow for dynamic access of row properties
[key: string]: any;
}
interface ITable {
table_name: string;
getURL: () => string;
getPrimaryKey: () => IColumn | undefined;
}
interface IColumn {
column_name: string;
}