@developers-joyride/shortify
Version:
High performance URL shortener library with multi-database support (MongoDB, SQLite, PostgreSQL, MySQL)
132 lines (113 loc) • 4.3 kB
text/typescript
import { Shortify } from "../src/index";
import dotenv from "dotenv";
dotenv.config();
async function postgresqlExample() {
console.log("🚀 PostgreSQL URL Shortener Example\n");
// PostgreSQL configuration
const config = {
type: "postgresql" as const,
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",
maxRetries: 5,
retryDelay: 5000,
};
try {
// Initialize Shortify with PostgreSQL
const shortify = new Shortify("https://short.ly", config);
// Connect to database
console.log("📡 Connecting to PostgreSQL...");
const connected = await shortify.connect();
if (!connected) {
throw new Error("Failed to connect to PostgreSQL");
}
console.log("✅ Connected to PostgreSQL successfully!\n");
// Example 1: Basic URL shortening
console.log("1️⃣ Basic URL Shortening");
const originalUrl =
"https://www.example.com/very-long-url-that-needs-to-be-shortened";
const shortUrl = await shortify.shorten(originalUrl);
console.log(`Original: ${originalUrl}`);
console.log(`Shortened: ${shortUrl.shortUrl}`);
console.log(`URL ID: ${shortUrl.urlId}\n`);
// Example 2: Resolve short URL
console.log("2️⃣ Resolving Short URL");
const resolvedUrl = await shortify.resolve(shortUrl.urlId);
if (resolvedUrl) {
console.log(`Resolved: ${resolvedUrl}\n`);
}
// Example 3: Simulate clicks by resolving multiple times
console.log("3️⃣ Simulating Clicks");
for (let i = 0; i < 5; i++) {
await shortify.resolve(shortUrl.urlId);
}
const stats = await shortify.getStats(shortUrl.urlId);
if (stats) {
console.log(`Updated clicks: ${stats.clicks}\n`);
}
// Example 4: Custom URL ID
console.log("4️⃣ Custom URL ID");
const customUrl = await shortify.shorten(
"https://www.github.com/Divyansh-007/shortify",
{ customUrlId: "github" }
);
console.log(`Custom URL: ${customUrl.shortUrl}`);
console.log(`Custom ID: ${customUrl.urlId}\n`);
// Example 5: URL with expiration
console.log("5️⃣ URL with Expiration");
const expiringUrl = await shortify.shorten(
"https://www.temporary-link.com",
{ expiresInDays: 1 }
);
console.log(`Expiring URL: ${expiringUrl.shortUrl}`);
console.log(`Expires at: ${expiringUrl.expiresAt}\n`);
// Example 6: Get URL statistics
console.log("6️⃣ URL Statistics");
const urlStats = await shortify.getStats(shortUrl.urlId);
if (urlStats) {
console.log(`Total clicks: ${urlStats.clicks}`);
console.log(`Created: ${urlStats.createdAt}`);
console.log(`Expires: ${urlStats.expiresAt || "Never"}\n`);
}
// Example 7: Delete URL
console.log("7️⃣ Delete URL");
const deleted = await shortify.delete(shortUrl.urlId);
console.log(`URL deleted: ${deleted}\n`);
// Example 8: Try to resolve deleted URL
console.log("8️⃣ Resolving Deleted URL");
const deletedUrl = await shortify.resolve(shortUrl.urlId);
console.log(`Deleted URL resolved: ${deletedUrl ? "Yes" : "No"}\n`);
// Example 9: Batch operations
console.log("9️⃣ Batch Operations");
const urls = [
"https://www.google.com",
"https://www.github.com",
"https://www.npmjs.com",
"https://www.typescriptlang.org",
];
const shortUrls: any[] = [];
for (const url of urls) {
const short = await shortify.shorten(url);
shortUrls.push(short);
console.log(`${url} → ${short.shortUrl}`);
}
console.log();
// Clean up
console.log("🧹 Cleaning up...");
for (const short of shortUrls) {
await shortify.delete(short.urlId);
}
await shortify.delete(customUrl.urlId);
await shortify.delete(expiringUrl.urlId);
console.log("✅ Cleanup completed!\n");
// Disconnect
await shortify.disconnect();
console.log("👋 Disconnected from PostgreSQL");
} catch (error) {
console.error("❌ Error:", error);
}
}
// Run the example
postgresqlExample().catch(console.error);