Hacker News new | past | comments | ask | show | jobs | submit login

Devs start with ORMs because they promise to make everything easy.

Then their apps grow complex enough that the ORMs get in the way.

Then the devs get sad.




Re: no ORMs - do you have any advice on how I could make this raw SQL insertion ( https://github.com/bbkane/starghaze/blob/54d255f524f8ef73384... ) more readable and less error prone? Right now I'm basically concatenating strings and hoping I don't misorder my parameters when I exec. 200 lines of this!! Am I doing raw SQL wrong?


> Right now I'm basically concatenating strings

Well, you're using query parameters, but I think I understand what you mean.

What I would do is write a stored procedure that takes whatever parameters, then I'd write PG SQL to generate bindings to that procedure for whatever language you're using (Golang here). I might write the bindings by hand if it's not a lot of work, but definitely would write a little compiler if it's a tone of work. That way you can get type-safety.


Thank you. I'm using SQLite3 to keep deployment simple, which unfortunately doesn't support stored procedures. Do you have any other suggestions to improve this code?


SQLite3 very much supports something like stored procedures though! Only, instead of calling them that it calls them `INSTEAD OF INSERT` triggers.

So, you'd do something like:

  CREATE VIEW IF NOT EXISTS things AS ...;
  CREATE TRIGGER IF NOT EXISTS create_thing
  INSTEAD OF INSERT ON things
  FOR EACH ROW
  BEGIN
      ...
  END;
and to invoke such a "stored procedure" you'd

  INSERT INTO things (...)
  VALUES (...), ...;
or `INSERT .. SELECT ..;`.

Now, with SQLite3 you don't get reflection via SQL, like you do in PG via the `pg_catalog` schema, but you do get reflection via the API. Also, because it's SQLite3, you don't get strong typing, and you get very few types, so even with reflection via the API, generating type-safe stub functions to invoke the "stored procedure" is not exactly easy or possible.

FYI, SQLite3 is Turing-complete. You can loop via recursive triggers. And you can do conditionals via `WHERE` clauses on the statements in your trigger bodies (and also via `OR IGNORE` and such).

EDIT: I made a post of this: https://news.ycombinator.com/item?id=31913062


SQLite supports named parameters: https://sqlite.org/c3ref/bind_blob.html

I don't know if Go's SQLite support in turn supports named parameters, but if it does, then that would likely help considerably with readability.


It takes a lot to say very little in Go sometimes, I'd say this is one of those cases. Mainly the if err != nil boiler is at issue here.

I don't think you are doing anything wrong from a noobies opinion.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: