Skip to main content

๐Ÿ” Fetching Documents

Getting your data out of the database is just as important as saving it! Cascade makes querying a joy, with powerful, expressive, and super-friendly APIs. ๐Ÿ•ต๏ธโ€โ™‚๏ธ

๐Ÿ”Ž Find by IDโ€‹

Fetch a single document by its ID:

src/app.ts
const user = await User.find(1);

๐Ÿšจ Find or Failโ€‹

Throw an error if not found (great for APIs!):

src/app.ts
const user = await User.findOrFail(1); // Throws if not found

๐Ÿ“‹ List All (with Filters!)โ€‹

Get all documents, or filter with powerful queries:

src/app.ts
const users = await User.list(); // All users
const activeUsers = await User.list({ isActive: true }); // Filtered

๐Ÿง™โ€โ™‚๏ธ Pro Tip: Advanced Filtersโ€‹

You can use advanced MongoDB-style queries:

src/app.ts
const users = await User.list({ age: { $gte: 18 } });

๐Ÿช„ Including Soft-Deleted (Trashed) Documentsโ€‹

Want to include soft-deleted docs? Use withTrashed:

src/app.ts
const users = await User.list({ withTrashed: true });

๐Ÿค Eager Loading Relationshipsโ€‹

Fetch related data in one go with with:

src/app.ts
const posts = await Post.list({ with: ["category"] });

๐Ÿงฉ Embedding Models (New Pattern!)โ€‹

When you use the with option, just pass the model class directly for embedded relationships:

src/app.ts
const posts = await Post.list({ with: [Category] }); // New & recommended!
Did you know?

You no longer need to use castModel(Category)โ€”just pass the model class directly! This is the new, recommended way. castModel is still supported for backward compatibility.

๐ŸŽ๏ธ Pagination & Sortingโ€‹

Cascade supports pagination and sorting out of the box:

src/app.ts
const users = await User.list({ page: 2, limit: 10, sort: { createdAt: -1 } });

๐Ÿง™โ€โ™‚๏ธ Pro Tipโ€‹

Combine filters, sorting, pagination, and relationships for supercharged queries!


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