Skip to main content

πŸ”’ Auto-Increment IDs

Need sequential IDs like in SQL? Cascade's auto-increment feature gives you easy, reliable, and unique numbers for your documents! πŸ”₯

🚦 How It Works​

Cascade manages a special counter collection behind the scenes. Every time you create a new document, it grabs the next number for youβ€”no collisions, no worries!

πŸ—οΈ Enabling Auto-Increment​

Just set autoIncrement = true in your model:

src/models/order.ts
import { Model } from "@warlock.js/cascade";

export class Order extends Model {
public static collection = "orders";
public autoIncrement = true;
}

πŸ§™β€β™‚οΈ Pro Tip: Custom Start Value​

You can set a custom starting value:

src/models/order.ts
public autoIncrement = { start: 1000 };

⚠️ Gotchas​

  • Auto-increment is great for user-facing IDs, but not for sharding or distributed writes.
  • Use MongoDB's ObjectId for globally unique IDs.
info

Auto-increment works perfectly with all Cascade featuresβ€”casting, events, and more!


🌟 What's Next?​