@@ -21,19 +21,30 @@ interface DbNameOption {
21
21
22
22
interface DbConnectionOption extends DbNameOption {
23
23
entities ?: ( Function | string | EntitySchema < any > ) [ ] ;
24
+ shouldDrop ?: boolean ;
25
+ shouldClear ?: boolean ;
24
26
}
25
27
26
- interface DbLoadingOption extends DbConnectionOption {
28
+ interface DbLoadingOption extends DbNameOption {
29
+ entities ?: ( Function | string | EntitySchema < any > ) [ ] ;
27
30
database : Uint8Array ;
28
31
}
29
32
30
33
export const connectDb = async ( {
31
34
name = defaultConnectionName ,
32
35
entities = defaultEntities ,
36
+ shouldDrop = false ,
37
+ shouldClear = false ,
33
38
} : DbConnectionOption ) => {
34
39
let connection : Connection ;
40
+ if ( shouldClear ) {
41
+ await window . localforage ?. removeItem ( name ) ;
42
+ }
35
43
try {
36
44
connection = getConnection ( name ) ;
45
+ if ( ! connection . isConnected ) {
46
+ await connection . connect ( ) ;
47
+ }
37
48
} catch ( error ) {
38
49
connection = getConnectionManager ( ) . create ( {
39
50
type : 'sqljs' ,
@@ -45,9 +56,11 @@ export const connectDb = async ({
45
56
useLocalForage : true ,
46
57
logging : ! isProd && [ 'query' , 'schema' ] ,
47
58
} ) ;
48
- }
49
- if ( ! connection . isConnected ) {
50
- await connection . connect ( ) ;
59
+
60
+ if ( ! connection . isConnected ) {
61
+ await connection . connect ( ) ;
62
+ }
63
+ await connection . synchronize ( shouldDrop ) ;
51
64
}
52
65
return connection ;
53
66
} ;
@@ -77,7 +90,7 @@ export const loadDbFromFile = async ({
77
90
export const createDbUrl = async ( {
78
91
name = defaultConnectionName ,
79
92
} : DbNameOption ) => {
80
- const connection = getConnection ( name ) ;
93
+ const connection = await connectDb ( { name} ) ;
81
94
const arrayBuffer = connection . sqljsManager . exportDatabase ( ) ;
82
95
const blob = new Blob ( [ arrayBuffer ] , {
83
96
type : 'application/octet-stream' ,
0 commit comments