Skip to content

Commit d97c0f4

Browse files
committed
comments
1 parent 60ce8d5 commit d97c0f4

File tree

1 file changed

+52
-14
lines changed

1 file changed

+52
-14
lines changed

src/utils/IndexedDBUtil.ts

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { DBSchema, openDB } from '@tempfix/idb';
66
const dbName: string = 'pid-component';
77
const dbVersion: number = undefined;
88

9-
interface PIDComponentDB extends DBSchema {
9+
/**
10+
* The database schema for the PID component.
11+
* @interface PIDComponentDB
12+
* @extends DBSchema
13+
*/
14+
export interface PIDComponentDB extends DBSchema {
1015
entities: {
1116
key: string;
1217
value: {
@@ -36,6 +41,11 @@ interface PIDComponentDB extends DBSchema {
3641
};
3742
}
3843

44+
/**
45+
* Opens the indexedDB database for the PID component and creates the object stores and indexes if they do not exist.
46+
* @type {Promise<IDBPDatabase<PIDComponentDB>>}
47+
* @const
48+
*/
3949
const dbPromise = openDB<PIDComponentDB>(dbName, dbVersion, {
4050
upgrade(db) {
4151
const entityStore = db.createObjectStore('entities', {
@@ -46,16 +56,23 @@ const dbPromise = openDB<PIDComponentDB>(dbName, dbVersion, {
4656
const relationStore = db.createObjectStore('relations', {
4757
autoIncrement: true,
4858
});
59+
60+
// Create indexes for the relations
4961
relationStore.createIndex('by-start', 'start', { unique: false });
5062
relationStore.createIndex('by-description', 'description', { unique: false });
5163
relationStore.createIndex('by-end', 'end', { unique: false });
5264
},
5365
});
5466

67+
/**
68+
* Adds an entity to the database.
69+
* @param {GenericIdentifierType} renderer The renderer to add to the database.
70+
*/
5571
export async function addEntity(renderer: GenericIdentifierType) {
5672
const context = document.documentURI;
5773
const db = await dbPromise;
5874

75+
// Add the entity to the entities object store
5976
await db
6077
.add('entities', {
6178
value: renderer.value,
@@ -71,16 +88,19 @@ export async function addEntity(renderer: GenericIdentifierType) {
7188
});
7289
console.debug('added entity', renderer);
7390

91+
// Add the relations to the relations object store
92+
// Start a new transaction
7493
const tx = db.transaction('relations', 'readwrite');
7594
const promises = [];
7695

7796
for (const item of renderer.items) {
97+
// Create a relation object
7898
const relation = {
7999
start: renderer.value,
80100
description: item.keyTitle,
81101
end: item.value,
82-
// connectionType: "startToEnd"
83102
};
103+
// Check if the relation already exists
84104
const index = tx.store.index('by-start');
85105
let cursor = await index.openCursor();
86106
while (cursor) {
@@ -90,13 +110,20 @@ export async function addEntity(renderer: GenericIdentifierType) {
90110
}
91111
cursor = await cursor.continue();
92112
}
113+
// Add the relation to the relations object store if it does not exist
93114
promises.push(tx.store.add(relation));
94115
}
95116
promises.push(tx.done);
96117
await Promise.all(promises);
97118
console.debug('added relations', promises);
98119
}
99120

121+
/**
122+
* Gets an entity from the database. If the entity does not exist, it is created.
123+
* @returns {Promise<GenericIdentifierType>} The renderer for the entity.
124+
* @param {string} value The stringified value of the entity, e.g. the PID.
125+
* @param {{type: string, values: {name: string, value: any}[]}[]} settings The settings for all renderers.
126+
*/
100127
export const getEntity = async function (
101128
value: string,
102129
settings: {
@@ -107,6 +134,7 @@ export const getEntity = async function (
107134
}[];
108135
}[],
109136
): Promise<GenericIdentifierType> {
137+
// Try to get the entity from the database
110138
try {
111139
const db = await dbPromise;
112140
const entity:
@@ -120,39 +148,49 @@ export const getEntity = async function (
120148
| undefined = await db.get('entities', value);
121149

122150
if (entity !== undefined) {
151+
// If the entity was found, check if the TTL has expired
123152
console.debug('Found entity for value in db', entity, value);
124153
const entitySettings = settings.find(value => value.type === entity.rendererKey)?.values;
125154
const ttl = entitySettings?.find(value => value.name === 'ttl');
126155

127156
if (ttl != undefined && ttl.value != undefined && (new Date().getTime() - entity.lastAccess.getTime() > ttl.value || ttl.value === 0)) {
157+
// If the TTL has expired, delete the entity from the database and move on to creating a new one (down below)
128158
console.log('TTL expired! Deleting entry in db', ttl.value, new Date().getTime() - entity.lastAccess.getTime());
129159
await deleteEntity(value);
130160
} else {
161+
// If the TTL has not expired, get a new renderer and return it
131162
console.log('TTL not expired or undefined', new Date().getTime() - entity.lastAccess.getTime());
132163
const renderer = new (renderers.find(renderer => renderer.key === entity.rendererKey).constructor)(value, entitySettings);
133-
// if (renderer.hasCorrectFormat()) {
134164
renderer.settings = entitySettings;
135165
await renderer.init(entity.lastData);
136166
return renderer;
137-
// }
138167
}
139168
}
140169
} catch (error) {
141170
console.error('Could not get entity from db', error);
142171
}
143172

173+
// If no entity was found, create a new one, initialize it and it to the database
144174
console.debug('No valid entity found for value in db', value);
145175
const renderer = await Parser.getBestFit(value, settings);
146-
// renderer.settings = settings.find(value => value.type === renderer.getSettingsKey())?.values
147-
// await renderer.init()
176+
renderer.settings = settings.find(value => value.type === renderer.getSettingsKey())?.values;
177+
await renderer.init();
148178
await addEntity(renderer);
149179
console.debug('added entity to db', value, renderer);
150180
return renderer;
151181
};
152182

183+
/**
184+
* Deletes an entity from the database.
185+
* @param value The value of the entity to delete.
186+
*/
153187
export async function deleteEntity(value: string) {
154188
const db = await dbPromise;
189+
190+
// Delete the entity
155191
await db.delete('entities', value);
192+
193+
// Delete all relations for the entity
156194
const tx = db.transaction('relations', 'readwrite');
157195
const index = tx.store.index('by-start');
158196
let cursor = await index.openCursor();
@@ -166,13 +204,13 @@ export async function deleteEntity(value: string) {
166204
await tx.done;
167205
}
168206

169-
export async function deleteAllByContext(context: string) {
207+
/**
208+
* Clears all entities from the database.
209+
* @returns {Promise<void>} A promise that resolves when all entities have been deleted.
210+
*/
211+
export async function clearEntities() {
170212
const db = await dbPromise;
171-
const tx = db.transaction('entities', 'readwrite');
172-
const entities = await tx.store.index('by-context').getAll(context);
173-
for (const entity of entities) {
174-
await deleteEntity(entity.value);
175-
}
176-
console.log('deleted all entities for context', context);
177-
await tx.done;
213+
await db.clear('entities');
214+
await db.clear('relations');
215+
console.log('cleared entities');
178216
}

0 commit comments

Comments
 (0)