custom-unique-id-generator
v1.2.4
Published
Custom unique ID generator with prefix and sequential numbering
Downloads
33
Maintainers
Readme
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
