From c920d3c63915c126e8616f9c5bebdfd382563b7a Mon Sep 17 00:00:00 2001 From: cavarzan Date: Thu, 24 May 2018 23:36:51 -0300 Subject: [PATCH 1/4] react-native-sqlite-storage promise support --- types/react-native-sqlite-storage/index.d.ts | 17 +++++++ .../react-native-sqlite-storage-tests.ts | 50 ++++++++++++++----- 2 files changed, 54 insertions(+), 13 deletions(-) diff --git a/types/react-native-sqlite-storage/index.d.ts b/types/react-native-sqlite-storage/index.d.ts index d0691edb86..1a4688fd67 100644 --- a/types/react-native-sqlite-storage/index.d.ts +++ b/types/react-native-sqlite-storage/index.d.ts @@ -1,11 +1,17 @@ // 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, success?: () => void, error?: (err: SQLError) => void): void; +export function deleteDatabase(params: DatabaseParams): Promise; export type Location = 'default' | 'Library' | 'Documents'; export interface DatabaseOptionalParams { createFromLocation?: number | string; @@ -55,17 +61,28 @@ 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; executeSql(sqlStatement: string, arguments?: any[], callback?: StatementCallback, errorCallback?: StatementErrorCallback): void; } export type TransactionCallback = (transaction: Transaction) => void; export type TransactionErrorCallback = (error: SQLError) => void; +export type StatementPromise = [ResultSet]; +export type TransactionStatementPromise = [Transaction, ResultSet]; 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; 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..89d46ba8c4 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.TransactionStatementPromise) => { + // 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.StatementPromise) => { + // handle result + }); }); From 0a7d31d63e76c00a3b2f6048190d6259a6701c10 Mon Sep 17 00:00:00 2001 From: cavarzan Date: Thu, 24 May 2018 23:48:13 -0300 Subject: [PATCH 2/4] fix mthod order and keep organized --- types/react-native-sqlite-storage/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/react-native-sqlite-storage/index.d.ts b/types/react-native-sqlite-storage/index.d.ts index 1a4688fd67..4a8262a802 100644 --- a/types/react-native-sqlite-storage/index.d.ts +++ b/types/react-native-sqlite-storage/index.d.ts @@ -10,8 +10,8 @@ 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, success?: () => void, error?: (err: SQLError) => void): void; 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 { createFromLocation?: number | string; From 02474f65996088b2b462af78a1ace7cda06639ec Mon Sep 17 00:00:00 2001 From: cavarzan Date: Fri, 25 May 2018 11:42:24 -0300 Subject: [PATCH 3/4] changed StatementPromise to ResultSetPromise and TransactionStatementPromise to TransactionResultSetPromise --- types/react-native-sqlite-storage/index.d.ts | 8 ++++---- .../react-native-sqlite-storage-tests.ts | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/types/react-native-sqlite-storage/index.d.ts b/types/react-native-sqlite-storage/index.d.ts index 4a8262a802..59aa52553c 100644 --- a/types/react-native-sqlite-storage/index.d.ts +++ b/types/react-native-sqlite-storage/index.d.ts @@ -61,14 +61,14 @@ 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; + executeSql(sqlStatement: string, arguments?: any[]): Promise; executeSql(sqlStatement: string, arguments?: any[], callback?: StatementCallback, errorCallback?: StatementErrorCallback): void; } export type TransactionCallback = (transaction: Transaction) => void; export type TransactionErrorCallback = (error: SQLError) => void; -export type StatementPromise = [ResultSet]; -export type TransactionStatementPromise = [Transaction, ResultSet]; +export type ResultSetPromise = [ResultSet]; +export type TransactionResultSetPromise = [Transaction, ResultSet]; export interface SQLiteDatabase { transaction(scope: (tx: Transaction) => void): Promise; @@ -77,7 +77,7 @@ export interface SQLiteDatabase { 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; + executeSql(statement: string, params?: any[]): Promise; executeSql(statement: string, params?: any[], success?: StatementCallback, error?: StatementErrorCallback): void; attach(nameToAttach: string, alias: string): Promise; 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 89d46ba8c4..f93e763004 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 @@ -24,7 +24,7 @@ sqlite.openDatabase({ name: 'test.db', location: 'default' }).then((db) => { .transaction((tx) => { tx .executeSql('SELECT * FROM Employees a, Departments b WHERE a.department = b.department_id', []) - .then((result: sqlite.TransactionStatementPromise) => { + .then((result: sqlite.TransactionResultSetPromise) => { // handle result }); }) @@ -34,7 +34,7 @@ sqlite.openDatabase({ name: 'test.db', location: 'default' }).then((db) => { .catch((e: sqlite.SQLError) => { // log error }); - db.executeSql('SELECT * FROM Employees a, Departments b WHERE a.department = b.department_id', []).then((result: sqlite.StatementPromise) => { + db.executeSql('SELECT * FROM Employees a, Departments b WHERE a.department = b.department_id', []).then((result: sqlite.ResultSetPromise) => { // handle result }); }); From 14370ec612cd48cab7c9a859e9a068f5ab52753d Mon Sep 17 00:00:00 2001 From: cavarzan Date: Tue, 29 May 2018 20:31:02 -0300 Subject: [PATCH 4/4] removed types --- types/react-native-sqlite-storage/index.d.ts | 6 ++---- .../react-native-sqlite-storage-tests.ts | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/types/react-native-sqlite-storage/index.d.ts b/types/react-native-sqlite-storage/index.d.ts index 59aa52553c..0b3e083407 100644 --- a/types/react-native-sqlite-storage/index.d.ts +++ b/types/react-native-sqlite-storage/index.d.ts @@ -61,14 +61,12 @@ 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; + executeSql(sqlStatement: string, arguments?: any[]): Promise<[Transaction, ResultSet]>; executeSql(sqlStatement: string, arguments?: any[], callback?: StatementCallback, errorCallback?: StatementErrorCallback): void; } export type TransactionCallback = (transaction: Transaction) => void; export type TransactionErrorCallback = (error: SQLError) => void; -export type ResultSetPromise = [ResultSet]; -export type TransactionResultSetPromise = [Transaction, ResultSet]; export interface SQLiteDatabase { transaction(scope: (tx: Transaction) => void): Promise; @@ -77,7 +75,7 @@ export interface SQLiteDatabase { 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; + executeSql(statement: string, params?: any[]): Promise<[ResultSet]>; executeSql(statement: string, params?: any[], success?: StatementCallback, error?: StatementErrorCallback): void; attach(nameToAttach: string, alias: string): Promise; 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 f93e763004..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 @@ -24,7 +24,7 @@ sqlite.openDatabase({ name: 'test.db', location: 'default' }).then((db) => { .transaction((tx) => { tx .executeSql('SELECT * FROM Employees a, Departments b WHERE a.department = b.department_id', []) - .then((result: sqlite.TransactionResultSetPromise) => { + .then((result: [sqlite.Transaction, sqlite.ResultSet]) => { // handle result }); }) @@ -34,7 +34,7 @@ sqlite.openDatabase({ name: 'test.db', location: 'default' }).then((db) => { .catch((e: sqlite.SQLError) => { // log error }); - db.executeSql('SELECT * FROM Employees a, Departments b WHERE a.department = b.department_id', []).then((result: sqlite.ResultSetPromise) => { + db.executeSql('SELECT * FROM Employees a, Departments b WHERE a.department = b.department_id', []).then((result: [sqlite.ResultSet]) => { // handle result }); });