1+ import executeCallback from 'localforage/src/utils/executeCallback' ;
2+ import normalizeKey from 'localforage/src/utils/normalizeKey' ;
3+ import { openOrCreate , deleteDatabase } from '@nativescript-community/sqlite' ;
4+
5+ const dbConnections = { } ;
6+
7+ function getDB ( options ) {
8+ const { name } = options ;
9+ if ( ! dbConnections [ name ] ) {
10+ dbConnections [ name ] = openOrCreate ( `:memory:${ name } .db` ) ;
11+ }
12+ return dbConnections [ name ] ;
13+ }
14+
15+ async function checkStore ( db , storeName ) {
16+ try {
17+ const result = await db . get ( 'SELECT name FROM sqlite_master WHERE type=? AND name=?' , [ 'table' , storeName ] ) ;
18+ if ( result ) {
19+ return true ;
20+ }
21+ await db . execute ( 'CREATE TABLE IF NOT EXISTS ' + storeName + ' (key PRIMARY KEY, value)' ) ;
22+ return true ;
23+ } catch ( error ) {
24+ console . error ( 'checkStore error' , error ) ;
25+ return false ;
26+ }
27+ }
28+
29+ export function _initStorage ( options ) {
30+ const db = getDB ( options ) ;
31+ const promise = checkStore ( db , options . storeName ) ;
32+ executeCallback ( promise ) ;
33+ return promise ;
34+ }
35+
36+ export function clear ( callback ) {
37+ const db = getDB ( this . _config ) ;
38+ const promise = db . execute ( 'DELETE FROM ' + this . _config . storeName ) . then ( ( ) => undefined ) ;
39+ executeCallback ( promise , callback ) ;
40+ return promise ;
41+ }
42+
43+ export function getItem ( key , callback ) {
44+ key = normalizeKey ( key ) ;
45+ const db = getDB ( this . _config ) ;
46+ const promise = db . get ( 'SELECT value FROM ' + this . _config . storeName + ' WHERE key = ?' , [ key ] ) . then ( result => {
47+ if ( result ) {
48+ return result . value ;
49+ }
50+ return null ;
51+ } ) ;
52+ executeCallback ( promise , callback ) ;
53+ return promise ;
54+ }
55+
56+ export function iterate ( iterator , callback ) {
57+ const db = getDB ( this . _config ) ;
58+ const promise = db . all ( 'SELECT key, value FROM ' + this . _config . storeName ) . then ( results => {
59+ let i = 0 ;
60+ for ( const row of results ) {
61+ const result = iterator ( row . value , row . key , i ++ ) ;
62+ if ( result !== undefined ) {
63+ return result ;
64+ }
65+ }
66+ } ) ;
67+ executeCallback ( promise , callback ) ;
68+ return promise ;
69+ }
70+
71+ export function key ( n , callback ) {
72+ const db = getDB ( this . _config ) ;
73+ const promise = db . get ( 'SELECT key FROM ' + this . _config . storeName + ' LIMIT 1 OFFSET ?' , [ n ] ) . then ( result => {
74+ if ( result ) {
75+ return result . key ;
76+ }
77+ return null ;
78+ } ) ;
79+ executeCallback ( promise , callback ) ;
80+ return promise ;
81+ }
82+
83+ export function keys ( callback ) {
84+ const db = getDB ( this . _config ) ;
85+ const promise = db . all ( 'SELECT key FROM ' + this . _config . storeName ) . then ( results => results . map ( row => row . key ) ) ;
86+ executeCallback ( promise , callback ) ;
87+ return promise ;
88+ }
89+
90+ export function length ( callback ) {
91+ const db = getDB ( this . _config ) ;
92+ const promise = db . get ( 'SELECT COUNT(key) as count FROM ' + this . _config . storeName ) . then ( result => result . count ) ;
93+ executeCallback ( promise , callback ) ;
94+ return promise ;
95+ }
96+
97+ export function removeItem ( key , callback ) {
98+ key = normalizeKey ( key ) ;
99+ const db = getDB ( this . _config ) ;
100+ const promise = db . execute ( 'DELETE FROM ' + this . _config . storeName + ' WHERE key = ?' , [ key ] ) . then ( ( ) => undefined ) ;
101+ executeCallback ( promise , callback ) ;
102+ return promise ;
103+ }
104+
105+ export function setItem ( key , value , callback ) {
106+ key = normalizeKey ( key ) ;
107+ const db = getDB ( this . _config ) ;
108+ const promise = db . execute ( 'INSERT OR REPLACE INTO ' + this . _config . storeName + ' (key, value) VALUES (?, ?)' , [ key , value ] ) . then ( ( ) => value ) ;
109+ executeCallback ( promise , callback ) ;
110+ return promise ;
111+ }
112+
113+ export function dropInstance ( options , callback ) {
114+ const db = dbConnections [ options . name ] ;
115+ const promise = new Promise < void > ( ( resolve , reject ) => {
116+ if ( db ) {
117+ db . close ( ) ;
118+ delete dbConnections [ options . name ] ;
119+ deleteDatabase ( options . name + '.db' ) ;
120+ resolve ( ) ;
121+ } else {
122+ resolve ( ) ;
123+ }
124+ } ) ;
125+
126+ executeCallback ( promise , callback ) ;
127+ return promise ;
128+ }
129+
130+ export const nativeScriptSqliteDriver = {
131+ _driver : 'nativeScriptSqliteDriver' ,
132+ _initStorage : _initStorage ,
133+ _support : ( ) => typeof openOrCreate === 'function' ,
134+ clear : clear ,
135+ getItem : getItem ,
136+ iterate : iterate ,
137+ key : key ,
138+ keys : keys ,
139+ length : length ,
140+ removeItem : removeItem ,
141+ setItem : setItem ,
142+ dropInstance : dropInstance
143+ } ;
144+
145+ export default nativeScriptSqliteDriver ;
0 commit comments