mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-13 21:35:02 +00:00
feat: upgrade to TypeORM 0.3
This commit is contained in:
parent
8cee4ec1e4
commit
761ff27771
57 changed files with 412 additions and 325 deletions
|
@ -1,11 +1,11 @@
|
|||
import moment from "moment-timezone";
|
||||
import { FindConditions, getRepository, In, IsNull, Not, Repository } from "typeorm";
|
||||
import { FindOptionsWhere, In, IsNull, Not, Repository } from "typeorm";
|
||||
import { Queue } from "../Queue";
|
||||
import { DAYS, DBDateFormat, HOURS, MINUTES } from "../utils";
|
||||
import { BaseGuildRepository } from "./BaseGuildRepository";
|
||||
import { connection } from "./db";
|
||||
import { dataSource } from "./dataSource";
|
||||
import { Counter } from "./entities/Counter";
|
||||
import { CounterTrigger, isValidCounterComparisonOp, TriggerComparisonOp } from "./entities/CounterTrigger";
|
||||
import { CounterTrigger, TriggerComparisonOp, isValidCounterComparisonOp } from "./entities/CounterTrigger";
|
||||
import { CounterTriggerState } from "./entities/CounterTriggerState";
|
||||
import { CounterValue } from "./entities/CounterValue";
|
||||
|
||||
|
@ -17,11 +17,11 @@ const MAX_COUNTER_VALUE = 2147483647; // 2^31-1, for MySQL INT
|
|||
const decayQueue = new Queue();
|
||||
|
||||
async function deleteCountersMarkedToBeDeleted(): Promise<void> {
|
||||
await getRepository(Counter).createQueryBuilder().where("delete_at <= NOW()").delete().execute();
|
||||
await dataSource.getRepository(Counter).createQueryBuilder().where("delete_at <= NOW()").delete().execute();
|
||||
}
|
||||
|
||||
async function deleteTriggersMarkedToBeDeleted(): Promise<void> {
|
||||
await getRepository(CounterTrigger).createQueryBuilder().where("delete_at <= NOW()").delete().execute();
|
||||
await dataSource.getRepository(CounterTrigger).createQueryBuilder().where("delete_at <= NOW()").delete().execute();
|
||||
}
|
||||
|
||||
setInterval(deleteCountersMarkedToBeDeleted, 1 * HOURS);
|
||||
|
@ -38,10 +38,10 @@ export class GuildCounters extends BaseGuildRepository {
|
|||
|
||||
constructor(guildId) {
|
||||
super(guildId);
|
||||
this.counters = getRepository(Counter);
|
||||
this.counterValues = getRepository(CounterValue);
|
||||
this.counterTriggers = getRepository(CounterTrigger);
|
||||
this.counterTriggerStates = getRepository(CounterTriggerState);
|
||||
this.counters = dataSource.getRepository(Counter);
|
||||
this.counterValues = dataSource.getRepository(CounterValue);
|
||||
this.counterTriggers = dataSource.getRepository(CounterTrigger);
|
||||
this.counterTriggerStates = dataSource.getRepository(CounterTriggerState);
|
||||
}
|
||||
|
||||
async findOrCreateCounter(name: string, perChannel: boolean, perUser: boolean): Promise<Counter> {
|
||||
|
@ -80,7 +80,7 @@ export class GuildCounters extends BaseGuildRepository {
|
|||
}
|
||||
|
||||
async markUnusedCountersToBeDeleted(idsToKeep: number[]): Promise<void> {
|
||||
const criteria: FindConditions<Counter> = {
|
||||
const criteria: FindOptionsWhere<Counter> = {
|
||||
guild_id: this.guildId,
|
||||
delete_at: IsNull(),
|
||||
};
|
||||
|
@ -256,10 +256,12 @@ export class GuildCounters extends BaseGuildRepository {
|
|||
throw new Error(`Invalid comparison value: ${reverseComparisonValue}`);
|
||||
}
|
||||
|
||||
return connection.transaction(async (entityManager) => {
|
||||
return dataSource.transaction(async (entityManager) => {
|
||||
const existing = await entityManager.findOne(CounterTrigger, {
|
||||
counter_id: counterId,
|
||||
name: triggerName,
|
||||
where: {
|
||||
counter_id: counterId,
|
||||
name: triggerName,
|
||||
},
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
|
@ -308,11 +310,13 @@ export class GuildCounters extends BaseGuildRepository {
|
|||
channelId = channelId || "0";
|
||||
userId = userId || "0";
|
||||
|
||||
return connection.transaction(async (entityManager) => {
|
||||
return dataSource.transaction(async (entityManager) => {
|
||||
const previouslyTriggered = await entityManager.findOne(CounterTriggerState, {
|
||||
trigger_id: counterTrigger.id,
|
||||
user_id: userId!,
|
||||
channel_id: channelId!,
|
||||
where: {
|
||||
trigger_id: counterTrigger.id,
|
||||
user_id: userId!,
|
||||
channel_id: channelId!,
|
||||
},
|
||||
});
|
||||
|
||||
if (previouslyTriggered) {
|
||||
|
@ -356,7 +360,7 @@ export class GuildCounters extends BaseGuildRepository {
|
|||
async checkAllValuesForTrigger(
|
||||
counterTrigger: CounterTrigger,
|
||||
): Promise<Array<{ channelId: string; userId: string }>> {
|
||||
return connection.transaction(async (entityManager) => {
|
||||
return dataSource.transaction(async (entityManager) => {
|
||||
const matchingValues = await entityManager
|
||||
.createQueryBuilder(CounterValue, "cv")
|
||||
.leftJoin(
|
||||
|
@ -407,7 +411,7 @@ export class GuildCounters extends BaseGuildRepository {
|
|||
channelId = channelId || "0";
|
||||
userId = userId || "0";
|
||||
|
||||
return connection.transaction(async (entityManager) => {
|
||||
return dataSource.transaction(async (entityManager) => {
|
||||
const matchingValue = await entityManager
|
||||
.createQueryBuilder(CounterValue, "cv")
|
||||
.innerJoin(
|
||||
|
@ -446,7 +450,7 @@ export class GuildCounters extends BaseGuildRepository {
|
|||
async checkAllValuesForReverseTrigger(
|
||||
counterTrigger: CounterTrigger,
|
||||
): Promise<Array<{ channelId: string; userId: string }>> {
|
||||
return connection.transaction(async (entityManager) => {
|
||||
return dataSource.transaction(async (entityManager) => {
|
||||
const matchingValues: Array<{
|
||||
id: string;
|
||||
triggerStateId: string;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue