Setup for Next.js SaaS Dev in Local
Prerequisites
NGROK Setup
- Create an NGROK account.
- On the NGROK Dashboard (Cloud Edge > Domains), generate a new domain (the first one is free).
- Run the following command to set up your domain:
ngrok http --domain=your-personal-domain.ngrok-free.app 3000
- Download and install NGROK on your local machine, and set it as an environment variable.
- Verify the installation by running the NGROK command in the terminal.
Clerk Setup
- Create a Clerk account.
- Create an application (give it any relevant name).
- Enable organization creation under Configure > Organization Settings.
- Customize the session token under Configure > Sessions, click Edit, and set the value as:
{"metadata": "{{user.public_metadata}}"}
- Create a webhook under Configure > Webhooks.
-
Set the custom URL to:
https://<Serverdomain>/en/api/clerk
(This is the domain generated by NGROK.)
- Subscribe to "Organization" and "User" events.
- Temporary Super Admin Access needs to be granted as of now.
Enode Setup
- Create an Enode account.
- Create an organization.
- Create a client on the Enode Dashboard. Set the client environment to Sandbox, and ensure the client and link UI names are the same.
SQL Setup
- Download and install MySQL server.
- Set up and start the server (create a user if it's new).
- Create a database.
Code Setup
- Install all dependencies:
npm i
Setting Environment Variables
Set the following environment variables in your .env
file:
-
DATABASE_URL
Set your local MySQL connection URL:
mysql://host_name:host_password@localhost:port_number/db_name
NEXT_PUBLIC_DEMO_MODE
Set to true
.
NEXT_PUBLIC_CLERK_WEBHOOK_SECRET
Copy the Signing Secret from Clerk > Configure > Webhooks.
NEXT_PUBLIC_ROOT_DOMAIN
Copy your NGROK domain (the text between --domain=
and port_number
).
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
Copy from Clerk > Developers > API Keys.
CLERK_SECRET_KEY
Copy from Clerk > Developers > API Keys.
ENODE_OAUTH_URL
Copy from Enode Dashboard > client_name > Settings > API Credentials.
ENODE_CLIENT_ID
Copy from Enode Dashboard > client_name > Settings > API Credentials.
ENODE_CLIENT_SECRET
Copy from Enode Dashboard > client_name > Settings > API Credentials.
-
ENODE_BASE_URL
Set as:
https://enode-api.sandbox.enode.io
-
ENODE_VEHICLE_ADD_REDIRECT
Set as:
http://localhost:3000/en/home/vehicles
Starting the App (In Sequence)
1. Start NGROK Local Server
Open the terminal and run:
ngrok http --domain=your-personal-domain.ngrok-free.app 3000
2. Update the Local Database
- Open the terminal in the app folder.
- Install Prisma globally if necessary:
npm i prisma -g
- Populate the database:
npx prisma migrate dev --name init
3. Set Environment Variables (Windows Only - If Required)
Open the terminal and set all environment variables from the .env
file:
set variable_name=value
4. Start the App
Open the terminal and run:
npm run dev
Prisma Migration Guide
This guide outlines the steps to manage database migrations using Prisma in both development and production environments.
Development Environment
1. Set Up Your Development Environment
- Ensure that your development environment is set up with the appropriate database.
- Update your
.env
file to point to your development database.
2. Make Changes to Your Prisma Schema
- Edit the
schema.prisma
file to reflect any changes to your database schema.
3. Create a Migration
-
Run the following command to create a new migration:
npx prisma migrate dev --name descriptive_migration_name
-
Replace descriptive_migration_name
with a meaningful name that describes the changes.
4. Apply the Migration
- The above command will automatically apply the migration to your development database.
5. Generate Prisma Client
6. Test Your Application
- Test your application in the development environment to ensure everything works as expected.
Production Environment
1. Prepare Your Production Environment
- Ensure your production database is configured and accessible.
- Update your environment variables to point to the production database.
2. Create Migrations for Production
- First, create the migration files in your development environment using the same command as before:
npx prisma migrate dev --name descriptive_migration_name
- This will generate the migration files in the
prisma/migrations
directory.
3. Apply Migrations in Production
- When you're ready to apply the migrations to your production database, run the following command:
npx prisma generate
- This command will apply any pending migrations without creating new migration files.
4. Generate Prisma Client in Production
- After deploying the migrations, ensure the Prisma Client is up to date with:
npx prisma generate
5. Test in Production
- Test your application in the production environment to ensure everything is functioning correctly after the migration.
6. Backup Your Database (Optional but Recommended)
- Always consider backing up your production database before applying migrations to protect against any potential issues.
Transitioning from Development to Production
- Develop and Test Locally : Make and test your changes in the development environment.
- Create Migration Files : Use the
migrate dev
command to create migration files.
- Deploy to Production : When ready, transition to production by using the
migrate deploy
command.
- Generate Client : Ensure the Prisma Client is generated after deploying migrations.
- Validate : Test your application in production to confirm that all features work as intended.
Notes:
- Make sure to replace
descriptive_migration_name
with appropriate names that describe the migration purpose.
- Adjust any environment variable configurations specific to your project as needed.
- You can add this section to your existing README file to help guide future contributors or yo
</code></div>``</div></pre>