
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
@hsa-technologies-00/mongoose-automatic-reference
Advanced tools
Mongoose plugin for automatic reference IDs like CERT-1, CERT-2 etc.
The @hsa-technologies-00/mongoose-automatic-reference
plugin automatically generates incrementing reference IDs for your Mongoose documents (e.g., "DOC-1", "DOC-2"). It maintains counters in a separate MongoDB collection and provides thread-safe ID generation.
Key Features:
npm install @hsa-technologies-00/mongoose-automatic-reference
or
yarn add @hsa-technologies-00/mongoose-automatic-reference
import mongoose from "mongoose";
import automaticReference from "@hsa-technologies-00/mongoose-automatic-reference";
const schema = new mongoose.Schema({ name: String });
schema.plugin(automaticReference, {
fieldName: "docId", // Field to store the generated ID
prefix: "DOC", // Prefix for the ID (e.g., DOC-1)
});
const Model = mongoose.model("Document", schema);
// When creating documents
const doc = await Model.create({ name: "Test" });
console.log(doc.docId); // Outputs "DOC-1"
schema.plugin(automaticReference, {
fieldName: "certificateId",
prefix: "CERT",
initialValue: 1000, // Start counting from 1000 (CERT-1000)
});
Option | Type | Required | Default | Description |
---|---|---|---|---|
fieldName | string | Yes | - | The field name to store the generated ID |
prefix | string | Yes | - | The prefix for the reference ID |
initialValue | number | No | 1 | Starting number for the sequence |
// Certificates model
const certificateSchema = new mongoose.Schema({ name: String });
certificateSchema.plugin(automaticReference, {
fieldName: "certId",
prefix: "CERT",
});
// Invoices model
const invoiceSchema = new mongoose.Schema({ amount: Number });
invoiceSchema.plugin(automaticReference, {
fieldName: "invoiceNumber",
prefix: "INV",
initialValue: 1000,
});
const Certificate = mongoose.model("Certificate", certificateSchema);
const Invoice = mongoose.model("Invoice", invoiceSchema);
// Usage
const cert = await Certificate.create({ name: "SSL Cert" });
const inv = await Invoice.create({ amount: 100 });
console.log(cert.certId); // e.g., "CERT-1"
console.log(inv.invoiceNumber); // e.g., "INV-1000"
const existingSchema = new mongoose.Schema({
/* ... */
});
// Add the plugin to existing collection
existingSchema.plugin(automaticReference, {
fieldName: "legacyId",
prefix: "LEG",
initialValue: 5000, // Start after existing IDs
});
const session = await mongoose.startSession();
session.startTransaction();
try {
const doc = await Model.create([{ name: "Tx Test" }], { session });
await session.commitTransaction();
console.log(doc[0].docId);
} catch (error) {
await session.abortTransaction();
throw error;
} finally {
session.endSession();
}
Extend the plugin with custom formatting:
schema.plugin(automaticReference, {
fieldName: "customId",
prefix: "CUST",
formatter: (prefix: string, num: number) => {
const year = new Date().getFullYear();
return `${year}-${prefix}-${String(num).padStart(5, "0")}`;
// e.g., "2023-CUST-00001"
},
});
To run tests:
npm test
Test environment requires:
mongodb://localhost:27017/test_db
)git checkout -b feature/xyz
)git commit -am 'Add feature xyz'
)git push origin feature/xyz
)MIT © Md. Harun Or Rashid
If you find this package useful, please consider giving it a ⭐️ on GitHub.
Enjoy building Mongoose plugins with ease! 🚀
FAQs
Mongoose plugin for automatic reference IDs like CERT-1, CERT-2 etc.
We found that @hsa-technologies-00/mongoose-automatic-reference demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.