|
1 | | -import Dexie from 'dexie'; |
| 1 | +import Dexie from "dexie"; |
2 | 2 |
|
3 | 3 | declare global { |
4 | | - interface Window { |
5 | | - store: { |
6 | | - sqlideScripts: Dexie.Table; |
7 | | - sqlideDatabases: Dexie.Table; |
8 | | - }; |
9 | | - } |
| 4 | + interface Window { |
| 5 | + hyperbook: { |
| 6 | + store: { |
| 7 | + sqlideScripts: Dexie.Table; |
| 8 | + sqlideDatabases: Dexie.Table; |
| 9 | + }; |
| 10 | + }; |
| 11 | + } |
10 | 12 | } |
11 | 13 |
|
12 | 14 | export class EmbeddedIndexedDB { |
13 | | - private scriptsTable: Dexie.Table; |
14 | | - private databasesTable: Dexie.Table; |
| 15 | + private scriptsTable: Dexie.Table; |
| 16 | + private databasesTable: Dexie.Table; |
15 | 17 |
|
16 | | - constructor(private databaseIdentifier: string) { |
17 | | - } |
| 18 | + constructor(private databaseIdentifier: string) {} |
18 | 19 |
|
19 | | - public open(successCallback: () => void) { |
20 | | - try { |
21 | | - // Access the existing Dexie tables |
22 | | - this.scriptsTable = window.store.sqlideScripts; |
23 | | - this.databasesTable = window.store.sqlideDatabases; |
24 | | - |
25 | | - // Verify the tables are available and ready |
26 | | - if (this.scriptsTable && this.databasesTable) { |
27 | | - // Dexie tables are already initialized, just call the callback |
28 | | - successCallback(); |
29 | | - } else { |
30 | | - console.log("Dexie tables not available at window.store"); |
31 | | - } |
32 | | - } catch (error) { |
33 | | - console.log("Couldn't access Dexie tables: " + error); |
34 | | - } |
35 | | - } |
| 20 | + public open(successCallback: () => void) { |
| 21 | + try { |
| 22 | + // Access the existing Dexie tables |
| 23 | + this.scriptsTable = window.hyperbook.store.sqlideScripts; |
| 24 | + this.databasesTable = window.hyperbook.store.sqlideDatabases; |
36 | 25 |
|
37 | | - public writeScript(scriptId: string, script: string) { |
38 | | - this.scriptsTable.put({ |
39 | | - scriptId: scriptId, |
40 | | - script: script |
41 | | - }).catch(error => { |
42 | | - console.error("Error writing script: ", error); |
43 | | - }); |
| 26 | + // Verify the tables are available and ready |
| 27 | + if (this.scriptsTable && this.databasesTable) { |
| 28 | + // Dexie tables are already initialized, just call the callback |
| 29 | + successCallback(); |
| 30 | + } else { |
| 31 | + console.log("Dexie tables not available at window.store"); |
| 32 | + } |
| 33 | + } catch (error) { |
| 34 | + console.log("Couldn't access Dexie tables: " + error); |
44 | 35 | } |
| 36 | + } |
45 | 37 |
|
46 | | - public removeScript(scriptId: string) { |
47 | | - this.scriptsTable.delete(scriptId).catch(error => { |
48 | | - console.error("Error removing script: ", error); |
49 | | - }); |
50 | | - } |
| 38 | + public writeScript(scriptId: string, script: string) { |
| 39 | + this.scriptsTable |
| 40 | + .put({ |
| 41 | + scriptId: scriptId, |
| 42 | + script: script, |
| 43 | + }) |
| 44 | + .catch((error) => { |
| 45 | + console.error("Error writing script: ", error); |
| 46 | + }); |
| 47 | + } |
51 | 48 |
|
52 | | - public getScript(scriptId: string, callback: (script: string) => void) { |
53 | | - this.scriptsTable.get(scriptId) |
54 | | - .then(result => { |
55 | | - if (result == null) { |
56 | | - callback(null); |
57 | | - } else { |
58 | | - callback(result.script); |
59 | | - } |
60 | | - }) |
61 | | - .catch(error => { |
62 | | - console.error("Error getting script: ", error); |
63 | | - callback(null); |
64 | | - }); |
65 | | - } |
| 49 | + public removeScript(scriptId: string) { |
| 50 | + this.scriptsTable.delete(scriptId).catch((error) => { |
| 51 | + console.error("Error removing script: ", error); |
| 52 | + }); |
| 53 | + } |
66 | 54 |
|
67 | | - public writeDatabase(databaseID: string, database: string) { |
68 | | - this.databasesTable.put({ |
69 | | - databaseId: databaseID, |
70 | | - database: database |
71 | | - }).catch(error => { |
72 | | - console.error("Error writing database: ", error); |
73 | | - }); |
74 | | - } |
| 55 | + public getScript(scriptId: string, callback: (script: string) => void) { |
| 56 | + this.scriptsTable |
| 57 | + .get(scriptId) |
| 58 | + .then((result) => { |
| 59 | + if (result == null) { |
| 60 | + callback(null); |
| 61 | + } else { |
| 62 | + callback(result.script); |
| 63 | + } |
| 64 | + }) |
| 65 | + .catch((error) => { |
| 66 | + console.error("Error getting script: ", error); |
| 67 | + callback(null); |
| 68 | + }); |
| 69 | + } |
75 | 70 |
|
76 | | - public removeDatabase(databaseId: string) { |
77 | | - this.databasesTable.delete(databaseId).catch(error => { |
78 | | - console.error("Error removing database: ", error); |
79 | | - }); |
80 | | - } |
| 71 | + public writeDatabase(databaseID: string, database: string) { |
| 72 | + this.databasesTable |
| 73 | + .put({ |
| 74 | + databaseId: databaseID, |
| 75 | + database: database, |
| 76 | + }) |
| 77 | + .catch((error) => { |
| 78 | + console.error("Error writing database: ", error); |
| 79 | + }); |
| 80 | + } |
81 | 81 |
|
82 | | - public getDatabase(databaseId: string, callback: (database: string) => void) { |
83 | | - this.databasesTable.get(databaseId) |
84 | | - .then(result => { |
85 | | - if (result == null) { |
86 | | - callback(null); |
87 | | - } else { |
88 | | - callback(result.database); |
89 | | - } |
90 | | - }) |
91 | | - .catch(error => { |
92 | | - console.error("Error getting database: ", error); |
93 | | - callback(null); |
94 | | - }); |
95 | | - } |
| 82 | + public removeDatabase(databaseId: string) { |
| 83 | + this.databasesTable.delete(databaseId).catch((error) => { |
| 84 | + console.error("Error removing database: ", error); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + public getDatabase(databaseId: string, callback: (database: string) => void) { |
| 89 | + this.databasesTable |
| 90 | + .get(databaseId) |
| 91 | + .then((result) => { |
| 92 | + if (result == null) { |
| 93 | + callback(null); |
| 94 | + } else { |
| 95 | + callback(result.database); |
| 96 | + } |
| 97 | + }) |
| 98 | + .catch((error) => { |
| 99 | + console.error("Error getting database: ", error); |
| 100 | + callback(null); |
| 101 | + }); |
| 102 | + } |
96 | 103 | } |
0 commit comments