UNPKG

@developers-joyride/shortify

Version:

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

103 lines (85 loc) 3.34 kB
import Shortify, { DatabaseConfig } from "../src/index"; async function demonstrateFixes() { console.log( "=== Demonstrating Collection Name and URL ID Uniqueness Fixes ===\n" ); // Example 1: Collection name preservation console.log("1. Collection Name Preservation:"); console.log( " - Custom collection name 'MyCustomCollection' should be preserved as-is" ); console.log(" - Previously it was being converted to lowercase\n"); // Example 2: URL ID uniqueness console.log("2. URL ID Uniqueness:"); console.log(" - Each shorten() call should generate a unique URL ID"); console.log(" - Previously, same original URL would return same URL ID\n"); // Create a simple in-memory SQLite instance for demonstration const dbConfig: DatabaseConfig = { type: "sqlite", database: ":memory:", tableName: "MyCustomTable", // This should be preserved }; const shortify = new Shortify("https://short.ly", dbConfig); await shortify.connect(); console.log("3. Testing URL ID Uniqueness:"); const originalUrl = "https://www.example.com/very-long-url-that-needs-shortening"; // Create multiple shortened URLs for the same original URL const result1 = await shortify.shorten(originalUrl); const result2 = await shortify.shorten(originalUrl); const result3 = await shortify.shorten(originalUrl); console.log(` Original URL: ${originalUrl}`); console.log( ` Result 1 - URL ID: ${result1.urlId}, Short URL: ${result1.shortUrl}` ); console.log( ` Result 2 - URL ID: ${result2.urlId}, Short URL: ${result2.shortUrl}` ); console.log( ` Result 3 - URL ID: ${result3.urlId}, Short URL: ${result3.shortUrl}` ); // Verify all URL IDs are unique const urlIds = [result1.urlId, result2.urlId, result3.urlId]; const uniqueUrlIds = new Set(urlIds); console.log( `\n All URL IDs are unique: ${uniqueUrlIds.size === urlIds.length}` ); console.log(` Expected: true, Got: ${uniqueUrlIds.size === urlIds.length}`); // Verify all resolve to the same original URL const resolved1 = await shortify.resolve(result1.urlId); const resolved2 = await shortify.resolve(result2.urlId); const resolved3 = await shortify.resolve(result3.urlId); console.log( `\n All resolve to original URL: ${ resolved1 === resolved2 && resolved2 === resolved3 }` ); console.log( ` Expected: true, Got: ${ resolved1 === resolved2 && resolved2 === resolved3 }` ); console.log("\n4. Testing Custom URL ID Conflict Handling:"); // Try to use the same custom URL ID twice const customId = "my-custom-id"; const customResult1 = await shortify.shorten("https://example1.com", { customUrlId: customId, }); const customResult2 = await shortify.shorten("https://example2.com", { customUrlId: customId, }); console.log(` First custom URL ID: ${customResult1.urlId}`); console.log(` Second custom URL ID: ${customResult2.urlId}`); console.log( ` Custom ID conflict handled: ${ customResult1.urlId !== customResult2.urlId }` ); console.log( ` Expected: true, Got: ${customResult1.urlId !== customResult2.urlId}` ); await shortify.disconnect(); console.log("\n=== Demonstration Complete ==="); } // Run the demonstration demonstrateFixes().catch(console.error);