Skip to content

Commit dad7150

Browse files
committed
feat: Use more strict error handling in SQLite DB
1 parent ab83a1e commit dad7150

File tree

1 file changed

+37
-23
lines changed

1 file changed

+37
-23
lines changed

src/db/sqlite.ts

+37-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sqlWasm from "!/sql.js/dist/sql-wasm.wasm";
2-
import { SqliteDatabaseError } from "@/db/errors";
2+
import { Code, PluginError } from "@/errors";
33
import { isUndefined } from "@/helpers";
44
import {
55
type Result,
@@ -9,7 +9,7 @@ import {
99
ok,
1010
okAsync,
1111
} from "neverthrow";
12-
import type { DataAdapter } from "obsidian";
12+
import type { DataAdapter, DataWriteOptions } from "obsidian";
1313
import type { Database } from "sql.js";
1414
import initSqlJs from "sql.js";
1515

@@ -37,20 +37,23 @@ export class SqliteDatabase {
3737
);
3838
}
3939

40-
public get instance(): Result<Database, SqliteDatabaseError> {
40+
public get instance(): Result<Database, PluginError<Code.Sqlite>> {
4141
if (!this.isInitialized) {
42-
return err(SqliteDatabaseError.DatabaseIsNotInitialized);
42+
return err(new PluginError(Code.Sqlite.DatabaseIsNotInitialized));
4343
}
4444

4545
return ok(this.db as Database);
4646
}
4747

4848
private initSqlModule() {
4949
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) => {
5457
this.SQL = SQL;
5558
return SQL;
5659
});
@@ -59,12 +62,12 @@ export class SqliteDatabase {
5962
}
6063

6164
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),
6568
);
6669
return this.initSqlModule().andThen((SQL) =>
67-
readDbFile
70+
readDbFile(this.dbFilePath)
6871
.map((data) => new SQL.Database(Buffer.from(data)))
6972
.orElse(() => ok(new SQL.Database())),
7073
);
@@ -73,7 +76,7 @@ export class SqliteDatabase {
7376
public init(
7477
dbFolder: string,
7578
dbFile: string,
76-
): ResultAsync<Database, SqliteDatabaseError> {
79+
): ResultAsync<Database, PluginError<Code.Sqlite>> {
7780
if (this.isInitialized) {
7881
return okAsync(this.db as Database);
7982
}
@@ -92,30 +95,41 @@ export class SqliteDatabase {
9295
});
9396
}
9497

95-
public save(): ResultAsync<void, SqliteDatabaseError> {
98+
public save(): ResultAsync<void, PluginError<Code.Sqlite>> {
9699
if (!this.isInitialized) {
97-
return errAsync(SqliteDatabaseError.DatabaseIsNotInitialized);
100+
return errAsync(
101+
new PluginError(Code.Sqlite.DatabaseIsNotInitialized),
102+
);
98103
}
99104

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+
100114
return this.instance
101115
.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));
108117
}
109118

110119
public migrate() {
111120
if (!this.isInitialized) {
112-
return errAsync(SqliteDatabaseError.DatabaseIsNotInitialized);
121+
return errAsync(
122+
new PluginError(Code.Sqlite.DatabaseIsNotInitialized),
123+
);
113124
}
114125

115126
// TODO Migrations
116127
}
117128

118-
public close(): ResultAsync<void, SqliteDatabaseError> {
129+
public close(): ResultAsync<void, PluginError<Code.Sqlite>> {
130+
if (!this.isInitialized) {
131+
return okAsync();
132+
}
119133
return this.instance.asyncMap(async (db) => {
120134
db.close();
121135
this.db = undefined;

0 commit comments

Comments
 (0)