UNPKG

@developers-joyride/shortify

Version:

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

67 lines (55 loc) 1.9 kB
import Shortify, { DatabaseConfig } from "../src/index"; async function sqliteExample() { console.log("=== SQLite Example ==="); const dbConfig: DatabaseConfig = { type: "sqlite", database: "./example.db", // File-based database maxRetries: 1, retryDelay: 100, }; const shortify = new Shortify("https://short.ly", dbConfig); try { await shortify.connect(); console.log("✅ Connected to SQLite database"); // Shorten a URL const result = await shortify.shorten( "https://www.example.com/very-long-url-that-needs-shortening" ); console.log("📝 Shortened URL:", result.shortUrl); console.log("🆔 URL ID:", result.urlId); // Resolve the shortened URL const originalUrl = await shortify.resolve(result.urlId); console.log("🔗 Original URL:", originalUrl); // Get statistics const stats = await shortify.getStats(result.urlId); console.log("📊 Clicks:", stats?.clicks); console.log("📅 Created:", stats?.createdAt); // Shorten another URL with custom options const customResult = await shortify.shorten( "https://www.example.com/another-long-url", { customUrlId: "custom123", expiresInDays: 7, } ); console.log("🎯 Custom URL:", customResult.shortUrl); console.log("⏰ Expires:", customResult.expiresAt); // Test URL duplication prevention const duplicateResult = await shortify.shorten( "https://www.example.com/very-long-url-that-needs-shortening" ); console.log( "🔄 Duplicate check - Same URL ID:", duplicateResult.urlId === result.urlId ); await shortify.disconnect(); console.log("✅ Disconnected from database"); } catch (error) { console.error( "❌ Error:", error instanceof Error ? error.message : String(error) ); } } // Run the example sqliteExample();