UNPKG

@developers-joyride/shortify

Version:

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

170 lines (143 loc) 6.3 kB
import Shortify from "../src"; import dotenv from "dotenv"; // Load environment variables dotenv.config(); async function main() { try { console.log("=== Custom Table Names Example ===\n"); // Example 1: SQLite with custom table name console.log("1. Creating SQLite instance with custom table name..."); const sqliteShortify = new Shortify("https://short.example.com/", { type: "sqlite", database: "./custom_table_example.db", tableName: "my_sqlite_urls", // Custom table name maxRetries: 3, retryDelay: 2000, }); console.log("Connecting to SQLite..."); await sqliteShortify.connect(); console.log("Connected to SQLite"); console.log("Table name: my_sqlite_urls\n"); // Shorten a URL const url1 = "https://example.com/sqlite-url"; console.log(`2. Shortening URL: ${url1}`); const shortened1 = await sqliteShortify.shorten(url1); console.log( `Shortened URL: ${shortened1.shortUrl} (ID: ${shortened1.urlId})` ); // Example 2: PostgreSQL with custom table name console.log("\n=== Example 2: PostgreSQL with custom table name ===\n"); const postgresShortify = new Shortify("https://short.example.com/", { type: "postgresql", host: process.env.POSTGRES_HOST || "localhost", port: parseInt(process.env.POSTGRES_PORT || "5432"), database: process.env.POSTGRES_DB || "shortify", username: process.env.POSTGRES_USER || "postgres", password: process.env.POSTGRES_PASSWORD || "password", tableName: "my_postgres_urls", // Custom table name maxRetries: 5, retryDelay: 1000, }); console.log("Connecting to PostgreSQL..."); await postgresShortify.connect(); console.log("Connected to PostgreSQL"); console.log("Table name: my_postgres_urls\n"); // Shorten another URL const url2 = "https://example.org/postgres-url"; console.log(`3. Shortening URL: ${url2}`); const shortened2 = await postgresShortify.shorten(url2); console.log( `Shortened URL: ${shortened2.shortUrl} (ID: ${shortened2.urlId})` ); // Example 3: MySQL with custom table name console.log("\n=== Example 3: MySQL with custom table name ===\n"); const mysqlShortify = new Shortify("https://short.example.com/", { type: "mysql", host: process.env.MYSQL_HOST || "localhost", port: parseInt(process.env.MYSQL_PORT || "3306"), database: process.env.MYSQL_DB || "shortify", username: process.env.MYSQL_USER || "root", password: process.env.MYSQL_PASSWORD || "password", tableName: "my_mysql_urls", // Custom table name maxRetries: 5, retryDelay: 1000, }); console.log("Connecting to MySQL..."); await mysqlShortify.connect(); console.log("Connected to MySQL"); console.log("Table name: my_mysql_urls\n"); // Shorten another URL const url3 = "https://example.net/mysql-url"; console.log(`4. Shortening URL: ${url3}`); const shortened3 = await mysqlShortify.shorten(url3); console.log( `Shortened URL: ${shortened3.shortUrl} (ID: ${shortened3.urlId})` ); // Example 4: Using default table names (backward compatibility) console.log("\n=== Example 4: Using default table names ===\n"); const defaultSqliteShortify = new Shortify("https://short.example.com/", { type: "sqlite", database: "./default_table_example.db", // No tableName specified - will use default "urls" }); console.log("Connecting to SQLite with default table name..."); await defaultSqliteShortify.connect(); console.log("Connected to SQLite"); console.log("Table name: urls (default)\n"); // Shorten another URL const url4 = "https://example.com/default-table-url"; console.log(`5. Shortening URL: ${url4}`); const shortened4 = await defaultSqliteShortify.shorten(url4); console.log( `Shortened URL: ${shortened4.shortUrl} (ID: ${shortened4.urlId})` ); // Test operations on different tables console.log("\n=== Testing Operations ==="); // Test SQLite custom table const resolved1 = await sqliteShortify.resolve(shortened1.urlId); console.log(`SQLite custom table - Resolved: ${resolved1}`); // Test PostgreSQL custom table const resolved2 = await postgresShortify.resolve(shortened2.urlId); console.log(`PostgreSQL custom table - Resolved: ${resolved2}`); // Test MySQL custom table const resolved3 = await mysqlShortify.resolve(shortened3.urlId); console.log(`MySQL custom table - Resolved: ${resolved3}`); // Test default table const resolved4 = await defaultSqliteShortify.resolve(shortened4.urlId); console.log(`Default table - Resolved: ${resolved4}`); // Clean up console.log("\n=== Cleaning up ==="); await sqliteShortify.delete(shortened1.urlId); await postgresShortify.delete(shortened2.urlId); await mysqlShortify.delete(shortened3.urlId); await defaultSqliteShortify.delete(shortened4.urlId); console.log("All URLs deleted"); // Disconnect all instances console.log("\nDisconnecting from databases..."); await sqliteShortify.disconnect(); await postgresShortify.disconnect(); await mysqlShortify.disconnect(); await defaultSqliteShortify.disconnect(); console.log("All instances disconnected"); console.log("\n=== Summary ==="); console.log("✓ Created 4 different table configurations:"); console.log(" - SQLite: my_sqlite_urls (custom)"); console.log(" - PostgreSQL: my_postgres_urls (custom)"); console.log(" - MySQL: my_mysql_urls (custom)"); console.log(" - SQLite: urls (default)"); console.log("✓ Each table operates independently"); console.log("✓ Backward compatibility maintained"); console.log("✓ Custom table names work across all database types"); } catch (error) { console.error("Error:", error); console.log( "\nNote: This example requires the respective databases to be running." ); console.log("For testing purposes, you can:"); console.log("1. Use only SQLite (no server required)"); console.log("2. Set up PostgreSQL/MySQL locally"); console.log("3. Use Docker containers for PostgreSQL/MySQL"); } } // Run the example main();