Skip to content

Commit 9eff9a8

Browse files
author
Oscar Franco
committed
Update index.ts with better comments and fix item function assignment
1 parent e205552 commit 9eff9a8

1 file changed

Lines changed: 43 additions & 20 deletions

File tree

src/index.ts

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,48 @@
1-
// IMPORTANT!!!!!!!!!!!
2-
// JSI BINDINGS DO NOT WORK WHEN CONNECTED TO THE DEBUGGER
3-
// This is a low level api
1+
/**
2+
* JSI BINDINGS DO NOT WORK WHEN CONNECTED TO THE CHROME DEBUGGER
3+
* Use flipper to debug your RN apps from now on
4+
*/
5+
6+
/**
7+
* Object returned by SQL Query executions {
8+
* status: 0 or undefined for correct execution, 1 for error
9+
* insertId: Represent the auto-generated row id if applicable
10+
* rowsAffected: Number of affected rows if result of a update query
11+
* message: if status === 1, here you will find error description
12+
* rows: if status is undefined or 0 this object will contain the query results
13+
* }
14+
*
15+
* @interface QueryResult
16+
*/
17+
interface QueryResult {
18+
status?: 0 | 1;
19+
insertId?: number;
20+
rowsAffected: number;
21+
message?: string;
22+
rows?: {
23+
/** Raw array with all dataset */
24+
_array: any[];
25+
/** The lengh of the dataset */
26+
length: number;
27+
/** A convenience function to acess the index based the row object
28+
* @param idx the row index
29+
* @returns the row structure identified by column names
30+
*/
31+
item: (idx: number) => any;
32+
};
33+
}
434
interface ISQLite {
535
open: (dbName: string, location?: string) => any;
636
close: (dbName: string) => any;
737
executeSql: (
838
dbName: string,
939
query: string,
1040
params: any[] | undefined
11-
) => {
12-
rows: {
13-
_array: any[];
14-
length: number;
15-
item: (idx: number) => any;
16-
};
17-
insertId?: number;
18-
rowsAffected: number;
19-
};
41+
) => QueryResult;
2042
// backgroundExecuteSql: (dbName: string, query: string, params: any[]) => any;
2143
}
2244

23-
declare global {
24-
var sqlite: ISQLite;
25-
}
45+
declare var sqlite: ISQLite;
2646

2747
// API FOR TYPEORM
2848
interface IConnectionOptions {
@@ -34,7 +54,7 @@ interface IDBConnection {
3454
executeSql: (
3555
sql: string,
3656
args: any[],
37-
ok: (res: any) => void,
57+
ok: (res: QueryResult) => void,
3858
fail: (msg: string) => void
3959
) => void;
4060
close: (ok: (res: any) => void, fail: (msg: string) => void) => void;
@@ -52,14 +72,17 @@ export const openDatabase = (
5272
executeSql: (
5373
sql: string,
5474
params: any[] | undefined,
55-
ok: any,
56-
fail: any
75+
ok: (res: QueryResult) => void,
76+
fail: (msg: string) => void
5777
) => {
5878
try {
5979
// console.warn(`[react-native-quick-sqlite], sql: `, sql, ` params: ` , params);
6080
let response = sqlite.executeSql(options.name, sql, params);
61-
// enhance object to allow the sqlite-storage typeorm driver to work
62-
response.rows.item = (idx: number) => response.rows._array[idx];
81+
82+
// Add 'item' function to result object to allow the sqlite-storage typeorm driver to work
83+
if (response.rows != null) {
84+
response.rows.item = (idx: number) => response.rows._array[idx];
85+
}
6386

6487
ok(response);
6588
} catch (e) {

0 commit comments

Comments
 (0)