3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 20:35:02 +00:00

Switch from Knex to TypeORM. Update Knub.

This commit is contained in:
Dragory 2018-10-26 06:41:20 +03:00
parent e3ff4cef45
commit f9c16263ae
49 changed files with 1192 additions and 1395 deletions

View file

@ -0,0 +1,50 @@
export class BaseRepository {
private static guildInstances: Map<string, any>;
private nextRelations: string[];
protected guildId: string;
constructor(guildId: string) {
this.guildId = guildId;
this.nextRelations = [];
}
/**
* Returns a cached instance of the inheriting class for the specified guildId,
* or creates a new instance if one doesn't exist yet
*/
public static getInstance<T extends typeof BaseRepository>(this: T, guildId: string): InstanceType<T> {
if (!this.guildInstances) {
this.guildInstances = new Map();
}
if (!this.guildInstances.has(guildId)) {
this.guildInstances.set(guildId, new this(guildId));
}
return this.guildInstances.get(guildId) as InstanceType<T>;
}
/**
* Primes the specified relation(s) to be used in the next database operation.
* Can be chained.
*/
public with(relations: string | string[]): this {
if (Array.isArray(relations)) {
this.nextRelations.push(...relations);
} else {
this.nextRelations.push(relations);
}
return this;
}
/**
* Gets and resets the relations primed using with()
*/
protected getRelations(): string[] {
const relations = this.nextRelations || [];
this.nextRelations = [];
return relations;
}
}

View file

@ -1,12 +1,12 @@
import { CaseType } from "./CaseType";
import { CaseTypes } from "./CaseTypes";
export const CaseTypeColors = {
[CaseType.Note]: 0x3498db,
[CaseType.Warn]: 0xdae622,
[CaseType.Mute]: 0xe6b122,
[CaseType.Unmute]: 0xa175b3,
[CaseType.Kick]: 0xe67e22,
[CaseType.Softban]: 0xe67e22,
[CaseType.Ban]: 0xcb4314,
[CaseType.Unban]: 0x9b59b6
[CaseTypes.Note]: 0x3498db,
[CaseTypes.Warn]: 0xdae622,
[CaseTypes.Mute]: 0xe6b122,
[CaseTypes.Unmute]: 0xa175b3,
[CaseTypes.Kick]: 0xe67e22,
[CaseTypes.Softban]: 0xe67e22,
[CaseTypes.Ban]: 0xcb4314,
[CaseTypes.Unban]: 0x9b59b6
};

View file

@ -1,4 +1,4 @@
export enum CaseType {
export enum CaseTypes {
Ban = 1,
Unban,
Note,

View file

@ -1,51 +1,52 @@
import uuid from "uuid/v4"; // tslint:disable-line
import moment from "moment-timezone";
import knex from "../knex";
import SpamLog from "../models/SpamLog";
import { ArchiveEntry } from "./entities/ArchiveEntry";
import { getRepository, Repository } from "typeorm";
import { BaseRepository } from "./BaseRepository";
const DEFAULT_EXPIRY_DAYS = 30;
function deleteExpiredArchives() {
knex("archives")
.where("expires_at", "<=", knex.raw("NOW()"))
.delete();
}
deleteExpiredArchives();
setInterval(deleteExpiredArchives, 1000 * 60 * 60); // Clean expired archives every hour
export class GuildArchives {
protected guildId: string;
export class GuildArchives extends BaseRepository {
protected archives: Repository<ArchiveEntry>;
constructor(guildId) {
this.guildId = guildId;
super(guildId);
this.archives = getRepository(ArchiveEntry);
// Clean expired archives at start and then every hour
this.deleteExpiredArchives();
setInterval(() => this.deleteExpiredArchives(), 1000 * 60 * 60);
}
generateNewLogId() {
return uuid();
private deleteExpiredArchives() {
this.archives
.createQueryBuilder()
.where("expires_at <= NOW()")
.delete()
.execute();
}
async find(id: string): Promise<SpamLog> {
const result = await knex("archives")
.where("id", id)
.first();
return result ? new SpamLog(result) : null;
async find(id: string): Promise<ArchiveEntry> {
return this.archives.findOne({
where: { id },
relations: this.getRelations()
});
}
async create(body: string, expiresAt: moment.Moment = null) {
const id = this.generateNewLogId();
/**
* @returns ID of the created entry
*/
async create(body: string, expiresAt: moment.Moment = null): Promise<string> {
if (!expiresAt) {
expiresAt = moment().add(DEFAULT_EXPIRY_DAYS, "days");
}
await knex("archives").insert({
id,
const result = await this.archives.insert({
guild_id: this.guildId,
body,
expires_at: expiresAt.format("YYYY-MM-DD HH:mm:ss")
});
return id;
return result.identifiers[0].id;
}
}

View file

@ -1,93 +1,78 @@
import knex from "../knex";
import Case from "../models/Case";
import CaseNote from "../models/CaseNote";
import { Case } from "./entities/Case";
import { CaseNote } from "./entities/CaseNote";
import { BaseRepository } from "./BaseRepository";
import { getRepository, In, Repository } from "typeorm";
export class GuildCases {
protected guildId: string;
export class GuildCases extends BaseRepository {
private cases: Repository<Case>;
private caseNotes: Repository<CaseNote>;
constructor(guildId) {
this.guildId = guildId;
super(guildId);
this.cases = getRepository(Case);
this.caseNotes = getRepository(CaseNote);
}
async get(ids: number[]): Promise<Case[]> {
const result = await knex("cases")
.whereIn("id", ids)
.select();
return result.map(r => new Case(r));
return this.cases.find({
relations: this.getRelations(),
where: {
guild_id: this.guildId,
id: In(ids)
}
});
}
async find(id: number): Promise<Case> {
const result = await knex("cases")
.where("guild_id", this.guildId)
.where("id", id)
.first();
return result ? new Case(result) : null;
return this.cases.findOne({
relations: this.getRelations(),
where: {
guild_id: this.guildId,
id
}
});
}
async findByCaseNumber(caseNumber: number): Promise<Case> {
const result = await knex("cases")
.where("guild_id", this.guildId)
.where("case_number", caseNumber)
.first();
return result ? new Case(result) : null;
}
async getCaseNotes(caseId: number): Promise<CaseNote[]> {
const results = await knex("case_notes")
.where("case_id", caseId)
.select();
return results.map(r => new CaseNote(r));
return this.cases.findOne({
relations: this.getRelations(),
where: {
guild_id: this.guildId,
case_number: caseNumber
}
});
}
async getByUserId(userId: string): Promise<Case[]> {
const results = await knex("cases")
.where("guild_id", this.guildId)
.where("user_id", userId)
.select();
return results.map(r => new Case(r));
}
async findFirstCaseNote(caseId: number): Promise<CaseNote> {
const result = await knex("case_notes")
.where("case_id", caseId)
.first();
return result ? new CaseNote(result) : null;
return this.cases.find({
relations: this.getRelations(),
where: {
guild_id: this.guildId,
user_id: userId
}
});
}
async create(data): Promise<number> {
return knex
.insert({
...data,
guild_id: this.guildId,
case_number: knex.raw(
"(SELECT IFNULL(MAX(case_number)+1, 1) FROM cases AS ma2 WHERE guild_id = ?)",
this.guildId
)
})
.returning("id")
.into("cases")
.then(ids => Number(ids[0]));
const result = await this.cases.insert({
...data,
guild_id: this.guildId,
case_number: () => `(SELECT IFNULL(MAX(case_number)+1, 1) FROM cases AS ma2 WHERE guild_id = ${this.guildId})`
});
return result.identifiers[0].id;
}
update(id, data) {
return knex("cases")
.where("id", id)
.update(data);
return this.cases.update(id, data);
}
createNote(caseId: number, data: any) {
return knex
.insert({
...data,
case_id: caseId
})
.into("case_notes")
.return();
async createNote(caseId: number, data: any): Promise<number> {
const result = await this.caseNotes.insert({
...data,
case_id: caseId
});
return result.identifiers[0].id;
}
}

View file

@ -1,31 +1,32 @@
import knex from "../knex";
import moment from "moment-timezone";
import Mute from "../models/Mute";
import { Mute } from "./entities/Mute";
import { BaseRepository } from "./BaseRepository";
import { getRepository, Repository, Brackets } from "typeorm";
export class GuildMutes {
protected guildId: string;
export class GuildMutes extends BaseRepository {
private mutes: Repository<Mute>;
constructor(guildId) {
this.guildId = guildId;
super(guildId);
this.mutes = getRepository(Mute);
}
async getExpiredMutes(): Promise<Mute[]> {
const result = await knex("mutes")
.where("guild_id", this.guildId)
.whereNotNull("expires_at")
.whereRaw("expires_at <= NOW()")
.select();
return result.map(r => new Mute(r));
return this.mutes
.createQueryBuilder("mutes")
.where("guild_id = :guild_id", { guild_id: this.guildId })
.where("expires_at IS NOT NULL")
.where("expires_at <= NOW()")
.getMany();
}
async findExistingMuteForUserId(userId: string): Promise<Mute> {
const result = await knex("mutes")
.where("guild_id", this.guildId)
.where("user_id", userId)
.first();
return result ? new Mute(result) : null;
return this.mutes.findOne({
where: {
guild_id: this.guildId,
user_id: userId
}
});
}
async addMute(userId, expiryTime) {
@ -35,13 +36,11 @@ export class GuildMutes {
.format("YYYY-MM-DD HH:mm:ss")
: null;
return knex
.insert({
guild_id: this.guildId,
user_id: userId,
expires_at: expiresAt
})
.into("mutes");
return this.mutes.insert({
guild_id: this.guildId,
user_id: userId,
expires_at: expiresAt
});
}
async updateExpiryTime(userId, newExpiryTime) {
@ -51,12 +50,15 @@ export class GuildMutes {
.format("YYYY-MM-DD HH:mm:ss")
: null;
return knex("mutes")
.where("guild_id", this.guildId)
.where("user_id", userId)
.update({
return this.mutes.update(
{
guild_id: this.guildId,
user_id: userId
},
{
expires_at: expiresAt
});
}
);
}
async addOrUpdateMute(userId, expiryTime) {
@ -70,25 +72,33 @@ export class GuildMutes {
}
async getActiveMutes(): Promise<Mute[]> {
const result = await knex("mutes")
.where("guild_id", this.guildId)
.where(q => q.whereRaw("expires_at > NOW()").orWhereNull("expires_at"))
.select();
return result.map(r => new Mute(r));
return this.mutes
.createQueryBuilder("mutes")
.where("guild_id = :guild_id", { guild_id: this.guildId })
.andWhere(
new Brackets(qb => {
qb.where("expires_at > NOW()").orWhere("expires_at IS NULL");
})
)
.getMany();
}
async setCaseId(userId, caseId) {
await knex("mutes")
.where("guild_id", this.guildId)
.where("user_id", userId)
.update({ case_id: caseId });
await this.mutes.update(
{
guild_id: this.guildId,
user_id: userId
},
{
case_id: caseId
}
);
}
async clear(userId) {
return knex("mutes")
.where("guild_id", this.guildId)
.where("user_id", userId)
.delete();
await this.mutes.delete({
guild_id: this.guildId,
user_id: userId
});
}
}

View file

@ -1,5 +1,6 @@
import knex from "../knex";
import PersistedData from "../models/PersistedData";
import { PersistedData } from "./entities/PersistedData";
import { BaseRepository } from "./BaseRepository";
import { getRepository, Repository } from "typeorm";
export interface IPartialPersistData {
roles?: string[];
@ -7,20 +8,21 @@ export interface IPartialPersistData {
is_voice_muted?: boolean;
}
export class GuildPersistedData {
protected guildId: string;
export class GuildPersistedData extends BaseRepository {
private persistedData: Repository<PersistedData>;
constructor(guildId) {
this.guildId = guildId;
super(guildId);
this.persistedData = getRepository(PersistedData);
}
async find(userId: string) {
const result = await knex("persisted_data")
.where("guild_id", this.guildId)
.where("user_id", userId)
.first();
return result ? new PersistedData(result) : null;
return this.persistedData.findOne({
where: {
guild_id: this.guildId,
user_id: userId
}
});
}
async set(userId: string, data: IPartialPersistData = {}) {
@ -31,12 +33,15 @@ export class GuildPersistedData {
const existing = await this.find(userId);
if (existing) {
await knex("persisted_data")
.where("guild_id", this.guildId)
.where("user_id", userId)
.update(finalData);
await this.persistedData.update(
{
guild_id: this.guildId,
user_id: userId
},
finalData
);
} else {
await knex("persisted_data").insert({
await this.persistedData.insert({
...finalData,
guild_id: this.guildId,
user_id: userId
@ -45,9 +50,9 @@ export class GuildPersistedData {
}
async clear(userId: string) {
await knex("persisted_data")
.where("guild_id", this.guildId)
.where("user_id", userId)
.delete();
await this.persistedData.delete({
guild_id: this.guildId,
user_id: userId
});
}
}

View file

@ -1,54 +1,57 @@
import knex from "../knex";
import ReactionRole from "../models/ReactionRole";
import { ReactionRole } from "./entities/ReactionRole";
import { BaseRepository } from "./BaseRepository";
import { getRepository, Repository } from "typeorm";
export class GuildReactionRoles {
protected guildId: string;
export class GuildReactionRoles extends BaseRepository {
private reactionRoles: Repository<ReactionRole>;
constructor(guildId) {
this.guildId = guildId;
super(guildId);
this.reactionRoles = getRepository(ReactionRole);
}
async all(): Promise<ReactionRole[]> {
const results = await knex("reaction_roles")
.where("guild_id", this.guildId)
.select();
return results.map(r => new ReactionRole(r));
return this.reactionRoles.find({
where: {
guild_id: this.guildId
}
});
}
async getForMessage(messageId: string): Promise<ReactionRole[]> {
const results = await knex("reaction_roles")
.where("guild_id", this.guildId)
.where("message_id", messageId)
.select();
return results.map(r => new ReactionRole(r));
return this.reactionRoles.find({
where: {
guild_id: this.guildId,
message_id: messageId
}
});
}
async getByMessageAndEmoji(messageId: string, emoji: string): Promise<ReactionRole> {
const result = await knex("reaction_roles")
.where("guild_id", this.guildId)
.where("message_id", messageId)
.where("emoji", emoji)
.first();
return result ? new ReactionRole(result) : null;
return this.reactionRoles.findOne({
where: {
guild_id: this.guildId,
message_id: messageId,
emoji
}
});
}
async removeFromMessage(messageId: string, emoji: string = null) {
let query = knex("reaction_roles")
.where("guild_id", this.guildId)
.where("message_id", messageId);
const criteria: any = {
guild_id: this.guildId,
message_id: messageId
};
if (emoji) {
query = query.where("emoji", emoji);
criteria.emoji = emoji;
}
await query.delete();
await this.reactionRoles.delete(criteria);
}
async add(channelId: string, messageId: string, emoji: string, roleId: string) {
await knex("reaction_roles").insert({
await this.reactionRoles.insert({
guild_id: this.guildId,
channel_id: channelId,
message_id: messageId,

View file

@ -1,36 +1,40 @@
import knex from "../knex";
import moment from "moment-timezone";
import Tag from "../models/Tag";
import { Tag } from "./entities/Tag";
import { getRepository, Repository } from "typeorm";
import { BaseRepository } from "./BaseRepository";
export class GuildTags {
protected guildId: string;
export class GuildTags extends BaseRepository {
private tags: Repository<Tag>;
constructor(guildId) {
this.guildId = guildId;
super(guildId);
this.tags = getRepository(Tag);
}
async find(tag): Promise<Tag> {
const result = await knex("tags")
.where("guild_id", this.guildId)
.where("tag", tag)
.first();
return result ? new Tag(result) : null;
return this.tags.findOne({
where: {
guild_id: this.guildId,
tag
}
});
}
async createOrUpdate(tag, body, userId) {
const existingTag = await this.find(tag);
if (existingTag) {
await knex("tags")
.where("guild_id", this.guildId)
.where("tag", tag)
.update({
await this.tags
.createQueryBuilder()
.update()
.set({
body,
user_id: userId,
created_at: knex.raw("NOW()")
});
created_at: () => "NOW()"
})
.where("guild_id = :guildId", { guildId: this.guildId })
.where("tag = :tag", { tag })
.execute();
} else {
await knex("tags").insert({
await this.tags.insert({
guild_id: this.guildId,
user_id: userId,
tag,
@ -40,9 +44,9 @@ export class GuildTags {
}
async delete(tag) {
await knex("tags")
.where("guild_id", this.guildId)
.where("tag", tag)
.delete();
await this.tags.delete({
guild_id: this.guildId,
tag
});
}
}

23
src/data/db.ts Normal file
View file

@ -0,0 +1,23 @@
import { SimpleError } from "../SimpleError";
import { Connection, createConnection } from "typeorm";
let connectionPromise: Promise<Connection>;
export let connection: Connection;
export function connect() {
if (!connectionPromise) {
connectionPromise = createConnection().then(newConnection => {
return newConnection.query("SELECT TIMEDIFF(NOW(), UTC_TIMESTAMP) AS tz").then(r => {
if (r[0].tz !== "00:00:00") {
throw new SimpleError(`Database timezone must be UTC (detected ${r[0].tz})`);
}
connection = newConnection;
return newConnection;
});
});
}
return connectionPromise;
}

View file

@ -0,0 +1,16 @@
import { Entity, Column, PrimaryColumn, PrimaryGeneratedColumn } from "typeorm";
@Entity("archives")
export class ArchiveEntry {
@Column()
@PrimaryGeneratedColumn("uuid")
id: string;
@Column() guild_id: string;
@Column() body: string;
@Column() created_at: string;
@Column() expires_at: string;
}

28
src/data/entities/Case.ts Normal file
View file

@ -0,0 +1,28 @@
import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from "typeorm";
import { CaseNote } from "./CaseNote";
@Entity("cases")
export class Case {
@PrimaryGeneratedColumn() id: number;
@Column() guild_id: string;
@Column() case_number: number;
@Column() user_id: string;
@Column() user_name: string;
@Column() mod_id: string;
@Column() mod_name: string;
@Column() type: number;
@Column() audit_log_id: string;
@Column() created_at: string;
@OneToMany(type => CaseNote, note => note.case)
notes: CaseNote[];
}

View file

@ -0,0 +1,21 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from "typeorm";
import { Case } from "./Case";
@Entity("case_notes")
export class CaseNote {
@PrimaryGeneratedColumn() id: number;
@Column() case_id: number;
@Column() mod_id: string;
@Column() mod_name: string;
@Column() body: string;
@Column() created_at: string;
@ManyToOne(type => Case, theCase => theCase.notes)
@JoinColumn({ name: "case_id" })
case: Case;
}

18
src/data/entities/Mute.ts Normal file
View file

@ -0,0 +1,18 @@
import { Entity, Column, PrimaryColumn } from "typeorm";
@Entity("mutes")
export class Mute {
@Column()
@PrimaryColumn()
guild_id: string;
@Column()
@PrimaryColumn()
user_id: string;
@Column() created_at: string;
@Column() expires_at: string;
@Column() case_id: number;
}

View file

@ -0,0 +1,18 @@
import { Entity, Column, PrimaryColumn } from "typeorm";
@Entity("persisted_data")
export class PersistedData {
@Column()
@PrimaryColumn()
guild_id: string;
@Column()
@PrimaryColumn()
user_id: string;
@Column() roles: string;
@Column() nickname: string;
@Column() is_voice_muted: number;
}

View file

@ -0,0 +1,22 @@
import { Entity, Column, PrimaryColumn } from "typeorm";
@Entity("reaction_roles")
export class ReactionRole {
@Column()
@PrimaryColumn()
guild_id: string;
@Column()
@PrimaryColumn()
channel_id: string;
@Column()
@PrimaryColumn()
message_id: string;
@Column()
@PrimaryColumn()
emoji: string;
@Column() role_id: string;
}

18
src/data/entities/Tag.ts Normal file
View file

@ -0,0 +1,18 @@
import { Entity, Column, PrimaryColumn, CreateDateColumn } from "typeorm";
@Entity("tags")
export class Tag {
@Column()
@PrimaryColumn()
guild_id: string;
@Column()
@PrimaryColumn()
tag: string;
@Column() user_id: string;
@Column() body: string;
@Column() created_at: string;
}