Go to file
2025-06-02 11:06:07 +05:30
dbConfig Enforce uniqueness on short_code + expiry_date + user_id via DB trigger 2025-06-02 11:06:07 +05:30
utils fix: return existing shortener if matching record already exists 2025-06-02 10:55:08 +05:30
.env.example initial commit 2025-05-28 11:33:08 +05:30
.gitignore initial commit 2025-05-28 11:33:08 +05:30
index.js Update index.js 2025-05-30 11:29:31 +00:00
package-lock.json initial commit 2025-05-28 11:33:08 +05:30
package.json initial commit 2025-05-28 11:33:08 +05:30
README.md initial commit 2025-05-28 11:33:08 +05:30

🔗 Link Shortener Plugin

A simple, pluggable Node.js module for shortening URLs using SQLite, with support for custom expiry dates and automatic cleanup via cron jobs.


📦 Features

  • Shorten long URLs with optional expiry dates
  • Retrieve the original URL from a short code
  • Auto-deletes expired links using a daily cron job
  • Uses SQLite for lightweight persistence
  • No external APIs required

🛠️ Installation

npm install digi-url-shortener

Make sure you have Node.js v18+ installed.

📁 Project Structure

    link-shortener/
    ├── database/
    │   └── links.db              # SQLite DB (created automatically)
    ├── utils/
    │   └── helperFunctions.js    # Core logic (shorten, retrieve, cleanup)
    ├── dbConfig/
    │   └── index.js              # SQLite CRUD operations
    ├── index.js                  # Entry point or API setup
    ├── .env.example              # Environment variables
    ├── package.json
    └── README.md

⚙️ Configuration

DB_PATH=./database/links.db

🚀 Usage

Import Functions

const {
  getShortenUrlCode,
  getOriginalUrlByShortCode,
  deleteExpiredShortUrlsCodes,
} = require("./interface");

Create a Short URL

const shortCode = await getShortenUrlCode({
  longUrl: "https://example.com",
  expiryInDays: 7,
  codeLength: 10,
});
console.log("Short code:", shortCode);

Get Original URL

const originalUrl = await getOriginalUrlByShortCode("shortCode123");
console.log("Original URL:", originalUrl);
deleteExpiredShortUrlsCodes();

Example

(async () => {
  const shortCode = await getShortenUrlCode({
    longUrl: "https://example.com",
    expiryInDays: 5,
    codeLength: 10,
  });

  console.log("Short code is:", shortCode);

  const original = await getOriginalUrlByShortCode(shortCode);
  console.log("Original URL:", original);
})();

License 📝

This project is licensed under the ISC License.

Author ✍️

DigiMantra