Skip to content

Commit

Permalink
refactor: migrate to ESM (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Dec 27, 2023
1 parent fdbb919 commit 502b4cb
Show file tree
Hide file tree
Showing 34 changed files with 143 additions and 122 deletions.
15 changes: 15 additions & 0 deletions app/db/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { type ConnectionOptions } from "mysql2";
import { serverConfig } from "../utils/config";

export function dbConfig() {
return {
host: serverConfig.APP_MYSQL_HOST,
port: serverConfig.APP_MYSQL_PORT,
user: serverConfig.APP_MYSQL_USER,
password: serverConfig.APP_MYSQL_PASSWORD,
database: serverConfig.APP_MYSQL_DATABASE,
ssl: serverConfig.APP_MYSQL_SSL ? {} : undefined,
multipleStatements: true,
timezone: "+00:00", // planetscale and development mysql image have UTC localtime
} satisfies ConnectionOptions;
}
5 changes: 2 additions & 3 deletions app/db/drizzle-client.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type { Connection } from "mysql2";
import { createConnection } from "mysql2/promise";
import { uninitialized } from "../utils/misc";
import type { PaginationMetadata, PaginationParams } from "../utils/pagination";
import knexfile from "./knexfile.server";
import { dbConfig } from "./config";
import type { DeckCache, PracticeActionType, PracticeQueueType } from "./types";

//
Expand Down Expand Up @@ -353,8 +353,7 @@ export async function initializeDrizzleClient() {
db = globalThis.__drizzleClient ??= await inner();

async function inner() {
const config = knexfile();
const connection = await createConnection(config.connection);
const connection = await createConnection(dbConfig());
return drizzle(connection, {
logger: process.env["DEBUG"]?.includes("drizzle"), // enable query logging by DEBUG=drizzle
});
Expand Down
16 changes: 16 additions & 0 deletions app/db/knexfile.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Knex } from "knex";
import { initializeConfigServer } from "../utils/config";
import { dbConfig } from "./config";

export default function knexfile() {
initializeConfigServer();

return {
client: "mysql2",
connection: dbConfig(),
migrations: {
directory: "migrations",
stub: "__migration-stub.ts",
},
} satisfies Knex.Config;
}
24 changes: 0 additions & 24 deletions app/db/knexfile.server.ts

This file was deleted.

4 changes: 2 additions & 2 deletions app/db/migrations/20220226100116_create-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
await knex.raw(`
CREATE TABLE users (
id BIGINT AUTO_INCREMENT,
Expand All @@ -20,7 +20,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
await knex.raw(`
DROP TABLE users;
`);
Expand Down
4 changes: 2 additions & 2 deletions app/db/migrations/20220402105114_add-user-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
await knex.raw(`
ALTER TABLE users ADD settings JSON DEFAULT NULL;
`);
Expand All @@ -12,7 +12,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
await knex.raw(`
ALTER TABLE users DROP COLUMN settings;
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
await knex.raw(`
ALTER TABLE users MODIFY createdAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
`);
Expand All @@ -15,7 +15,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
await knex.raw(`
ALTER TABLE users MODIFY updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
await knex.raw(`
UPDATE users SET settings = JSON_OBJECT() WHERE settings IS NULL
`);
Expand All @@ -15,7 +15,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
await knex.raw(`
ALTER TABLE users MODIFY settings JSON DEFAULT NULL
`);
Expand Down
10 changes: 6 additions & 4 deletions app/db/migrations/20220408030906_change-users-settings.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import * as assert from "assert/strict";

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
if (process.env.MIGRATION_UNIT_TEST) await testUp.before(knex);

await knex.raw(`
Expand All @@ -21,7 +23,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
if (process.env.MIGRATION_UNIT_TEST) await testDown.before(knex);

await knex.raw(`
Expand All @@ -48,7 +50,7 @@ const testUp = {
const [rows] = await knex.raw(
`SELECT language1, language2 FROM users ORDER BY username`
);
require("assert").deepStrictEqual(rows, [
assert.deepEqual(rows, [
{ language1: null, language2: null },
{ language1: "fr", language2: null },
{ language1: "fr", language2: "en" },
Expand All @@ -70,7 +72,7 @@ const testDown = {
const [rows] = await knex.raw(
`SELECT settings FROM users ORDER BY username`
);
require("assert").deepStrictEqual(rows, [
assert.deepEqual(rows, [
{ settings: { language1: null, language2: null } },
{ settings: { language1: "fr", language2: null } },
{ settings: { language1: "fr", language2: "en" } },
Expand Down
4 changes: 2 additions & 2 deletions app/db/migrations/20220410120806_create-videos.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
await knex.raw(`
CREATE TABLE videos (
id BIGINT AUTO_INCREMENT,
Expand Down Expand Up @@ -43,7 +43,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
await knex.raw(`
DROP TABLE captionEntries;
`);
Expand Down
4 changes: 2 additions & 2 deletions app/db/migrations/20220411031243_create-bookmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
await knex.raw(`
CREATE TABLE bookmarkEntries (
id BIGINT AUTO_INCREMENT,
Expand All @@ -26,7 +26,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
await knex.raw(`
DROP TABLE bookmarkEntries;
`);
Expand Down
4 changes: 2 additions & 2 deletions app/db/migrations/20220417071104_create-practice-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
await knex.raw(`
CREATE TABLE decks (
id BIGINT AUTO_INCREMENT,
Expand Down Expand Up @@ -51,7 +51,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
await knex.raw(`DROP TABLE practiceActions`);
await knex.raw(`DROP TABLE practiceEntries`);
await knex.raw(`DROP TABLE decks`);
Expand Down
4 changes: 2 additions & 2 deletions app/db/migrations/20220504095705_add-deck-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
await knex.raw(`
ALTER TABLE decks
ADD newEntriesPerDay INTEGER NOT NULL DEFAULT 50,
Expand All @@ -16,7 +16,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
await knex.raw(`
ALTER TABLE decks
DROP newEntriesPerDay,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
for (const table of TABLES) {
await knex.raw(`
ALTER TABLE ${table} MODIFY createdAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP;
Expand All @@ -15,7 +15,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
for (const table of TABLES) {
await knex.raw(`
ALTER TABLE ${table} MODIFY updatedAt DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
Expand Down
4 changes: 2 additions & 2 deletions app/db/migrations/20220508102506_add-deck-randomize.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
await knex.raw(`
ALTER TABLE decks ADD randomMode BOOL NOT NULL DEFAULT FALSE
`);
Expand All @@ -12,7 +12,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
await knex.raw(`
ALTER TABLE decks DROP randomMode
`);
Expand Down
4 changes: 2 additions & 2 deletions app/db/migrations/20220528054447_add-user-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
await knex.raw(`
ALTER TABLE users ADD timezone VARCHAR(32) NOT NULL DEFAULT '+00:00'
`);
Expand All @@ -12,7 +12,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
await knex.raw(`
ALTER TABLE users DROP timezone
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
await knex.raw(`
ALTER TABLE videos ADD bookmarkEntriesCount INT NOT NULL DEFAULT 0
`);
Expand All @@ -12,7 +12,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
await knex.raw(`
ALTER TABLE videos DROP bookmarkEntriesCount
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
await knex.raw(`
ALTER TABLE practiceEntries ADD practiceActionsCount INT NOT NULL DEFAULT 0
`);
Expand All @@ -12,7 +12,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
await knex.raw(`
ALTER TABLE practiceEntries DROP practiceActionsCount
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
await knex.raw(`
ALTER TABLE decks ADD practiceEntriesCountByQueueType JSON NOT NULL DEFAULT (JSON_OBJECT('NEW', 0, 'LEARN', 0, 'REVIEW', 0))
`);
Expand All @@ -12,7 +12,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
await knex.raw(`
ALTER TABLE decks DROP practiceEntriesCountByQueueType
`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = async function (knex) {
export const up = async function (knex) {
await knex.raw(`
ALTER TABLE practiceActions ADD KEY practiceActions_createdAt_key (createdAt)
`);
Expand All @@ -12,7 +12,7 @@ exports.up = async function (knex) {
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = async function (knex) {
export const down = async function (knex) {
await knex.raw(`
ALTER TABLE practiceActions DROP KEY practiceActions_createdAt_key
`);
Expand Down
Loading

0 comments on commit 502b4cb

Please sign in to comment.