npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

custom-unique-id-generator

v1.2.4

Published

Custom unique ID generator with prefix and sequential numbering

Downloads

33

Readme

npm downloads license test

custom-unique-id-generator

A simple and customizable package to generate persistent, sequential, human-readable unique IDs with configurable prefixes and storage options (file or in-memory).


Features

  • Generate sequential IDs with a custom prefix and total length (e.g., ABC0001, HOD1021)
  • Supports persistent storage in JSON files or in-memory storage
  • Safe for concurrent use with basic locking
  • Easy to integrate in Node.js projects
  • Lightweight and configurable

Installation

npm install custom-unique-id-generator

Basic Usage

import { PersistentUniqueIDGenerator } from 'custom-unique-id-generator';

const generator = new PersistentUniqueIDGenerator({ prefix: "INV", totalLength: 7, storage: { type: "file", filePath: "./invoice-counter.json" } });

const id = await generator.generate(); // Returns: "INV0001"

Sequelize Integration Example

hooks: { beforeCreate: async (record) => { if (!record.id) { record.id = await generator.generate(); } } }

CLI Usage Example (if added later)

npx custom-unique-id-generator --prefix=USR --length=6

All Options Documented

| Option | Type | Description | |--------|------|-------------| | prefix | string | Prefix for generated ID (e.g., USR) | | totalLength | number | Total length including prefix (e.g., 6 → USR001) | | storage.type | "file" or "memory" | Storage backend | | filePath | string | (Required if type=file) Path to store counter |

Example: Generate multiple IDs

async function generateMultiple() { for (let i = 0; i < 10; i++) { const id = await generator.generate(); console.log("Generated ID:", id); } }

generateMultiple();

sample table stacher

import { DataTypes, } from 'sequelize'; import { PersistentUniqueIDGenerator } from "custom-unique-id-generator";

// Create a unique ID generator instance const generator = new PersistentUniqueIDGenerator({ prefix: "POR", totalLength: 6, // Example: POR001 storage: { type: "file", filePath: "./counter-data.json", }, });

export default (sequelize) => {

  const attributes = {
      purchase_order_id: { type: DataTypes.STRING, primaryKey: true },

      type: { type: DataTypes.STRING, defaultValue: null },

      attachment: { type: DataTypes.JSONB, defaultValue: [] },

      company: { type: DataTypes.STRING, defaultValue: null },

      date: { type: DataTypes.DATE, defaultValue: null },

      po_number: { type: DataTypes.STRING, defaultValue: null },

      created_by: { type: DataTypes.STRING, defaultValue: null, },

      updated_by: { type: DataTypes.STRING, defaultValue: null, },

      created_on: { type: DataTypes.DATE, defaultValue: DataTypes.NOW, },

      updated_on: { type: DataTypes.DATE, defaultValue: null },

  };

  const PurchaseOrder = sequelize.define('purchase_order', attributes, {
      tableName: 'purchase_order',
      timestamps: false,
      hooks: {
          beforeCreate: async (purchaseOrder: any) => {
              if (!purchaseOrder.purchase_order_id) {
                  purchaseOrder.purchase_order_id = await generator.generate();
              }
          },
      },
  });

    // Reset counter if table is empty (run once on model initialization)
  (async () => {
      try {
          await sequelize.authenticate();
          const count = await PurchaseOrder.count();
          if (count === 0) {
              await generator.resetIfTableIsEmpty(true);
          }
      } catch (err) {
          console.error('Error initializing purchase_order:', err);
      }
  })();

  return PurchaseOrder;

};

License

MIT License

Contributing

Contributions, issues, and feature requests are welcome!

Author

Jothi Manikandan