Merge pull request #26024 from cavarzan/master

[react-native-sqlite-storage] Added promise support
This commit is contained in:
Benjamin Lichtman
2018-05-31 10:15:55 -07:00
committed by GitHub
2 changed files with 52 additions and 13 deletions

View File

@@ -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 <https://github.com/dryganets>
// Deividi Cavarzan <https://github.com/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<SQLiteDatabase>;
export function openDatabase(params: DatabaseParams, success?: () => void, error?: (e: SQLError) => void): SQLiteDatabase;
export function deleteDatabase(params: DatabaseParams): Promise<void>;
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>;
transaction(scope: (tx: Transaction) => void, error?: TransactionErrorCallback, success?: TransactionCallback): void;
readTransaction(scope: (tx: Transaction) => void): Promise<TransactionCallback>;
readTransaction(scope: (tx: Transaction) => void, error?: TransactionErrorCallback, success?: TransactionCallback): void;
close(): Promise<void>;
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<void>;
attach(nameToAttach: string, alias: string, success?: () => void, error?: (err: SQLError) => void): void;
dettach(alias: string): Promise<void>;
dettach(alias: string, success?: () => void, error?: (err: SQLError) => void): void;
}

View File

@@ -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
});
});