"title"=>"Ruby on Rails-like ORM and scaffolding in Golang anyone?",
"summary"=>nil,
"content"=>"
I ❤️ Ruby on Rails. With all my 💛. I can rapid-prototype any 3–4 models app in a couple of hours and do a POC in an afternoon, and this is thanks to scaffolding. I can write this and RoR will create a proper Model, Controller, and ~4 Views (MVC) and DB schema with rock-solid change-management :
rails generate scaffold Album title:string year:decimal singer:reference
rails generate scaffold Track title:string duration:decimal album:references
rails generate scaffold Article title:string body:string comment:string rating:integer url:string is_public:boolean
bundle exec rake db:migrate
However, my colleagues bully me that🔻Ruby is dead, 🚈 Rails is dead, and long live 📦 Node.Js and so on. So I thought: it’s 🎅 December, let’s learn a new language. I thought of learning C# — just kidding (I’m already a pianist)— I thought that Golang was the way to go. As of December 1st 🎅, everyone on Facebook has a single thing in mind: Whammageddon!
After taking a 2h course in Go and doing my first word count, my head got big and I thought: I’m now ready for the next step: a silly Whammageddon app to record my friends whammageddon spectacular stories!
🚈 Golang on Rails options
I tried (meaning, I spent 2 hours each) a few options (Bee, Revel, Buffalo), and I’ll describe them in order. Note that I might be unfair to the first in chronological order as my ability to go get something wasn’t as good as in try number 3 :)
1: 🐝 Beego
Beego was the first Google result for “Ruby on Rails” for go. To be fair to bee, I was on a long flight to Israel and tried to make it work with jumpy airport internet and the experience was horrible (as a true Italian, I thought of filing a PR to enlarge gocapabilities with a new 4-letter verb). Nothing wrong with Bee, though.
This is how a migration looks like:
Pros 😃:
- I really enjoyed its ability to simply create ORM model from CLI. Example:
$ bee generate scaffold riccardo_post -fields 'title:string,body:text,active:bool,published:datetime'
- It also allows to have different models connecting to different datasources (wOOt! I wonder how you do a JOIN from Europe to Australia). Example: ```
bee generate scaffold user -fields="first_name:string:255,last_name:string:255,email:string:255" -driver=postgres -conn="postgres://user:pass@localhost:5432/db?sslmode=disable"
bee generate scaffold riccpost -fields="title:string,body:text" -driver=mysql -conn="root:@tcp(127.0.0.1:3306)/test"
- CLI allows to create docs and swagger interface on the fly via bee run -downdoc=true -gendoc=true .
- I ~supports 80% scaffolding for model, controllers, DB/migration but creates an empty view (github issue). In my case, it gets the controller dependencies wrong (probably because its a monorepo) and I need to manually fix it ( long path to beego-whammageddon/models). 😫
- bee dockerize creates a Dockerfile for you. I’ve always wished for the same on RoR!
Cons 😢 :
- Lack of Scaffolding capabilities on the view side. It rocks on model/migration, but it doesn’t create the needed views.
- Scaffolding on Controller side seems broken to me. I get Method Not Allowed, which is answered here (missing Get()function — why would you do that?!?)
2. 🥳 Revel
When I read from Amrit: “ If you have used Ruby on Rails, you are going to love Revel. If you are a Python developer who has used Django, you will like Beego.” I felt: what have I done in the past 4 hours?!? I had an agnition, or shall I say… a revelation!!
Revel has a very simple and straightforward docs page which will guide you through a multi-page, stateless form-rich hello world within 10 minutes.
It has modules (pluggable packages) for NewRelic, Static files, Logging, ..
It has nice examples repos; the closest to Ror seems to be the Booking app (docs — code). Code is from 2020.
It uses GORP as ORM. However, after first excitement, I realized also Revel lacks a proper way to scaffold
I was too stupid to make it work and I threw the sponge. Hoewver with 2 lines you can actually get this — wow!
Commands to achieve this (copied from here):
git clone https://github.com/revel/examples.git "${GOPATH}/src/github.com/revel/examples"
#DOESNT WORK FOR ME - I might be stuipid
revel run github.com/revel/examples/booking
# THIS WORKS FOR ME
revel run "${GOPATH}/src/github.com/revel/examples"
So if you have time to understand and reverse engineer the booking app, you’re going to enjoy Revel a lot. However, the inability to scaffold from scratch is a real turn off for me.
Here I totally failed to do any scaffolding: no model/controller/view for me. I’ll probably spend some time in the weekend reverse-engineering the booking app… 😢
3. 🦬Buffalo
Now, the first thing you see with Buffalo is — wOOt? They even bothered adding a CSS! Clearly, there’s some money and a real company behind this (like DHH and Basecamp behind Rails). Graphics is captivating and I love it already!
First, it uses pop which supports PostgreS Cockroach MySQL and Sqlite3 out of the box.
With Soda and Fizz, you get a Rails-like experience in setting up the DB and migrating it:
// migrate UP
create_table("users") {
t.Column("email", "string", {})
t.Column("twitter_handle", "string", {"size": 50})
t.Column("age", "integer", {"default": 0})
t.Column("admin", "bool", {"default": false})
t.Column("company_id", "uuid", {"default_raw": "uuid_generate_v1()"})
t.Column("bio", "text", {"null": true})
t.Column("joined_at", "timestamp", {})
}
// migrate down
drop_table("users")
This is absolutely fantastic ! Unfortunately it doesn’t seem to have the CLI interface to create a scaffold 😐, while you can reach a great experience by typing the code yourself.
To use generators, it looks to me you need to install pop as a separate module: https://github.com/gobuffalo/buffalo-pop. After doing so, you get buffalo pop g --help which returns this same output. (Before, it would return a weird output).
Now, I was able with a single command line (non documented anywhere, just by trying out!) to create a new model with my fields and also its migration, but not the controller and the views :/
# For '--skip-migration' read below
buffalo pop g model --skip-migration foobar RiccTitle:string RiccDescription:text
buffalo pop g model --skip-migration barbaz RiccDescription:text UltimateAnswer:int32 Active:bool
Note: he first time you call the function without skip miogration, but every subsequent invocation it will create another migration for the same model; so if you just want to auto-magically update the model until you got it right, you call it as above.
Like in Rails, DB can be easily configured in a single YAML or via ENV variables.
# database.yaml
development:
dialect: postgres
database: whamageddon_buffalo_development
user: postgres
password: postgres
host: 127.0.0.1
pool: 5
test:
url: {{envOr "TEST_DATABASE_URL" "postgres://postgres:postgres@127.0.0.1:5432/whamageddon_buffalo_test?sslmode=disable"}}
production:
url: {{envOr "DATABASE_URL" "postgres://postgres:postgres@127.0.0.1:5432/whamageddon_buffalo_production?sslmode=disable"}}
[Golang vs Ruby] on ORM and Scaffolding
So my first impressions on Go (vs Ruby):
- ➕blazing fast
- ➕compile in a single binary — easy to dockerize/ship.
- ➖ Go has a tendency to want your app to be single repo. What if I want to have 3 folders under the same repo — can I Mr Go?
- ➖ Lack of a convincing zero-brain scaffolding technique.
Conclusions
Like Bonucci told the Brits at the end of European Championship in London, “ne dovete mangiare di pastasciutta!” (you still have a lot of pasta to eat), I believe Golang has still a long way to go to get to Ruby feasts when it comes to ORM and Rails rapid-prototyping.
This morning, while swimming, I wanted to create a nice table with 3–5 stars for all, but Medium doesn’t support tables. Hence I’ll just get the winner per topic:
- Documentation: revel & buffalo
- Look and feel: Buffalo
- Documentation for day-0 hello app: revel.
- Model CLI creation: beego.
- Migration: Buffalo (thanks to fizz)
- Scaffolding: none (rails rulez).
However, go has a long list of advantages that makes this passage worthwhile:
- damn easy dockerization (single chubby binary)
- no dependencies craze in prod (single chubby binary)
Note there are also other solution apart from Bee, Revel and Buffalo: Gin&Gonic, Goji, Iris, … and I hear echo also strikes a great balance (think of Sinatra if you’re a rubyist).
Note. I’m a ~day1 user of Go. I’m pretty sure some things I’ve said might be incorrect. I’d be honoured to get your feedback in form of comments.
","author"=>"Riccardo Carlesso",
"link"=>"https://medium.com/@palladiusbonton/ruby-on-rails-like-orm-in-golang-anyone-gopher-random-4558047da41?source=rss-b5293b96912f------2",
"published_date"=>Thu, 22 Dec 2022 09:16:13.000000000 UTC +00:00,
"image_url"=>nil,
"feed_url"=>"https://medium.com/@palladiusbonton/ruby-on-rails-like-orm-in-golang-anyone-gopher-random-4558047da41?source=rss-b5293b96912f------2",
"language"=>nil,
"active"=>true,
"ricc_source"=>"feedjira::v1",
"created_at"=>Sun, 31 Mar 2024 20:27:08.933085000 UTC +00:00,
"updated_at"=>Mon, 21 Oct 2024 15:34:22.183767000 UTC +00:00,
"newspaper"=>"Riccardo Carlesso - Medium",
"macro_region"=>"Blogs"}