@unvired/react-native-unvired-sdk 0.0.12 → 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/README.md +884 -255
- package/dist/database/Database.d.ts +71 -0
- package/dist/database/Database.js +156 -0
- package/dist/database/DatabaseManager.d.ts +12 -0
- package/dist/database/DatabaseManager.js +18 -0
- package/dist/database/index.d.ts +6 -0
- package/dist/database/index.js +5 -0
- 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/database/types.d.ts +94 -0
- package/dist/database/types.js +4 -0
- package/dist/device-info/BaseDeviceInfo.d.ts +39 -0
- package/dist/device-info/BaseDeviceInfo.js +116 -0
- package/dist/device-info/DeviceInfo.d.ts +9 -0
- package/dist/device-info/DeviceInfo.js +11 -0
- package/dist/device-info/DeviceInfo.types.d.ts +11 -0
- package/dist/device-info/DeviceInfo.types.js +3 -0
- package/dist/device-info/index.d.ts +3 -0
- package/dist/device-info/index.js +3 -0
- package/dist/file-system/BaseFileSystem.d.ts +61 -0
- package/dist/file-system/BaseFileSystem.js +164 -0
- package/dist/file-system/FileSystem.d.ts +9 -0
- package/dist/file-system/FileSystem.js +11 -0
- package/dist/file-system/FileSystem.types.d.ts +11 -0
- package/dist/file-system/FileSystem.types.js +3 -0
- package/dist/file-system/index.d.ts +3 -0
- package/dist/file-system/index.js +3 -0
- 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/index.d.ts +1 -0
- package/dist/local-storage/index.js +2 -0
- package/dist/local-storage/localStorage.d.ts +49 -0
- package/dist/local-storage/localStorage.js +128 -0
- 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/logger/{Logger.js → index.js} +1 -4
- package/dist/main.d.ts +11 -0
- package/dist/main.js +24 -0
- package/dist/push-notification/BasePushNotification.d.ts +52 -0
- package/dist/push-notification/BasePushNotification.js +180 -0
- package/dist/push-notification/PushNotification.d.ts +9 -0
- package/dist/push-notification/PushNotification.js +11 -0
- package/dist/push-notification/PushNotification.types.d.ts +12 -0
- package/dist/push-notification/PushNotification.types.js +3 -0
- package/dist/push-notification/index.d.ts +3 -0
- package/dist/push-notification/index.js +3 -0
- package/example/USAGE_EXAMPLE.ts +167 -0
- package/package.json +17 -6
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -4
- /package/dist/logger/{Logger.d.ts → index.d.ts} +0 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database helper class with common database operations
|
|
3
|
+
*/
|
|
4
|
+
export declare class Database {
|
|
5
|
+
private dbName;
|
|
6
|
+
private location?;
|
|
7
|
+
constructor(dbName: string, location?: string);
|
|
8
|
+
/**
|
|
9
|
+
* Execute a raw SQL query
|
|
10
|
+
*/
|
|
11
|
+
executeSql(query: string, params?: any[]): Promise<any[]>;
|
|
12
|
+
/**
|
|
13
|
+
* Create a table
|
|
14
|
+
*/
|
|
15
|
+
createTable(tableName: string, columns: Record<string, string>): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Drop a table
|
|
18
|
+
*/
|
|
19
|
+
dropTable(tableName: string): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Insert a record
|
|
22
|
+
*/
|
|
23
|
+
insert(tableName: string, data: Record<string, any>): Promise<number>;
|
|
24
|
+
/**
|
|
25
|
+
* Insert multiple records in a transaction
|
|
26
|
+
*/
|
|
27
|
+
insertBatch(tableName: string, records: Record<string, any>[]): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Update records
|
|
30
|
+
*/
|
|
31
|
+
update(tableName: string, data: Record<string, any>, where: string, whereParams?: any[]): Promise<number>;
|
|
32
|
+
/**
|
|
33
|
+
* Delete records
|
|
34
|
+
*/
|
|
35
|
+
delete(tableName: string, where: string, whereParams?: any[]): Promise<number>;
|
|
36
|
+
/**
|
|
37
|
+
* Select records
|
|
38
|
+
*/
|
|
39
|
+
select(tableName: string, columns?: string[], where?: string, whereParams?: any[], orderBy?: string, limit?: number): Promise<any[]>;
|
|
40
|
+
/**
|
|
41
|
+
* Select a single record
|
|
42
|
+
*/
|
|
43
|
+
selectOne(tableName: string, columns?: string[], where?: string, whereParams?: any[]): Promise<any | null>;
|
|
44
|
+
/**
|
|
45
|
+
* Count records
|
|
46
|
+
*/
|
|
47
|
+
count(tableName: string, where?: string, whereParams?: any[]): Promise<number>;
|
|
48
|
+
/**
|
|
49
|
+
* Check if a table exists
|
|
50
|
+
*/
|
|
51
|
+
tableExists(tableName: string): Promise<boolean>;
|
|
52
|
+
/**
|
|
53
|
+
* Get all table names
|
|
54
|
+
*/
|
|
55
|
+
getAllTables(): Promise<string[]>;
|
|
56
|
+
/**
|
|
57
|
+
* Clear all data from a table
|
|
58
|
+
*/
|
|
59
|
+
truncate(tableName: string): Promise<void>;
|
|
60
|
+
/**
|
|
61
|
+
* Execute multiple SQL statements in a transaction
|
|
62
|
+
*/
|
|
63
|
+
transaction(queries: Array<{
|
|
64
|
+
query: string;
|
|
65
|
+
params?: any[];
|
|
66
|
+
}>): Promise<void>;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Create a new database instance
|
|
70
|
+
*/
|
|
71
|
+
export declare function createDatabase(dbName: string, location?: string): Database;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { DatabaseManager } from './DatabaseManager';
|
|
2
|
+
/**
|
|
3
|
+
* Database helper class with common database operations
|
|
4
|
+
*/
|
|
5
|
+
export class Database {
|
|
6
|
+
constructor(dbName, location) {
|
|
7
|
+
this.dbName = dbName;
|
|
8
|
+
this.location = location;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Execute a raw SQL query
|
|
12
|
+
*/
|
|
13
|
+
async executeSql(query, params = []) {
|
|
14
|
+
return new Promise((resolve, reject) => {
|
|
15
|
+
const adapter = DatabaseManager.getDatabaseAdapter();
|
|
16
|
+
adapter.execute({ dbName: this.dbName, query, params }, (results) => resolve(results), (error) => reject(new Error(String(error))));
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create a table
|
|
21
|
+
*/
|
|
22
|
+
async createTable(tableName, columns) {
|
|
23
|
+
const columnDefs = Object.entries(columns)
|
|
24
|
+
.map(([name, type]) => `${name} ${type}`)
|
|
25
|
+
.join(', ');
|
|
26
|
+
const query = `CREATE TABLE IF NOT EXISTS ${tableName} (${columnDefs})`;
|
|
27
|
+
await this.executeSql(query);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Drop a table
|
|
31
|
+
*/
|
|
32
|
+
async dropTable(tableName) {
|
|
33
|
+
const query = `DROP TABLE IF EXISTS ${tableName}`;
|
|
34
|
+
await this.executeSql(query);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Insert a record
|
|
38
|
+
*/
|
|
39
|
+
async insert(tableName, data) {
|
|
40
|
+
var _a;
|
|
41
|
+
const columns = Object.keys(data).join(', ');
|
|
42
|
+
const placeholders = Object.keys(data).map(() => '?').join(', ');
|
|
43
|
+
const values = Object.values(data);
|
|
44
|
+
const query = `INSERT INTO ${tableName} (${columns}) VALUES (${placeholders})`;
|
|
45
|
+
const result = await this.executeSql(query, values);
|
|
46
|
+
// Return the insert ID if available
|
|
47
|
+
return ((_a = result[0]) === null || _a === void 0 ? void 0 : _a.insertId) || 0;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Insert multiple records in a transaction
|
|
51
|
+
*/
|
|
52
|
+
async insertBatch(tableName, records) {
|
|
53
|
+
if (records.length === 0)
|
|
54
|
+
return;
|
|
55
|
+
const columns = Object.keys(records[0]).join(', ');
|
|
56
|
+
const placeholders = Object.keys(records[0]).map(() => '?').join(', ');
|
|
57
|
+
const query = `INSERT INTO ${tableName} (${columns}) VALUES (${placeholders})`;
|
|
58
|
+
for (const record of records) {
|
|
59
|
+
const values = Object.values(record);
|
|
60
|
+
await this.executeSql(query, values);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Update records
|
|
65
|
+
*/
|
|
66
|
+
async update(tableName, data, where, whereParams = []) {
|
|
67
|
+
var _a;
|
|
68
|
+
const setClause = Object.keys(data)
|
|
69
|
+
.map(key => `${key} = ?`)
|
|
70
|
+
.join(', ');
|
|
71
|
+
const values = [...Object.values(data), ...whereParams];
|
|
72
|
+
const query = `UPDATE ${tableName} SET ${setClause} WHERE ${where}`;
|
|
73
|
+
const result = await this.executeSql(query, values);
|
|
74
|
+
return ((_a = result[0]) === null || _a === void 0 ? void 0 : _a.rowsAffected) || 0;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Delete records
|
|
78
|
+
*/
|
|
79
|
+
async delete(tableName, where, whereParams = []) {
|
|
80
|
+
var _a;
|
|
81
|
+
const query = `DELETE FROM ${tableName} WHERE ${where}`;
|
|
82
|
+
const result = await this.executeSql(query, whereParams);
|
|
83
|
+
return ((_a = result[0]) === null || _a === void 0 ? void 0 : _a.rowsAffected) || 0;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Select records
|
|
87
|
+
*/
|
|
88
|
+
async select(tableName, columns = ['*'], where, whereParams = [], orderBy, limit) {
|
|
89
|
+
let query = `SELECT ${columns.join(', ')} FROM ${tableName}`;
|
|
90
|
+
if (where) {
|
|
91
|
+
query += ` WHERE ${where}`;
|
|
92
|
+
}
|
|
93
|
+
if (orderBy) {
|
|
94
|
+
query += ` ORDER BY ${orderBy}`;
|
|
95
|
+
}
|
|
96
|
+
if (limit) {
|
|
97
|
+
query += ` LIMIT ${limit}`;
|
|
98
|
+
}
|
|
99
|
+
return this.executeSql(query, whereParams);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Select a single record
|
|
103
|
+
*/
|
|
104
|
+
async selectOne(tableName, columns = ['*'], where, whereParams = []) {
|
|
105
|
+
const results = await this.select(tableName, columns, where, whereParams, undefined, 1);
|
|
106
|
+
return results.length > 0 ? results[0] : null;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Count records
|
|
110
|
+
*/
|
|
111
|
+
async count(tableName, where, whereParams = []) {
|
|
112
|
+
var _a;
|
|
113
|
+
let query = `SELECT COUNT(*) as count FROM ${tableName}`;
|
|
114
|
+
if (where) {
|
|
115
|
+
query += ` WHERE ${where}`;
|
|
116
|
+
}
|
|
117
|
+
const result = await this.executeSql(query, whereParams);
|
|
118
|
+
return ((_a = result[0]) === null || _a === void 0 ? void 0 : _a.count) || 0;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Check if a table exists
|
|
122
|
+
*/
|
|
123
|
+
async tableExists(tableName) {
|
|
124
|
+
const query = `SELECT name FROM sqlite_master WHERE type='table' AND name=?`;
|
|
125
|
+
const result = await this.executeSql(query, [tableName]);
|
|
126
|
+
return result.length > 0;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Get all table names
|
|
130
|
+
*/
|
|
131
|
+
async getAllTables() {
|
|
132
|
+
const query = `SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%'`;
|
|
133
|
+
const result = await this.executeSql(query);
|
|
134
|
+
return result.map(row => row.name);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Clear all data from a table
|
|
138
|
+
*/
|
|
139
|
+
async truncate(tableName) {
|
|
140
|
+
await this.executeSql(`DELETE FROM ${tableName}`);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Execute multiple SQL statements in a transaction
|
|
144
|
+
*/
|
|
145
|
+
async transaction(queries) {
|
|
146
|
+
for (const { query, params } of queries) {
|
|
147
|
+
await this.executeSql(query, params || []);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Create a new database instance
|
|
153
|
+
*/
|
|
154
|
+
export function createDatabase(dbName, location) {
|
|
155
|
+
return new Database(dbName, location);
|
|
156
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IDatabaseAdapter } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* DatabaseManager - Manages database operations across platforms
|
|
4
|
+
* - iOS/Android/Windows: Uses react-native-sqlite-storage
|
|
5
|
+
* - Web: Uses WebSQL
|
|
6
|
+
*/
|
|
7
|
+
export declare class DatabaseManager {
|
|
8
|
+
/**
|
|
9
|
+
* Get the database adapter implementation
|
|
10
|
+
*/
|
|
11
|
+
static getDatabaseAdapter(): IDatabaseAdapter;
|
|
12
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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();
|
|
6
|
+
/**
|
|
7
|
+
* DatabaseManager - Manages database operations across platforms
|
|
8
|
+
* - iOS/Android/Windows: Uses react-native-sqlite-storage
|
|
9
|
+
* - Web: Uses WebSQL
|
|
10
|
+
*/
|
|
11
|
+
export class DatabaseManager {
|
|
12
|
+
/**
|
|
13
|
+
* Get the database adapter implementation
|
|
14
|
+
*/
|
|
15
|
+
static getDatabaseAdapter() {
|
|
16
|
+
return dbImpl.getAdapter();
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database module exports
|
|
3
|
+
*/
|
|
4
|
+
export { DatabaseManager } from './DatabaseManager';
|
|
5
|
+
export { Database, createDatabase } from './Database';
|
|
6
|
+
export type { IDatabaseAdapter, DatabaseOptions, ExecuteOptions, SaveWebDBOptions, ExportWebDBOptions, DeleteUserDataOptions, SuccessCallback, ErrorCallback, ResultCallback, SQLResultSet, SQLTransaction, SQLiteDatabase } from './types';
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Database types and interfaces for the Unvired SDK
|
|
3
|
+
*/
|
|
4
|
+
export interface DatabaseOptions {
|
|
5
|
+
name: string;
|
|
6
|
+
location?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface ExecuteOptions {
|
|
9
|
+
dbName: string;
|
|
10
|
+
query: string;
|
|
11
|
+
params?: any[];
|
|
12
|
+
}
|
|
13
|
+
export interface SaveWebDBOptions {
|
|
14
|
+
dbName: string;
|
|
15
|
+
data: any;
|
|
16
|
+
}
|
|
17
|
+
export interface ExportWebDBOptions {
|
|
18
|
+
dbName: string;
|
|
19
|
+
}
|
|
20
|
+
export interface DeleteUserDataOptions {
|
|
21
|
+
dbName: string;
|
|
22
|
+
userId?: string;
|
|
23
|
+
}
|
|
24
|
+
export type SuccessCallback<T = any> = (result: T) => void;
|
|
25
|
+
export type ErrorCallback = (error: Error | string) => void;
|
|
26
|
+
export type ResultCallback<T = any> = (result: T) => void;
|
|
27
|
+
/**
|
|
28
|
+
* Database adapter interface
|
|
29
|
+
*/
|
|
30
|
+
export interface IDatabaseAdapter {
|
|
31
|
+
/**
|
|
32
|
+
* Create a new database
|
|
33
|
+
*/
|
|
34
|
+
create(options: DatabaseOptions, successCallback: SuccessCallback, errorCallback: ErrorCallback): void;
|
|
35
|
+
/**
|
|
36
|
+
* Execute a SQL statement
|
|
37
|
+
*/
|
|
38
|
+
execute(options: ExecuteOptions, successCallback: SuccessCallback<any[]>, errorCallback: ErrorCallback): void;
|
|
39
|
+
/**
|
|
40
|
+
* Execute a SQL statement on a specific database path
|
|
41
|
+
*/
|
|
42
|
+
executeStatementOnPath(dbPath: string, sqlQuery: string, callback: ResultCallback<any[]>): void;
|
|
43
|
+
/**
|
|
44
|
+
* Execute a SELECT query on a specific database path
|
|
45
|
+
*/
|
|
46
|
+
selectFromPath(dbPath: string, sqlQuery: string, callback: ResultCallback<any[]>): void;
|
|
47
|
+
/**
|
|
48
|
+
* Create a database at a specific path
|
|
49
|
+
*/
|
|
50
|
+
createDatabase(dbPath: string, callback: ResultCallback<void>): void;
|
|
51
|
+
/**
|
|
52
|
+
* Get the file path of a database
|
|
53
|
+
*/
|
|
54
|
+
getDBFilePath(options: DatabaseOptions, callback: ResultCallback<string>): void;
|
|
55
|
+
/**
|
|
56
|
+
* Save web database data
|
|
57
|
+
*/
|
|
58
|
+
saveWebDB(options: SaveWebDBOptions, callback: ResultCallback<any>, errorCallback: ErrorCallback): void;
|
|
59
|
+
/**
|
|
60
|
+
* Export web database
|
|
61
|
+
*/
|
|
62
|
+
exportWebDB(options: ExportWebDBOptions, callback: ResultCallback<any>, errorCallback: ErrorCallback): void;
|
|
63
|
+
/**
|
|
64
|
+
* Delete user data from database
|
|
65
|
+
*/
|
|
66
|
+
deleteUserData(options: DeleteUserDataOptions, callback: ResultCallback<void>, errorCallback: ErrorCallback): void;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* SQL result set interface
|
|
70
|
+
*/
|
|
71
|
+
export interface SQLResultSet {
|
|
72
|
+
insertId?: number;
|
|
73
|
+
rowsAffected: number;
|
|
74
|
+
rows: {
|
|
75
|
+
length: number;
|
|
76
|
+
item: (index: number) => any;
|
|
77
|
+
raw: () => any[];
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Transaction interface
|
|
82
|
+
*/
|
|
83
|
+
export interface SQLTransaction {
|
|
84
|
+
executeSql(sqlStatement: string, args?: any[], callback?: (transaction: SQLTransaction, resultSet: SQLResultSet) => void, errorCallback?: (transaction: SQLTransaction, error: Error) => boolean): void;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Database connection interface
|
|
88
|
+
*/
|
|
89
|
+
export interface SQLiteDatabase {
|
|
90
|
+
transaction(callback: (transaction: SQLTransaction) => void, errorCallback?: (error: Error) => void, successCallback?: () => void): void;
|
|
91
|
+
readTransaction(callback: (transaction: SQLTransaction) => void, errorCallback?: (error: Error) => void, successCallback?: () => void): void;
|
|
92
|
+
executeSql(statement: string, params?: any[], success?: (results: any) => void, error?: (error: Error) => void): void;
|
|
93
|
+
close(success?: () => void, error?: (error: Error) => void): void;
|
|
94
|
+
}
|