silosdk 0.0.9 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -5
- package/dist/internal/indexed-db.cjs +44 -0
- package/dist/internal/indexed-db.mjs +41 -0
- package/dist/media/shared.cjs +99 -0
- package/dist/media/shared.d.cts +26 -0
- package/dist/media/shared.d.mts +26 -0
- package/dist/media/shared.mjs +93 -0
- package/dist/media/storage.native.cjs +102 -0
- package/dist/media/storage.native.mjs +101 -0
- package/dist/media/storage.web.cjs +79 -0
- package/dist/media/storage.web.mjs +78 -0
- package/dist/media.cjs +5 -163
- package/dist/media.d.cts +7 -27
- package/dist/media.d.mts +7 -27
- package/dist/media.mjs +4 -161
- package/dist/media.web.cjs +9 -0
- package/dist/media.web.d.cts +12 -0
- package/dist/media.web.d.mts +12 -0
- package/dist/media.web.mjs +8 -0
- package/dist/settings/shared.cjs +63 -0
- package/dist/settings/shared.d.cts +19 -0
- package/dist/settings/shared.d.mts +19 -0
- package/dist/settings/shared.mjs +59 -0
- package/dist/settings/storage.native.cjs +24 -0
- package/dist/settings/storage.native.mjs +22 -0
- package/dist/settings/storage.web.cjs +34 -0
- package/dist/settings/storage.web.mjs +34 -0
- package/dist/settings.cjs +4 -66
- package/dist/settings.d.cts +4 -18
- package/dist/settings.d.mts +4 -18
- package/dist/settings.mjs +4 -64
- package/dist/settings.web.cjs +8 -0
- package/dist/settings.web.d.cts +6 -0
- package/dist/settings.web.d.mts +6 -0
- package/dist/settings.web.mjs +8 -0
- package/dist/store/shared.cjs +338 -0
- package/dist/store/shared.d.cts +58 -0
- package/dist/store/shared.d.mts +58 -0
- package/dist/store/shared.mjs +333 -0
- package/dist/store/storage.native.cjs +133 -0
- package/dist/store/storage.native.mjs +132 -0
- package/dist/store/storage.web.cjs +142 -0
- package/dist/store/storage.web.mjs +142 -0
- package/dist/store.cjs +4 -421
- package/dist/store.d.cts +4 -58
- package/dist/store.d.mts +4 -58
- package/dist/store.mjs +4 -421
- package/dist/store.web.cjs +15 -0
- package/dist/store.web.d.cts +7 -0
- package/dist/store.web.d.mts +7 -0
- package/dist/store.web.mjs +12 -0
- package/package.json +31 -1
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
const require_shared = require('./shared.cjs');
|
|
2
|
+
const require_indexed_db = require('../internal/indexed-db.cjs');
|
|
3
|
+
|
|
4
|
+
//#region src/store/storage.web.ts
|
|
5
|
+
const indexedLinkIds = /* @__PURE__ */ new Set();
|
|
6
|
+
const storeStorage = {
|
|
7
|
+
loadRows,
|
|
8
|
+
insertRows,
|
|
9
|
+
updateRows,
|
|
10
|
+
deleteRows,
|
|
11
|
+
loadLinkRows,
|
|
12
|
+
insertLinkRow,
|
|
13
|
+
deleteLinkRow,
|
|
14
|
+
hasLinkRow
|
|
15
|
+
};
|
|
16
|
+
const openSiloIndexedDb = require_indexed_db.createIndexedDbOpener("silo", 1, (db) => {
|
|
17
|
+
if (!db.objectStoreNames.contains("sources")) db.createObjectStore("sources", { keyPath: "key" }).createIndex("source", "source", { unique: false });
|
|
18
|
+
if (!db.objectStoreNames.contains("links")) db.createObjectStore("links", { keyPath: "id" }).createIndex("collections", "collections", { unique: false });
|
|
19
|
+
});
|
|
20
|
+
function sourceKey(sourceName, id) {
|
|
21
|
+
return `${encodeURIComponent(sourceName)}:${encodeURIComponent(id)}`;
|
|
22
|
+
}
|
|
23
|
+
async function loadRows(sourceName) {
|
|
24
|
+
const transaction = (await openSiloIndexedDb()).transaction("sources", "readonly");
|
|
25
|
+
const index = transaction.objectStore("sources").index("source");
|
|
26
|
+
const done = require_indexed_db.transactionDone(transaction);
|
|
27
|
+
const rows = await require_indexed_db.requestResult(index.getAll(sourceName));
|
|
28
|
+
await done;
|
|
29
|
+
return rows.map((row) => require_shared.createDoc(row.id, JSON.parse(row.data)));
|
|
30
|
+
}
|
|
31
|
+
async function insertRows(sourceName, rows) {
|
|
32
|
+
const db = await openSiloIndexedDb();
|
|
33
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
34
|
+
const transaction = db.transaction("sources", "readwrite");
|
|
35
|
+
const store = transaction.objectStore("sources");
|
|
36
|
+
for (const row of rows) {
|
|
37
|
+
const data = require_shared.extractDocumentData(row);
|
|
38
|
+
store.add({
|
|
39
|
+
key: sourceKey(sourceName, row.id),
|
|
40
|
+
source: sourceName,
|
|
41
|
+
id: row.id,
|
|
42
|
+
data: JSON.stringify(data),
|
|
43
|
+
createdAt: timestamp,
|
|
44
|
+
updatedAt: timestamp,
|
|
45
|
+
version: 1
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
await require_indexed_db.transactionDone(transaction);
|
|
49
|
+
}
|
|
50
|
+
async function updateRows(sourceName, mutations) {
|
|
51
|
+
const db = await openSiloIndexedDb();
|
|
52
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
53
|
+
const existingRows = await loadIndexedSourceRows(db, mutations.map((mutation) => sourceKey(sourceName, String(mutation.key))));
|
|
54
|
+
const transaction = db.transaction("sources", "readwrite");
|
|
55
|
+
const store = transaction.objectStore("sources");
|
|
56
|
+
for (const [index, mutation] of mutations.entries()) {
|
|
57
|
+
const id = String(mutation.key);
|
|
58
|
+
const key = sourceKey(sourceName, id);
|
|
59
|
+
const data = require_shared.extractDocumentData(mutation.modified);
|
|
60
|
+
const existing = existingRows[index];
|
|
61
|
+
mutation.modified = require_shared.createDoc(id, data);
|
|
62
|
+
store.put({
|
|
63
|
+
key,
|
|
64
|
+
source: sourceName,
|
|
65
|
+
id,
|
|
66
|
+
data: JSON.stringify(data),
|
|
67
|
+
createdAt: existing?.createdAt ?? timestamp,
|
|
68
|
+
updatedAt: timestamp,
|
|
69
|
+
version: existing ? existing.version + 1 : 1
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
await require_indexed_db.transactionDone(transaction);
|
|
73
|
+
}
|
|
74
|
+
async function loadIndexedSourceRows(db, keys) {
|
|
75
|
+
const transaction = db.transaction("sources", "readonly");
|
|
76
|
+
const store = transaction.objectStore("sources");
|
|
77
|
+
const done = require_indexed_db.transactionDone(transaction);
|
|
78
|
+
const rows = await Promise.all(keys.map((key) => require_indexed_db.requestResult(store.get(key))));
|
|
79
|
+
await done;
|
|
80
|
+
return rows;
|
|
81
|
+
}
|
|
82
|
+
async function deleteRows(sourceName, mutations) {
|
|
83
|
+
const db = await openSiloIndexedDb();
|
|
84
|
+
const linkRows = await loadLinkRows();
|
|
85
|
+
const transaction = db.transaction(["sources", "links"], "readwrite");
|
|
86
|
+
const sources = transaction.objectStore("sources");
|
|
87
|
+
const links = transaction.objectStore("links");
|
|
88
|
+
for (const mutation of mutations) {
|
|
89
|
+
const id = String(mutation.key);
|
|
90
|
+
if (mutation.original) require_shared.deletedDocs.add(mutation.original);
|
|
91
|
+
require_shared.deletedDocs.add(mutation.modified);
|
|
92
|
+
sources.delete(sourceKey(sourceName, id));
|
|
93
|
+
for (const row of linkRows) {
|
|
94
|
+
const [firstCollection, secondCollection] = require_shared.decodePair(row.collections);
|
|
95
|
+
const [firstId, secondId] = require_shared.decodePair(row.ids);
|
|
96
|
+
if (firstCollection === sourceName && firstId === id || secondCollection === sourceName && secondId === id) {
|
|
97
|
+
links.delete(row.id);
|
|
98
|
+
indexedLinkIds.delete(row.id);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
await require_indexed_db.transactionDone(transaction);
|
|
103
|
+
}
|
|
104
|
+
async function loadLinkRows() {
|
|
105
|
+
const transaction = (await openSiloIndexedDb()).transaction("links", "readonly");
|
|
106
|
+
const done = require_indexed_db.transactionDone(transaction);
|
|
107
|
+
const rows = await require_indexed_db.requestResult(transaction.objectStore("links").getAll());
|
|
108
|
+
await done;
|
|
109
|
+
indexedLinkIds.clear();
|
|
110
|
+
for (const row of rows) indexedLinkIds.add(row.id);
|
|
111
|
+
return rows;
|
|
112
|
+
}
|
|
113
|
+
async function insertLinkRow(row) {
|
|
114
|
+
const db = await openSiloIndexedDb();
|
|
115
|
+
const readTransaction = db.transaction("links", "readonly");
|
|
116
|
+
const readDone = require_indexed_db.transactionDone(readTransaction);
|
|
117
|
+
const existing = await require_indexed_db.requestResult(readTransaction.objectStore("links").get(row.id));
|
|
118
|
+
await readDone;
|
|
119
|
+
const transaction = db.transaction("links", "readwrite");
|
|
120
|
+
transaction.objectStore("links").put(row);
|
|
121
|
+
await require_indexed_db.transactionDone(transaction);
|
|
122
|
+
indexedLinkIds.add(row.id);
|
|
123
|
+
return !!existing;
|
|
124
|
+
}
|
|
125
|
+
async function deleteLinkRow(id) {
|
|
126
|
+
const db = await openSiloIndexedDb();
|
|
127
|
+
const readTransaction = db.transaction("links", "readonly");
|
|
128
|
+
const readDone = require_indexed_db.transactionDone(readTransaction);
|
|
129
|
+
const existing = await require_indexed_db.requestResult(readTransaction.objectStore("links").get(id));
|
|
130
|
+
await readDone;
|
|
131
|
+
const transaction = db.transaction("links", "readwrite");
|
|
132
|
+
transaction.objectStore("links").delete(id);
|
|
133
|
+
await require_indexed_db.transactionDone(transaction);
|
|
134
|
+
indexedLinkIds.delete(id);
|
|
135
|
+
return !!existing;
|
|
136
|
+
}
|
|
137
|
+
function hasLinkRow(id) {
|
|
138
|
+
return indexedLinkIds.has(id);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
//#endregion
|
|
142
|
+
exports.storeStorage = storeStorage;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { createDoc, decodePair, deletedDocs, extractDocumentData } from "./shared.mjs";
|
|
2
|
+
import { createIndexedDbOpener, requestResult, transactionDone } from "../internal/indexed-db.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/store/storage.web.ts
|
|
5
|
+
const indexedLinkIds = /* @__PURE__ */ new Set();
|
|
6
|
+
const storeStorage = {
|
|
7
|
+
loadRows,
|
|
8
|
+
insertRows,
|
|
9
|
+
updateRows,
|
|
10
|
+
deleteRows,
|
|
11
|
+
loadLinkRows,
|
|
12
|
+
insertLinkRow,
|
|
13
|
+
deleteLinkRow,
|
|
14
|
+
hasLinkRow
|
|
15
|
+
};
|
|
16
|
+
const openSiloIndexedDb = createIndexedDbOpener("silo", 1, (db) => {
|
|
17
|
+
if (!db.objectStoreNames.contains("sources")) db.createObjectStore("sources", { keyPath: "key" }).createIndex("source", "source", { unique: false });
|
|
18
|
+
if (!db.objectStoreNames.contains("links")) db.createObjectStore("links", { keyPath: "id" }).createIndex("collections", "collections", { unique: false });
|
|
19
|
+
});
|
|
20
|
+
function sourceKey(sourceName, id) {
|
|
21
|
+
return `${encodeURIComponent(sourceName)}:${encodeURIComponent(id)}`;
|
|
22
|
+
}
|
|
23
|
+
async function loadRows(sourceName) {
|
|
24
|
+
const transaction = (await openSiloIndexedDb()).transaction("sources", "readonly");
|
|
25
|
+
const index = transaction.objectStore("sources").index("source");
|
|
26
|
+
const done = transactionDone(transaction);
|
|
27
|
+
const rows = await requestResult(index.getAll(sourceName));
|
|
28
|
+
await done;
|
|
29
|
+
return rows.map((row) => createDoc(row.id, JSON.parse(row.data)));
|
|
30
|
+
}
|
|
31
|
+
async function insertRows(sourceName, rows) {
|
|
32
|
+
const db = await openSiloIndexedDb();
|
|
33
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
34
|
+
const transaction = db.transaction("sources", "readwrite");
|
|
35
|
+
const store = transaction.objectStore("sources");
|
|
36
|
+
for (const row of rows) {
|
|
37
|
+
const data = extractDocumentData(row);
|
|
38
|
+
store.add({
|
|
39
|
+
key: sourceKey(sourceName, row.id),
|
|
40
|
+
source: sourceName,
|
|
41
|
+
id: row.id,
|
|
42
|
+
data: JSON.stringify(data),
|
|
43
|
+
createdAt: timestamp,
|
|
44
|
+
updatedAt: timestamp,
|
|
45
|
+
version: 1
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
await transactionDone(transaction);
|
|
49
|
+
}
|
|
50
|
+
async function updateRows(sourceName, mutations) {
|
|
51
|
+
const db = await openSiloIndexedDb();
|
|
52
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
53
|
+
const existingRows = await loadIndexedSourceRows(db, mutations.map((mutation) => sourceKey(sourceName, String(mutation.key))));
|
|
54
|
+
const transaction = db.transaction("sources", "readwrite");
|
|
55
|
+
const store = transaction.objectStore("sources");
|
|
56
|
+
for (const [index, mutation] of mutations.entries()) {
|
|
57
|
+
const id = String(mutation.key);
|
|
58
|
+
const key = sourceKey(sourceName, id);
|
|
59
|
+
const data = extractDocumentData(mutation.modified);
|
|
60
|
+
const existing = existingRows[index];
|
|
61
|
+
mutation.modified = createDoc(id, data);
|
|
62
|
+
store.put({
|
|
63
|
+
key,
|
|
64
|
+
source: sourceName,
|
|
65
|
+
id,
|
|
66
|
+
data: JSON.stringify(data),
|
|
67
|
+
createdAt: existing?.createdAt ?? timestamp,
|
|
68
|
+
updatedAt: timestamp,
|
|
69
|
+
version: existing ? existing.version + 1 : 1
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
await transactionDone(transaction);
|
|
73
|
+
}
|
|
74
|
+
async function loadIndexedSourceRows(db, keys) {
|
|
75
|
+
const transaction = db.transaction("sources", "readonly");
|
|
76
|
+
const store = transaction.objectStore("sources");
|
|
77
|
+
const done = transactionDone(transaction);
|
|
78
|
+
const rows = await Promise.all(keys.map((key) => requestResult(store.get(key))));
|
|
79
|
+
await done;
|
|
80
|
+
return rows;
|
|
81
|
+
}
|
|
82
|
+
async function deleteRows(sourceName, mutations) {
|
|
83
|
+
const db = await openSiloIndexedDb();
|
|
84
|
+
const linkRows = await loadLinkRows();
|
|
85
|
+
const transaction = db.transaction(["sources", "links"], "readwrite");
|
|
86
|
+
const sources = transaction.objectStore("sources");
|
|
87
|
+
const links = transaction.objectStore("links");
|
|
88
|
+
for (const mutation of mutations) {
|
|
89
|
+
const id = String(mutation.key);
|
|
90
|
+
if (mutation.original) deletedDocs.add(mutation.original);
|
|
91
|
+
deletedDocs.add(mutation.modified);
|
|
92
|
+
sources.delete(sourceKey(sourceName, id));
|
|
93
|
+
for (const row of linkRows) {
|
|
94
|
+
const [firstCollection, secondCollection] = decodePair(row.collections);
|
|
95
|
+
const [firstId, secondId] = decodePair(row.ids);
|
|
96
|
+
if (firstCollection === sourceName && firstId === id || secondCollection === sourceName && secondId === id) {
|
|
97
|
+
links.delete(row.id);
|
|
98
|
+
indexedLinkIds.delete(row.id);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
await transactionDone(transaction);
|
|
103
|
+
}
|
|
104
|
+
async function loadLinkRows() {
|
|
105
|
+
const transaction = (await openSiloIndexedDb()).transaction("links", "readonly");
|
|
106
|
+
const done = transactionDone(transaction);
|
|
107
|
+
const rows = await requestResult(transaction.objectStore("links").getAll());
|
|
108
|
+
await done;
|
|
109
|
+
indexedLinkIds.clear();
|
|
110
|
+
for (const row of rows) indexedLinkIds.add(row.id);
|
|
111
|
+
return rows;
|
|
112
|
+
}
|
|
113
|
+
async function insertLinkRow(row) {
|
|
114
|
+
const db = await openSiloIndexedDb();
|
|
115
|
+
const readTransaction = db.transaction("links", "readonly");
|
|
116
|
+
const readDone = transactionDone(readTransaction);
|
|
117
|
+
const existing = await requestResult(readTransaction.objectStore("links").get(row.id));
|
|
118
|
+
await readDone;
|
|
119
|
+
const transaction = db.transaction("links", "readwrite");
|
|
120
|
+
transaction.objectStore("links").put(row);
|
|
121
|
+
await transactionDone(transaction);
|
|
122
|
+
indexedLinkIds.add(row.id);
|
|
123
|
+
return !!existing;
|
|
124
|
+
}
|
|
125
|
+
async function deleteLinkRow(id) {
|
|
126
|
+
const db = await openSiloIndexedDb();
|
|
127
|
+
const readTransaction = db.transaction("links", "readonly");
|
|
128
|
+
const readDone = transactionDone(readTransaction);
|
|
129
|
+
const existing = await requestResult(readTransaction.objectStore("links").get(id));
|
|
130
|
+
await readDone;
|
|
131
|
+
const transaction = db.transaction("links", "readwrite");
|
|
132
|
+
transaction.objectStore("links").delete(id);
|
|
133
|
+
await transactionDone(transaction);
|
|
134
|
+
indexedLinkIds.delete(id);
|
|
135
|
+
return !!existing;
|
|
136
|
+
}
|
|
137
|
+
function hasLinkRow(id) {
|
|
138
|
+
return indexedLinkIds.has(id);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
//#endregion
|
|
142
|
+
export { storeStorage };
|