diff --git a/src/index.ts b/src/index.ts index e8ba327bb8..739cba5ae4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import './lib/globals'; import './lib/data/itemAliases'; import './lib/data/trophies'; import './lib/crons'; diff --git a/src/lib/Task.ts b/src/lib/Task.ts index 44c40147ef..88eeda2ba4 100644 --- a/src/lib/Task.ts +++ b/src/lib/Task.ts @@ -94,7 +94,7 @@ import { nightmareZoneTask } from './../tasks/minions/minigames/nightmareZoneAct import { underwaterAgilityThievingTask } from './../tasks/minions/underwaterActivity'; import { modifyBusyCounter } from './busyCounterCache'; import { minionActivityCache } from './constants'; -import { convertStoredActivityToFlatActivity, prisma } from './settings/prisma'; +import { convertStoredActivityToFlatActivity } from './settings/prisma'; import { activitySync, minionActivityCacheDelete } from './settings/settings'; import { logError } from './util/logError'; diff --git a/src/lib/globals.ts b/src/lib/globals.ts new file mode 100644 index 0000000000..7330120120 --- /dev/null +++ b/src/lib/globals.ts @@ -0,0 +1,41 @@ +import { isMainThread } from 'node:worker_threads'; +import { TSRedis } from '@oldschoolgg/toolkit/dist/lib/TSRedis'; +import { PrismaClient } from '@prisma/client'; + +import { production } from '../config'; +import { globalConfig } from './constants'; + +declare global { + var prisma: PrismaClient; + var redis: TSRedis; +} + +function makePrismaClient(): PrismaClient { + if (!production && !process.env.TEST) console.log('Making prisma client...'); + if (!isMainThread && !process.env.TEST) { + throw new Error('Prisma client should only be created on the main thread.'); + } + + return new PrismaClient({ + log: [ + { + emit: 'event', + level: 'query' + } + ] + }); +} +// biome-ignore lint/suspicious/noRedeclare: +const prisma = global.prisma || makePrismaClient(); +global.prisma = prisma; + +function makeRedisClient(): TSRedis { + if (!production && !process.env.TEST) console.log('Making Redis client...'); + if (!isMainThread && !process.env.TEST) { + throw new Error('Redis client should only be created on the main thread.'); + } + return new TSRedis({ mocked: !globalConfig.redisPort, port: globalConfig.redisPort }); +} +// biome-ignore lint/suspicious/noRedeclare: +const redis = global.redis || makeRedisClient(); +global.redis = redis; diff --git a/src/lib/musicCape.ts b/src/lib/musicCape.ts index 4b658e4695..4e18293e53 100644 --- a/src/lib/musicCape.ts +++ b/src/lib/musicCape.ts @@ -9,7 +9,7 @@ import { championScrolls } from './data/CollectionsExport'; import { RandomEvents } from './randomEvents'; import type { MinigameName } from './settings/minigames'; import { Minigames } from './settings/minigames'; -import { getUsersActivityCounts, prisma } from './settings/prisma'; +import { getUsersActivityCounts } from './settings/prisma'; import type { RequirementFailure } from './structures/Requirements'; import { Requirements } from './structures/Requirements'; import { itemNameFromID } from './util'; diff --git a/src/lib/settings/prisma.ts b/src/lib/settings/prisma.ts index 6ad522b590..2fc77cb41d 100644 --- a/src/lib/settings/prisma.ts +++ b/src/lib/settings/prisma.ts @@ -1,48 +1,11 @@ import { isMainThread } from 'node:worker_threads'; -import { TSRedis } from '@oldschoolgg/toolkit/dist/lib/TSRedis'; import type { Activity, Prisma } from '@prisma/client'; -import { PrismaClient, activity_type_enum } from '@prisma/client'; +import { activity_type_enum } from '@prisma/client'; import { production } from '../../config'; -import { globalConfig } from '../constants'; import type { ActivityTaskData } from '../types/minions'; import { sqlLog } from '../util/logger'; -declare global { - var prisma: PrismaClient; - var redis: TSRedis; -} - -function makePrismaClient(): PrismaClient { - if (!production && !process.env.TEST) console.log('Making prisma client...'); - if (!isMainThread && !process.env.TEST) { - throw new Error('Prisma client should only be created on the main thread.'); - } - - return new PrismaClient({ - log: [ - { - emit: 'event', - level: 'query' - } - ] - }); -} -// biome-ignore lint/suspicious/noRedeclare: -export const prisma = global.prisma || makePrismaClient(); -global.prisma = prisma; - -function makeRedisClient(): TSRedis { - if (!production && !process.env.TEST) console.log('Making Redis client...'); - if (!isMainThread && !process.env.TEST) { - throw new Error('Redis client should only be created on the main thread.'); - } - return new TSRedis({ mocked: !globalConfig.redisPort, port: globalConfig.redisPort }); -} -// biome-ignore lint/suspicious/noRedeclare: -export const redis = global.redis || makeRedisClient(); -global.redis = redis; - export const queryCountStore = { value: 0 }; if (isMainThread) { diff --git a/src/lib/settings/settings.ts b/src/lib/settings/settings.ts index 9cf5c4a0c7..98d19f3121 100644 --- a/src/lib/settings/settings.ts +++ b/src/lib/settings/settings.ts @@ -16,7 +16,7 @@ import { minionActivityCache } from '../constants'; import { channelIsSendable, isGroupActivity } from '../util'; import { handleInteractionError, interactionReply } from '../util/interactionReply'; import { logError } from '../util/logError'; -import { convertStoredActivityToFlatActivity, prisma } from './prisma'; +import { convertStoredActivityToFlatActivity } from './prisma'; export * from './minigames'; diff --git a/src/lib/tickers.ts b/src/lib/tickers.ts index a99ba81155..9813ff8dba 100644 --- a/src/lib/tickers.ts +++ b/src/lib/tickers.ts @@ -10,7 +10,7 @@ import { BitField, Channel, PeakTier, informationalButtons } from './constants'; import { GrandExchange } from './grandExchange'; import { cacheGEPrices } from './marketPrices'; import { collectMetrics } from './metrics'; -import { prisma, queryCountStore } from './settings/prisma'; +import { queryCountStore } from './settings/prisma'; import { runCommand } from './settings/settings'; import { getFarmingInfo } from './skilling/functions/getFarmingInfo'; import Farming from './skilling/skills/farming'; diff --git a/src/lib/util/repeatStoredTrip.ts b/src/lib/util/repeatStoredTrip.ts index b1ffae0eb0..25549dad68 100644 --- a/src/lib/util/repeatStoredTrip.ts +++ b/src/lib/util/repeatStoredTrip.ts @@ -8,7 +8,7 @@ import { autocompleteMonsters } from '../../mahoji/commands/k'; import type { PvMMethod } from '../constants'; import { SlayerActivityConstants } from '../minions/data/combatConstants'; import { darkAltarRunes } from '../minions/functions/darkAltarCommand'; -import { convertStoredActivityToFlatActivity, prisma } from '../settings/prisma'; +import { convertStoredActivityToFlatActivity } from '../settings/prisma'; import { runCommand } from '../settings/settings'; import type { ActivityTaskOptionsWithQuantity, diff --git a/src/mahoji/commands/admin.ts b/src/mahoji/commands/admin.ts index 30b2675086..89c626fbfd 100644 --- a/src/mahoji/commands/admin.ts +++ b/src/mahoji/commands/admin.ts @@ -33,7 +33,7 @@ import { generateGearImage } from '../../lib/gear/functions/generateGearImage'; import type { GearSetup } from '../../lib/gear/types'; import { GrandExchange } from '../../lib/grandExchange'; import { runRolesTask } from '../../lib/rolesTask'; -import { countUsersWithItemInCl, prisma } from '../../lib/settings/prisma'; +import { countUsersWithItemInCl } from '../../lib/settings/prisma'; import { cancelTask, minionActivityCacheDelete } from '../../lib/settings/settings'; import { sorts } from '../../lib/sorts'; import { Gear } from '../../lib/structures/Gear'; diff --git a/src/mahoji/commands/tools.ts b/src/mahoji/commands/tools.ts index 7fbe6b9f36..886739a859 100644 --- a/src/mahoji/commands/tools.ts +++ b/src/mahoji/commands/tools.ts @@ -25,7 +25,7 @@ import pets from '../../lib/data/pets'; import killableMonsters, { effectiveMonsters, NightmareMonster } from '../../lib/minions/data/killableMonsters'; import type { MinigameName } from '../../lib/settings/minigames'; import { Minigames } from '../../lib/settings/minigames'; -import { convertStoredActivityToFlatActivity, prisma } from '../../lib/settings/prisma'; +import { convertStoredActivityToFlatActivity } from '../../lib/settings/prisma'; import Skills from '../../lib/skilling/skills'; import { formatDuration, diff --git a/src/tasks/minions/minigames/infernoActivity.ts b/src/tasks/minions/minigames/infernoActivity.ts index 7a8c46ad2e..c5dc66f388 100644 --- a/src/tasks/minions/minigames/infernoActivity.ts +++ b/src/tasks/minions/minigames/infernoActivity.ts @@ -6,7 +6,7 @@ import type { ItemBank } from 'oldschooljs/dist/meta/types'; import { formatDuration } from '@oldschoolgg/toolkit'; import { Events } from '../../../lib/constants'; import { diariesObject, userhasDiaryTier } from '../../../lib/diaries'; -import { countUsersWithItemInCl, prisma } from '../../../lib/settings/prisma'; +import { countUsersWithItemInCl } from '../../../lib/settings/prisma'; import { getMinigameScore, incrementMinigameScore } from '../../../lib/settings/settings'; import { SkillsEnum } from '../../../lib/skilling/types'; import { calculateSlayerPoints, getUsersCurrentSlayerInfo } from '../../../lib/slayer/slayerUtil';