import { IAPICacheDriver } from '../interfaces' ; type PromiseResolve < T > = ( value ? : T | PromiseLike < T > ) => void ; type PromiseReject = ( error ? : any ) => void ; export interface ISQLiteBinding { DEBUG : ( enabled : boolean ) => void ; enablePromise : ( enabled : boolean ) => void ; openDatabase : ( options : any ) => Promise < any > ; deleteDatabase : ( dbname : string ) => Promise < void > ; } export interface ISQLiteDriverOptions { debug ? : boolean ; openDatabaseOptions ? : { name : string ; location : 'default' | 'Library' | 'Documents' ; } ; } interface ISQLiteDatabase { transaction : ( callback : ( tx : ITransaction ) => void ) => void ; } interface ITransaction { executeSql : ( query : string , args ? : any [ ] , callback ? : ( tx : ITransaction , results : IQueryResults ) => void ) => Promise < [ ITransaction , IQueryResults ] > ; } interface IQueryResults { insertId ? : number ; rows : { item : ( index : number ) => any ; raw : ( index : number ) => any ; length : number ; } ; rowsAffected : number ; } export default async ( SQLite , options ) : Promise < IAPICacheDriver > => { SQLite . DEBUG ( options . debug || false ) ; SQLite . enablePromise ( true ) ; try { const db = await SQLite . openDatabase ( { name : 'apipeline.db' , location : 'default' , ... ( options . openDatabaseOptions || { } ) } ) ; db . transaction ( ( tx ) => { tx . executeSql ( 'CREATE TABLE IF NOT EXISTS cache (id TEXT PRIMARY KEY NOT NULL, value TEXT);' ) ; } ) ; return { getItem : getItem ( db ) , setItem : setItem ( db ) , removeItem : removeItem ( db ) } ; } catch ( err ) { throw new Error ( `APIPeline : Cannot open SQLite database : ${ err }. Check your SQLite configuration.` ) ; } } ; function getItem ( db ) { return ( key ) => { return new Promise ( ( resolve , reject ) => { db . transaction ( ( tx ) => { tx . executeSql ( 'SELECT * FROM cache WHERE id=?' , [ key ] ) . then ( ( res ) => { const results = res [ 1 ] ; const item = results . rows . item ( 0 ) ; return resolve ( item && item . value || null ) ; } ) . catch ( ( err ) => { return reject ( err ) ; } ) ; } ) ; } ) ; } ; } function setItem ( db ) { return ( key , value ) => { return new Promise ( ( resolve , reject ) => { db . transaction ( ( tx ) => { tx . executeSql ( 'INSERT OR REPLACE INTO cache VALUES (?,?)' , [ key , value ] ) . then ( ( ) => { return resolve ( ) ; } ) . catch ( ( err ) => { return reject ( err ) ; } ) ; } ) ; } ) ; } ; } function removeItem ( db ) { return ( key ) => { return new Promise ( ( resolve , reject ) => { db . transaction ( ( tx ) => { tx . executeSql ( 'DELETE FROM cache WHERE id=?' , [ key ] ) . then ( ( ) => { return resolve ( ) ; } ) . catch ( ( err ) => { return reject ( err ) ; } ) ; } ) ; } ) ; } ; }