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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
6318887
install sqlite3
sjabcdefin Jan 13, 2025
3081ef8
Create programs using node-sqlite3 in the form of callback
sjabcdefin Jan 15, 2025
0a558c9
Create programs using Promise wrapper for node-sqlite3 methods(run, all)
sjabcdefin Jan 21, 2025
c5fb47a
Create programs using async/await
sjabcdefin Jan 22, 2025
952081f
Rename files to use "error" instead of "err" and "no_error" instead o…
sjabcdefin Jan 27, 2025
2047128
Use double quotes instead of backquotes
sjabcdefin Jan 27, 2025
f1c8683
Refactor SQL queries by removing unnecessary line breaks and indentation
sjabcdefin Jan 27, 2025
77123aa
Removed abstraction in asynchronous processing and rewrote it in a di…
sjabcdefin Jan 29, 2025
9b90b88
Writes an empty array [] even if no parameters are specified
sjabcdefin Jan 29, 2025
95cd7d9
Fixed db.all to use arrow function instead of function expression
sjabcdefin Jan 29, 2025
b7d6d4d
Rename insertTitleQuery to insertBookRecordQuery
sjabcdefin Jan 30, 2025
526b0a3
Change log messages to passive voice
sjabcdefin Feb 1, 2025
45f478f
Correct spelling mistakes
sjabcdefin Feb 1, 2025
edf50f3
Rename variable name in record insertquery
sjabcdefin Feb 8, 2025
ef60c8e
Rename variable name in table dletion query
sjabcdefin Feb 8, 2025
26e8ba8
Remove empty array when parameters are not spericied
sjabcdefin Feb 8, 2025
7f8856a
Remove main function
sjabcdefin Feb 8, 2025
00979e0
Fixed to use arrow function instead of function expression
sjabcdefin Feb 8, 2025
68c7e42
Fixed to insert records serially instead of parallel
sjabcdefin Feb 8, 2025
233099e
Refactor db.close() to be an asynchronous function and move it to sql…
sjabcdefin Feb 8, 2025
56986a0
Remove the default value [] for the params argument in runQueryAsync
sjabcdefin Feb 8, 2025
f23f1de
Add params argument to db.all
sjabcdefin Feb 8, 2025
12973cf
Fixed to catch only the specified exception
sjabcdefin Feb 8, 2025
6ef5baf
Fixed to use arrow functions for db.run where this is not used
sjabcdefin Feb 12, 2025
d722ec9
Rename variable name in insertquery and selectquery
sjabcdefin Feb 12, 2025
9601d22
Rename closeQueryAsync to closeDatabaseAsync
sjabcdefin Feb 12, 2025
a57a47a
Fixed to use concise writing when arrow functions only contain return
sjabcdefin Feb 12, 2025
8ec4514
Refactor to be Top-level await
sjabcdefin Feb 12, 2025
92ebeb1
Move DB close processing to finally block
sjabcdefin Feb 12, 2025
eddc819
Handle specific SQLite errors and rethrow unexpected ones
sjabcdefin Feb 12, 2025
4d22ca9
Rename files from asyncawait to async_await
sjabcdefin Feb 13, 2025
1726f9d
Fixed to rethrow the original exception
sjabcdefin Feb 13, 2025
64325e2
Fixed to handle only expected errors
sjabcdefin Feb 13, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,378 changes: 1,359 additions & 19 deletions 03.asynchronous/package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion 03.asynchronous/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"globals": "^15.9.0",
"prettier": "^3.3.3"
},
"type": "module"
"type": "module",
"dependencies": {
"sqlite3": "^5.1.7"
}
}
56 changes: 56 additions & 0 deletions 03.asynchronous/sqlite_async_await_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env node

import sqlite3 from "sqlite3";
import {
runQueryAsync,
allQueryAsync,
closeDatabaseAsync,
} from "./sqlite_utils.js";

sqlite3.verbose();
const db = new sqlite3.Database(":memory:");

const createTableQuery =
"CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL UNIQUE);";
const insertRowQuery = "INSERT INTO books (title) VALUES (?)";
const selectAllRowsQuery = "SELECT * FROM movies";
const dropTableQuery = "DROP TABLE books";

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

try {
await runQueryAsync(db, createTableQuery);
console.log("Table was created successfully");

for (const title of titles) {
try {
const result = await runQueryAsync(db, insertRowQuery, [title]);
console.log(`Record was inserted successfully with ID: ${result.lastID}`);
} catch (err) {
if (err instanceof Error && err.code === "SQLITE_CONSTRAINT") {
console.error(`Error occurred while inserting record: ${err.message}`);
} else {
throw err;
}
}
}

try {
const rows = await allQueryAsync(db, selectAllRowsQuery);
console.log("All records were fetched successfully");
for (const row of rows) {
console.log(`id:${row.id}, title:${row.title}`);
}
} catch (err) {
if (err instanceof Error && err.code === "SQLITE_ERROR") {
console.error(`Error occurred while fetching records: ${err.message}`);
} else {
throw err;
}
}

await runQueryAsync(db, dropTableQuery);
console.log("Table was deleted successfully");
} finally {
await closeDatabaseAsync(db);
}
40 changes: 40 additions & 0 deletions 03.asynchronous/sqlite_async_await_no_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env node

import sqlite3 from "sqlite3";
import {
runQueryAsync,
allQueryAsync,
closeDatabaseAsync,
} from "./sqlite_utils.js";

sqlite3.verbose();
const db = new sqlite3.Database(":memory:");

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

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

try {
await runQueryAsync(db, createTableQuery);
console.log("Table was created successfully");

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

const rows = await allQueryAsync(db, selectAllRowsQuery);
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");
} finally {
await closeDatabaseAsync(db);
}
59 changes: 59 additions & 0 deletions 03.asynchronous/sqlite_callback_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env node

import sqlite3 from "sqlite3";

sqlite3.verbose();
const db = new sqlite3.Database(":memory:");

const createTableQuery =
"CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL UNIQUE);";
const insertRowQuery = "INSERT INTO books (title) VALUES (?)";
const selectAllRowsQuery = "SELECT * FROM movies";
const dropTableQuery = "DROP TABLE books";

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

db.run(createTableQuery, () => {
console.log("Table was created successfully");
db.run(insertRowQuery, titles[0], function (err) {
if (err) {
console.error(`Error occurred while inserting record: ${err.message}`);
} else {
console.log(`Record was inserted successfully with ID: ${this.lastID}`);
}
db.run(insertRowQuery, titles[1], function (err) {
if (err) {
console.error(`Error occurred while inserting record: ${err.message}`);
} else {
console.log(`Record was inserted successfully with ID: ${this.lastID}`);
}
db.run(insertRowQuery, titles[2], function (err) {
if (err) {
console.error(
`Error occurred while inserting record: ${err.message}`,
);
} else {
console.log(
`Record was inserted successfully with ID: ${this.lastID}`,
);
}
db.all(selectAllRowsQuery, (err, rows) => {
if (err) {
console.error(
`Error occurred while fetching records: ${err.message}`,
);
} else {
console.log("All records were fetched successfully");
for (const row of rows) {
console.log(`id:${row.id}, title:${row.title}`);
}
}
db.run(dropTableQuery, () => {
console.log("Table was deleted successfully");
db.close();
});
});
});
});
});
});
37 changes: 37 additions & 0 deletions 03.asynchronous/sqlite_callback_no_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env node

import sqlite3 from "sqlite3";

sqlite3.verbose();
const db = new sqlite3.Database(":memory:");

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

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

db.run(createTableQuery, () => {
console.log("Table was created successfully");
db.run(insertRowQuery, titles[0], function () {
console.log(`Record was inserted successfully with ID: ${this.lastID}`);
db.run(insertRowQuery, titles[1], function () {
console.log(`Record was inserted successfully with ID: ${this.lastID}`);
db.run(insertRowQuery, titles[2], function () {
console.log(`Record was inserted successfully with ID: ${this.lastID}`);
db.all(selectAllRowsQuery, (err, rows) => {
console.log("All records were fetched successfully");
for (const row of rows) {
console.log(`id:${row.id}, title:${row.title}`);
}
db.run(dropTableQuery, () => {
console.log("Table was deleted successfully");
db.close();
});
});
});
});
});
});
64 changes: 64 additions & 0 deletions 03.asynchronous/sqlite_promise_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env node

import sqlite3 from "sqlite3";
import {
runQueryAsync,
allQueryAsync,
closeDatabaseAsync,
} from "./sqlite_utils.js";

sqlite3.verbose();
const db = new sqlite3.Database(":memory:");

const createTableQuery =
"CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL UNIQUE);";
const insertRowQuery = "INSERT INTO books (title) VALUES (?)";
const selectAllRowsQuery = "SELECT * FROM movies";
const dropTableQuery = "DROP TABLE books";

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

runQueryAsync(db, createTableQuery)
.then(() => {
console.log("Table was created successfully");
return runQueryAsync(db, insertRowQuery, titles[0]);
})
.then((result) => {
console.log(`Record was inserted successfully with ID: ${result.lastID}`);
})
.catch((err) => {
console.error(`Error occurred while inserting record: ${err.message}`);
})
.then(() => runQueryAsync(db, insertRowQuery, titles[1]))
.then((result) => {
console.log(`Record was inserted successfully with ID: ${result.lastID}`);
})
.catch((err) => {
console.error(`Error occurred while inserting record: ${err.message}`);
})
.then(() => runQueryAsync(db, insertRowQuery, titles[2]))
.then((result) => {
console.log(`Record was inserted successfully with ID: ${result.lastID}`);
})
.catch((err) => {
if (err.code === "SQLITE_CONSTRAINT") {
console.error(`Error occurred while inserting record: ${err.message}`);
}
})
.then(() => allQueryAsync(db, selectAllRowsQuery))
.then((rows) => {
console.log("All records were fetched successfully");
for (const row of rows) {
console.log(`id:${row.id}, title:${row.title}`);
}
})
.catch((err) => {
if (err.code === "SQLITE_ERROR") {
console.error(`Error occurred while fetching records: ${err.message}`);
}
})
.then(() => runQueryAsync(db, dropTableQuery))
.then(() => {
console.log("Table was deleted successfully");
})
.finally(() => closeDatabaseAsync(db));
48 changes: 48 additions & 0 deletions 03.asynchronous/sqlite_promise_no_error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env node

import sqlite3 from "sqlite3";
import {
runQueryAsync,
allQueryAsync,
closeDatabaseAsync,
} from "./sqlite_utils.js";

sqlite3.verbose();
const db = new sqlite3.Database(":memory:");

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

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

runQueryAsync(db, createTableQuery)
.then(() => {
console.log("Table was created successfully");
return runQueryAsync(db, insertRowQuery, titles[0]);
})
.then((result) => {
console.log(`Record was inserted successfully with ID: ${result.lastID}`);
return runQueryAsync(db, insertRowQuery, titles[1]);
})
.then((result) => {
console.log(`Record was inserted successfully with ID: ${result.lastID}`);
return runQueryAsync(db, insertRowQuery, titles[2]);
})
.then((result) => {
console.log(`Record was inserted successfully with ID: ${result.lastID}`);
return allQueryAsync(db, selectAllRowsQuery);
})
.then((rows) => {
console.log("All records were fetched successfully");
for (const row of rows) {
console.log(`id:${row.id}, title:${row.title}`);
}
return runQueryAsync(db, dropTableQuery);
})
.then(() => {
console.log("Table was deleted successfully");
})
.finally(() => closeDatabaseAsync(db));
35 changes: 35 additions & 0 deletions 03.asynchronous/sqlite_utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export function runQueryAsync(db, query, params) {
return new Promise((resolve, reject) => {
db.run(query, params, function (err) {
if (err) {
reject(err);
} else {
resolve(this);
}
});
});
}

export function allQueryAsync(db, query, params) {
return new Promise((resolve, reject) => {
db.all(query, params, (err, rows) => {
if (err) {
reject(err);
} else {
resolve(rows);
}
});
});
}

export function closeDatabaseAsync(db) {
return new Promise((resolve, reject) => {
db.close((err) => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}