@coorax/react 0.0.0-canary.1 → 0.0.0-canary.3
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/dist/components/commons/button.d.ts +8 -0
- package/dist/components/corax-context-provider.d.ts +6 -0
- package/dist/components/corax-flow.d.ts +4 -0
- package/dist/components/fallbacks/image.fallback.d.ts +3 -0
- package/dist/components/slots/buttons/block-actions-button.d.ts +3 -0
- package/dist/components/slots/buttons/toolbar-button.d.ts +3 -0
- package/dist/components/slots/maps/block-actions.d.ts +1 -0
- package/dist/components/slots/maps/overlays.d.ts +1 -0
- package/dist/components/slots/maps/text-inspector.d.ts +2 -0
- package/dist/components/slots/maps/toolbar.d.ts +1 -0
- package/dist/components/slots/overlays/focus-overlay.d.ts +3 -0
- package/dist/components/slots/overlays/hover-overlay.d.ts +3 -0
- package/dist/components/slots/overlays/selection-overlay.d.ts +3 -0
- package/dist/components/slots/slot-button.d.ts +6 -0
- package/dist/components/slots/slot-component.d.ts +5 -0
- package/dist/components/slots/slots.d.ts +4 -0
- package/dist/constants/field.constants.d.ts +2 -0
- package/dist/hooks/custom/use-custom-ref.d.ts +1 -0
- package/dist/hooks/custom/use-isomorphic-layout-effect.d.ts +2 -0
- package/{src/hooks/index.ts → dist/hooks/index.d.ts} +1 -0
- package/dist/hooks/use-block-entry.d.ts +2 -0
- package/dist/hooks/use-block.d.ts +2 -0
- package/dist/hooks/use-flow.d.ts +17 -0
- package/dist/hooks/use-keyboard.d.ts +10 -0
- package/dist/hooks/use-registry.d.ts +3 -0
- package/dist/hooks/use-renderer.d.ts +2 -0
- package/dist/hooks/use-slice.d.ts +2 -0
- package/dist/hooks/use-slots.d.ts +2 -0
- package/dist/hooks/use-tasks.d.ts +7 -0
- package/dist/hooks/use-text-input.d.ts +2 -0
- package/dist/hooks/use-ui-context.d.ts +2 -0
- package/dist/hooks/useSelection.d.ts +2 -0
- package/{src/index.ts → dist/index.d.ts} +3 -4
- package/dist/index.js +2751 -0
- package/dist/lib/comp.utils.d.ts +4 -0
- package/dist/lib/func.utils.d.ts +3 -0
- package/dist/lib/selection.utils.d.ts +5 -0
- package/dist/lib/slots.utils.d.ts +4 -0
- package/dist/lifecycle/fields/focus.events/onbeforeinput.d.ts +3 -0
- package/dist/lifecycle/fields/focus.events/onblur.d.ts +3 -0
- package/dist/lifecycle/fields/focus.events/onfocus.d.ts +3 -0
- package/dist/lifecycle/fields/keyboard.events/onkeydown.d.ts +3 -0
- package/dist/lifecycle/fields/lifecycle.fields.d.ts +12 -0
- package/dist/lifecycle/fields/lifecycle.fields.types.d.ts +15 -0
- package/dist/lifecycle/fields/lifecycle.utils.d.ts +2 -0
- package/dist/lifecycle/fields/mouse.events/onclick.d.ts +3 -0
- package/dist/lifecycle/fields/mouse.events/ondoubleclick.d.ts +4 -0
- package/dist/lifecycle/fields/mouse.events/onmouseenter.d.ts +3 -0
- package/dist/lifecycle/fields/mouse.events/onmouseleave.d.ts +3 -0
- package/dist/lifecycle/fields/synthetic.events/onload.d.ts +3 -0
- package/dist/lifecycle/fields/synthetic.events/onselect.d.ts +3 -0
- package/dist/lifecycle/index.d.ts +1 -0
- package/dist/modules/autosave.module.d.ts +31 -0
- package/dist/modules/fallbacks.module.d.ts +7 -0
- package/dist/modules/slots.module.d.ts +7 -0
- package/dist/modules/ui-primitives.module.d.ts +7 -0
- package/dist/primitives/fields/field.primitives.d.ts +2 -0
- package/dist/primitives/index.d.ts +3 -0
- package/{src/registry/fallbacks.registry.tsx → dist/registry/fallbacks.registry.d.ts} +3 -4
- package/dist/registry/primitives.registry.d.ts +22 -0
- package/dist/registry/registry.d.ts +15 -0
- package/dist/registry/slots.registry.d.ts +4 -0
- package/{src/registry/ui.primitives.registry.tsx → dist/registry/ui.primitives.registry.d.ts} +3 -4
- package/dist/renderer/renderer.d.ts +23 -0
- package/dist/renderer/renderer.test.d.ts +1 -0
- package/{src/renderer/renderer.types.ts → dist/renderer/renderer.types.d.ts} +0 -1
- package/dist/renderer/renderer.utils.d.ts +14 -0
- package/dist/renderer/utils/divider.renderer.utils.d.ts +6 -0
- package/dist/services/localdb/db/db.d.ts +74 -0
- package/dist/services/localdb/db/db.test.d.ts +1 -0
- package/dist/services/localdb/lexer/lexer.d.ts +14 -0
- package/dist/services/localdb/lexer/lexer.test.d.ts +1 -0
- package/dist/services/localdb/lexer/lexer.types.d.ts +5 -0
- package/dist/services/localdb/operator/operator.d.ts +10 -0
- package/dist/services/localdb/parser/parser.constants.d.ts +3 -0
- package/dist/services/localdb/parser/parser.d.ts +14 -0
- package/dist/services/localdb/parser/parser.test.d.ts +1 -0
- package/dist/services/localdb/parser/parser.types.d.ts +43 -0
- package/dist/services/localdb/sheet/sheet.constants.d.ts +3 -0
- package/dist/services/localdb/sheet/sheet.d.ts +14 -0
- package/dist/services/localdb/sheet/sheet.types.d.ts +13 -0
- package/dist/styles.css +1094 -0
- package/package.json +29 -12
- package/.oxlintrc.json +0 -8
- package/src/components/commons/button.tsx +0 -59
- package/src/components/corax-context-provider.tsx +0 -47
- package/src/components/corax-flow.tsx +0 -44
- package/src/components/fallbacks/image.fallback.tsx +0 -23
- package/src/components/slots/buttons/block-actions-button.tsx +0 -85
- package/src/components/slots/buttons/toolbar-button.tsx +0 -40
- package/src/components/slots/maps/block-actions.tsx +0 -84
- package/src/components/slots/maps/overlays.tsx +0 -9
- package/src/components/slots/maps/text-inspector.tsx +0 -5
- package/src/components/slots/maps/toolbar.tsx +0 -79
- package/src/components/slots/overlays/focus-overlay.tsx +0 -30
- package/src/components/slots/overlays/hover-overlay.tsx +0 -18
- package/src/components/slots/overlays/selection-overlay.tsx +0 -20
- package/src/components/slots/slot-button.tsx +0 -20
- package/src/components/slots/slot-component.tsx +0 -17
- package/src/components/slots/slots.tsx +0 -29
- package/src/constants/field.constants.ts +0 -3
- package/src/hooks/custom/use-custom-ref.ts +0 -11
- package/src/hooks/use-block-entry.ts +0 -44
- package/src/hooks/use-block.ts +0 -11
- package/src/hooks/use-flow.ts +0 -83
- package/src/hooks/use-keyboard.ts +0 -66
- package/src/hooks/use-registry.ts +0 -18
- package/src/hooks/use-renderer.ts +0 -8
- package/src/hooks/use-slice.ts +0 -13
- package/src/hooks/use-slots.ts +0 -13
- package/src/hooks/use-tasks.ts +0 -33
- package/src/hooks/use-text-input.ts +0 -83
- package/src/hooks/use-ui-context.ts +0 -16
- package/src/hooks/useSelection.ts +0 -27
- package/src/lib/comp.utils.tsx +0 -44
- package/src/lib/func.utils.ts +0 -15
- package/src/lib/selection.utils.ts +0 -33
- package/src/lib/slots.utils.tsx +0 -23
- package/src/lifecycle/fields/focus.events/onbeforeinput.ts +0 -21
- package/src/lifecycle/fields/focus.events/onblur.ts +0 -19
- package/src/lifecycle/fields/focus.events/onfocus.ts +0 -16
- package/src/lifecycle/fields/keyboard.events/onkeydown.ts +0 -128
- package/src/lifecycle/fields/lifecycle.fields.ts +0 -116
- package/src/lifecycle/fields/lifecycle.fields.types.ts +0 -33
- package/src/lifecycle/fields/lifecycle.utils.ts +0 -26
- package/src/lifecycle/fields/mouse.events/onclick.ts +0 -79
- package/src/lifecycle/fields/mouse.events/ondoubleclick.ts +0 -27
- package/src/lifecycle/fields/mouse.events/onmouseenter.ts +0 -27
- package/src/lifecycle/fields/mouse.events/onmouseleave.ts +0 -21
- package/src/lifecycle/fields/synthetic.events/onload.ts +0 -16
- package/src/lifecycle/fields/synthetic.events/onselect.ts +0 -57
- package/src/lifecycle/index.ts +0 -1
- package/src/modules/autosave.module.ts +0 -238
- package/src/modules/fallbacks.module.tsx +0 -19
- package/src/modules/slots.module.tsx +0 -28
- package/src/modules/ui-primitives.module.tsx +0 -20
- package/src/primitives/fields/field.primitives.tsx +0 -86
- package/src/primitives/index.ts +0 -8
- package/src/registry/primitives.registry.tsx +0 -57
- package/src/registry/registry.ts +0 -44
- package/src/registry/slots.registry.tsx +0 -6
- package/src/renderer/renderer.test.ts +0 -34
- package/src/renderer/renderer.tsx +0 -164
- package/src/renderer/renderer.utils.tsx +0 -139
- package/src/renderer/utils/divider.renderer.utils.tsx +0 -53
- package/src/services/localdb/db/db.test.ts +0 -351
- package/src/services/localdb/db/db.ts +0 -628
- package/src/services/localdb/lexer/lexer.test.ts +0 -123
- package/src/services/localdb/lexer/lexer.ts +0 -173
- package/src/services/localdb/lexer/lexer.types.ts +0 -34
- package/src/services/localdb/operator/operator.ts +0 -16
- package/src/services/localdb/parser/parser.constants.ts +0 -23
- package/src/services/localdb/parser/parser.test.ts +0 -172
- package/src/services/localdb/parser/parser.ts +0 -427
- package/src/services/localdb/parser/parser.types.ts +0 -56
- package/src/services/localdb/sheet/sheet.constants.ts +0 -32
- package/src/services/localdb/sheet/sheet.ts +0 -47
- package/src/services/localdb/sheet/sheet.types.ts +0 -38
- package/tsconfig.app.json +0 -26
- package/tsconfig.json +0 -9
- package/tsconfig.node.json +0 -9
- package/tsconfig.tsbuildinfo +0 -1
- package/vite.config.ts +0 -9
- /package/{src/components/index.ts → dist/components/index.d.ts} +0 -0
- /package/{src/components/slots/index.ts → dist/components/slots/index.d.ts} +0 -0
- /package/{src/components/slots/maps/index.ts → dist/components/slots/maps/index.d.ts} +0 -0
- /package/{src/modules/index.ts → dist/modules/index.d.ts} +0 -0
- /package/{src/registry/index.ts → dist/registry/index.d.ts} +0 -0
|
@@ -1,628 +0,0 @@
|
|
|
1
|
-
import { DBClient } from "../operator/operator";
|
|
2
|
-
import type { InsertStatement, SelectStatement, Statement } from "../parser/parser.types";
|
|
3
|
-
import { TablesSheet } from "../sheet/sheet";
|
|
4
|
-
import { DEFAULT_FIELDS_SCHEMA } from "../sheet/sheet.constants";
|
|
5
|
-
import type { LocalDBNativeType, LocalDBType, Table } from "../sheet/sheet.types";
|
|
6
|
-
|
|
7
|
-
export type LocalDBErrorType = Error | DOMException;
|
|
8
|
-
|
|
9
|
-
interface DB {
|
|
10
|
-
isAvailable: boolean;
|
|
11
|
-
isError: Error | null;
|
|
12
|
-
|
|
13
|
-
QueryDB<T extends any>(query: string, params?: any[]): Promise<LocalDBErrorType | T[]>;
|
|
14
|
-
DeleteDB(): Promise<LocalDBErrorType | null>;
|
|
15
|
-
MigrateDB(query: string): Promise<LocalDBErrorType | null>;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export class LocalDB implements DB {
|
|
19
|
-
private DBName: string = "MYDB";
|
|
20
|
-
private DBVersion: number = 1;
|
|
21
|
-
private readonly defaultStores: string[] = [];
|
|
22
|
-
private readonly DBTypesStoreName = "__db.types";
|
|
23
|
-
|
|
24
|
-
public readonly isAvailable: boolean;
|
|
25
|
-
public isError: Error | null = null;
|
|
26
|
-
|
|
27
|
-
private db: IDBDatabase | null = null;
|
|
28
|
-
private client: DBClient = new DBClient();
|
|
29
|
-
private sheet: TablesSheet = new TablesSheet();
|
|
30
|
-
|
|
31
|
-
constructor(DbName?: string) {
|
|
32
|
-
if (DbName) this.DBName = DbName;
|
|
33
|
-
this.isAvailable = typeof window !== "undefined" && !!window?.indexedDB;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
// private async syncDB() {
|
|
37
|
-
// this.ensureObjectStores(this.db!, [this.DBTypesStoreName]);
|
|
38
|
-
// const tables = this.sheet.GetTables();
|
|
39
|
-
// const [_, objectStore] = this.openTx(this.DBTypesStoreName, () => null);
|
|
40
|
-
// await Promise.all(
|
|
41
|
-
// tables.map((table) => this.insertData(objectStore, { ...table, id: table.name })),
|
|
42
|
-
// );
|
|
43
|
-
// }
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
*
|
|
47
|
-
* @param ev `Event`
|
|
48
|
-
* @param cb `resolve` or `reject` or any other callback
|
|
49
|
-
* But in our case `reject`
|
|
50
|
-
*/
|
|
51
|
-
private onDBError(cb: (value?: any) => void, ev: Event) {
|
|
52
|
-
console.error("[ERROR] IndexedDB error:", ev);
|
|
53
|
-
this.isError = new Error((ev.target as IDBOpenDBRequest | null)?.error?.name ?? ev.type);
|
|
54
|
-
cb(new Error((ev.target as IDBOpenDBRequest | null)?.error?.name ?? ev.type));
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
*
|
|
59
|
-
* @param ev `Event`
|
|
60
|
-
* @param cb `resolve` or `reject` or any other callback
|
|
61
|
-
* But in our case `resolve`
|
|
62
|
-
*/
|
|
63
|
-
private onDBSuccess(cb: (value?: any) => void, ev: Event) {
|
|
64
|
-
// console.log("[INFO] db connected successfully!");
|
|
65
|
-
this.db = (ev.target as IDBOpenDBRequest).result;
|
|
66
|
-
this.isError = null;
|
|
67
|
-
cb(null);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
private closeConnection() {
|
|
71
|
-
if (this.db) {
|
|
72
|
-
this.db.close();
|
|
73
|
-
this.db = null;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
private ensureObjectStores(db: IDBDatabase, storeNames: string[] = this.defaultStores) {
|
|
78
|
-
for (const name of storeNames) {
|
|
79
|
-
if (db.objectStoreNames.contains(name)) continue;
|
|
80
|
-
db.createObjectStore(name, { keyPath: "id" });
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
private bumpVersion(value?: number) {
|
|
85
|
-
this.DBVersion = value ?? this.DBVersion + 1;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
public async DeleteDB(): Promise<LocalDBErrorType | null> {
|
|
89
|
-
if (!this.isAvailable) {
|
|
90
|
-
console.warn("[WARN] IndexDB not available in this browser!");
|
|
91
|
-
return new Error("[ERROR] IndexDB not available in this browser!");
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
this.closeConnection();
|
|
95
|
-
|
|
96
|
-
return new Promise((resolve) => {
|
|
97
|
-
const request = window.indexedDB.deleteDatabase(this.DBName);
|
|
98
|
-
|
|
99
|
-
request.onsuccess = () => {
|
|
100
|
-
// console.log("[INFO] Database deleted successfully");
|
|
101
|
-
this.db = null;
|
|
102
|
-
resolve(null);
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
request.onerror = () => {
|
|
106
|
-
console.error("[ERROR] Failed to delete database", request.error);
|
|
107
|
-
resolve(request.error ?? new Error("Failed to delete database"));
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
request.onblocked = () => {
|
|
111
|
-
console.warn("[WARN] Database deletion blocked. Close all open connections first.");
|
|
112
|
-
resolve(new Error("[WARN] Database deletion blocked. Close all open connections first."));
|
|
113
|
-
};
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
private async init() {
|
|
118
|
-
return new Promise((resolve, reject) => {
|
|
119
|
-
if (!this.isAvailable) {
|
|
120
|
-
console.warn("[WARN] IndexDB not available in this browser!");
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
if (this.db) resolve(true);
|
|
124
|
-
|
|
125
|
-
const request = window.indexedDB.open(
|
|
126
|
-
this.DBName,
|
|
127
|
-
this.DBVersion !== 1 ? this.DBVersion : undefined,
|
|
128
|
-
);
|
|
129
|
-
|
|
130
|
-
request.onupgradeneeded = () => {
|
|
131
|
-
this.ensureObjectStores(request.result, this.defaultStores);
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
request.onerror = this.onDBError.bind(this, reject);
|
|
135
|
-
request.onsuccess = (ev: Event) => {
|
|
136
|
-
this.bumpVersion(request.result.version);
|
|
137
|
-
this.onDBSuccess(() => resolve(true), ev);
|
|
138
|
-
};
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
/**
|
|
143
|
-
* Récupère toutes les données d'une table IndexedDB
|
|
144
|
-
* @param objectStore Le object store de la table
|
|
145
|
-
* @param columns Les colonnes à récupérer (["*"] pour toutes)
|
|
146
|
-
* @returns Promise avec les données récupérées (T représente le tableau de résultats)
|
|
147
|
-
*/
|
|
148
|
-
private async retrieveData<T extends any>(
|
|
149
|
-
objectStore: IDBObjectStore,
|
|
150
|
-
columns: string[],
|
|
151
|
-
): Promise<T[]> {
|
|
152
|
-
return new Promise((resolve) => {
|
|
153
|
-
const data: any[] = [];
|
|
154
|
-
|
|
155
|
-
const cursorReq = objectStore.openCursor();
|
|
156
|
-
cursorReq.onsuccess = (ev) => {
|
|
157
|
-
const cursor = (ev.target as IDBRequest<IDBCursorWithValue | null>).result;
|
|
158
|
-
|
|
159
|
-
if (cursor) {
|
|
160
|
-
const value = cursor.value;
|
|
161
|
-
|
|
162
|
-
if (typeof value !== "object" || value === null) {
|
|
163
|
-
cursor.continue();
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
const row = { ...value };
|
|
168
|
-
|
|
169
|
-
if (!columns.includes("*")) {
|
|
170
|
-
for (const key of Object.keys(row)) {
|
|
171
|
-
if (!columns.includes(key)) {
|
|
172
|
-
delete row[key];
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
data.push(row);
|
|
178
|
-
cursor.continue();
|
|
179
|
-
} else {
|
|
180
|
-
resolve(data as T[]);
|
|
181
|
-
}
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
cursorReq.onerror = (ev) => {
|
|
185
|
-
console.error(`Cursor error: ${(ev.target as IDBRequest).error?.message}`);
|
|
186
|
-
resolve([]);
|
|
187
|
-
};
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* Récupère une donnée par son ID dans une table IndexedDB
|
|
193
|
-
* @param objectStore Le object store de la table
|
|
194
|
-
* @param id L'identifiant de l'enregistrement
|
|
195
|
-
* @param columns Les colonnes à récupérer (["*"] pour toutes)
|
|
196
|
-
* @returns Promise avec la donnée récupérée ou null si non trouvée
|
|
197
|
-
*/
|
|
198
|
-
private async retrieveById<T>(
|
|
199
|
-
objectStore: IDBObjectStore,
|
|
200
|
-
id: string,
|
|
201
|
-
columns: string[],
|
|
202
|
-
): Promise<T | null> {
|
|
203
|
-
return new Promise((resolve) => {
|
|
204
|
-
const request = objectStore.get(id);
|
|
205
|
-
|
|
206
|
-
request.onsuccess = () => {
|
|
207
|
-
const value = request.result;
|
|
208
|
-
|
|
209
|
-
if (!value) {
|
|
210
|
-
resolve(null);
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
if (typeof value !== "object") {
|
|
215
|
-
console.error(`Expected an object, received ${typeof value}`);
|
|
216
|
-
resolve(null);
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
const row = { ...value };
|
|
221
|
-
|
|
222
|
-
if (!columns.includes("*")) {
|
|
223
|
-
for (const key of Object.keys(row)) {
|
|
224
|
-
if (!columns.includes(key)) {
|
|
225
|
-
delete row[key];
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
resolve(row as T);
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
request.onerror = (ev) => {
|
|
234
|
-
console.error(`Retrieve by ID error: ${(ev.target as IDBRequest).error?.message}`);
|
|
235
|
-
resolve(null);
|
|
236
|
-
};
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Insère ou met à jour des données dans une table IndexedDB
|
|
242
|
-
* @param objectStore Le object store de la table
|
|
243
|
-
* @param data Les données à insérer
|
|
244
|
-
* @returns Promise avec null ou Error
|
|
245
|
-
*/
|
|
246
|
-
private async insertData<T extends Record<string, LocalDBNativeType>>(
|
|
247
|
-
objectStore: IDBObjectStore,
|
|
248
|
-
data: T,
|
|
249
|
-
): Promise<null | Error> {
|
|
250
|
-
return new Promise((resolve) => {
|
|
251
|
-
if (Array.isArray(data)) {
|
|
252
|
-
throw new Error("Oops! you provide an array...");
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// Use put to allow insert or upsert
|
|
256
|
-
const request = objectStore.put(data);
|
|
257
|
-
|
|
258
|
-
request.onsuccess = () => {
|
|
259
|
-
resolve(null);
|
|
260
|
-
};
|
|
261
|
-
|
|
262
|
-
request.onerror = (ev) => {
|
|
263
|
-
resolve(new Error(`Insert error: ${(ev.target as IDBRequest).error?.message}`));
|
|
264
|
-
};
|
|
265
|
-
});
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Ouvre une transaction sur une ou plusieurs tables IndexedDB
|
|
270
|
-
* @param tableNames Nom(s) de table(s)
|
|
271
|
-
* @param reject Fonction reject pour la gestion d'erreurs
|
|
272
|
-
* @param mode IDBTransactionMode ("readwrite" ou "readonly")
|
|
273
|
-
* @returns L'object store de la première table
|
|
274
|
-
*/
|
|
275
|
-
private openTx(
|
|
276
|
-
tableNames: string | string[],
|
|
277
|
-
reject: (reason?: any) => void,
|
|
278
|
-
mode: IDBTransactionMode = "readwrite",
|
|
279
|
-
): [IDBTransaction, IDBObjectStore] {
|
|
280
|
-
const rawNames = Array.isArray(tableNames) ? tableNames : [tableNames];
|
|
281
|
-
const uniqueNames = [...new Set(rawNames.filter(Boolean))];
|
|
282
|
-
|
|
283
|
-
for (const name of uniqueNames) {
|
|
284
|
-
if (!this.db?.objectStoreNames.contains(name)) {
|
|
285
|
-
throw new Error(`Table ${name} does not exist!`);
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
const tx = this.db!.transaction([this.DBTypesStoreName, ...uniqueNames], mode);
|
|
290
|
-
|
|
291
|
-
tx.onerror = this.onDBError.bind(this, reject);
|
|
292
|
-
|
|
293
|
-
return [tx, tx.objectStore(uniqueNames[0]!)];
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
private convertFieldValueType(value: any, type: LocalDBType): any {
|
|
297
|
-
switch (type) {
|
|
298
|
-
case "number":
|
|
299
|
-
if (typeof value === "number") return value;
|
|
300
|
-
if (!/^-?\d+$/.test(String(value).trim())) {
|
|
301
|
-
throw new Error(`Invalid number expr. Got ${value}`);
|
|
302
|
-
}
|
|
303
|
-
return parseInt(String(value).trim(), 10);
|
|
304
|
-
|
|
305
|
-
case "boolean":
|
|
306
|
-
if (typeof value === "boolean") return value;
|
|
307
|
-
if (!["true", "false"].includes(String(value).trim())) {
|
|
308
|
-
throw new Error(`Invalid boolean expr. Got ${value}`);
|
|
309
|
-
}
|
|
310
|
-
return String(value).trim() === "true";
|
|
311
|
-
|
|
312
|
-
case "bigint":
|
|
313
|
-
return BigInt(value);
|
|
314
|
-
|
|
315
|
-
case "symbol":
|
|
316
|
-
return typeof value === "symbol" ? value : Symbol.for(String(value));
|
|
317
|
-
|
|
318
|
-
case "object":
|
|
319
|
-
if (typeof value === "object" && value !== null) return value;
|
|
320
|
-
if (typeof value === "string") {
|
|
321
|
-
try {
|
|
322
|
-
return JSON.parse(value);
|
|
323
|
-
} catch {
|
|
324
|
-
return {};
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
return {};
|
|
328
|
-
|
|
329
|
-
case "array.string":
|
|
330
|
-
if (Array.isArray(value)) return value.map(String);
|
|
331
|
-
if (value instanceof Set) return Array.from(value).map(String);
|
|
332
|
-
if (typeof value === "string") {
|
|
333
|
-
try {
|
|
334
|
-
const parsed = JSON.parse(value);
|
|
335
|
-
if (Array.isArray(parsed)) return parsed.map(String);
|
|
336
|
-
} catch {}
|
|
337
|
-
return value
|
|
338
|
-
.split(",")
|
|
339
|
-
.map((s) => s.trim())
|
|
340
|
-
.filter(Boolean);
|
|
341
|
-
}
|
|
342
|
-
return [];
|
|
343
|
-
|
|
344
|
-
case "file":
|
|
345
|
-
case "blob":
|
|
346
|
-
return value;
|
|
347
|
-
|
|
348
|
-
default:
|
|
349
|
-
return typeof value === "string" ? value.trim() : String(value ?? "");
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
private async executeQuery<T extends any>(requests: Statement[]): Promise<Error | T[]> {
|
|
354
|
-
this.isError = null;
|
|
355
|
-
let lastResult: any[] = [];
|
|
356
|
-
|
|
357
|
-
for (const request of requests) {
|
|
358
|
-
try {
|
|
359
|
-
switch (request.type) {
|
|
360
|
-
case "SELECT": {
|
|
361
|
-
const selectReq = request as SelectStatement;
|
|
362
|
-
const joinTables = (selectReq.joins ?? []).map((j) => j.table);
|
|
363
|
-
const allTables = [selectReq.table, ...joinTables];
|
|
364
|
-
|
|
365
|
-
const [tx, primaryStore] = this.openTx(
|
|
366
|
-
allTables,
|
|
367
|
-
(err) => {
|
|
368
|
-
this.isError = err;
|
|
369
|
-
},
|
|
370
|
-
"readonly",
|
|
371
|
-
);
|
|
372
|
-
|
|
373
|
-
// 1. Retrieve primary table data
|
|
374
|
-
let mainRows = await this.retrieveData<any>(primaryStore, ["*"]);
|
|
375
|
-
|
|
376
|
-
// 2. Filter primary rows by WHERE condition if any
|
|
377
|
-
if (selectReq.where && selectReq.where.length > 0) {
|
|
378
|
-
mainRows = mainRows.filter((row) => {
|
|
379
|
-
return selectReq.where!.every((cond) => {
|
|
380
|
-
if (
|
|
381
|
-
cond.table &&
|
|
382
|
-
cond.table !== selectReq.table &&
|
|
383
|
-
cond.table !== selectReq.alias
|
|
384
|
-
) {
|
|
385
|
-
return true;
|
|
386
|
-
}
|
|
387
|
-
const val = row[cond.column];
|
|
388
|
-
if (cond.operator === "=") {
|
|
389
|
-
return String(val) === String(cond.value);
|
|
390
|
-
}
|
|
391
|
-
if (cond.operator === "!=") {
|
|
392
|
-
return String(val) !== String(cond.value);
|
|
393
|
-
}
|
|
394
|
-
return true;
|
|
395
|
-
});
|
|
396
|
-
});
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
// 3. Process JOINs
|
|
400
|
-
if (selectReq.joins && selectReq.joins.length > 0) {
|
|
401
|
-
for (const join of selectReq.joins) {
|
|
402
|
-
const joinStore = tx.objectStore(join.table);
|
|
403
|
-
let joinRows = await this.retrieveData<any>(joinStore, ["*"]);
|
|
404
|
-
|
|
405
|
-
// Filter join rows by WHERE if any
|
|
406
|
-
if (selectReq.where && selectReq.where.length > 0) {
|
|
407
|
-
joinRows = joinRows.filter((row) => {
|
|
408
|
-
return selectReq.where!.every((cond) => {
|
|
409
|
-
if (cond.table && cond.table !== join.table && cond.table !== join.alias) {
|
|
410
|
-
return true;
|
|
411
|
-
}
|
|
412
|
-
const val = row[cond.column];
|
|
413
|
-
if (cond.operator === "=") {
|
|
414
|
-
return String(val) === String(cond.value);
|
|
415
|
-
}
|
|
416
|
-
if (cond.operator === "!=") {
|
|
417
|
-
return String(val) !== String(cond.value);
|
|
418
|
-
}
|
|
419
|
-
return true;
|
|
420
|
-
});
|
|
421
|
-
});
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
// Determine join keys
|
|
425
|
-
let leftCol = "id";
|
|
426
|
-
let rightCol = `${selectReq.table.replace(/s$/, "")}_id`;
|
|
427
|
-
|
|
428
|
-
if (join.on) {
|
|
429
|
-
leftCol = join.on.leftColumn;
|
|
430
|
-
rightCol = join.on.rightColumn;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
const survivingRows: any[] = [];
|
|
434
|
-
|
|
435
|
-
for (const mainRow of mainRows) {
|
|
436
|
-
const mainVal = mainRow[leftCol];
|
|
437
|
-
const matched = joinRows.filter(
|
|
438
|
-
(child) => String(child[rightCol]) === String(mainVal),
|
|
439
|
-
);
|
|
440
|
-
|
|
441
|
-
if (join.type === "INNER" && matched.length === 0) {
|
|
442
|
-
continue;
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
// Form the complete document structure
|
|
446
|
-
// For records with an 'id' (Corax fields, containers, components, medias),
|
|
447
|
-
// format as dictionary Record<id, object>
|
|
448
|
-
const collection: Record<string, any> = {};
|
|
449
|
-
let allHaveId = matched.length > 0;
|
|
450
|
-
|
|
451
|
-
for (const child of matched) {
|
|
452
|
-
if (!child.id) allHaveId = false;
|
|
453
|
-
const cleanChild = { ...child };
|
|
454
|
-
delete cleanChild[rightCol];
|
|
455
|
-
collection[child.id] = cleanChild;
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
if (allHaveId || matched.length === 0) {
|
|
459
|
-
mainRow[join.table] = collection;
|
|
460
|
-
} else {
|
|
461
|
-
mainRow[join.table] = matched;
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
survivingRows.push(mainRow);
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
mainRows = survivingRows;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
// 4. Filter columns if columns is not ["*"]
|
|
472
|
-
if (!selectReq.columns.includes("*")) {
|
|
473
|
-
mainRows = mainRows.map((row) => {
|
|
474
|
-
const filtered: Record<string, any> = {};
|
|
475
|
-
for (const col of selectReq.columns) {
|
|
476
|
-
if (col in row) {
|
|
477
|
-
filtered[col] = row[col];
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
return filtered;
|
|
481
|
-
});
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
lastResult = mainRows;
|
|
485
|
-
break;
|
|
486
|
-
}
|
|
487
|
-
|
|
488
|
-
case "INSERT": {
|
|
489
|
-
const insertReq = request as InsertStatement;
|
|
490
|
-
const [tx, objectStore] = this.openTx(
|
|
491
|
-
insertReq.table,
|
|
492
|
-
(err) => {
|
|
493
|
-
this.isError = err;
|
|
494
|
-
},
|
|
495
|
-
"readwrite",
|
|
496
|
-
);
|
|
497
|
-
|
|
498
|
-
const table = await this.retrieveById<Table>(
|
|
499
|
-
tx.objectStore(this.DBTypesStoreName),
|
|
500
|
-
insertReq.table,
|
|
501
|
-
["*"],
|
|
502
|
-
);
|
|
503
|
-
if (!table) {
|
|
504
|
-
return new Error(
|
|
505
|
-
`Table ${insertReq.table} types not found. Please try Migrate your DB`,
|
|
506
|
-
);
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
const newData: Record<string, LocalDBNativeType> = {};
|
|
510
|
-
const fieldsToUse = insertReq.fields?.length ? insertReq.fields : table.order;
|
|
511
|
-
const valuesToUse = insertReq.values?.length ? insertReq.values : insertReq.columns;
|
|
512
|
-
|
|
513
|
-
for (let idx = 0; idx < fieldsToUse.length; idx++) {
|
|
514
|
-
const fieldId = fieldsToUse[idx]!;
|
|
515
|
-
const LocalDBDataType = table.fields[fieldId] as LocalDBType;
|
|
516
|
-
|
|
517
|
-
const typeSchema = DEFAULT_FIELDS_SCHEMA.get(LocalDBDataType);
|
|
518
|
-
if (!typeSchema) {
|
|
519
|
-
return new Error(`Unknown table type ${LocalDBDataType}`);
|
|
520
|
-
}
|
|
521
|
-
|
|
522
|
-
const correspondingValue = valuesToUse[idx];
|
|
523
|
-
if (correspondingValue === undefined) {
|
|
524
|
-
continue;
|
|
525
|
-
}
|
|
526
|
-
|
|
527
|
-
const convertedValue = this.convertFieldValueType(
|
|
528
|
-
correspondingValue,
|
|
529
|
-
LocalDBDataType,
|
|
530
|
-
);
|
|
531
|
-
|
|
532
|
-
const safeValue = typeSchema.safeParse(convertedValue);
|
|
533
|
-
if (!safeValue.success) {
|
|
534
|
-
return new Error(
|
|
535
|
-
`Mismatch field type at ${fieldId}. Must be ${LocalDBDataType}. Stack: ${safeValue.error.message}`,
|
|
536
|
-
);
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
newData[fieldId] = convertedValue;
|
|
540
|
-
}
|
|
541
|
-
|
|
542
|
-
await this.insertData(objectStore, newData);
|
|
543
|
-
break;
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
default:
|
|
547
|
-
break;
|
|
548
|
-
}
|
|
549
|
-
} catch (error) {
|
|
550
|
-
return error instanceof Error ? error : new Error(JSON.stringify(error));
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
return lastResult as T[];
|
|
555
|
-
}
|
|
556
|
-
|
|
557
|
-
async QueryDB<T extends any = any>(
|
|
558
|
-
query: string,
|
|
559
|
-
params?: any[],
|
|
560
|
-
): Promise<LocalDBErrorType | T[]> {
|
|
561
|
-
if (!this.db) await this.init();
|
|
562
|
-
const req = this.client.NewRequest(query, params);
|
|
563
|
-
|
|
564
|
-
return this.executeQuery<T>(req);
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
async MigrateDB(query: string): Promise<Error | null> {
|
|
568
|
-
const statements = this.client.NewRequest(query);
|
|
569
|
-
const isBadReq = statements.some((s) => s.type !== "CREATE");
|
|
570
|
-
|
|
571
|
-
if (isBadReq) {
|
|
572
|
-
return new Error(`Bad request at migration. All must be CREATE statement!`);
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
const tables = await this.sheet.RegisterSheet(statements as Statement<"CREATE">[]);
|
|
576
|
-
|
|
577
|
-
const uniqueNames = [...new Set(tables.map((table) => table.name))];
|
|
578
|
-
if (uniqueNames.includes(this.DBTypesStoreName)) {
|
|
579
|
-
return new Error(`Reserved store name ${this.DBTypesStoreName}!`);
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
if (!this.isAvailable) {
|
|
583
|
-
return new Error("[WARN] IndexDB not available in this browser!");
|
|
584
|
-
}
|
|
585
|
-
if (!this.db) await this.init();
|
|
586
|
-
|
|
587
|
-
const namesToCreate = uniqueNames.filter((name) => !this.db?.objectStoreNames.contains(name));
|
|
588
|
-
if (!namesToCreate.length) {
|
|
589
|
-
// console.log("[INFO] No migration needed");
|
|
590
|
-
return null;
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
this.closeConnection();
|
|
594
|
-
|
|
595
|
-
this.bumpVersion();
|
|
596
|
-
// console.log("[INFO] Migrating database to version", this.DBVersion);
|
|
597
|
-
|
|
598
|
-
return new Promise((resolve) => {
|
|
599
|
-
const request = window.indexedDB.open(this.DBName, this.DBVersion);
|
|
600
|
-
|
|
601
|
-
request.onupgradeneeded = () => {
|
|
602
|
-
const db = request.result;
|
|
603
|
-
this.ensureObjectStores(db, [this.DBTypesStoreName, ...namesToCreate]);
|
|
604
|
-
};
|
|
605
|
-
|
|
606
|
-
request.onsuccess = async (ev) => {
|
|
607
|
-
const db = request.result;
|
|
608
|
-
|
|
609
|
-
const objectStore = db
|
|
610
|
-
.transaction([this.DBTypesStoreName], "readwrite")
|
|
611
|
-
.objectStore(this.DBTypesStoreName);
|
|
612
|
-
await Promise.all(
|
|
613
|
-
tables.map((table) => this.insertData(objectStore, { ...table, id: table.name })),
|
|
614
|
-
);
|
|
615
|
-
|
|
616
|
-
this.onDBSuccess.call(this, resolve, ev);
|
|
617
|
-
};
|
|
618
|
-
request.onerror = this.onDBError.bind(this, resolve);
|
|
619
|
-
|
|
620
|
-
request.onblocked = () => {
|
|
621
|
-
console.warn(
|
|
622
|
-
"[WARN] Migration blocked because another IndexedDB connection is still open.",
|
|
623
|
-
);
|
|
624
|
-
resolve(new Error("IndexedDB migration blocked"));
|
|
625
|
-
};
|
|
626
|
-
});
|
|
627
|
-
}
|
|
628
|
-
}
|