better-giveaways
Version:
A modern, feature-rich Discord giveaway manager with TypeScript support, flexible storage adapters, and comprehensive event system
164 lines (163 loc) • 5.71 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SequelizeAdapter = void 0;
const GiveawayModel_1 = require("../models/GiveawayModel");
/**
* Database storage adapter using Sequelize ORM.
*
* This adapter provides persistent storage for giveaway data using any SQL database
* supported by Sequelize (PostgreSQL, MySQL, MariaDB, SQLite, Microsoft SQL Server).
* It's ideal for production environments, larger bots, or when you need advanced
* database features like transactions, relationships, and complex queries.
*
* Features:
* - Support for multiple database engines
* - Automatic table creation and schema management
* - Built-in connection pooling and optimization
* - Transaction support for data integrity
* - Scalable for high-volume applications
*
* @example
* ```typescript
* import { Sequelize } from 'sequelize';
* import { SequelizeAdapter } from 'better-giveaways';
*
* // PostgreSQL example
* const sequelize = new Sequelize('postgresql://user:pass@localhost:5432/giveaways');
*
* // SQLite example (good for development)
* const sequelize = new Sequelize({
* dialect: 'sqlite',
* storage: './giveaways.db'
* });
*
* // MySQL example
* const sequelize = new Sequelize('mysql://user:pass@localhost:3306/giveaways');
*
* const adapter = new SequelizeAdapter(sequelize);
* const giveawayManager = new GiveawayManager(client, adapter, options);
*
* // Don't forget to sync the database
* await sequelize.sync();
* ```
*/
class SequelizeAdapter {
/**
* Creates a new SequelizeAdapter instance.
*
* The adapter will automatically initialize the GiveawayModel using the provided
* Sequelize instance. Make sure to call `sequelize.sync()` to create the database
* tables before using the adapter.
*
* @param sequelize - A configured Sequelize instance connected to your database
*
* @example
* ```typescript
* import { Sequelize } from 'sequelize';
*
* const sequelize = new Sequelize({
* dialect: 'postgres',
* host: 'localhost',
* database: 'giveaways',
* username: 'user',
* password: 'password',
* logging: false // Set to console.log to see SQL queries
* });
*
* const adapter = new SequelizeAdapter(sequelize);
*
* // Create tables if they don't exist
* await sequelize.sync();
* ```
*/
constructor(sequelize) {
this.model = (0, GiveawayModel_1.initGiveawayModel)(sequelize);
}
/**
* Saves or updates giveaway data in the database.
*
* Uses Sequelize's upsert operation which will insert a new record if the
* giveaway doesn't exist, or update the existing record if it does.
*
* @param data - The giveaway data to save
* @throws {Error} If database operation fails
*/
save(data) {
return __awaiter(this, void 0, void 0, function* () {
yield this.model.upsert({
giveawayId: data.giveawayId,
messageId: data.messageId,
channelId: data.channelId,
prize: data.prize,
winnerCount: data.winnerCount,
endAt: data.endAt,
ended: data.ended,
requirements: data.requirements,
});
});
}
/**
* Retrieves a specific giveaway by its primary key (giveawayId).
*
* @param id - The unique giveaway ID to retrieve
* @returns The giveaway data or null if not found
* @throws {Error} If database operation fails
*/
get(id) {
return __awaiter(this, void 0, void 0, function* () {
const record = yield this.model.findByPk(id);
if (!record)
return null;
return record;
});
}
/**
* Retrieves all giveaways from the database.
*
* @returns An array of all giveaway data
* @throws {Error} If database operation fails
*/
getAll() {
return __awaiter(this, void 0, void 0, function* () {
const records = yield this.model.findAll();
return records;
});
}
/**
* Deletes a giveaway from the database.
*
* @param id - The unique giveaway ID to delete
* @throws {Error} If database operation fails
*/
delete(id) {
return __awaiter(this, void 0, void 0, function* () {
yield this.model.destroy({ where: { giveawayId: id } });
});
}
/**
* Updates an existing giveaway with new data.
*
* @param id - The giveaway ID to update
* @param data - The new giveaway data
* @throws {Error} If database operation fails
*/
edit(id, data) {
return __awaiter(this, void 0, void 0, function* () {
yield this.model.update(data, {
where: {
giveawayId: id,
},
});
});
}
}
exports.SequelizeAdapter = SequelizeAdapter;