UNPKG

@developers-joyride/shortify

Version:

High performance URL shortener library with multi-database support (MongoDB, SQLite, PostgreSQL, MySQL)

94 lines (93 loc) 3.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DatabaseFactory = void 0; const mongodb_adapter_1 = require("../adapters/mongodb.adapter"); const sqlite_adapter_1 = require("../adapters/sqlite.adapter"); const mysql_adapter_1 = require("../adapters/mysql.adapter"); const postgresql_adapter_1 = require("../adapters/postgresql.adapter"); class DatabaseFactory { static createAdapter(config) { switch (config.type) { case "mongodb": if (!config.connectionString) { throw new Error("MongoDB connection string is required"); } return new mongodb_adapter_1.MongoAdapter({ mongoUri: config.connectionString, maxRetries: config.maxRetries, retryDelay: config.retryDelay, collectionName: config.collectionName, }); case "sqlite": if (!config.database) { throw new Error("SQLite database path is required"); } return new sqlite_adapter_1.SqliteAdapter({ database: config.database, maxRetries: config.maxRetries, retryDelay: config.retryDelay, tableName: config.tableName, }); case "postgresql": if (!config.host || !config.database || !config.username || !config.password) { throw new Error("PostgreSQL host, database, username, and password are required"); } return new postgresql_adapter_1.PostgresAdapter({ host: config.host, port: config.port || 5432, database: config.database, username: config.username, password: config.password, maxRetries: config.maxRetries, retryDelay: config.retryDelay, tableName: config.tableName, }); case "mysql": if (!config.host || !config.database || !config.username || !config.password) { throw new Error("MySQL host, database, username, and password are required"); } return new mysql_adapter_1.MysqlAdapter({ host: config.host, port: config.port || 3306, database: config.database, username: config.username, password: config.password, maxRetries: config.maxRetries, retryDelay: config.retryDelay, tableName: config.tableName, }); default: throw new Error(`Unsupported database type: ${config.type}`); } } // Convenience methods for common configurations static createMongoAdapter(mongoUri, options) { return new mongodb_adapter_1.MongoAdapter({ mongoUri, maxRetries: options?.maxRetries, retryDelay: options?.retryDelay, collectionName: options?.collectionName, }); } static createSqliteAdapter(database, options) { return new sqlite_adapter_1.SqliteAdapter({ database, maxRetries: options?.maxRetries, retryDelay: options?.retryDelay, tableName: options?.tableName, }); } static createMysqlAdapter(config) { return new mysql_adapter_1.MysqlAdapter(config); } static createPostgresAdapter(config) { return new postgresql_adapter_1.PostgresAdapter(config); } } exports.DatabaseFactory = DatabaseFactory;