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.
89 lines (87 loc) • 2.54 kB
JavaScript
// lib/adapters/storage/SequelizeStorage.ts
var SequelizeStorage = class {
/**
* Create a new SequelizeStorage instance.
* @param config - Configuration object with sequelize model
*/
constructor(config) {
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
};