-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprompt-repository.js
52 lines (52 loc) · 1.66 KB
/
prompt-repository.js
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
/* global chrome */
const generateId = () => {
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < 12; i += 1) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
};
const getCurrentTimestamp = () => new Date().toISOString();
const PromptRepository = (storageKey) => ({
async create(entity) {
const entities = await this.getAll();
const newEntity = {
id: generateId(),
createdAt: getCurrentTimestamp(),
updatedAt: getCurrentTimestamp(),
...entity,
};
entities.push(newEntity);
await chrome.storage.local.set({ [storageKey]: entities });
return newEntity;
},
async update(id, updatedEntity) {
const entities = await this.getAll();
const index = entities.findIndex((entity) => entity.id === id);
if (index !== -1) {
entities[index] = {
...entities[index],
...updatedEntity,
updatedAt: getCurrentTimestamp(),
};
await chrome.storage.local.set({ [storageKey]: entities });
return entities[index];
}
console.error('Entity not found');
},
async delete(id) {
const entities = await this.getAll();
const filteredEntities = entities.filter((entity) => entity.id !== id);
await chrome.storage.local.set({ [storageKey]: filteredEntities });
},
async getAll() {
const result = await chrome.storage.local.get(storageKey);
return result[storageKey] || [];
},
async getById(id) {
const entities = await this.getAll();
return entities.find((entity) => entity.id === id) || null;
},
});
export const promptRepository = PromptRepository('opale-prompt');