silosdk 0.0.8 → 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.
Files changed (52) hide show
  1. package/README.md +29 -5
  2. package/dist/internal/indexed-db.cjs +44 -0
  3. package/dist/internal/indexed-db.mjs +41 -0
  4. package/dist/media/shared.cjs +99 -0
  5. package/dist/media/shared.d.cts +26 -0
  6. package/dist/media/shared.d.mts +26 -0
  7. package/dist/media/shared.mjs +93 -0
  8. package/dist/media/storage.native.cjs +102 -0
  9. package/dist/media/storage.native.mjs +101 -0
  10. package/dist/media/storage.web.cjs +79 -0
  11. package/dist/media/storage.web.mjs +78 -0
  12. package/dist/media.cjs +5 -131
  13. package/dist/media.d.cts +8 -25
  14. package/dist/media.d.mts +8 -25
  15. package/dist/media.mjs +4 -129
  16. package/dist/media.web.cjs +9 -0
  17. package/dist/media.web.d.cts +12 -0
  18. package/dist/media.web.d.mts +12 -0
  19. package/dist/media.web.mjs +8 -0
  20. package/dist/settings/shared.cjs +63 -0
  21. package/dist/settings/shared.d.cts +19 -0
  22. package/dist/settings/shared.d.mts +19 -0
  23. package/dist/settings/shared.mjs +59 -0
  24. package/dist/settings/storage.native.cjs +24 -0
  25. package/dist/settings/storage.native.mjs +22 -0
  26. package/dist/settings/storage.web.cjs +34 -0
  27. package/dist/settings/storage.web.mjs +34 -0
  28. package/dist/settings.cjs +4 -66
  29. package/dist/settings.d.cts +4 -18
  30. package/dist/settings.d.mts +4 -18
  31. package/dist/settings.mjs +4 -64
  32. package/dist/settings.web.cjs +8 -0
  33. package/dist/settings.web.d.cts +6 -0
  34. package/dist/settings.web.d.mts +6 -0
  35. package/dist/settings.web.mjs +8 -0
  36. package/dist/store/shared.cjs +338 -0
  37. package/dist/store/shared.d.cts +58 -0
  38. package/dist/store/shared.d.mts +58 -0
  39. package/dist/store/shared.mjs +333 -0
  40. package/dist/store/storage.native.cjs +133 -0
  41. package/dist/store/storage.native.mjs +132 -0
  42. package/dist/store/storage.web.cjs +142 -0
  43. package/dist/store/storage.web.mjs +142 -0
  44. package/dist/store.cjs +4 -421
  45. package/dist/store.d.cts +4 -58
  46. package/dist/store.d.mts +4 -58
  47. package/dist/store.mjs +4 -421
  48. package/dist/store.web.cjs +15 -0
  49. package/dist/store.web.d.cts +7 -0
  50. package/dist/store.web.d.mts +7 -0
  51. package/dist/store.web.mjs +12 -0
  52. package/package.json +31 -1
@@ -0,0 +1,132 @@
1
+ import { createDoc, decodePair, deletedDocs, extractDocumentData } from "./shared.mjs";
2
+ import { openDatabaseSync } from "expo-sqlite";
3
+
4
+ //#region src/store/storage.native.ts
5
+ let database;
6
+ const storeStorage = {
7
+ loadRows,
8
+ insertRows,
9
+ updateRows,
10
+ deleteRows,
11
+ loadLinkRows,
12
+ insertLinkRow,
13
+ deleteLinkRow,
14
+ hasLinkRow
15
+ };
16
+ function getDatabase() {
17
+ if (!database) {
18
+ database = openDatabaseSync("silo.db");
19
+ database.execSync(`
20
+ PRAGMA journal_mode = WAL;
21
+
22
+ CREATE TABLE IF NOT EXISTS sources (
23
+ source TEXT NOT NULL,
24
+ id TEXT NOT NULL,
25
+ data TEXT NOT NULL,
26
+ createdAt TEXT NOT NULL,
27
+ updatedAt TEXT NOT NULL,
28
+ version INTEGER NOT NULL DEFAULT 1,
29
+ PRIMARY KEY (source, id)
30
+ );
31
+
32
+ CREATE TABLE IF NOT EXISTS links (
33
+ id TEXT PRIMARY KEY,
34
+ collections TEXT NOT NULL,
35
+ ids TEXT NOT NULL,
36
+ createdAt TEXT NOT NULL
37
+ );
38
+
39
+ CREATE UNIQUE INDEX IF NOT EXISTS links_collections_ids_idx
40
+ ON links(collections, ids);
41
+
42
+ CREATE INDEX IF NOT EXISTS links_collections_idx
43
+ ON links(collections);
44
+ `);
45
+ }
46
+ return database;
47
+ }
48
+ function loadRows(sourceName) {
49
+ return getDatabase().getAllSync(`SELECT id, data FROM sources WHERE source = ?`, [sourceName]).map((row) => createDoc(row.id, JSON.parse(row.data)));
50
+ }
51
+ function insertRows(sourceName, rows) {
52
+ const db = getDatabase();
53
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
54
+ db.withTransactionSync(() => {
55
+ for (const row of rows) {
56
+ const data = extractDocumentData(row);
57
+ db.runSync(`INSERT INTO sources (source, id, data, createdAt, updatedAt, version)
58
+ VALUES (?, ?, ?, ?, ?, 1)`, [
59
+ sourceName,
60
+ row.id,
61
+ JSON.stringify(data),
62
+ timestamp,
63
+ timestamp
64
+ ]);
65
+ }
66
+ });
67
+ return Promise.resolve();
68
+ }
69
+ function updateRows(sourceName, mutations) {
70
+ const db = getDatabase();
71
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
72
+ db.withTransactionSync(() => {
73
+ for (const mutation of mutations) {
74
+ const data = extractDocumentData(mutation.modified);
75
+ mutation.modified = createDoc(String(mutation.key), data);
76
+ db.runSync(`UPDATE sources
77
+ SET data = ?, updatedAt = ?, version = version + 1
78
+ WHERE source = ? AND id = ?`, [
79
+ JSON.stringify(data),
80
+ timestamp,
81
+ sourceName,
82
+ String(mutation.key)
83
+ ]);
84
+ }
85
+ });
86
+ return Promise.resolve();
87
+ }
88
+ function deleteRows(sourceName, mutations) {
89
+ const db = getDatabase();
90
+ db.withTransactionSync(() => {
91
+ for (const mutation of mutations) {
92
+ if (mutation.original) deletedDocs.add(mutation.original);
93
+ deletedDocs.add(mutation.modified);
94
+ db.runSync(`DELETE FROM sources WHERE source = ? AND id = ?`, [sourceName, String(mutation.key)]);
95
+ deleteLinksForSourceRow(sourceName, String(mutation.key));
96
+ }
97
+ });
98
+ return Promise.resolve();
99
+ }
100
+ function loadLinkRows() {
101
+ return getDatabase().getAllSync(`SELECT id, collections, ids, createdAt FROM links`);
102
+ }
103
+ function insertLinkRow(row) {
104
+ const exists = hasLinkRow(row.id);
105
+ getDatabase().runSync(`INSERT OR IGNORE INTO links (id, collections, ids, createdAt)
106
+ VALUES (?, ?, ?, ?)`, [
107
+ row.id,
108
+ row.collections,
109
+ row.ids,
110
+ row.createdAt
111
+ ]);
112
+ return exists;
113
+ }
114
+ function deleteLinkRow(id) {
115
+ const exists = hasLinkRow(id);
116
+ getDatabase().runSync(`DELETE FROM links WHERE id = ?`, [id]);
117
+ return exists;
118
+ }
119
+ function hasLinkRow(id) {
120
+ return !!getDatabase().getFirstSync(`SELECT id FROM links WHERE id = ?`, [id]);
121
+ }
122
+ function deleteLinksForSourceRow(sourceName, id) {
123
+ const rows = loadLinkRows();
124
+ for (const row of rows) {
125
+ const [firstCollection, secondCollection] = decodePair(row.collections);
126
+ const [firstId, secondId] = decodePair(row.ids);
127
+ if (firstCollection === sourceName && firstId === id || secondCollection === sourceName && secondId === id) deleteLinkRow(row.id);
128
+ }
129
+ }
130
+
131
+ //#endregion
132
+ export { storeStorage };
@@ -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 };