• Blog
  • Documentation
  • Courses
  • Changelog
  • AI Starters
  • UI Kit
  • FAQ
  • Supamode
    New
  • Pricing

Launch your next SaaS in record time with Makerkit, a React SaaS Boilerplate for Next.js and Supabase.

Makerkit is a product of Makerkit Pte Ltd (registered in the Republic of Singapore)Company Registration No: 202407149CFor support or inquiries, please contact us

About
  • FAQ
  • Contact
  • Verify your Discord
  • Consultation
  • Open Source
  • Become an Affiliate
Product
  • Documentation
  • Blog
  • Changelog
  • UI Blocks
  • Figma UI Kit
  • AI SaaS Starters
License
  • Activate License
  • Upgrade License
  • Invite Member
Legal
  • Terms of License
  • Auth Overview
  • Global Configuration
    • Setting up your Firebase Project
    • Setting up Firebase Functions
  • Writing data to Firestore
  • Commands
  • Introduction
  • Production Checklist
  • Introduction
  • Overview
  • Stripe Configuration
  • Running Tests
  • Introduction
  • Setting up Firebase Auth
  • Fetching data from Firestore
  • Technical Details
  • Extending Organizations
  • Stripe Webhooks
  • CI Tests
  • Initial Setup
  • React Hooks
  • Auth Flow
  • API requests
  • Code Style
  • Clone the repository
  • Security Rules
  • User Permissions
  • Limitations
  • Project Structure
  • Third-Party Providers
  • Reading data from Storage
  • Running the application
  • Subscription Permissions
  • One-Time Payments
  • Running the App
  • Email Link Authentication
  • Uploading data to Storage
  • Security Rules
  • Migrate to Lemon Squeezy
  • Project Configuration
  • Multi-Factor Authentication
  • Writing your own Fetch
  • Translations and Locales
  • Coding Conventions
  • Environment Variables
  • Architecture and Folder Structure
    • Structure your Application
    • Data Model
  • Requiring Email verification
  • Sending Emails
  • Tailwind CSS and Styling
  • Validating API payload with Zod
  • Authentication
  • Onboarding Flow
  • Logging
  • Development: adding custom features
  • Prevent abuse with AppCheck
  • Enable CORS
  • Encrypting Secrets
  • User Roles
  • Firestore: Data Fetching
  • Custom React Hooks
  • Custom React Hooks
  • Firestore: Data Writing
  • Troubleshooting
  • Forms
  • Application Pages
  • API Routes
  • API Routes Validation
  • Translations
  • Adding pages to the Marketing Site
  • Deploying to Production
  • Updating to the latest version
This kit is no longer maintained.

Structure your Makerkit Application | Remix.js Firebase SaaS Kit

Learn how to structure your application with additional entities and business logic in your Remix.js Firebase SaaS Kit

In the previous section, we learned the fundamentals of Makerkit's architecture and the application layers.

In this section, we learn how to structure your application in practical terms with an example. For example, your application has an entity "events": how do we add this entity to a Makerkit application?

NB: entities rarely (or never) get added to "core". Business domain is added to "components" and "lib".

In short, this is how we add a new entity to the application:

  1. First, we add a new folder to lib. If the entity is "event", we add lib/events.
  2. Then, we add the components of the event domain to components/events
  3. Finally, we add the pages of the event domain to pages/events
Structure
- app
- components
- events
- EventsContainerComponent.tsx
- ...
- lib
- events
- types
- event-model.ts
- ...
- hooks
- use-fetch-events.ts
- use-create-event.ts
- ...
- utils
- create-event-model.ts
- routes
- __app
- events
- page.tsx
- $event.tsx

1) Adding the entity's business domain

We will add various business logic units in the lib/events folder, such as types, custom hooks, API calls, factories, functions, utilities, etc.

Types

First, we define a type EventModel at lib/events/types/event-model.ts:

lib/events/types/event-model.ts
export interface EventModel {
name: string;
description: string;
}

Custom Hooks

For example, let's write a custom hook that retrieves a list of "events" from a Firestore collection.

We create a file at lib/events/hooks/use-fetch-events.ts with the following content:

lib/events/hooks/use-fetch-events.ts
import EventModel from '~/lib/events/types/event-model'
export function useFetchEvents() {
const firestore = useFirestore();
const eventsCollection = 'events';
const collectionRef = collection(
firestore,
eventsCollection,
) as CollectionReference<EventModel>;
return useFirestoreCollectionData(collectionRef, {
idField: 'id',
});
}

Good! We have a way to fetch our events, but we have to use it somewhere: to do so, let's create a component EventsListContainer.

NB: remember to update the Firestore rules to be able to read the collection.

2) Components

As said before, we add React components that belong to the "events" domain to components/events.

In the component below, we will fetch a list of events with useFetchEvents:

components/events/EventsListContainer.tsx
import { useFetchEvents } from '~/lib/events/hooks/use-fetch-events';
import Alert from `~/core/ui/Alert`;
const EventsListContainer: React.FC = () => {
const { data: events, status } = useFetchEvents();
if (status === `loading`) {
return <p>Loading Events...</p>
}
if (status === `error`) {
return (
<Alert type='error'>
Ops, we encountered an error!
</Alert>
);
}
return (
<div>
{events.map(event => {
return (
<div key={event.name}>
<p>{event.name}</p>
<p>{event.description}</p>;
</div>
);
})}
</div>
)
};
export default EventsListContainer;

3) Routes

Finally, we can add the events component EventsListContainer to a page. To do so, let's create a page component at routes/__app/events/page.tsx:

routes/__app/events/page.tsx
import EventsListContainer from '~/components/EventsListContainer';
const EventsPage: React.FC = () => {
return (
<EventsListContainer />
);
};
export default EventsPage;

🎉 That's it! We have now built a nicely structured "events" domain.