@developers-joyride/shortify
Version:
High performance URL shortener library with multi-database support (MongoDB, SQLite, PostgreSQL, MySQL)
123 lines (103 loc) • 4.23 kB
text/typescript
import Shortify from "../src";
import dotenv from "dotenv";
// Load environment variables
dotenv.config();
async function main() {
try {
console.log("=== Custom Collection Name Example ===\n");
// Example 1: Using the new constructor with custom collection name
console.log("1. Creating Shortify instance with custom collection name...");
const shortify = new Shortify("https://short.example.com/", {
type: "mongodb",
connectionString:
process.env.MONGODB_URI || "mongodb://localhost:27017/shortify",
collectionName: "my_custom_urls", // Custom collection name
maxRetries: 3,
retryDelay: 2000,
});
console.log("Connecting to MongoDB...");
await shortify.connect();
console.log("Connected to MongoDB");
console.log("Collection name: my_custom_urls\n");
// Shorten a URL
const url = "https://example.com/very-long-url-that-needs-shortening";
console.log(`2. Shortening URL: ${url}`);
const shortened = await shortify.shorten(url);
console.log(
`Shortened URL: ${shortened.shortUrl} (ID: ${shortened.urlId})`
);
// Resolve the shortened URL
console.log(`\n3. Resolving URL ID: ${shortened.urlId}`);
const original = await shortify.resolve(shortened.urlId);
console.log(`Original URL: ${original}`);
// Get stats for the shortened URL
console.log(`\n4. Getting stats for URL ID: ${shortened.urlId}`);
const stats = await shortify.getStats(shortened.urlId);
console.log("Stats:", stats);
// Example 2: Using the static createWithMongo method with custom collection
console.log(
"\n=== Example 2: Using createWithMongo with custom collection ===\n"
);
const shortify2 = Shortify.createWithMongo(
"https://short.example.com/",
process.env.MONGODB_URI || "mongodb://localhost:27017/shortify",
{
collectionName: "another_custom_collection",
maxRetries: 5,
retryDelay: 1000,
}
);
console.log("Connecting to MongoDB with second instance...");
await shortify2.connect();
console.log("Connected to MongoDB");
console.log("Collection name: another_custom_collection\n");
// Shorten another URL
const url2 = "https://example.org/another-long-url";
console.log(`5. Shortening URL: ${url2}`);
const shortened2 = await shortify2.shorten(url2);
console.log(
`Shortened URL: ${shortened2.shortUrl} (ID: ${shortened2.urlId})`
);
// Example 3: Using default collection name (backward compatibility)
console.log("\n=== Example 3: Using default collection name ===\n");
const shortify3 = Shortify.createWithMongo(
"https://short.example.com/",
process.env.MONGODB_URI || "mongodb://localhost:27017/shortify"
// No collectionName specified - will use default "urls"
);
console.log("Connecting to MongoDB with third instance...");
await shortify3.connect();
console.log("Connected to MongoDB");
console.log("Collection name: urls (default)\n");
// Shorten another URL
const url3 = "https://example.net/default-collection-url";
console.log(`6. Shortening URL: ${url3}`);
const shortened3 = await shortify3.shorten(url3);
console.log(
`Shortened URL: ${shortened3.shortUrl} (ID: ${shortened3.urlId})`
);
// Clean up
console.log("\n=== Cleaning up ===");
await shortify.delete(shortened.urlId);
await shortify2.delete(shortened2.urlId);
await shortify3.delete(shortened3.urlId);
console.log("All URLs deleted");
// Disconnect all instances
console.log("\nDisconnecting from MongoDB...");
await shortify.disconnect();
await shortify2.disconnect();
await shortify3.disconnect();
console.log("All instances disconnected");
console.log("\n=== Summary ===");
console.log("✓ Created 3 different collections:");
console.log(" - my_custom_urls");
console.log(" - another_custom_collection");
console.log(" - urls (default)");
console.log("✓ Each collection operates independently");
console.log("✓ Backward compatibility maintained");
} catch (error) {
console.error("Error:", error);
}
}
// Run the example
main();