@unvired/react-native-unvired-sdk 0.0.22 → 0.0.23
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/Database.d.ts +2 -53
- package/dist/database/Database.js +6 -128
- package/dist/database/services/DatabaseNative.js +83 -22
- package/package.json +1 -1
package/BuildNo.txt
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
R-0.000.
|
|
1
|
+
R-0.000.0023
|
|
@@ -10,60 +10,9 @@ export declare class Database {
|
|
|
10
10
|
*/
|
|
11
11
|
executeSql(query: string, params?: any[]): Promise<any[]>;
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* Delete a database
|
|
14
14
|
*/
|
|
15
|
-
|
|
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>;
|
|
15
|
+
deleteDB(): Promise<void>;
|
|
67
16
|
}
|
|
68
17
|
/**
|
|
69
18
|
* Create a new database instance
|
|
@@ -17,135 +17,13 @@ export class Database {
|
|
|
17
17
|
});
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
|
-
*
|
|
20
|
+
* Delete a database
|
|
21
21
|
*/
|
|
22
|
-
async
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
.
|
|
26
|
-
|
|
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
|
-
}
|
|
22
|
+
async deleteDB() {
|
|
23
|
+
return new Promise((resolve, reject) => {
|
|
24
|
+
const adapter = DatabaseManager.getDatabaseAdapter();
|
|
25
|
+
adapter.deleteUserData({ dbName: this.dbName }, () => resolve(), (error) => reject(new Error(String(error))));
|
|
26
|
+
});
|
|
149
27
|
}
|
|
150
28
|
}
|
|
151
29
|
/**
|
|
@@ -9,10 +9,9 @@ export class DatabaseNative {
|
|
|
9
9
|
if (this.databases.has(dbKey)) {
|
|
10
10
|
return this.databases.get(dbKey);
|
|
11
11
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
// For now, we rely on the name.
|
|
12
|
+
if (!name) {
|
|
13
|
+
throw new Error("Database name is required");
|
|
14
|
+
}
|
|
16
15
|
const db = open({ name });
|
|
17
16
|
this.databases.set(dbKey, db);
|
|
18
17
|
return db;
|
|
@@ -29,43 +28,105 @@ export class DatabaseNative {
|
|
|
29
28
|
}
|
|
30
29
|
},
|
|
31
30
|
execute: async (options, successCallback, errorCallback) => {
|
|
32
|
-
var _a;
|
|
33
31
|
try {
|
|
34
32
|
const db = this.getDatabase(options.dbName);
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
//
|
|
39
|
-
const
|
|
40
|
-
|
|
33
|
+
// FIX: Await the result properly
|
|
34
|
+
const res = await db.execute(options.query, options.params);
|
|
35
|
+
let resultToReturn = [];
|
|
36
|
+
// Cast to any to avoid TS error "Property '_array' does not exist on type 'never'"
|
|
37
|
+
const rawRes = res;
|
|
38
|
+
if (rawRes) {
|
|
39
|
+
if (rawRes.rows) {
|
|
40
|
+
const rawRows = rawRes.rows;
|
|
41
|
+
if (Array.isArray(rawRows)) {
|
|
42
|
+
resultToReturn = rawRows;
|
|
43
|
+
}
|
|
44
|
+
else if (rawRows._array && Array.isArray(rawRows._array)) {
|
|
45
|
+
resultToReturn = rawRows._array;
|
|
46
|
+
}
|
|
47
|
+
else if (typeof rawRows.length === 'number') {
|
|
48
|
+
// JSI iterator
|
|
49
|
+
for (let i = 0; i < rawRows.length; i++) {
|
|
50
|
+
// Use item() if available, otherwise index access
|
|
51
|
+
const r = rawRows.item ? rawRows.item(i) : rawRows[i];
|
|
52
|
+
resultToReturn.push(r);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
else if (Array.isArray(rawRes)) {
|
|
57
|
+
resultToReturn = rawRes;
|
|
58
|
+
}
|
|
59
|
+
// Attach insertId if present (though interface usually expects strict array,
|
|
60
|
+
// JS runtime allows attaching props to array)
|
|
61
|
+
if (rawRes.insertId !== undefined) {
|
|
62
|
+
resultToReturn.insertId = rawRes.insertId;
|
|
63
|
+
resultToReturn.rowsAffected = rawRes.rowsAffected;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
successCallback(resultToReturn);
|
|
41
67
|
}
|
|
42
68
|
catch (error) {
|
|
43
69
|
errorCallback(error instanceof Error ? error.message : String(error));
|
|
44
70
|
}
|
|
45
71
|
},
|
|
46
72
|
executeStatementOnPath: async (dbPath, sqlQuery, callback) => {
|
|
47
|
-
var _a
|
|
73
|
+
var _a;
|
|
48
74
|
try {
|
|
49
75
|
const dbName = ((_a = dbPath.split("/").pop()) === null || _a === void 0 ? void 0 : _a.replace(".db", "")) || "default";
|
|
50
76
|
const db = this.getDatabase(dbName);
|
|
51
|
-
const res = db.execute(sqlQuery);
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
|
|
77
|
+
const res = await db.execute(sqlQuery);
|
|
78
|
+
let resultToReturn = [];
|
|
79
|
+
const rawRes = res;
|
|
80
|
+
if (rawRes && rawRes.rows) {
|
|
81
|
+
const rawRows = rawRes.rows;
|
|
82
|
+
if (typeof rawRows.length === 'number') {
|
|
83
|
+
for (let i = 0; i < rawRows.length; i++) {
|
|
84
|
+
const r = rawRows.item ? rawRows.item(i) : rawRows[i];
|
|
85
|
+
resultToReturn.push(r);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
else if (Array.isArray(rawRows)) {
|
|
89
|
+
resultToReturn = rawRows;
|
|
90
|
+
}
|
|
91
|
+
else if (rawRows._array) {
|
|
92
|
+
resultToReturn = rawRows._array;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Attach insertId if present
|
|
96
|
+
if (rawRes && rawRes.insertId !== undefined) {
|
|
97
|
+
resultToReturn.insertId = rawRes.insertId;
|
|
98
|
+
resultToReturn.rowsAffected = rawRes.rowsAffected;
|
|
99
|
+
}
|
|
100
|
+
callback(resultToReturn);
|
|
55
101
|
}
|
|
56
102
|
catch (error) {
|
|
57
103
|
callback([]);
|
|
58
104
|
}
|
|
59
105
|
},
|
|
60
106
|
selectFromPath: async (dbPath, sqlQuery, callback) => {
|
|
61
|
-
var _a
|
|
107
|
+
var _a;
|
|
62
108
|
try {
|
|
63
109
|
const dbName = ((_a = dbPath.split("/").pop()) === null || _a === void 0 ? void 0 : _a.replace(".db", "")) || "default";
|
|
64
110
|
const db = this.getDatabase(dbName);
|
|
65
|
-
const res = db.execute(sqlQuery);
|
|
66
|
-
|
|
67
|
-
const
|
|
68
|
-
|
|
111
|
+
const res = await db.execute(sqlQuery);
|
|
112
|
+
let resultToReturn = [];
|
|
113
|
+
const rawRes = res;
|
|
114
|
+
if (rawRes && rawRes.rows) {
|
|
115
|
+
const rawRows = rawRes.rows;
|
|
116
|
+
if (typeof rawRows.length === 'number') {
|
|
117
|
+
for (let i = 0; i < rawRows.length; i++) {
|
|
118
|
+
const r = rawRows.item ? rawRows.item(i) : rawRows[i];
|
|
119
|
+
resultToReturn.push(r);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else if (Array.isArray(rawRows)) {
|
|
123
|
+
resultToReturn = rawRows;
|
|
124
|
+
}
|
|
125
|
+
else if (rawRows._array) {
|
|
126
|
+
resultToReturn = rawRows._array;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
callback(resultToReturn);
|
|
69
130
|
}
|
|
70
131
|
catch (error) {
|
|
71
132
|
callback([]);
|
|
@@ -106,7 +167,7 @@ export class DatabaseNative {
|
|
|
106
167
|
? "DELETE FROM user_data WHERE user_id = ?"
|
|
107
168
|
: "DELETE FROM user_data";
|
|
108
169
|
const params = options.userId ? [options.userId] : [];
|
|
109
|
-
db.execute(query, params);
|
|
170
|
+
await db.execute(query, params);
|
|
110
171
|
callback();
|
|
111
172
|
}
|
|
112
173
|
catch (error) {
|
package/package.json
CHANGED