-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
201 lines (185 loc) · 6.44 KB
/
index.ts
File metadata and controls
201 lines (185 loc) · 6.44 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
/**
* Type definition for querying data in the table.
* @template T - The type of the table data.
*/
type Query<T> = Partial<Record<keyof T, T[keyof T]>>;
/**
* A class to manage local storage tables with CRUD operations and synchronization with remote APIs.
* @template T - The type of the data stored in the table.
*/
class LocalStorageTable<T extends { id?: number }> {
private tableName: string;
/**
* Creates an instance of LocalStorageTable.
* @param tableName - The name of the table in local storage.
*/
constructor(tableName: string) {
this.tableName = tableName;
}
/**
* Retrieves the table data from local storage.
* @returns An array of data items from local storage.
*/
private _getTable(): T[] {
try {
return JSON.parse(localStorage.getItem(this.tableName) || '[]') as T[];
} catch (e) {
console.error('Error reading from localStorage:', e);
return [];
}
}
/**
* Saves the table data to local storage.
* @param data - An array of data items to save.
*/
private _setTable(data: T[]): void {
try {
localStorage.setItem(this.tableName, JSON.stringify(data));
} catch (e) {
console.error('Error writing to localStorage:', e);
}
}
/**
* Inserts a new item into the table.
* @param item - The item to insert.
* @returns The inserted item, with a generated ID.
*/
public insert(item: T): T {
const table = this._getTable();
item.id = Date.now();
table.push(item);
this._setTable(table);
return item;
}
/**
* Selects items from the table based on a query.
* @param query - The query to filter items by.
* @param page - The page number for pagination (default is 1).
* @param limit - The number of items per page (default is 10).
* @returns An array of items that match the query.
*/
public select(query: Query<T> = {}, page: number = 1, limit: number = 10): T[] {
const table = this._getTable();
const filtered = table.filter(item =>
Object.keys(query).every(key => item[key] === query[key])
);
const startIndex = (page - 1) * limit;
return filtered.slice(startIndex, startIndex + limit);
}
/**
* Updates items in the table that match a query.
* @param query - The query to match items to update.
* @param updates - The updates to apply to matching items.
* @returns An array of updated items.
*/
public update(query: Query<T>, updates: Partial<T>): T[] {
const table = this._getTable();
const updatedTable = table.map(item => {
if (Object.keys(query).every(key => item[key] === query[key])) {
return { ...item, ...updates };
}
return item;
});
this._setTable(updatedTable);
return this.select(query);
}
/**
* Deletes items from the table that match a query.
* @param query - The query to match items to delete.
* @returns The number of items deleted.
*/
public delete(query: Query<T>): number {
const table = this._getTable();
const newTable = table.filter(item =>
!Object.keys(query).every(key => item[key] === query[key])
);
this._setTable(newTable);
return table.length - newTable.length;
}
/**
* Clears all items from the table.
*/
public clear(): void {
this._setTable([]);
}
/**
* Fetches data from an API and stores it in a local storage table periodically.
* @param apiUrl - The API URL to fetch data from.
* @param timeInterval - The interval (in milliseconds) to fetch data.
* @param dataTableName - The local storage table name to store the data.
* @param headers - Optional headers to include in the API request.
*/
public async fetchData(apiUrl: string, timeInterval: number, dataTableName: string, headers: HeadersInit = {}): Promise<void> {
const fetchAndStoreData = async () => {
try {
const response = await fetch(apiUrl, { headers });
const data = await response.json();
const client = new LocalStorageTable<T>(dataTableName);
client._setTable(data);
} catch (error) {
console.error('Error fetching data from API:', error);
}
};
await fetchAndStoreData(); // Initial fetch
setInterval(fetchAndStoreData, timeInterval); // Fetch data periodically
}
/**
* Synchronizes the local storage table data with a remote API using POST requests periodically.
* @param apiUrl - The API URL to post data to.
* @param tableName - The local storage table name to synchronize.
* @param timeInterval - The interval (in milliseconds) to synchronize data.
* @param headers - Optional headers to include in the API request.
*/
public async syncPost(apiUrl: string, tableName: string, timeInterval: number, headers: HeadersInit = {}): Promise<void> {
const sync = async () => {
const client = new LocalStorageTable<T>(tableName);
const table = client._getTable();
for (const item of table) {
try {
await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...headers,
},
body: JSON.stringify(item),
});
} catch (error) {
console.error('Error posting data to API:', error);
}
}
};
await sync(); // Initial sync
setInterval(sync, timeInterval); // Sync data periodically
}
/**
* Synchronizes the local storage table data with a remote API using PUT requests periodically.
* @param apiUrl - The API URL to update data at.
* @param tableName - The local storage table name to synchronize.
* @param timeInterval - The interval (in milliseconds) to synchronize data.
* @param headers - Optional headers to include in the API request.
*/
public async syncUpdate(apiUrl: string, tableName: string, timeInterval: number, headers: HeadersInit = {}): Promise<void> {
const sync = async () => {
const client = new LocalStorageTable<T>(tableName);
const table = client._getTable();
for (const item of table) {
try {
await fetch(apiUrl, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
...headers,
},
body: JSON.stringify(item),
});
} catch (error) {
console.error('Error updating data in API:', error);
}
}
};
await sync(); // Initial sync
setInterval(sync, timeInterval); // Sync data periodically
}
}
export default LocalStorageTable;