Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

非同期処理(JavaScript)のプログラムを作成 #3

Merged
merged 33 commits into from
Feb 18, 2025

Conversation

sjabcdefin
Copy link
Owner

@sjabcdefin sjabcdefin commented Jan 25, 2025

非同期処理(JavaScript)のプログラムを作成いたしました。
該当のソースは、以下となります。

  1. コールバック
    ・エラーなし:sqlite_callback_no_error.js
    ・エラーあり:sqlite_callback_error.js

  2. Promise
    ・エラーなし:sqlite_promise_no_error.js
    ・エラーあり:sqlite_promise_error.js

  3. async/await
    ・エラーなし:sqlite_asyncawait_no_error.js
    ・エラーあり:sqlite_asyncawait_error.js

ご確認お願いいたします。🙇‍♀️

Node.jsからSQLite3を使用するため、sqlite3をインストール
node-sqlite3をコールバックの形式で使用し、以下の操作についてエラーあり/なしのプログラムをそれぞれ作成
1. テーブルを作成
2. レコードを追加し、自動採番されたIDを標準出力に出力する
3. レコードを取得し、それを標準出力に出力する
4. テーブルを削除する
node-sqlite3のコールバック形式の関数をラップしてPromise化したプログラムを作成
Promise の項目で作った関数を async, await の形式で使用するプログラムを作成

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noerr ではなく no err だと思います。それとファイル名に略称を使うのは分かりづらいのでやめましょう。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ファイル名について、noerr を no_error、err を error に修正いたしました。

コミット:952081f

Comment on lines 25 to 63
function createTable() {
db.run(createTableQuery, insertTitles);
}

function insertTitles() {
console.log("Table created");
for (const title of titles) {
db.run(insertTitleQuery, [title], displayID);
}
}

function displayID() {
console.log(`Record inserted successfully with ID: ${this.lastID}`);
insertCount++;
if (insertCount === titles.length) {
fetchAll();
}
}

function fetchAll() {
db.all(selectAllQuery, displayAll);
}

function displayAll(err, rows) {
console.log("All records fetched successfully");
for (const row of rows) {
console.log(`id:${row.id}, title:${row.title}`);
}
deleteTable();
}

function deleteTable() {
db.run(deleteTableQuery, closeDB);
}

function closeDB() {
console.log("Table deleted");
db.close();
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

いずれのプログラムでも独自の抽象度の関数を定義するのは避けてください。非同期処理の表現方法が移り変わっていく様子をプログラム間で対応づけて理解してほしいのですが、それが実現できなくなってしまうためです。

Copy link
Owner Author

@sjabcdefin sjabcdefin Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

独自の抽象度の関数を削除し、非同期処理をそのまま記述するように修正いたしました。
コミット:77123aa

ひとまず、コールバックベースを修正しております。
そのため、Promiseやasync/awaitでご指摘いただいた以下の項目について、コールバックベースでも修正いたしました。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:7f8856a

Promise、async/awaitについて、非同期処理の表現方法が移り変わっていく様子を分かりやすくするため、main関数を削除いたしました。

Comment on lines 31 to 33
for (const title of titles) {
db.run(insertTitleQuery, [title], displayID);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コールバックベースの非同期処理を for 文で制御することはできませんよ。

Copy link
Owner Author

@sjabcdefin sjabcdefin Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すみません。
コールバックベースの非同期処理を制御できる繰り返し処理はない認識であっているでしょうか。💦🙇‍♀️
(複数の非同期処理の待ち合わせはコールバックベースではサポートされていないに該当する。)
レコード追加分、db.runを記述いたしました。

コミット:77123aa

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ないわけではないです。再起を用いればコールバックベースでも繰り返し処理を実現することはできます。ただし、node-sqlite3 が提供する関数だけでそれを実現することはできません。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ご回答ありがとうございます。😌
かしこまりました。


function main() {
return runQueryAsync(db, createTableQuery)
.then(function () {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

見づらいので使い分けが必要ない場合は function 式ではなくアロー関数を使ってください。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:00979e0

アロー関数を使用するように修正いたしました。
なお、sqlite_utils.js の runQueryAsync関数のdb.runのみコールバック関数内でthisを使用しているため、function () { ... }式を使用しています。🙏🙇‍♀️
#3 (comment) でのやり取り結果で、修正が必要な場合は、修正いたします。🙇‍♀️

Comment on lines 25 to 34
const requests = titles.map(function (title) {
return runQueryAsync(db, insertTitleQuery, [title]).then(
function (result) {
console.log(
`Record inserted successfully with ID: ${result.lastID}`,
);
},
);
});
return Promise.all(requests);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

各処理は並列ではなく直列に実行してください。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:68c7e42

並列ではなく、直列で実施するように修正いたしました。

@@ -0,0 +1,23 @@
export function runQueryAsync(db, query, params = []) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

質問ですが、params にデフォルト引数を設定しているのは何のためですか?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

質問に対する回答がないです。

Copy link
Owner Author

@sjabcdefin sjabcdefin Feb 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

失礼いたしました。🙏🙇‍♀️
node-sqlite3 の API ドキュメント https://github.com/TryGhost/node-sqlite3/wiki/API
に、
以下の記載があったため、第2引数に[]を指定したほうがよいと思い、デフォルト引数として指定いたしました。
認識に誤りがありましたら、教えていただけると幸いです。

run(sql [, param, ...] [, callback])
...
In case you want to keep the callback as the 3rd parameter, you should set param to "[]" ( Empty Array ) as per TryGhost/node-sqlite3#116

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:56986a0

以下の理由のため、[]を削除(省略)いたしました。
・#116 の問題は発生していない
・クエリに?がある場合は、paramsが必ず指定され、?がない場合、paramsはundefinedになるが、クエリの実行には問題ない。

});
}

export function allQueryAsync(db, query) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

元の all 関数もパラメータを受け付けるはずです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:f23f1de

allQueryAsync関数及びdb.allの引数に、paramsを追加いたしました。

Comment on lines 14 to 16
const insertTitleQuery = `INSERT INTO books (title) VALUES (?)`;
const selectAllQuery = `SELECT * FROM books`;
const deleteTableQuery = `DROP TABLE books`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文字列中に変数を埋め込んでいるわけではないのにテンプレート文字列が使われています。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

すみません。以前のプラクティスでも同様の指摘をいただいておりました。😭🙏
ダブルクウォーテーションを使用するように修正いたしました。

コミット:2047128

Comment on lines 8 to 13
const createTableQuery = `
CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL UNIQUE
)
`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これだと改行分やインデント分も文字列に含まれるので、以下のような文字列になってしまいます。

<改行>
<空白><空白>CREATE TABLE books (
<空白><空白><空白><空白>id INTEGER PRIMARY KEY AUTOINCREMENT,
<空白><空白><空白><空白>title TEXT NOT NULL UNIQUE
<空白><空白>)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

特別長いSQL文ではないため、
可読性を上げるための余分な改行やインデントは不要と考え、一行で書くようにいたしました。
コミット:f1c8683

Comment on lines 29 to 31
} catch (err) {
console.error(`Error occurred while inserting record: ${err.message}`);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch では全ての例外が無条件に捕捉されるので、この実装だと例外が握り潰されています。例外は握り潰さずに、捕捉したい例外のみを捕捉するようにしてください。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:12973cf

err.codeの判定処理を追加し、捕捉したい例外のみを捕捉するように修正いたしました。
こちらのご指摘は、Promiseにも該当すると思い、Promiseも修正しております。🙏

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

例外が引き続き握り潰されています。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:eddc819

想定しているエラー(SQLITE_CONSTRAINT、SQLITE_ERROR)以外についても握りつぶさず、捕捉するように修正いたしました。

…f "noerr"

可読性を向上させるために、ファイル名を変更
文字列中に変数を埋め込んでいるわけではないため、バッククウォートではなくダブルクウォーテーションを使用
SQLクエリから不要な改行とインデントを削除し、一行に統一
…rect form

独自の抽象関数を使用せず、非同期処理を直接記述する形式に修正
- コールバックベースの処理をそのまま記述
- 非同期処理の流れが分かりやすくなるよう変更
- for文を使用せず、レコード追加を追加分そのまま記述
パラメータの指定がない場合も[]を記載するように修正
見やすくなるように、アロー演算子を使用するように修正

const createTableQuery =
"CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL UNIQUE);";
const insertTitleQuery = "INSERT INTO books (title) VALUES (?)";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

insert するのは title カラムではなく books テーブルのレコードではないですかね。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

insertTitleQueryinsertBookRecordQueryに変更いたしました。
コミット:b7d6d4d

const titles = ["I Am a Cat", "KOKORO", "SANSHIRO"];

db.run(createTableQuery, [], function () {
console.log("Table created");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create は他動詞なので文言が文法的におかしそうです。

Copy link
Owner Author

@sjabcdefin sjabcdefin Feb 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Table was created successfully に修正いたしました。
create は他動詞であるため、受動態とし、成功したことが分かりやすいように、successfully を追加いたしました。

また、insert, fetch, delete も他動詞であるため、同様の修正をいたしました。

コミット:526b0a3

(追記)
Table was deleted successfully について、successfully にスペルミスがあることに気付いたため、修正いたしました。
コミット:45f478f


const titles = ["I Am a Cat", "KOKORO", "SANSHIRO"];

db.run(createTableQuery, [], function () {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正されていないです。#3 (comment) それから、パラメータの値として [] を渡しているのはなぜでしょうか?

Copy link
Owner Author

@sjabcdefin sjabcdefin Feb 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node-sqlite3 の API ドキュメント https://github.com/TryGhost/node-sqlite3/wiki/API

失礼いたしました。
node-sqlite3 の API ドキュメントに、以下の記載があったため、
db.runでは、function () { ... }を使用したほうがよいのかなと思ったのですが、
そういうわけではないでしょうか。🙏🙇‍♀️

run(sql [, param, ...] [, callback])
...
you must use an old-school function () { ... } style callback rather than a lambda function, otherwise this.lastID and this.changes will be undefined.

パラメータの値として第2引数に[]を渡している理由ですが、
node-sqlite3 の API ドキュメントに、以下の記載があったため、第2引数に[]を指定いたしました。
認識誤っていましたら、教えていただけると幸いです。

run(sql [, param, ...] [, callback])
...
In case you want to keep the callback as the 3rd parameter, you should set param to "[]" ( Empty Array ) as per issue #116

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

失礼いたしました。
node-sqlite3 の API ドキュメントに、以下の記載があったため、
db.runでは、function () { ... }を使用したほうがよいのかなと思ったのですが、
そういうわけではないでしょうか。🙏🙇‍♀️

確認ですが、コールバック関数内で this を参照しているのですか?

パラメータの値として第2引数に[]を渡している理由ですが、
node-sqlite3 の API ドキュメントに、以下の記載があったため、第2引数に[]を指定いたしました。
認識誤っていましたら、教えていただけると幸いです。

確認ですが、#116 の issue の問題が発生しているのですか?

Copy link
Owner Author

@sjabcdefin sjabcdefin Feb 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確認ですが、コールバック関数内で this を参照しているのですか?

コールバック関数内でthisを参照しています。🙏🙇‍♀️
thisを使用しないほうが好ましいのでしょうか。

確認ですが、#116 の issue の問題が発生しているのですか?

コミット:26e8ba8

#116 の問題は発生していません。
そのため、パラメータの指定が不要の場合は、[]を削除(省略)いたしました。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コールバック関数内でthisを参照しています。🙏🙇‍♀️ thisを使用しないほうが好ましいのでしょうか。

this がどの関数に対応するものなのかをよく見直してください。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:6ef5baf

thisを使うdb.runのコールバックはfunction形式のままとし、
thisを使わないdb.runのコールバックはアロー関数に修正いたしました。

すみません。ご指摘の内容の修正について、上記で合っているでしょうか。💦
合っていない場合、再度ご指摘いただきますようお願いいたします。🙏🙇‍♀️

Comment on lines 31 to 33
for (const title of titles) {
db.run(insertTitleQuery, [title], displayID);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ないわけではないです。再起を用いればコールバックベースでも繰り返し処理を実現することはできます。ただし、node-sqlite3 が提供する関数だけでそれを実現することはできません。

@@ -0,0 +1,23 @@
export function runQueryAsync(db, query, params = []) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

質問に対する回答がないです。

titleカラムを挿入するのではなく、booksテーブルに新しいレコードを追加することを明確にするため。
正しい文法とするため、他動詞(create, insert, fetch, delete)について、受動態に変更。
successfullyのスペルミスを修正
Comment on lines 10 to 11
const insertBookRecordQuery = "INSERT INTO books (title) VALUES (?)";
const selectAllQuery = "SELECT * FROM books";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この 2 つの変数名に一貫性がないです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:edf50f3

insertBookRecordQueryinsertRecordQueryに修正いたしました。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

問題が解決していないように思いますが。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:d722ec9

「行を挿入するクエリ」として、insertRecordQueryinsertRowQueryに、
「全ての行を選択するクエリ」として、selectAllQueryselectAllRowsQueryに修正いたしました。
行挿入の方が適切かと考え、RecordをRowに変更しております。


const titles = ["I Am a Cat", "KOKORO", "SANSHIRO"];

db.run(createTableQuery, [], function () {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

失礼いたしました。
node-sqlite3 の API ドキュメントに、以下の記載があったため、
db.runでは、function () { ... }を使用したほうがよいのかなと思ったのですが、
そういうわけではないでしょうか。🙏🙇‍♀️

確認ですが、コールバック関数内で this を参照しているのですか?

パラメータの値として第2引数に[]を渡している理由ですが、
node-sqlite3 の API ドキュメントに、以下の記載があったため、第2引数に[]を指定いたしました。
認識誤っていましたら、教えていただけると幸いです。

確認ですが、#116 の issue の問題が発生しているのですか?

"CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL UNIQUE);";
const insertBookRecordQuery = "INSERT INTO books (title) VALUES (?)";
const selectAllQuery = "SELECT * FROM books";
const deleteTableQuery = "DROP TABLE books";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SQL と対応づけるなら drop にしたほうがよいと思います。他の変数名は SQL の命令と対応づいているのにこの変数だけ対応づいていない点も気になります。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:ef60c8e

deleteTableQuerydropTableQueryに修正いたしました。

他の変数名との一貫性を取るため、レコード挿入クエリの変数名を変更
SQL と対応づけるため、テーブル削除クエリの変数名を変更
パラメータが指定されていない場合、空配列を省略
非同期処理の表現方法が移り変わっていく様子を分かりやすくするため
レコード挿入について、並列ではなく直列で実施されるように修正
…ite_utils.js as closeQueryAsync

closeも非同期処理を行う関数であるため
クエリに?が含まれていない場合、パラメータを指定する必要はないため
元の all 関数もパラメータを受け付けるため
捕捉したい例外のみを捕捉するように修正

const titles = ["I Am a Cat", "KOKORO", "SANSHIRO"];

db.run(createTableQuery, [], function () {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コールバック関数内でthisを参照しています。🙏🙇‍♀️ thisを使用しないほうが好ましいのでしょうか。

this がどの関数に対応するものなのかをよく見直してください。

Comment on lines 10 to 11
const insertBookRecordQuery = "INSERT INTO books (title) VALUES (?)";
const selectAllQuery = "SELECT * FROM books";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

問題が解決していないように思いますが。

for (const row of rows) {
console.log(`id:${row.id}, title:${row.title}`);
}
db.run(dropTableQuery, function () {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正されていないです。#3 (comment)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:6ef5baf

thisを使わないdb.runのコールバックはアロー関数に修正いたしました。

});
}

export function closeQueryAsync(db) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close は query ではないし、query を close するわけでもないです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:9601d22

「データベース接続を閉じる」処理として、closeDatabaseAsyncに修正いたしました。

Comment on lines 48 to 50
.finally(() => {
return closeQueryAsync(db);
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

アロー関数の中身が return しかないときは簡潔文体を使ってください。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:a57a47a

アロー関数の中身がreturn しかないときは、簡潔文体を使うよう修正いたしました。
sqlite_promise_error.jsも同様に修正しております。

Comment on lines 21 to 40
(async () => {
await runQueryAsync(db, createTableQuery);
console.log("Table was created successfully");

for (const title of titles) {
const result = await runQueryAsync(db, insertRecordQuery, [title]);
console.log(`Record was inserted successfully with ID: ${result.lastID}`);
}

const rows = await allQueryAsync(db, selectAllQuery);
console.log("All records were fetched successfully");
for (const row of rows) {
console.log(`id:${row.id}, title:${row.title}`);
}

await runQueryAsync(db, dropTableQuery);
console.log("Table was deleted successfully");

await closeQueryAsync(db);
})();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

即時実行関数式ではなく Top-level await を使ってください。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:8ec4514

Top-level await に修正いたしました。
sqlite_promise_error.jsも同様に修正しております。

await runQueryAsync(db, dropTableQuery);
console.log("Table was deleted successfully");

await closeQueryAsync(db);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Promise のプログラムではこの処理は finally で実行されていたはずです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:92ebeb1

closeDatabaseAsyncをfinallyブロックで実行するように修正いたしました。
sqlite_promise_error.jsも同様に修正しております。

const result = await runQueryAsync(db, insertRecordQuery, [title]);
console.log(`Record was inserted successfully with ID: ${result.lastID}`);
} catch (err) {
if (err.code === "SQLITE_CONSTRAINT") {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この条件だと err 変数にプロパティアクセスできない値が含まれていると err.code の部分でエラーが起きます。それから Error オブジェクトでなくても条件を満たしていればこの条件にマッチしてしまいます。例えば以下のようなオブジェクトが例外として送出される場合です。

{ code: "SQLITE_CONSTRAINT" }

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:eddc819

判定にerr instanceof Errorを追加し、
err変数がプロパティアクセスできない値やErrorオブジェクトではない場合は、想定外のエラーとなるように修正いたしました。

Comment on lines 29 to 31
} catch (err) {
console.error(`Error occurred while inserting record: ${err.message}`);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

例外が引き続き握り潰されています。

db.run のコールバック関数内で this を使用しない箇所について、可読性向上のために function 式からアロー関数へ変更
レコード挿入クエリとレコード選択クエリの変数名について、一貫性を取るため
closeQueryAsync という関数名を、実際に行っている「データベース接続を閉じる」処理を反映するために closeDatabaseAsync に変更。
可読性向上のため、アロー関数にreturnのみが含まれる場合は簡潔文体を使用
即時実行関数式ではなく Top-level await に修正
DBクローズ処理をfinallyブロックに移動
想定したエラー(SQLITE_CONSTRAINT, SQLITE_ERROR)のみを適切に処理し、意図しない例外の握りつぶしを防ぐため。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コメントし忘れていましたが、asyncawait ではなく async/await なので 2 語です。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:4d22ca9

asyncawait のファイル名を async_await に修正いたしました。
sqlite_async_await_error.js についても同様に修正しております。

if (err instanceof Error && err.code === "SQLITE_CONSTRAINT") {
console.error(`Error occurred while inserting record: ${err.message}`);
} else {
throw new Error("Unexpected error occured while inserting record");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

元の例外と同じものを再送出してください。でないと元の例外が伝播しなくなってしまいます。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:1726f9d

元の例外を再送出するように修正いたしました。

Comment on lines 54 to 55
} catch (err) {
console.error(err);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

今回のプログラムでは、エラーが起こる想定の部分に対してのみエラーハンドリングを行ってください。この catch に対応するものはないはずです。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

コミット:64325e2

エラーが起こる想定の部分に対してのみエラーハンドリングをするため、この catch ブロック は削除いたしました。

asyncawait ではなく async/await の 2 語であるため
元の例外を伝播させるため
今回のプログラムでは、エラーが起こる想定の部分に対してのみエラーハンドリングを行うため
@sjabcdefin sjabcdefin merged commit 9467998 into main Feb 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants