style: run format:apply and cleanup unused imports
This commit is contained in:
+53
-55
@@ -1,75 +1,73 @@
|
|||||||
import {
|
import {
|
||||||
Attributes,
|
DataTypes,
|
||||||
DataTypes,
|
Model,
|
||||||
Model,
|
ModelStatic,
|
||||||
ModelAttributes,
|
Sequelize
|
||||||
ModelStatic,
|
|
||||||
Sequelize
|
|
||||||
} from 'sequelize';
|
} from 'sequelize';
|
||||||
|
|
||||||
export class DatabaseManager {
|
export class DatabaseManager {
|
||||||
private readonly db: Sequelize;
|
private readonly db: Sequelize;
|
||||||
|
|
||||||
public guildData: ModelStatic<Model> | null = null;
|
public guildData: ModelStatic<Model> | null = null;
|
||||||
public userData: ModelStatic<Model> | null = null;
|
public userData: ModelStatic<Model> | null = null;
|
||||||
|
|
||||||
private constructor() {
|
private constructor() {
|
||||||
this.db = new Sequelize('db', 'luma', '', {
|
this.db = new Sequelize('db', 'luma', '', {
|
||||||
host: 'localhost',
|
host: 'localhost',
|
||||||
dialect: 'sqlite',
|
dialect: 'sqlite',
|
||||||
storage: 'db.sqlite',
|
storage: 'db.sqlite',
|
||||||
logging: false
|
logging: false
|
||||||
});
|
});
|
||||||
|
|
||||||
this.db.sync();
|
this.db.sync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async init(userKeys: object, guildKeys: object) {
|
public async init(userKeys: object, guildKeys: object) {
|
||||||
this.userData = this.db.define('user_data', {
|
this.userData = this.db.define('user_data', {
|
||||||
user_id: { type: DataTypes.STRING, unique: true, primaryKey: true },
|
user_id: { type: DataTypes.STRING, unique: true, primaryKey: true },
|
||||||
...userKeys
|
...userKeys
|
||||||
});
|
});
|
||||||
|
|
||||||
this.guildData = this.db.define('guild_data', {
|
this.guildData = this.db.define('guild_data', {
|
||||||
guild_id: { type: DataTypes.STRING, unique: true, primaryKey: true },
|
guild_id: { type: DataTypes.STRING, unique: true, primaryKey: true },
|
||||||
...guildKeys
|
...guildKeys
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.db.sync({ alter: true });
|
await this.db.sync({ alter: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
public sync(): void {
|
public sync(): void {
|
||||||
this.db.sync();
|
this.db.sync();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getUser(userId: string) {
|
public async getUser(userId: string) {
|
||||||
if (!this.userData) throw new Error("Database not initialized");
|
if (!this.userData) throw new Error('Database not initialized');
|
||||||
|
|
||||||
const [user] = await this.userData.findOrCreate({
|
const [user] = await this.userData.findOrCreate({
|
||||||
where: { user_id: userId },
|
where: { user_id: userId },
|
||||||
defaults: { user_id: userId }
|
defaults: { user_id: userId }
|
||||||
});
|
});
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getGuild(guildId: string) {
|
public async getGuild(guildId: string) {
|
||||||
if (!this.guildData) throw new Error("Database not initialized");
|
if (!this.guildData) throw new Error('Database not initialized');
|
||||||
|
|
||||||
const [guild] = await this.guildData.findOrCreate({
|
const [guild] = await this.guildData.findOrCreate({
|
||||||
where: { guild_id: guildId },
|
where: { guild_id: guildId },
|
||||||
defaults: { guild_id: guildId }
|
defaults: { guild_id: guildId }
|
||||||
});
|
});
|
||||||
return guild;
|
return guild;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
singleton logic
|
singleton logic
|
||||||
*/
|
*/
|
||||||
static #instance: DatabaseManager | null = null;
|
static #instance: DatabaseManager | null = null;
|
||||||
|
|
||||||
public static get get(): DatabaseManager {
|
public static get get(): DatabaseManager {
|
||||||
if (!DatabaseManager.#instance)
|
if (!DatabaseManager.#instance)
|
||||||
DatabaseManager.#instance = new DatabaseManager();
|
DatabaseManager.#instance = new DatabaseManager();
|
||||||
return DatabaseManager.#instance;
|
return DatabaseManager.#instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user