diff --git a/types/react-native-sqlite-storage/index.d.ts b/types/react-native-sqlite-storage/index.d.ts index d0691edb86..0b3e083407 100644 --- a/types/react-native-sqlite-storage/index.d.ts +++ b/types/react-native-sqlite-storage/index.d.ts @@ -1,10 +1,16 @@ // Type definitions for react-native-sqlite-storage 3.3 // Project: https://github.com/andpor/react-native-sqlite-storage // Definitions by: Sergei Dryganets +// Deividi Cavarzan // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.4 +export function DEBUG(isDebug: boolean): void; +export function enablePromise(enablePromise: boolean): void; + +export function openDatabase(params: DatabaseParams): Promise; export function openDatabase(params: DatabaseParams, success?: () => void, error?: (e: SQLError) => void): SQLiteDatabase; +export function deleteDatabase(params: DatabaseParams): Promise; export function deleteDatabase(params: DatabaseParams, success?: () => void, error?: (err: SQLError) => void): void; export type Location = 'default' | 'Library' | 'Documents'; export interface DatabaseOptionalParams { @@ -55,6 +61,7 @@ export interface SQLError { export type StatementCallback = (transaction: Transaction, resultSet: ResultSet) => void; export type StatementErrorCallback = (transaction: Transaction, error: SQLError) => void; export interface Transaction { + executeSql(sqlStatement: string, arguments?: any[]): Promise<[Transaction, ResultSet]>; executeSql(sqlStatement: string, arguments?: any[], callback?: StatementCallback, errorCallback?: StatementErrorCallback): void; } @@ -62,10 +69,18 @@ export type TransactionCallback = (transaction: Transaction) => void; export type TransactionErrorCallback = (error: SQLError) => void; export interface SQLiteDatabase { + transaction(scope: (tx: Transaction) => void): Promise; transaction(scope: (tx: Transaction) => void, error?: TransactionErrorCallback, success?: TransactionCallback): void; + readTransaction(scope: (tx: Transaction) => void): Promise; readTransaction(scope: (tx: Transaction) => void, error?: TransactionErrorCallback, success?: TransactionCallback): void; + close(): Promise; close(success: () => void, error: (err: SQLError) => void): void; + executeSql(statement: string, params?: any[]): Promise<[ResultSet]>; executeSql(statement: string, params?: any[], success?: StatementCallback, error?: StatementErrorCallback): void; + + attach(nameToAttach: string, alias: string): Promise; attach(nameToAttach: string, alias: string, success?: () => void, error?: (err: SQLError) => void): void; + + dettach(alias: string): Promise; dettach(alias: string, success?: () => void, error?: (err: SQLError) => void): void; } diff --git a/types/react-native-sqlite-storage/react-native-sqlite-storage-tests.ts b/types/react-native-sqlite-storage/react-native-sqlite-storage-tests.ts index 76451cfe32..471b03908c 100644 --- a/types/react-native-sqlite-storage/react-native-sqlite-storage-tests.ts +++ b/types/react-native-sqlite-storage/react-native-sqlite-storage-tests.ts @@ -1,16 +1,40 @@ import * as sqlite from 'react-native-sqlite-storage'; -const db = sqlite.openDatabase({name: 'test.db', location: 'default'}, () => { - db.transaction((tx) => { - tx.executeSql('SELECT * FROM Employees a, Departments b WHERE a.department = b.department_id', [], (tx, results) => { - // Get rows with Web SQL Database spec compliance. - const len = results.rows.length; - for (let i = 0; i < len; i++) { - const row = results.rows.item(i); - const log = `Employee name: ${row.name}, Dept Name: ${row.deptName}`; - } - }); - }); -}, (err) => { - // log error +const db = sqlite.openDatabase( + { name: 'test.db', location: 'default' }, + () => { + db.transaction((tx) => { + tx.executeSql('SELECT * FROM Employees a, Departments b WHERE a.department = b.department_id', [], (tx, results) => { + // Get rows with Web SQL Database spec compliance. + const len = results.rows.length; + for (let i = 0; i < len; i++) { + const row = results.rows.item(i); + const log = `Employee name: ${row.name}, Dept Name: ${row.deptName}`; + } + }); + }); + }, + (err) => { + // log error + } +); + +sqlite.openDatabase({ name: 'test.db', location: 'default' }).then((db) => { + db + .transaction((tx) => { + tx + .executeSql('SELECT * FROM Employees a, Departments b WHERE a.department = b.department_id', []) + .then((result: [sqlite.Transaction, sqlite.ResultSet]) => { + // handle result + }); + }) + .then(() => { + // handle transaction finished + }) + .catch((e: sqlite.SQLError) => { + // log error + }); + db.executeSql('SELECT * FROM Employees a, Departments b WHERE a.department = b.department_id', []).then((result: [sqlite.ResultSet]) => { + // handle result + }); });