From 332ca853f350c69c95a145738db93cf5a8963cea Mon Sep 17 00:00:00 2001 From: barthofu Date: Fri, 21 Jun 2024 12:08:51 +0000 Subject: [PATCH] chore(deps): bump mikro-orm to v6 --- mikro-orm.config.ts | 6 ++-- package-lock.json | 19 +++--------- package.json | 18 +++++------ src/commands/Admin/prefix.ts | 2 +- src/configs/database.ts | 48 +++++++++++++++++------------ src/entities/Data.ts | 8 ++--- src/entities/Guild.ts | 4 +-- src/entities/Image.ts | 2 +- src/entities/Pastebin.ts | 2 +- src/entities/Stat.ts | 2 +- src/entities/User.ts | 4 +-- src/services/Database.ts | 7 ++--- src/services/ImagesUpload.ts | 5 +-- src/services/Pastebin.ts | 4 +-- src/services/Stats.ts | 4 +-- src/utils/functions/synchronizer.ts | 8 ++--- src/utils/types/database.d.ts | 27 ++-------------- src/utils/types/environment.ts | 5 ++- 18 files changed, 74 insertions(+), 101 deletions(-) diff --git a/mikro-orm.config.ts b/mikro-orm.config.ts index 57eb8715..3c31e0cb 100644 --- a/mikro-orm.config.ts +++ b/mikro-orm.config.ts @@ -3,7 +3,7 @@ import 'reflect-metadata' import process from 'node:process' -import { Options } from '@mikro-orm/core' +import { defineConfig, Options } from '@mikro-orm/core' import * as entities from '@/entities' import { PluginsManager } from '@/services' @@ -15,8 +15,8 @@ export default async () => { const pluginsManager = await resolveDependency(PluginsManager) await pluginsManager.loadPlugins() - return { + return defineConfig({ ...mikroORMConfig[process.env.NODE_ENV || 'development'] as Options, entities: [...Object.values(entities), ...pluginsManager.getEntities()], - } + }) } diff --git a/package-lock.json b/package-lock.json index 88ac35a0..7209e333 100644 --- a/package-lock.json +++ b/package-lock.json @@ -72,10 +72,9 @@ "@antfu/eslint-config": "^2.6.4", "@types/archiver": "^6.0.2", "@types/body-parser": "^1.19.5", - "@types/cron": "~2.4.0", "@types/dateformat": "~5.0.2", "@types/express": "~4.17.21", - "@types/node": "~20.14.6", + "@types/node": "~20.14.7", "@types/node-os-utils": "~1.3.4", "@types/pidusage": "~2.0.5", "@types/semver": "^7.5.8", @@ -2127,16 +2126,6 @@ "@types/node": "*" } }, - "node_modules/@types/cron": { - "version": "2.4.0", - "resolved": "https://registry.npmjs.org/@types/cron/-/cron-2.4.0.tgz", - "integrity": "sha512-5bBaAkqvSFBX8JMi/xCofNzG5E594TNsApMz68dLd/sQYz/HGQqgcxGHTRjOvD4G3Y+YF1Oo3S7QdCvKt1KAJQ==", - "deprecated": "This is a stub types definition. cron provides its own type definitions, so you do not need this installed.", - "dev": true, - "dependencies": { - "cron": "*" - } - }, "node_modules/@types/dateformat": { "version": "5.0.2", "resolved": "https://registry.npmjs.org/@types/dateformat/-/dateformat-5.0.2.tgz", @@ -2279,9 +2268,9 @@ } }, "node_modules/@types/node": { - "version": "20.14.6", - "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.6.tgz", - "integrity": "sha512-JbA0XIJPL1IiNnU7PFxDXyfAwcwVVrOoqyzzyQTyMeVhBzkJVMSkC1LlVsRQ2lpqiY4n6Bb9oCS6lzDKVQxbZw==", + "version": "20.14.7", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.14.7.tgz", + "integrity": "sha512-uTr2m2IbJJucF3KUxgnGOZvYbN0QgkGyWxG6973HCpMYFy2KfcgYuIwkJQMQkt1VbBMlvWRbpshFTLxnxCZjKQ==", "dependencies": { "undici-types": "~5.26.4" } diff --git a/package.json b/package.json index b06320ea..164ed637 100644 --- a/package.json +++ b/package.json @@ -30,15 +30,12 @@ "@discordx/importer": "~1.3.1", "@discordx/pagination": "~3.5.4", "@discordx/utilities": "~7.0.1", - "@mikro-orm/cli": "~5.6.16", - "@mikro-orm/core": "~5.6.16", - "@mikro-orm/mariadb": "~5.6.16", - "@mikro-orm/migrations": "~5.6.16", - "@mikro-orm/mongodb": "~5.6.16", - "@mikro-orm/mysql": "~5.6.16", - "@mikro-orm/postgresql": "~5.6.16", + "@mikro-orm/cli": "~6.2.9", + "@mikro-orm/core": "~6.2.9", + "@mikro-orm/entity-generator": "~6.2.9", + "@mikro-orm/migrations": "~6.2.9", "@mikro-orm/sql-highlighter": "~1.0.1", - "@mikro-orm/sqlite": "~5.6.16", + "@mikro-orm/sqlite": "~6.2.9", "@tsed/common": "^7.73.0", "@tsed/core": "^7.73.0", "@tsed/di": "^7.73.0", @@ -92,10 +89,9 @@ "@antfu/eslint-config": "^2.6.4", "@types/archiver": "^6.0.2", "@types/body-parser": "^1.19.5", - "@types/cron": "~2.4.0", "@types/dateformat": "~5.0.2", "@types/express": "~4.17.21", - "@types/node": "~20.14.6", + "@types/node": "~20.14.7", "@types/node-os-utils": "~1.3.4", "@types/pidusage": "~2.0.5", "@types/semver": "^7.5.8", @@ -109,7 +105,7 @@ "ts-node": "~10.9.2", "tsc-alias": "~1.8.10", "tsconfig-paths": "~4.2.0", - "typescript": "~5.5.2" + "typescript": "~5.4.5" }, "engines": { "node": ">=18" diff --git a/src/commands/Admin/prefix.ts b/src/commands/Admin/prefix.ts index 8f81547c..42b0056b 100644 --- a/src/commands/Admin/prefix.ts +++ b/src/commands/Admin/prefix.ts @@ -38,7 +38,7 @@ export default class PrefixCommand { if (guildData) { guildData.prefix = prefix || null - this.db.get(Guild).persistAndFlush(guildData) + this.db.em.persistAndFlush(guildData) simpleSuccessEmbed( interaction, diff --git a/src/configs/database.ts b/src/configs/database.ts index fececadd..c4b92e0f 100644 --- a/src/configs/database.ts +++ b/src/configs/database.ts @@ -1,5 +1,8 @@ import { Options } from '@mikro-orm/core' +import { EntityGenerator } from '@mikro-orm/entity-generator' +import { Migrator } from '@mikro-orm/migrations' import { SqlHighlighter } from '@mikro-orm/sql-highlighter' +import { SqliteDriver } from '@mikro-orm/sqlite' // eslint-disable-next-line unused-imports/no-unused-imports import { env } from '@/env' @@ -27,44 +30,44 @@ const envMikroORMConfig = { /** * SQLite */ - type: 'sqlite', + driver: SqliteDriver, dbName: `${databaseConfig.path}db.sqlite`, /** * MongoDB */ - // type: 'mongo', + // driver: MongoDriver, // clientUrl: env['DATABASE_HOST'], /** * PostgreSQL */ - // type: 'postgresql', - // dbName: env['DATABASE_NAME'], - // host: env['DATABASE_HOST'], - // port: Number(env['DATABASE_PORT']),, - // user: env['DATABASE_USER'], - // password: env['DATABASE_PASSWORD'], + // driver: PostgreSqlDriver, + // dbName: env.DATABASE_NAME, + // host: env.DATABASE_HOST, + // port: Number(env.DATABASE_PORT), + // user: env.DATABASE_USER, + // password: env.DATABASE_PASSWORD, /** * MySQL */ - // type: 'mysql', - // dbName: env['DATABASE_NAME'], - // host: env['DATABASE_HOST'], - // port: Number(env['DATABASE_PORT']), - // user: env['DATABASE_USER'], - // password: env['DATABASE_PASSWORD'], + // driver: MySqlDriver, + // dbName: env.DATABASE_NAME, + // host: env.DATABASE_HOST, + // port: Number(env.DATABASE_PORT), + // user: env.DATABASE_USER, + // password: env.DATABASE_PASSWORD, /** * MariaDB */ - // type: 'mariadb', - // dbName: env['DATABASE_NAME'], - // host: env['DATABASE_HOST'], - // port: Number(env['DATABASE_PORT']), - // user: env['DATABASE_USER'], - // password: env['DATABASE_PASSWORD'], + // driver: MariaDbDriver, + // dbName: env.DATABASE_NAME, + // host: env.DATABASE_HOST, + // port: Number(env.DATABASE_PORT), + // user: env.DATABASE_USER, + // password: env.DATABASE_PASSWORD, highlighter: new SqlHighlighter(), debug: false, @@ -74,6 +77,11 @@ const envMikroORMConfig = { emit: 'js', snapshot: true, }, + + extensions: [ + Migrator, + EntityGenerator, + ], }, development: { diff --git a/src/entities/Data.ts b/src/entities/Data.ts index 7a8c466a..a6c95425 100644 --- a/src/entities/Data.ts +++ b/src/entities/Data.ts @@ -19,7 +19,7 @@ type DataType = keyof typeof defaultData // ================= Entity ================== // =========================================== -@Entity({ customRepository: () => DataRepository }) +@Entity({ repository: () => DataRepository }) export class Data extends CustomBaseEntity { [EntityRepositoryType]?: DataRepository @@ -52,10 +52,10 @@ export class DataRepository extends EntityRepository { newData.key = key newData.value = JSON.stringify(value) - await this.persistAndFlush(newData) + await this.em.persistAndFlush(newData) } else { data.value = JSON.stringify(value) - await this.flush() + await this.em.flush() } } @@ -67,7 +67,7 @@ export class DataRepository extends EntityRepository { newData.key = key newData.value = JSON.stringify(value) - await this.persistAndFlush(newData) + await this.em.persistAndFlush(newData) } } diff --git a/src/entities/Guild.ts b/src/entities/Guild.ts index 3188b27a..e26dc462 100644 --- a/src/entities/Guild.ts +++ b/src/entities/Guild.ts @@ -7,7 +7,7 @@ import { CustomBaseEntity } from './BaseEntity' // ================= Entity ================== // =========================================== -@Entity({ customRepository: () => GuildRepository }) +@Entity({ repository: () => GuildRepository }) export class Guild extends CustomBaseEntity { [EntityRepositoryType]?: GuildRepository @@ -37,7 +37,7 @@ export class GuildRepository extends EntityRepository { if (guild) { guild.lastInteract = new Date() - await this.flush() + await this.em.flush() } } diff --git a/src/entities/Image.ts b/src/entities/Image.ts index 77bbdb8d..40b212c1 100644 --- a/src/entities/Image.ts +++ b/src/entities/Image.ts @@ -7,7 +7,7 @@ import { CustomBaseEntity } from './BaseEntity' // ================= Entity ================== // =========================================== -@Entity({ customRepository: () => ImageRepository }) +@Entity({ repository: () => ImageRepository }) export class Image extends CustomBaseEntity { [EntityRepositoryType]?: ImageRepository diff --git a/src/entities/Pastebin.ts b/src/entities/Pastebin.ts index 9aa0b878..d67ce822 100644 --- a/src/entities/Pastebin.ts +++ b/src/entities/Pastebin.ts @@ -5,7 +5,7 @@ import { EntityRepository } from '@mikro-orm/sqlite' // ================= Entity ================== // =========================================== -@Entity({ customRepository: () => PastebinRepository }) +@Entity({ repository: () => PastebinRepository }) export class Pastebin { [EntityRepositoryType]?: PastebinRepository diff --git a/src/entities/Stat.ts b/src/entities/Stat.ts index 5006502b..da7df9d2 100644 --- a/src/entities/Stat.ts +++ b/src/entities/Stat.ts @@ -5,7 +5,7 @@ import { EntityRepository } from '@mikro-orm/sqlite' // ================= Entity ================== // =========================================== -@Entity({ customRepository: () => StatRepository }) +@Entity({ repository: () => StatRepository }) export class Stat { [EntityRepositoryType]?: StatRepository diff --git a/src/entities/User.ts b/src/entities/User.ts index 5e3284e7..637d289d 100644 --- a/src/entities/User.ts +++ b/src/entities/User.ts @@ -7,7 +7,7 @@ import { CustomBaseEntity } from './BaseEntity' // ================= Entity ================== // =========================================== -@Entity({ customRepository: () => UserRepository }) +@Entity({ repository: () => UserRepository }) export class User extends CustomBaseEntity { [EntityRepositoryType]?: UserRepository @@ -31,7 +31,7 @@ export class UserRepository extends EntityRepository { if (user) { user.lastInteract = new Date() - await this.flush() + await this.em.flush() } } diff --git a/src/services/Database.ts b/src/services/Database.ts index 4de2446c..476ebbf1 100644 --- a/src/services/Database.ts +++ b/src/services/Database.ts @@ -186,11 +186,10 @@ export class Database { } isSQLiteDatabase(): boolean { - const type = mikroORMConfig[env.NODE_ENV]!.type + const config = mikroORMConfig[env.NODE_ENV] - if (type) - return ['sqlite', 'better-sqlite'].includes(type) - else return false + // @ts-expect-error + return !!config.dbName && !config.port } } diff --git a/src/services/ImagesUpload.ts b/src/services/ImagesUpload.ts index 8d8f7b35..58e54255 100644 --- a/src/services/ImagesUpload.ts +++ b/src/services/ImagesUpload.ts @@ -61,7 +61,8 @@ export class ImagesUpload { // delete the image if it is not in the filesystem anymore if (!images.includes(imagePath)) { - await this.imageRepo.remove(image).flush() + await this.imageRepo.nativeDelete(image) + await this.db.em.flush() await this.deleteImageFromImgur(image) } else if (!await this.isImgurImageValid(image.url)) { // reupload if the image is not on imgur anymore @@ -140,7 +141,7 @@ export class ImagesUpload { image.tags = imageBasePath.split('/') image.hash = imageHash image.deleteHash = uploadResponse.data.deletehash || '' - await this.imageRepo.persistAndFlush(image) + await this.db.em.persistAndFlush(image) // log the success this.logger.log( diff --git a/src/services/Pastebin.ts b/src/services/Pastebin.ts index 1e80757d..fc48b22b 100644 --- a/src/services/Pastebin.ts +++ b/src/services/Pastebin.ts @@ -32,7 +32,7 @@ export class Pastebin { if (lifetime) pasteEntity.lifetime = Math.floor(lifetime) - await this.db.get(PastebinEntity).persistAndFlush(pasteEntity) + await this.db.em.persistAndFlush(pasteEntity) return paste.paste } @@ -46,7 +46,7 @@ export class Pastebin { return await this.client.deletePaste(id, paste.editCode) - await this.db.get(PastebinEntity).remove(paste) + await this.db.get(PastebinEntity).nativeDelete(paste) } @Schedule('*/30 * * * *') diff --git a/src/services/Stats.ts b/src/services/Stats.ts index c403343e..22e0b733 100644 --- a/src/services/Stats.ts +++ b/src/services/Stats.ts @@ -47,7 +47,7 @@ export class Stats { if (additionalData) stat.additionalData = additionalData - await this.statsRepo.persistAndFlush(stat) + await this.db.em.persistAndFlush(stat) } /** @@ -140,7 +140,7 @@ export class Stats { return slashCommands.sort((a: any, b: any) => b.count - a.count) } else if ('aggregate' in this.db.em) { - // @ts-expect-error - aggregate is not in the types + // @ts-expect-error const slashCommands = await this.db.em.aggregate(Stat, [ { $match: allInteractions, diff --git a/src/utils/functions/synchronizer.ts b/src/utils/functions/synchronizer.ts index 387472f9..8ffdfe21 100644 --- a/src/utils/functions/synchronizer.ts +++ b/src/utils/functions/synchronizer.ts @@ -22,7 +22,7 @@ export async function syncUser(user: DUser) { // add user to the db const newUser = new User() newUser.id = user.id - await userRepo.persistAndFlush(newUser) + await db.em.persistAndFlush(newUser) // record new user both in logs and stats stats.register('NEW_USER', user.id) @@ -51,7 +51,7 @@ export async function syncGuild(guildId: string, client: Client) { // recover deleted guild deletedGuildData.deleted = false - await guildRepo.persistAndFlush(deletedGuildData) + await db.em.persistAndFlush(deletedGuildData) stats.register('RECOVER_GUILD', guildId) logger.logGuild('RECOVER_GUILD', guildId) @@ -60,7 +60,7 @@ export async function syncGuild(guildId: string, client: Client) { const newGuild = new Guild() newGuild.id = guildId - await guildRepo.persistAndFlush(newGuild) + await db.em.persistAndFlush(newGuild) stats.register('NEW_GUILD', guildId) logger.logGuild('NEW_GUILD', guildId) @@ -69,7 +69,7 @@ export async function syncGuild(guildId: string, client: Client) { // guild is deleted but still exists in the database guildData.deleted = true - await guildRepo.persistAndFlush(guildData) + await db.em.persistAndFlush(guildData) stats.register('DELETE_GUILD', guildId) logger.logGuild('DELETE_GUILD', guildId) diff --git a/src/utils/types/database.d.ts b/src/utils/types/database.d.ts index 20f271ac..9f638e16 100644 --- a/src/utils/types/database.d.ts +++ b/src/utils/types/database.d.ts @@ -3,28 +3,5 @@ type DatabaseSize = { backups: number | null } -type DatabaseConfigs = { - sqlite: { - driver: import('@mikro-orm/sqlite').SqliteDriver - entityManager: import('@mikro-orm/sqlite').SqlEntityManager - } - postgresql: { - driver: import('@mikro-orm/postgresql').PostgreSqlDriver - entityManager: import('@mikro-orm/postgresql').SqlEntityManager - } - mysql: { - driver: import('@mikro-orm/mysql').MySqlDriver - entityManager: import('@mikro-orm/mysql').SqlEntityManager - } - mariadb: { - driver: import('@mikro-orm/mariadb').MariaDbDriver - entityManager: import('@mikro-orm/mariadb').SqlEntityManager - } - mongo: { - driver: import('@mikro-orm/mongodb').MongoDriver - entityManager: import('@mikro-orm/mongodb').MongoEntityManager - } -} - -type DatabaseDriver = DatabaseConfigs[typeof import('@/configs').mikroORMConfig['production']['type']]['driver'] -type DatabaseEntityManager = DatabaseConfigs[typeof import('@/configs').mikroORMConfig['production']['type']]['entityManager'] +type DatabaseDriver = import('@mikro-orm/sqlite').SqliteDriver +type DatabaseEntityManager = import('@mikro-orm/sqlite').SqlEntityManager \ No newline at end of file diff --git a/src/utils/types/environment.ts b/src/utils/types/environment.ts index 3297cff5..9957ab00 100644 --- a/src/utils/types/environment.ts +++ b/src/utils/types/environment.ts @@ -24,7 +24,10 @@ export const env = cleanEnv(process.env, { }) export function checkEnvironmentVariables() { - if (!['sqlite', 'better-sqlite'].includes(mikroORMConfig[env.NODE_ENV].type)) { + const config = mikroORMConfig[env.NODE_ENV] + // @ts-expect-error + const isSqliteDatabase = !!config.dbName && !config.port + if (isSqliteDatabase) { cleanEnv(process.env, { DATABASE_HOST: str(), DATABASE_PORT: num(),