waitlist-mailer
Version:
Modern, modular TypeScript library for managing waitlists with pluggable storage and mail providers. Supports MongoDB, SQL databases, and custom adapters with zero required dependencies for basic usage.
108 lines (106 loc) • 3.46 kB
JavaScript
// lib/adapters/storage/SequelizeStorage.ts
var SequelizeStorage = class {
/**
* Create a new SequelizeStorage instance.
* @param config - Configuration object with sequelize model
* @throws {Error} If sequelize is not installed or model is invalid
*/
constructor(config) {
try {
if (!config.model) {
throw new Error("Sequelize model is required. Please provide a valid Sequelize model instance.");
}
if (!config.model.sequelize) {
throw new Error("Invalid Sequelize model. Make sure you're passing a compiled Sequelize model.");
}
if (!config.model.findOne || !config.model.create) {
throw new Error("Invalid Sequelize model. Model must have findOne and create methods.");
}
} catch (error) {
if (error.message.includes("Sequelize") || error.message.includes("model")) {
throw error;
}
throw new Error(
"SequelizeStorage requires sequelize to be installed. Install it with: npm install sequelize pg (for PostgreSQL) or npm install sequelize mysql2 (for MySQL) or npm install sequelize better-sqlite3 (for SQLite)"
);
}
this.model = config.model;
this.normalizeEmail = config.normalizeEmail ?? true;
}
normalizeEmailAddress(email) {
return this.normalizeEmail ? email.toLowerCase() : email;
}
/**
* Add an email to the database.
* @param email - The email to add
* @param data - Optional metadata
* @throws {Error} If the email already exists or database operation fails
*/
async add(email, data) {
const normalized = this.normalizeEmailAddress(email);
const existing = await this.model.findOne({ where: { email: normalized } });
if (existing) {
throw new Error(`Email already exists: ${email}`);
}
try {
await this.model.create({
email: normalized,
metadata: data ? JSON.stringify(data) : null,
createdAt: /* @__PURE__ */ new Date()
});
} catch (error) {
if (error.name === "SequelizeUniqueConstraintError") {
throw new Error(`Email already exists: ${email}`);
}
throw error;
}
}
/**
* Remove an email from the database.
* @param email - The email to remove
* @returns true if the email was found and removed
*/
async remove(email) {
const normalized = this.normalizeEmailAddress(email);
const result = await this.model.destroy({ where: { email: normalized } });
return result > 0;
}
/**
* Check if an email exists in the database.
* @param email - The email to check
* @returns true if the email exists
*/
async exists(email) {
const normalized = this.normalizeEmailAddress(email);
const count = await this.model.count({ where: { email: normalized } });
return count > 0;
}
/**
* Retrieve all entries from the database.
* @returns Array of all WaitlistEntry objects
*/
async getAll() {
const records = await this.model.findAll({ raw: true });
return records.map((record) => ({
email: record.email,
metadata: record.metadata ? JSON.parse(record.metadata) : void 0,
createdAt: record.createdAt
}));
}
/**
* Count total entries in the database.
* @returns Number of entries
*/
async count() {
return this.model.count();
}
/**
* Clear all entries from the database.
*/
async clear() {
await this.model.destroy({ where: {} });
}
};
export {
SequelizeStorage
};