Skip to content

Commit

Permalink
chore(deps): bump mikro-orm to v6
Browse files Browse the repository at this point in the history
  • Loading branch information
barthofu committed Jun 21, 2024
1 parent b982e82 commit 08d655f
Show file tree
Hide file tree
Showing 18 changed files with 74 additions and 101 deletions.
6 changes: 3 additions & 3 deletions mikro-orm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<DatabaseDriver>,
entities: [...Object.values(entities), ...pluginsManager.getEntities()],
}
})
}
19 changes: 4 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 7 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/commands/Admin/prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
48 changes: 28 additions & 20 deletions src/configs/database.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
Expand All @@ -74,6 +77,11 @@ const envMikroORMConfig = {
emit: 'js',
snapshot: true,
},

extensions: [
Migrator,
EntityGenerator,
],
},

development: {
Expand Down
8 changes: 4 additions & 4 deletions src/entities/Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type DataType = keyof typeof defaultData
// ================= Entity ==================
// ===========================================

@Entity({ customRepository: () => DataRepository })
@Entity({ repository: () => DataRepository })
export class Data extends CustomBaseEntity {

[EntityRepositoryType]?: DataRepository
Expand Down Expand Up @@ -52,10 +52,10 @@ export class DataRepository extends EntityRepository<Data> {
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()
}
}

Expand All @@ -67,7 +67,7 @@ export class DataRepository extends EntityRepository<Data> {
newData.key = key
newData.value = JSON.stringify(value)

await this.persistAndFlush(newData)
await this.em.persistAndFlush(newData)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/entities/Guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CustomBaseEntity } from './BaseEntity'
// ================= Entity ==================
// ===========================================

@Entity({ customRepository: () => GuildRepository })
@Entity({ repository: () => GuildRepository })
export class Guild extends CustomBaseEntity {

[EntityRepositoryType]?: GuildRepository
Expand Down Expand Up @@ -37,7 +37,7 @@ export class GuildRepository extends EntityRepository<Guild> {

if (guild) {
guild.lastInteract = new Date()
await this.flush()
await this.em.flush()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/entities/Image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CustomBaseEntity } from './BaseEntity'
// ================= Entity ==================
// ===========================================

@Entity({ customRepository: () => ImageRepository })
@Entity({ repository: () => ImageRepository })
export class Image extends CustomBaseEntity {

[EntityRepositoryType]?: ImageRepository
Expand Down
2 changes: 1 addition & 1 deletion src/entities/Pastebin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EntityRepository } from '@mikro-orm/sqlite'
// ================= Entity ==================
// ===========================================

@Entity({ customRepository: () => PastebinRepository })
@Entity({ repository: () => PastebinRepository })
export class Pastebin {

[EntityRepositoryType]?: PastebinRepository
Expand Down
2 changes: 1 addition & 1 deletion src/entities/Stat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EntityRepository } from '@mikro-orm/sqlite'
// ================= Entity ==================
// ===========================================

@Entity({ customRepository: () => StatRepository })
@Entity({ repository: () => StatRepository })
export class Stat {

[EntityRepositoryType]?: StatRepository
Expand Down
4 changes: 2 additions & 2 deletions src/entities/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { CustomBaseEntity } from './BaseEntity'
// ================= Entity ==================
// ===========================================

@Entity({ customRepository: () => UserRepository })
@Entity({ repository: () => UserRepository })
export class User extends CustomBaseEntity {

[EntityRepositoryType]?: UserRepository
Expand All @@ -31,7 +31,7 @@ export class UserRepository extends EntityRepository<User> {

if (user) {
user.lastInteract = new Date()
await this.flush()
await this.em.flush()
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/services/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
5 changes: 3 additions & 2 deletions src/services/ImagesUpload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions src/services/Pastebin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 * * * *')
Expand Down
4 changes: 2 additions & 2 deletions src/services/Stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class Stats {
if (additionalData)
stat.additionalData = additionalData

await this.statsRepo.persistAndFlush(stat)
await this.db.em.persistAndFlush(stat)
}

/**
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/utils/functions/synchronizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 08d655f

Please sign in to comment.