Introducing Next-Rails CLI
Picture this: with just a few simple commands, you can set up a full-stack Next.js application, complete with PostgreSQL, migrations, API routes, and pages. This isn't a developer's fantasy, but the reality I've been crafting. Welcome to the world of next-rails, a CLI tool created to supercharge your Next.js development and make tedious setup a thing of the past.
You can find next-rails on npm
You can find the source code in my github repo
In case the embed doesn't work here is a link to my youtube video introducing next-rails
What is next-rails?
I fell in love with the migration/seed/scaffolding features of the Ruby on Rails CLI.
I wanted to bring that power and syntax to Next.js applications.
So I did.
How do I use it?
You can use it with npx or install it globally.
npx next-rails new my-app
This will create a brand new Next.js application using create-next-app
and then install a bunch of "extras" on top.
Some of the extras include:
- typescript
- eslint
- prettier
- tailwindcss
shadcn
compatibility- postgresql support
- a fork of
knex
for migrations and seeds and query building - Dockerfiles for development, with a
docker-compose.yml
to instantly spin up a Next.js development environment with postgresql (including PGVector support!)
After you've created your app is where the fun really begins. You can use the next-rails
CLI to scaffold out a full-stack CRUD (Create Read Update Delete) application with:
- Database Migrations (src/db/migrations)
- API Routes (src/pages/api/yourmodel)
- Page Routes (src/pages/yourmodel)
- Model - TypeScript interface (src/db/models/yourmodel)
Just like in Rails, your model name should be PascalCase and singular. The CLI will automatically pluralize it for you, the same way that Rails does.
# From the 'my-app' directory
npx next-rails generate scaffold User name:string age:integer
# You can use g for short
npx next-rails g scaffold Todo title:string completed:boolean user:references
# Migrate your changes to the database
npx next-rails db:migrate
What's next?
For now I'm going to keep using this in my own projects and see how it goes. I'm sure there are bugs and I'm sure there are features that I haven't thought of yet. Please file an issue on GitHub with feature requests or bug reports. Also definitely contribute if you know how! I don't pay as much attention to making the front-end as pretty as I'd like, but the bones are there for someone to contribute to the page templates and make the apps scaffolded with next-rails
to look gorgeous too.
That's all for now! Thanks for reading and I hope you enjoy using next-rails
as much as I do!
If you found this post helpful, feel free to connect with me on LinkedIn. Please let me know if you have any questions or feedback!