Skip to main content

โœจ Creating Documents

Creating new documents in Cascade is a breeze! Whether you like the classic OOP way or prefer a one-liner, Cascade has you covered. ๐Ÿช„

๐Ÿ—๏ธ The Classic Way: New + Saveโ€‹

  1. Create an instance:
  2. Call save() to persist it:
src/app.ts
import { User } from "./models/user";

const user = new User({
name: "Hasan Zohdy",
email: "hassanzohdy@gmail.com",
isActive: true,
});

await user.save();

This gives you full controlโ€”modify fields, run methods, and then save when you're ready.

๐Ÿš€ The Fast Lane: Static createโ€‹

For most cases, you'll love the static create method. It creates and saves in one go:

src/app.ts
import { User } from "./models/user";

const user = await User.create({
name: "Hasan Zohdy",
email: "hassanzohdy@gmail.com",
isActive: true,
});

console.log(user.data); // outputs all data
tip

Recommended! The static create method is the cleanest and most common way to add new documents.

๐Ÿ“ What Happens Under the Hood?โ€‹

  • Cascade auto-generates an id (number) and _id (ObjectId) for you.
  • Default values and type casting are applied automatically.
  • Any model events (like onCreating, onCreated) will run.

๐Ÿง™โ€โ™‚๏ธ Pro Tip: Use Model Methodsโ€‹

You can add your own methods to models for custom logic before/after saving. Models are just classesโ€”make them as smart as you want!


๐ŸŒŸ What's Next?โ€‹