Revision 92fc67cf
Added by koszko over 1 year ago
common/indexeddb.js | ||
---|---|---|
59 | 59 |
const version_nr = ver => ver.slice(0, 3).reduce(nr_reductor, [2, 0])[1]; |
60 | 60 |
|
61 | 61 |
const stores = [ |
62 |
["files", {keyPath: "sha256"}],
|
|
62 |
["file", {keyPath: "sha256"}],
|
|
63 | 63 |
["file_uses", {keyPath: "sha256"}], |
64 | 64 |
["resource", {keyPath: "identifier"}], |
65 | 65 |
["mapping", {keyPath: "identifier"}], |
66 |
["settings", {keyPath: "name"}],
|
|
66 |
["setting", {keyPath: "name"}],
|
|
67 | 67 |
["blocking", {keyPath: "pattern"}], |
68 |
["repos", {keyPath: "url"}]
|
|
68 |
["repo", {keyPath: "url"}]
|
|
69 | 69 |
]; |
70 | 70 |
|
71 | 71 |
let db = null; |
... | ... | |
111 | 111 |
store = opened_db.createObjectStore(store_name, key_mode); |
112 | 112 |
|
113 | 113 |
const ctx = make_context(store.transaction, initial_data.file); |
114 |
await _save_items(initial_data.resources, initial_data.mappings, ctx);
|
|
114 |
await _save_items(initial_data.resource, initial_data.mapping, [], ctx);
|
|
115 | 115 |
|
116 | 116 |
return opened_db; |
117 | 117 |
} |
... | ... | |
186 | 186 |
async function start_items_transaction(item_store_names, files) |
187 | 187 |
{ |
188 | 188 |
const db = await get_db(); |
189 |
const scope = [...item_store_names, "files", "file_uses"];
|
|
189 |
const scope = [...item_store_names, "file", "file_uses"]; |
|
190 | 190 |
return make_context(db.transaction(scope, "readwrite"), files); |
191 | 191 |
} |
192 | 192 |
#EXPORT start_items_transaction |
... | ... | |
226 | 226 |
if (uses.uses < 1) { |
227 | 227 |
if (!is_new) { |
228 | 228 |
idb_del(context.transaction, "file_uses", sha256); |
229 |
idb_del(context.transaction, "files", sha256);
|
|
229 |
idb_del(context.transaction, "file", sha256);
|
|
230 | 230 |
} |
231 | 231 |
|
232 | 232 |
continue; |
... | ... | |
246 | 246 |
throw "file not present: " + sha256; |
247 | 247 |
} |
248 | 248 |
|
249 |
idb_put(context.transaction, "files", {sha256, contents: file});
|
|
249 |
idb_put(context.transaction, "file", {sha256, contents: file}); |
|
250 | 250 |
} |
251 | 251 |
|
252 | 252 |
return context.result; |
... | ... | |
257 | 257 |
* How a sample data argument to the function below might look like: |
258 | 258 |
* |
259 | 259 |
* data = { |
260 |
* resources: {
|
|
260 |
* resource: { |
|
261 | 261 |
* "resource1": { |
262 | 262 |
* "1": { |
263 | 263 |
* // some stuff |
... | ... | |
272 | 272 |
* } |
273 | 273 |
* }, |
274 | 274 |
* }, |
275 |
* mappings: {
|
|
275 |
* mapping: { |
|
276 | 276 |
* "mapping1": { |
277 | 277 |
* "2": { |
278 | 278 |
* // some stuff |
... | ... | |
295 | 295 |
async function save_items(data) |
296 | 296 |
{ |
297 | 297 |
const item_store_names = ["resource", "mapping"]; |
298 |
if ("repo" in data) |
|
299 |
item_store_names.push("repo"); |
|
300 |
|
|
298 | 301 |
const context = await start_items_transaction(item_store_names, data.file); |
299 | 302 |
|
300 |
return _save_items(data.resources, data.mappings, context);
|
|
303 |
return _save_items(data.resource, data.mapping, data.repo || [], context);
|
|
301 | 304 |
} |
302 | 305 |
#EXPORT save_items |
303 | 306 |
|
304 |
async function _save_items(resources, mappings, context) |
|
307 |
async function _save_items(resources, mappings, repos, context)
|
|
305 | 308 |
{ |
306 | 309 |
resources = Object.values(resources || {}).map(entities.get_newest); |
307 | 310 |
mappings = Object.values(mappings || {}).map(entities.get_newest); |
... | ... | |
309 | 312 |
for (const item of resources.concat(mappings)) |
310 | 313 |
await save_item(item, context); |
311 | 314 |
|
315 |
for (const repo_url of repos) |
|
316 |
await idb_put(context.transaction, "repo", {url: repo_url}); |
|
317 |
|
|
312 | 318 |
await finalize_transaction(context); |
313 | 319 |
} |
314 | 320 |
|
... | ... | |
378 | 384 |
|
379 | 385 |
/* |
380 | 386 |
* A simplified kind of transaction for modifying stores without special |
381 |
* inter-store integrity constraints ("settings", "blocking", "repos").
|
|
387 |
* inter-store integrity constraints ("setting", "blocking", "repo").
|
|
382 | 388 |
*/ |
383 | 389 |
async function start_simple_transaction(store_name) |
384 | 390 |
{ |
... | ... | |
386 | 392 |
return make_context(db.transaction(store_name, "readwrite"), {}); |
387 | 393 |
} |
388 | 394 |
|
389 |
/* Functions to access the "settings" store. */
|
|
395 |
/* Functions to access the "setting" store. */ |
|
390 | 396 |
async function set_setting(name, value) |
391 | 397 |
{ |
392 |
const context = await start_simple_transaction("settings");
|
|
393 |
broadcast.prepare(context.sender, "idb_changes_settings", name);
|
|
394 |
await idb_put(context.transaction, "settings", {name, value});
|
|
398 |
const context = await start_simple_transaction("setting"); |
|
399 |
broadcast.prepare(context.sender, "idb_changes_setting", name); |
|
400 |
await idb_put(context.transaction, "setting", {name, value}); |
|
395 | 401 |
return finalize_transaction(context); |
396 | 402 |
} |
397 | 403 |
#EXPORT set_setting |
398 | 404 |
|
399 | 405 |
async function get_setting(name) |
400 | 406 |
{ |
401 |
const transaction = (await get_db()).transaction("settings");
|
|
402 |
return ((await idb_get(transaction, "settings", name)) || {}).value;
|
|
407 |
const transaction = (await get_db()).transaction("setting"); |
|
408 |
return ((await idb_get(transaction, "setting", name)) || {}).value; |
|
403 | 409 |
} |
404 | 410 |
#EXPORT get_setting |
405 | 411 |
|
... | ... | |
429 | 435 |
} |
430 | 436 |
#EXPORT get_allowing |
431 | 437 |
|
432 |
/* Functions to access the "repos" store. */
|
|
438 |
/* Functions to access the "repo" store. */ |
|
433 | 439 |
async function set_repo(url, remove=false) |
434 | 440 |
{ |
435 |
const context = await start_simple_transaction("repos");
|
|
436 |
broadcast.prepare(context.sender, "idb_changes_repos", url);
|
|
441 |
const context = await start_simple_transaction("repo"); |
|
442 |
broadcast.prepare(context.sender, "idb_changes_repo", url); |
|
437 | 443 |
if (remove) |
438 |
await idb_del(context.transaction, "repos", url);
|
|
444 |
await idb_del(context.transaction, "repo", url); |
|
439 | 445 |
else |
440 |
await idb_put(context.transaction, "repos", {url});
|
|
446 |
await idb_put(context.transaction, "repo", {url}); |
|
441 | 447 |
return finalize_transaction(context); |
442 | 448 |
} |
443 | 449 |
#EXPORT set_repo |
... | ... | |
445 | 451 |
const del_repo = url => set_repo(url, true); |
446 | 452 |
#EXPORT del_repo |
447 | 453 |
|
448 |
const get_repos = () => get_all("repos").then(list => list.map(obj => obj.url));
|
|
454 |
const get_repos = () => get_all("repo").then(list => list.map(obj => obj.url)); |
|
449 | 455 |
#EXPORT get_repos |
450 | 456 |
|
451 | 457 |
/* Callback used when listening to broadcasts while tracking db changes. */ |
... | ... | |
460 | 466 |
/* |
461 | 467 |
* Monitor changes to `store_name` IndexedDB object store. |
462 | 468 |
* |
463 |
* `store_name` should be either "resource", "mapping", "settings", "blocking"
|
|
464 |
* or "repos".
|
|
469 |
* `store_name` should be either "resource", "mapping", "setting", "blocking" |
|
470 |
* or "repo". |
|
465 | 471 |
* |
466 | 472 |
* `onchange` should be a callback that will be called when an item is added, |
467 | 473 |
* modified or removed from the store. The callback will be passed an object |
... | ... | |
492 | 498 |
} |
493 | 499 |
|
494 | 500 |
const track = {}; |
495 |
const trackable = ["resource", "mapping", "settings", "blocking", "repos"];
|
|
501 |
const trackable = ["resource", "mapping", "setting", "blocking", "repo"];
|
|
496 | 502 |
for (const store_name of trackable) |
497 | 503 |
track[store_name] = onchange => start_tracking(store_name, onchange); |
498 | 504 |
#EXPORT track |
Also available in: Unified diff
change store names and data keys to singular