@unvired/react-native-unvired-sdk 0.0.13 → 0.0.14
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/BuildNo.txt +1 -1
- package/dist/database/DatabaseManager.d.ts +3 -23
- package/dist/database/DatabaseManager.js +9 -314
- package/dist/database/services/DatabaseNative.d.ts +8 -0
- package/dist/database/services/DatabaseNative.js +149 -0
- package/dist/database/services/DatabaseWeb.d.ts +13 -0
- package/dist/database/services/DatabaseWeb.js +138 -0
- package/dist/file-system/BaseFileSystem.d.ts +6 -5
- package/dist/file-system/BaseFileSystem.js +20 -50
- package/dist/file-system/services/FileSystemNative.d.ts +17 -0
- package/dist/file-system/services/FileSystemNative.js +73 -0
- package/dist/file-system/services/FileSystemWeb.d.ts +16 -0
- package/dist/file-system/services/FileSystemWeb.js +41 -0
- package/dist/local-storage/localStorage.d.ts +3 -2
- package/dist/local-storage/localStorage.js +16 -11
- package/dist/local-storage/services/StorageNative.d.ts +10 -0
- package/dist/local-storage/services/StorageNative.js +27 -0
- package/dist/local-storage/services/StorageWeb.d.ts +10 -0
- package/dist/local-storage/services/StorageWeb.js +26 -0
- package/dist/main.d.ts +0 -2
- package/dist/main.js +0 -4
- package/package.json +4 -2
- package/dist/PlatformAdapter.d.ts +0 -57
- package/dist/PlatformAdapter.js +0 -116
package/BuildNo.txt
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
R-0.000.
|
|
1
|
+
R-0.000.0014
|
|
@@ -1,32 +1,12 @@
|
|
|
1
1
|
import { IDatabaseAdapter } from './types';
|
|
2
2
|
/**
|
|
3
|
-
* DatabaseManager - Manages
|
|
3
|
+
* DatabaseManager - Manages database operations across platforms
|
|
4
|
+
* - iOS/Android/Windows: Uses react-native-sqlite-storage
|
|
5
|
+
* - Web: Uses WebSQL
|
|
4
6
|
*/
|
|
5
7
|
export declare class DatabaseManager {
|
|
6
|
-
private static databases;
|
|
7
|
-
private static defaultLocation;
|
|
8
|
-
/**
|
|
9
|
-
* Get or create a database connection
|
|
10
|
-
*/
|
|
11
|
-
private static getDatabase;
|
|
12
|
-
/**
|
|
13
|
-
* Close a database connection
|
|
14
|
-
*/
|
|
15
|
-
private static closeDatabase;
|
|
16
|
-
/**
|
|
17
|
-
* Convert SQLResultSet to array of objects
|
|
18
|
-
*/
|
|
19
|
-
private static resultSetToArray;
|
|
20
8
|
/**
|
|
21
9
|
* Get the database adapter implementation
|
|
22
10
|
*/
|
|
23
11
|
static getDatabaseAdapter(): IDatabaseAdapter;
|
|
24
|
-
/**
|
|
25
|
-
* Close all database connections
|
|
26
|
-
*/
|
|
27
|
-
static closeAllDatabases(): Promise<void>;
|
|
28
|
-
/**
|
|
29
|
-
* Delete a database
|
|
30
|
-
*/
|
|
31
|
-
static deleteDatabase(name: string, location?: string): Promise<void>;
|
|
32
12
|
}
|
|
@@ -1,323 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { Platform } from 'react-native';
|
|
2
|
+
import { DatabaseNative } from './services/DatabaseNative';
|
|
3
|
+
import { DatabaseWeb } from './services/DatabaseWeb';
|
|
4
|
+
// Select database implementation based on platform
|
|
5
|
+
const dbImpl = Platform.OS === 'web' ? new DatabaseWeb() : new DatabaseNative();
|
|
5
6
|
/**
|
|
6
|
-
* DatabaseManager - Manages
|
|
7
|
+
* DatabaseManager - Manages database operations across platforms
|
|
8
|
+
* - iOS/Android/Windows: Uses react-native-sqlite-storage
|
|
9
|
+
* - Web: Uses WebSQL
|
|
7
10
|
*/
|
|
8
11
|
export class DatabaseManager {
|
|
9
|
-
/**
|
|
10
|
-
* Get or create a database connection
|
|
11
|
-
*/
|
|
12
|
-
static async getDatabase(name, location) {
|
|
13
|
-
const dbKey = `${name}_${location || this.defaultLocation}`;
|
|
14
|
-
if (this.databases.has(dbKey)) {
|
|
15
|
-
return this.databases.get(dbKey);
|
|
16
|
-
}
|
|
17
|
-
try {
|
|
18
|
-
const dbLocation = (location || this.defaultLocation);
|
|
19
|
-
const db = await SQLite.openDatabase({
|
|
20
|
-
name: name,
|
|
21
|
-
location: dbLocation,
|
|
22
|
-
});
|
|
23
|
-
this.databases.set(dbKey, db);
|
|
24
|
-
return db;
|
|
25
|
-
}
|
|
26
|
-
catch (error) {
|
|
27
|
-
console.error(`Error opening database ${name}:`, error);
|
|
28
|
-
throw error;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Close a database connection
|
|
33
|
-
*/
|
|
34
|
-
static async closeDatabase(name, location) {
|
|
35
|
-
const dbKey = `${name}_${location || this.defaultLocation}`;
|
|
36
|
-
const db = this.databases.get(dbKey);
|
|
37
|
-
if (db) {
|
|
38
|
-
await db.close();
|
|
39
|
-
this.databases.delete(dbKey);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Convert SQLResultSet to array of objects
|
|
44
|
-
*/
|
|
45
|
-
static resultSetToArray(resultSet) {
|
|
46
|
-
const results = [];
|
|
47
|
-
for (let i = 0; i < resultSet.rows.length; i++) {
|
|
48
|
-
results.push(resultSet.rows.item(i));
|
|
49
|
-
}
|
|
50
|
-
return results;
|
|
51
|
-
}
|
|
52
12
|
/**
|
|
53
13
|
* Get the database adapter implementation
|
|
54
14
|
*/
|
|
55
15
|
static getDatabaseAdapter() {
|
|
56
|
-
return
|
|
57
|
-
/**
|
|
58
|
-
* Create a new database
|
|
59
|
-
*/
|
|
60
|
-
create: async (options, successCallback, errorCallback) => {
|
|
61
|
-
try {
|
|
62
|
-
const db = await DatabaseManager.getDatabase(options.name, options.location);
|
|
63
|
-
successCallback({ success: true, database: options.name });
|
|
64
|
-
}
|
|
65
|
-
catch (error) {
|
|
66
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
67
|
-
console.error('Error creating database:', errorMsg);
|
|
68
|
-
errorCallback(errorMsg);
|
|
69
|
-
}
|
|
70
|
-
},
|
|
71
|
-
/**
|
|
72
|
-
* Execute a SQL statement
|
|
73
|
-
*/
|
|
74
|
-
execute: async (options, successCallback, errorCallback) => {
|
|
75
|
-
try {
|
|
76
|
-
const db = await DatabaseManager.getDatabase(options.dbName);
|
|
77
|
-
const results = await new Promise((resolve, reject) => {
|
|
78
|
-
db.transaction((tx) => {
|
|
79
|
-
tx.executeSql(options.query, options.params || [], (_, resultSet) => {
|
|
80
|
-
const data = DatabaseManager.resultSetToArray(resultSet);
|
|
81
|
-
resolve(data);
|
|
82
|
-
}, (_, error) => {
|
|
83
|
-
reject(error);
|
|
84
|
-
return false;
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
successCallback(results);
|
|
89
|
-
}
|
|
90
|
-
catch (error) {
|
|
91
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
92
|
-
console.error('Error executing SQL:', errorMsg);
|
|
93
|
-
errorCallback(errorMsg);
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
/**
|
|
97
|
-
* Execute a SQL statement on a specific database path
|
|
98
|
-
*/
|
|
99
|
-
executeStatementOnPath: async (dbPath, sqlQuery, callback) => {
|
|
100
|
-
var _a;
|
|
101
|
-
try {
|
|
102
|
-
// Extract database name from path
|
|
103
|
-
const dbName = ((_a = dbPath.split('/').pop()) === null || _a === void 0 ? void 0 : _a.replace('.db', '')) || 'default';
|
|
104
|
-
const db = await DatabaseManager.getDatabase(dbName);
|
|
105
|
-
const results = await new Promise((resolve, reject) => {
|
|
106
|
-
db.transaction((tx) => {
|
|
107
|
-
tx.executeSql(sqlQuery, [], (_, resultSet) => {
|
|
108
|
-
const data = DatabaseManager.resultSetToArray(resultSet);
|
|
109
|
-
resolve(data);
|
|
110
|
-
}, (_, error) => {
|
|
111
|
-
console.error('Error in executeStatementOnPath:', error);
|
|
112
|
-
resolve([]);
|
|
113
|
-
return false;
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
callback(results);
|
|
118
|
-
}
|
|
119
|
-
catch (error) {
|
|
120
|
-
console.error('Error executing statement on path:', error);
|
|
121
|
-
callback([]);
|
|
122
|
-
}
|
|
123
|
-
},
|
|
124
|
-
/**
|
|
125
|
-
* Execute a SELECT query on a specific database path
|
|
126
|
-
*/
|
|
127
|
-
selectFromPath: async (dbPath, sqlQuery, callback) => {
|
|
128
|
-
var _a;
|
|
129
|
-
try {
|
|
130
|
-
const dbName = ((_a = dbPath.split('/').pop()) === null || _a === void 0 ? void 0 : _a.replace('.db', '')) || 'default';
|
|
131
|
-
const db = await DatabaseManager.getDatabase(dbName);
|
|
132
|
-
const results = await new Promise((resolve, reject) => {
|
|
133
|
-
db.readTransaction((tx) => {
|
|
134
|
-
tx.executeSql(sqlQuery, [], (_, resultSet) => {
|
|
135
|
-
const data = DatabaseManager.resultSetToArray(resultSet);
|
|
136
|
-
resolve(data);
|
|
137
|
-
}, (_, error) => {
|
|
138
|
-
console.error('Error in selectFromPath:', error);
|
|
139
|
-
resolve([]);
|
|
140
|
-
return false;
|
|
141
|
-
});
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
callback(results);
|
|
145
|
-
}
|
|
146
|
-
catch (error) {
|
|
147
|
-
console.error('Error selecting from path:', error);
|
|
148
|
-
callback([]);
|
|
149
|
-
}
|
|
150
|
-
},
|
|
151
|
-
/**
|
|
152
|
-
* Create a database at a specific path
|
|
153
|
-
*/
|
|
154
|
-
createDatabase: async (dbPath, callback) => {
|
|
155
|
-
var _a;
|
|
156
|
-
try {
|
|
157
|
-
const dbName = ((_a = dbPath.split('/').pop()) === null || _a === void 0 ? void 0 : _a.replace('.db', '')) || 'default';
|
|
158
|
-
await DatabaseManager.getDatabase(dbName);
|
|
159
|
-
callback();
|
|
160
|
-
}
|
|
161
|
-
catch (error) {
|
|
162
|
-
console.error('Error creating database:', error);
|
|
163
|
-
callback();
|
|
164
|
-
}
|
|
165
|
-
},
|
|
166
|
-
/**
|
|
167
|
-
* Get the file path of a database
|
|
168
|
-
*/
|
|
169
|
-
getDBFilePath: async (options, callback) => {
|
|
170
|
-
try {
|
|
171
|
-
// Return the database name with .db extension
|
|
172
|
-
// The actual path is managed by react-native-sqlite-storage based on the location
|
|
173
|
-
const dbPath = `${options.name}.db`;
|
|
174
|
-
callback(dbPath);
|
|
175
|
-
}
|
|
176
|
-
catch (error) {
|
|
177
|
-
console.error('Error getting database file path:', error);
|
|
178
|
-
callback('');
|
|
179
|
-
}
|
|
180
|
-
},
|
|
181
|
-
/**
|
|
182
|
-
* Save web database data
|
|
183
|
-
*/
|
|
184
|
-
saveWebDB: async (options, callback, errorCallback) => {
|
|
185
|
-
try {
|
|
186
|
-
const db = await DatabaseManager.getDatabase(options.dbName);
|
|
187
|
-
// Convert web DB data to SQLite
|
|
188
|
-
// This is a simplified implementation - adjust based on your data structure
|
|
189
|
-
const data = options.data;
|
|
190
|
-
if (Array.isArray(data)) {
|
|
191
|
-
for (const item of data) {
|
|
192
|
-
if (item.query && item.params) {
|
|
193
|
-
await new Promise((resolve, reject) => {
|
|
194
|
-
db.transaction((tx) => {
|
|
195
|
-
tx.executeSql(item.query, item.params, () => resolve(), (_, error) => {
|
|
196
|
-
reject(error);
|
|
197
|
-
return false;
|
|
198
|
-
});
|
|
199
|
-
});
|
|
200
|
-
});
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
callback({ success: true, message: 'Web DB saved successfully' });
|
|
205
|
-
}
|
|
206
|
-
catch (error) {
|
|
207
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
208
|
-
console.error('Error saving web DB:', errorMsg);
|
|
209
|
-
errorCallback(errorMsg);
|
|
210
|
-
}
|
|
211
|
-
},
|
|
212
|
-
/**
|
|
213
|
-
* Export web database
|
|
214
|
-
*/
|
|
215
|
-
exportWebDB: async (options, callback, errorCallback) => {
|
|
216
|
-
try {
|
|
217
|
-
const db = await DatabaseManager.getDatabase(options.dbName);
|
|
218
|
-
// Get all tables
|
|
219
|
-
const tables = await new Promise((resolve, reject) => {
|
|
220
|
-
db.readTransaction((tx) => {
|
|
221
|
-
tx.executeSql("SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'", [], (_, resultSet) => {
|
|
222
|
-
const data = DatabaseManager.resultSetToArray(resultSet);
|
|
223
|
-
resolve(data);
|
|
224
|
-
}, (_, error) => {
|
|
225
|
-
reject(error);
|
|
226
|
-
return false;
|
|
227
|
-
});
|
|
228
|
-
});
|
|
229
|
-
});
|
|
230
|
-
const exportData = {};
|
|
231
|
-
// Export data from each table
|
|
232
|
-
for (const table of tables) {
|
|
233
|
-
const tableName = table.name;
|
|
234
|
-
const tableData = await new Promise((resolve, reject) => {
|
|
235
|
-
db.readTransaction((tx) => {
|
|
236
|
-
tx.executeSql(`SELECT * FROM ${tableName}`, [], (_, resultSet) => {
|
|
237
|
-
const data = DatabaseManager.resultSetToArray(resultSet);
|
|
238
|
-
resolve(data);
|
|
239
|
-
}, (_, error) => {
|
|
240
|
-
reject(error);
|
|
241
|
-
return false;
|
|
242
|
-
});
|
|
243
|
-
});
|
|
244
|
-
});
|
|
245
|
-
exportData[tableName] = tableData;
|
|
246
|
-
}
|
|
247
|
-
callback({
|
|
248
|
-
success: true,
|
|
249
|
-
data: exportData,
|
|
250
|
-
tables: tables.map(t => t.name)
|
|
251
|
-
});
|
|
252
|
-
}
|
|
253
|
-
catch (error) {
|
|
254
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
255
|
-
console.error('Error exporting web DB:', errorMsg);
|
|
256
|
-
errorCallback(errorMsg);
|
|
257
|
-
}
|
|
258
|
-
},
|
|
259
|
-
/**
|
|
260
|
-
* Delete user data from database
|
|
261
|
-
*/
|
|
262
|
-
deleteUserData: async (options, callback, errorCallback) => {
|
|
263
|
-
try {
|
|
264
|
-
const db = await DatabaseManager.getDatabase(options.dbName);
|
|
265
|
-
if (options.userId) {
|
|
266
|
-
// Delete specific user data
|
|
267
|
-
await new Promise((resolve, reject) => {
|
|
268
|
-
db.transaction((tx) => {
|
|
269
|
-
tx.executeSql('DELETE FROM user_data WHERE user_id = ?', [options.userId], () => resolve(), (_, error) => {
|
|
270
|
-
reject(error);
|
|
271
|
-
return false;
|
|
272
|
-
});
|
|
273
|
-
});
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
else {
|
|
277
|
-
// Delete all user data
|
|
278
|
-
await new Promise((resolve, reject) => {
|
|
279
|
-
db.transaction((tx) => {
|
|
280
|
-
tx.executeSql('DELETE FROM user_data', [], () => resolve(), (_, error) => {
|
|
281
|
-
reject(error);
|
|
282
|
-
return false;
|
|
283
|
-
});
|
|
284
|
-
});
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
callback();
|
|
288
|
-
}
|
|
289
|
-
catch (error) {
|
|
290
|
-
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
291
|
-
console.error('Error deleting user data:', errorMsg);
|
|
292
|
-
errorCallback(errorMsg);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
/**
|
|
298
|
-
* Close all database connections
|
|
299
|
-
*/
|
|
300
|
-
static async closeAllDatabases() {
|
|
301
|
-
const closePromises = Array.from(this.databases.values()).map(db => new Promise((resolve) => {
|
|
302
|
-
db.close(() => resolve(), () => resolve());
|
|
303
|
-
}));
|
|
304
|
-
await Promise.all(closePromises);
|
|
305
|
-
this.databases.clear();
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* Delete a database
|
|
309
|
-
*/
|
|
310
|
-
static async deleteDatabase(name, location) {
|
|
311
|
-
try {
|
|
312
|
-
await this.closeDatabase(name, location);
|
|
313
|
-
const dbLocation = (location || this.defaultLocation);
|
|
314
|
-
await SQLite.deleteDatabase({ name, location: dbLocation });
|
|
315
|
-
}
|
|
316
|
-
catch (error) {
|
|
317
|
-
console.error(`Error deleting database ${name}:`, error);
|
|
318
|
-
throw error;
|
|
319
|
-
}
|
|
16
|
+
return dbImpl.getAdapter();
|
|
320
17
|
}
|
|
321
18
|
}
|
|
322
|
-
DatabaseManager.databases = new Map();
|
|
323
|
-
DatabaseManager.defaultLocation = 'default';
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import SQLite from "react-native-sqlite-storage";
|
|
2
|
+
SQLite.enablePromise(true);
|
|
3
|
+
SQLite.DEBUG(false);
|
|
4
|
+
export class DatabaseNative {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.databases = new Map();
|
|
7
|
+
this.defaultLocation = "default";
|
|
8
|
+
}
|
|
9
|
+
async getDatabase(name, location) {
|
|
10
|
+
const dbKey = `${name}_${location || this.defaultLocation}`;
|
|
11
|
+
if (this.databases.has(dbKey)) {
|
|
12
|
+
return this.databases.get(dbKey);
|
|
13
|
+
}
|
|
14
|
+
const dbLocation = (location || this.defaultLocation);
|
|
15
|
+
const db = await SQLite.openDatabase({ name, location: dbLocation });
|
|
16
|
+
this.databases.set(dbKey, db);
|
|
17
|
+
return db;
|
|
18
|
+
}
|
|
19
|
+
resultSetToArray(resultSet) {
|
|
20
|
+
const results = [];
|
|
21
|
+
for (let i = 0; i < resultSet.rows.length; i++) {
|
|
22
|
+
results.push(resultSet.rows.item(i));
|
|
23
|
+
}
|
|
24
|
+
return results;
|
|
25
|
+
}
|
|
26
|
+
getAdapter() {
|
|
27
|
+
return {
|
|
28
|
+
create: async (options, successCallback, errorCallback) => {
|
|
29
|
+
try {
|
|
30
|
+
await this.getDatabase(options.name, options.location);
|
|
31
|
+
successCallback({ success: true, database: options.name });
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
errorCallback(error instanceof Error ? error.message : String(error));
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
execute: async (options, successCallback, errorCallback) => {
|
|
38
|
+
try {
|
|
39
|
+
const db = await this.getDatabase(options.dbName);
|
|
40
|
+
const results = await new Promise((resolve, reject) => {
|
|
41
|
+
db.transaction((tx) => {
|
|
42
|
+
tx.executeSql(options.query, options.params || [], (_, resultSet) => {
|
|
43
|
+
resolve(this.resultSetToArray(resultSet));
|
|
44
|
+
}, (_, error) => {
|
|
45
|
+
reject(error);
|
|
46
|
+
return false;
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
successCallback(results);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
errorCallback(error instanceof Error ? error.message : String(error));
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
executeStatementOnPath: async (dbPath, sqlQuery, callback) => {
|
|
57
|
+
var _a;
|
|
58
|
+
try {
|
|
59
|
+
const dbName = ((_a = dbPath.split("/").pop()) === null || _a === void 0 ? void 0 : _a.replace(".db", "")) || "default";
|
|
60
|
+
const db = await this.getDatabase(dbName);
|
|
61
|
+
const results = await new Promise((resolve) => {
|
|
62
|
+
db.transaction((tx) => {
|
|
63
|
+
tx.executeSql(sqlQuery, [], (_, resultSet) => {
|
|
64
|
+
resolve(this.resultSetToArray(resultSet));
|
|
65
|
+
}, () => {
|
|
66
|
+
resolve([]);
|
|
67
|
+
return false;
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
callback(results);
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
callback([]);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
selectFromPath: async (dbPath, sqlQuery, callback) => {
|
|
78
|
+
var _a;
|
|
79
|
+
try {
|
|
80
|
+
const dbName = ((_a = dbPath.split("/").pop()) === null || _a === void 0 ? void 0 : _a.replace(".db", "")) || "default";
|
|
81
|
+
const db = await this.getDatabase(dbName);
|
|
82
|
+
const results = await new Promise((resolve) => {
|
|
83
|
+
db.readTransaction((tx) => {
|
|
84
|
+
tx.executeSql(sqlQuery, [], (_, resultSet) => {
|
|
85
|
+
resolve(this.resultSetToArray(resultSet));
|
|
86
|
+
}, () => {
|
|
87
|
+
resolve([]);
|
|
88
|
+
return false;
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
callback(results);
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
callback([]);
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
createDatabase: async (dbPath, callback) => {
|
|
99
|
+
var _a;
|
|
100
|
+
try {
|
|
101
|
+
const dbName = ((_a = dbPath.split("/").pop()) === null || _a === void 0 ? void 0 : _a.replace(".db", "")) || "default";
|
|
102
|
+
await this.getDatabase(dbName);
|
|
103
|
+
callback();
|
|
104
|
+
}
|
|
105
|
+
catch (error) {
|
|
106
|
+
callback();
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
getDBFilePath: async (options, callback) => {
|
|
110
|
+
callback(`${options.name}.db`);
|
|
111
|
+
},
|
|
112
|
+
saveWebDB: async (options, callback, errorCallback) => {
|
|
113
|
+
// Not needed for native - only for web
|
|
114
|
+
callback({
|
|
115
|
+
success: true,
|
|
116
|
+
message: "Not applicable for native",
|
|
117
|
+
});
|
|
118
|
+
},
|
|
119
|
+
exportWebDB: async (options, callback, errorCallback) => {
|
|
120
|
+
// Not needed for native - only for web
|
|
121
|
+
callback({
|
|
122
|
+
success: true,
|
|
123
|
+
message: "Not applicable for native",
|
|
124
|
+
});
|
|
125
|
+
},
|
|
126
|
+
deleteUserData: async (options, callback, errorCallback) => {
|
|
127
|
+
try {
|
|
128
|
+
const db = await this.getDatabase(options.dbName);
|
|
129
|
+
await new Promise((resolve, reject) => {
|
|
130
|
+
db.transaction((tx) => {
|
|
131
|
+
const query = options.userId
|
|
132
|
+
? "DELETE FROM user_data WHERE user_id = ?"
|
|
133
|
+
: "DELETE FROM user_data";
|
|
134
|
+
const params = options.userId ? [options.userId] : [];
|
|
135
|
+
tx.executeSql(query, params, () => resolve(), (_, error) => {
|
|
136
|
+
reject(error);
|
|
137
|
+
return false;
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
callback();
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
errorCallback(error instanceof Error ? error.message : String(error));
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { IDatabaseAdapter } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* DatabaseWeb - Web database implementation using sql.js
|
|
4
|
+
* Uses SQLite compiled to WebAssembly for full SQLite compatibility in browsers
|
|
5
|
+
*/
|
|
6
|
+
export declare class DatabaseWeb {
|
|
7
|
+
private databases;
|
|
8
|
+
private SQL;
|
|
9
|
+
private initSQL;
|
|
10
|
+
private getDatabase;
|
|
11
|
+
private executeQuery;
|
|
12
|
+
getAdapter(): IDatabaseAdapter;
|
|
13
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import initSqlJs from 'sql.js';
|
|
2
|
+
/**
|
|
3
|
+
* DatabaseWeb - Web database implementation using sql.js
|
|
4
|
+
* Uses SQLite compiled to WebAssembly for full SQLite compatibility in browsers
|
|
5
|
+
*/
|
|
6
|
+
export class DatabaseWeb {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.databases = new Map();
|
|
9
|
+
this.SQL = null;
|
|
10
|
+
}
|
|
11
|
+
async initSQL() {
|
|
12
|
+
if (!this.SQL) {
|
|
13
|
+
this.SQL = await initSqlJs({
|
|
14
|
+
locateFile: (file) => `https://sql.js.org/dist/${file}`
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return this.SQL;
|
|
18
|
+
}
|
|
19
|
+
async getDatabase(name) {
|
|
20
|
+
if (this.databases.has(name)) {
|
|
21
|
+
return this.databases.get(name);
|
|
22
|
+
}
|
|
23
|
+
const SQL = await this.initSQL();
|
|
24
|
+
const db = new SQL.Database();
|
|
25
|
+
this.databases.set(name, db);
|
|
26
|
+
return db;
|
|
27
|
+
}
|
|
28
|
+
executeQuery(db, query, params = []) {
|
|
29
|
+
const results = [];
|
|
30
|
+
const stmt = db.prepare(query);
|
|
31
|
+
stmt.bind(params);
|
|
32
|
+
while (stmt.step()) {
|
|
33
|
+
const row = stmt.getAsObject();
|
|
34
|
+
results.push(row);
|
|
35
|
+
}
|
|
36
|
+
stmt.free();
|
|
37
|
+
return results;
|
|
38
|
+
}
|
|
39
|
+
getAdapter() {
|
|
40
|
+
return {
|
|
41
|
+
create: async (options, successCallback, errorCallback) => {
|
|
42
|
+
try {
|
|
43
|
+
await this.getDatabase(options.name);
|
|
44
|
+
successCallback({ success: true, database: options.name });
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
errorCallback(error instanceof Error ? error.message : String(error));
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
execute: async (options, successCallback, errorCallback) => {
|
|
51
|
+
try {
|
|
52
|
+
const db = await this.getDatabase(options.dbName);
|
|
53
|
+
const results = this.executeQuery(db, options.query, options.params);
|
|
54
|
+
successCallback(results);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
errorCallback(error instanceof Error ? error.message : String(error));
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
executeStatementOnPath: async (dbPath, sqlQuery, callback) => {
|
|
61
|
+
var _a;
|
|
62
|
+
try {
|
|
63
|
+
const dbName = ((_a = dbPath.split('/').pop()) === null || _a === void 0 ? void 0 : _a.replace('.db', '')) || 'default';
|
|
64
|
+
const db = await this.getDatabase(dbName);
|
|
65
|
+
const results = this.executeQuery(db, sqlQuery);
|
|
66
|
+
callback(results);
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
callback([]);
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
selectFromPath: async (dbPath, sqlQuery, callback) => {
|
|
73
|
+
var _a;
|
|
74
|
+
try {
|
|
75
|
+
const dbName = ((_a = dbPath.split('/').pop()) === null || _a === void 0 ? void 0 : _a.replace('.db', '')) || 'default';
|
|
76
|
+
const db = await this.getDatabase(dbName);
|
|
77
|
+
const results = this.executeQuery(db, sqlQuery);
|
|
78
|
+
callback(results);
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
callback([]);
|
|
82
|
+
}
|
|
83
|
+
},
|
|
84
|
+
createDatabase: async (dbPath, callback) => {
|
|
85
|
+
var _a;
|
|
86
|
+
try {
|
|
87
|
+
const dbName = ((_a = dbPath.split('/').pop()) === null || _a === void 0 ? void 0 : _a.replace('.db', '')) || 'default';
|
|
88
|
+
await this.getDatabase(dbName);
|
|
89
|
+
callback();
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
callback();
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
getDBFilePath: async (options, callback) => {
|
|
96
|
+
callback(`sqljs://${options.name}`);
|
|
97
|
+
},
|
|
98
|
+
saveWebDB: async (options, callback, errorCallback) => {
|
|
99
|
+
try {
|
|
100
|
+
const db = await this.getDatabase(options.dbName);
|
|
101
|
+
if (Array.isArray(options.data)) {
|
|
102
|
+
for (const item of options.data) {
|
|
103
|
+
if (item.query && item.params) {
|
|
104
|
+
db.run(item.query, item.params);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
callback({ success: true });
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
errorCallback(error instanceof Error ? error.message : String(error));
|
|
112
|
+
}
|
|
113
|
+
},
|
|
114
|
+
exportWebDB: async (options, callback, errorCallback) => {
|
|
115
|
+
try {
|
|
116
|
+
const db = await this.getDatabase(options.dbName);
|
|
117
|
+
const tables = this.executeQuery(db, "SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'");
|
|
118
|
+
callback({ success: true, tables: tables.map(t => t.name) });
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
errorCallback(error instanceof Error ? error.message : String(error));
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
deleteUserData: async (options, callback, errorCallback) => {
|
|
125
|
+
try {
|
|
126
|
+
const db = await this.getDatabase(options.dbName);
|
|
127
|
+
const query = options.userId ? 'DELETE FROM user_data WHERE user_id = ?' : 'DELETE FROM user_data';
|
|
128
|
+
const params = options.userId ? [options.userId] : [];
|
|
129
|
+
db.run(query, params);
|
|
130
|
+
callback();
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
errorCallback(error instanceof Error ? error.message : String(error));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
}
|