aboutsummaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
Diffstat (limited to 'db')
-rw-r--r--db/db.ts28
1 files changed, 13 insertions, 15 deletions
diff --git a/db/db.ts b/db/db.ts
index d5a2b76..5605f40 100644
--- a/db/db.ts
+++ b/db/db.ts
@@ -3,7 +3,6 @@ import * as path from 'https://deno.land/std@0.102.0/path/mod.ts';
import { TokenTags } from "../language/tags.ts";
import "../util/string.ts";
-import YomikunError from "../util/error.ts";
export interface DBDictInfo {
id: number;
@@ -49,14 +48,15 @@ interface DBFindResult {
*/
export default class DB {
private connection: Database;
- public ready: boolean = false;
+ public ready: Promise<void>;
+ private here = path.dirname(path.fromFileUrl(import.meta.url));
private paths = {
db: {
- dict: path.resolve('db', 'dict.db'),
- user: path.resolve('db', 'user.db'),
+ dict: path.resolve(this.here, 'dict.db'),
+ user: path.resolve(this.here, 'user.db'),
},
query: {
- find: path.resolve('db', 'find.sql'),
+ find: path.resolve(this.here, 'find.sql'),
},
} as const;
private statement: {
@@ -68,25 +68,23 @@ export default class DB {
this.connection = new Database(":memory:", { create: false });
this.statement = {
attach: this.connection.prepare("attach database ? as ?"),
- queryTerm: this.connection.prepare(""), // initialized in prepare()
+ queryTerm: this.connection.prepare(""),
};
this.attach(this.paths.db.dict, 'dict');
this.attach(this.paths.db.user, 'user');
+ this.ready = new Promise<void>(async resolve => {
+ const statement = await Deno.readTextFile(this.paths.query.find);
+ this.statement.queryTerm = this.connection.prepare(statement);
+ resolve();
+ });
}
private attach(dbPath: string, alias?: string) {
this.statement.attach.run(dbPath, alias);
}
- async prepare() {
- const statement = await Deno.readTextFile(this.paths.query.find);
- this.statement.queryTerm = this.connection.prepare(statement);
- this.ready = true;
- }
-
- findTerm(term: string): FindResult[] {
- if (!this.ready) throw new YomikunError("DB not ready yet, call `async DB::prepare()` first");
-
+ async findTerm(term: string): Promise<FindResult[]> {
+ await this.ready;
var results = this.statement.queryTerm.all({ term }) as unknown as DBFindResult[];
var terms: FindResult[] = results?.map(term => {
if (term.rules == null) term.rules = "";