1
1
import sqlWasm from "!/sql.js/dist/sql-wasm.wasm" ;
2
- import { SqliteDatabaseError } from "@/db /errors" ;
2
+ import { Code , PluginError } from "@/errors" ;
3
3
import { isUndefined } from "@/helpers" ;
4
4
import {
5
5
type Result ,
9
9
ok ,
10
10
okAsync ,
11
11
} from "neverthrow" ;
12
- import type { DataAdapter } from "obsidian" ;
12
+ import type { DataAdapter , DataWriteOptions } from "obsidian" ;
13
13
import type { Database } from "sql.js" ;
14
14
import initSqlJs from "sql.js" ;
15
15
@@ -37,20 +37,23 @@ export class SqliteDatabase {
37
37
) ;
38
38
}
39
39
40
- public get instance ( ) : Result < Database , SqliteDatabaseError > {
40
+ public get instance ( ) : Result < Database , PluginError < Code . Sqlite > > {
41
41
if ( ! this . isInitialized ) {
42
- return err ( SqliteDatabaseError . DatabaseIsNotInitialized ) ;
42
+ return err ( new PluginError ( Code . Sqlite . DatabaseIsNotInitialized ) ) ;
43
43
}
44
44
45
45
return ok ( this . db as Database ) ;
46
46
}
47
47
48
48
private initSqlModule ( ) {
49
49
if ( isUndefined ( this . SQL ) ) {
50
- return ResultAsync . fromPromise (
51
- initSqlJs ( { wasmBinary : sqlWasm . buffer as ArrayBuffer } ) ,
52
- ( ) => SqliteDatabaseError . ModuleInitializationFailed ,
53
- ) . map ( ( SQL ) => {
50
+ const initModule = ResultAsync . fromThrowable (
51
+ initSqlJs ,
52
+ ( ) => new PluginError ( Code . Sqlite . ModuleInitializationFailed ) ,
53
+ ) ;
54
+ return initModule ( {
55
+ wasmBinary : sqlWasm . buffer as ArrayBuffer ,
56
+ } ) . map ( ( SQL ) => {
54
57
this . SQL = SQL ;
55
58
return SQL ;
56
59
} ) ;
@@ -59,12 +62,12 @@ export class SqliteDatabase {
59
62
}
60
63
61
64
private getOrCreateDbInstance ( ) {
62
- const readDbFile = ResultAsync . fromPromise (
63
- this . adapter . readBinary ( this . dbFilePath ) ,
64
- ( ) => SqliteDatabaseError . FileIsNotExists ,
65
+ const readDbFile = ResultAsync . fromThrowable (
66
+ ( normalizedPath : string ) => this . adapter . readBinary ( normalizedPath ) ,
67
+ ( ) => new PluginError ( Code . Sqlite . FileIsNotExists ) ,
65
68
) ;
66
69
return this . initSqlModule ( ) . andThen ( ( SQL ) =>
67
- readDbFile
70
+ readDbFile ( this . dbFilePath )
68
71
. map ( ( data ) => new SQL . Database ( Buffer . from ( data ) ) )
69
72
. orElse ( ( ) => ok ( new SQL . Database ( ) ) ) ,
70
73
) ;
@@ -73,7 +76,7 @@ export class SqliteDatabase {
73
76
public init (
74
77
dbFolder : string ,
75
78
dbFile : string ,
76
- ) : ResultAsync < Database , SqliteDatabaseError > {
79
+ ) : ResultAsync < Database , PluginError < Code . Sqlite > > {
77
80
if ( this . isInitialized ) {
78
81
return okAsync ( this . db as Database ) ;
79
82
}
@@ -92,30 +95,41 @@ export class SqliteDatabase {
92
95
} ) ;
93
96
}
94
97
95
- public save ( ) : ResultAsync < void , SqliteDatabaseError > {
98
+ public save ( ) : ResultAsync < void , PluginError < Code . Sqlite > > {
96
99
if ( ! this . isInitialized ) {
97
- return errAsync ( SqliteDatabaseError . DatabaseIsNotInitialized ) ;
100
+ return errAsync (
101
+ new PluginError ( Code . Sqlite . DatabaseIsNotInitialized ) ,
102
+ ) ;
98
103
}
99
104
105
+ const saveDbData = ResultAsync . fromThrowable (
106
+ (
107
+ normalizedPath : string ,
108
+ data : ArrayBuffer ,
109
+ options ?: DataWriteOptions ,
110
+ ) => this . adapter . writeBinary ( normalizedPath , data , options ) ,
111
+ ( ) => new PluginError ( Code . Sqlite . DatabaseSaveFailed ) ,
112
+ ) ;
113
+
100
114
return this . instance
101
115
. map ( ( db ) => db . export ( ) . buffer as ArrayBuffer )
102
- . asyncAndThen ( ( data ) =>
103
- ResultAsync . fromPromise (
104
- this . adapter . writeBinary ( this . dbFilePath , data ) ,
105
- ( ) => SqliteDatabaseError . DatabaseSaveFailed ,
106
- ) ,
107
- ) ;
116
+ . asyncAndThen ( ( data ) => saveDbData ( this . dbFilePath , data ) ) ;
108
117
}
109
118
110
119
public migrate ( ) {
111
120
if ( ! this . isInitialized ) {
112
- return errAsync ( SqliteDatabaseError . DatabaseIsNotInitialized ) ;
121
+ return errAsync (
122
+ new PluginError ( Code . Sqlite . DatabaseIsNotInitialized ) ,
123
+ ) ;
113
124
}
114
125
115
126
// TODO Migrations
116
127
}
117
128
118
- public close ( ) : ResultAsync < void , SqliteDatabaseError > {
129
+ public close ( ) : ResultAsync < void , PluginError < Code . Sqlite > > {
130
+ if ( ! this . isInitialized ) {
131
+ return okAsync ( ) ;
132
+ }
119
133
return this . instance . asyncMap ( async ( db ) => {
120
134
db . close ( ) ;
121
135
this . db = undefined ;
0 commit comments