6 ms·
Ent: An Entity Framework for Go
- metadat 4y agoI thought this looked familiar.. Discussed 2 years ago: https://news.ycombinator.com/item?id=26008521 https://news.ycombinator.com/item?id=26008521. (39 points, 9 comments)
- ivanvanderbyl 4y agoI first heard about Ent on HN about 2 years ago. At the time I was evaluating every ORM in Go to find something that would fit the bill and Ent happened to be exactly what we needed. We've now been using it for 2 years and it's been one of the best bets we made on a technology choice. Things that have worked really well: * Auto DB migrations using Ent+Atlas. Ent implements a lot of great low level defaults that I just don't want to need to think about (foreign key constraints, indexes, naming of join tables etc) * Generating our GraphQL API from Ent Schema * Generating Protobuf definitions for internal tools to talk to (now using Buf for the actual tooling, but having the protos generated saved a huge lot of time). * Being able to quickly craft really complex multi-edge joins without really thinking through the SQL allows for quick implementation of new features. * Query optimizations such as using WINDOW clauses for pagination in GraphQL queries (I wouldn't have even thought this was possible). * The generated code is quite a lot of lines, but it's really nicely structured and idiomatic, making it easy to extend. There's been heaps of other neat finds along the way, but that's a summary. Shout out to Ariel and Rotem for being excellent stewards of the Ent community and helping us solve some complex problems along the way.
- lordofgibbons 4y ago>* Generating our GraphQL API from Ent Schema How do you make sure you don't accidentally expose an-authorized data to the user with auto-generating GraphQL APIs? Does Ent have built-in authorization validation?
- ivanvanderbyl 4y agoI should have mentioned this, Ent has built in authorization at the row level: https://entgo.io/docs/privacy https://entgo.io/docs/privacy There are some gotchas with this, but like all auth you need to take the time to think through it and once you do it is extremely powerful. This approach means you can essentially forget about needing to scope queries to specific users throughout your codebase as Ent will automatically apply that part of the query wherever it is needed. I know there's been some comments on this thread and others about coupling auth to your ORM, I think it's necessary as it's otherwise too easy to forget this somewhere deep in your code and accidentally expose everything to the wrong user.
- rotemtam 4y agoThanks for the kind words, Ivan! You're awesome
- jitl 4y agoI think Ent for Go has a ton of potential. Although I haven’t used this library, I have spent a lot of time studying the design space of ORMs because I’m currently iterating on an internal library that does ORM-like things at Notion. I really like the Ent approach because it allows working with a graph-based data model directly in the embedding language, instead of forcing developers to learn a new query language like Datomic datalog or SPARQL, while simultaneously avoiding lock-in to a specific data store backend. I would like to build something like Ent that is ergonomic and usable on both the client and the server in Typescript, and facilitates easily moving business logic between the two. The context at Notion is that we have a very smart client using something ORM-like and a somewhat dumb server using plain row record values, so while we do share some code between the two, it’s difficult to port larger features from the client to the server. It’s kind of the reverse situation that most people have, which is a smart server pushing GraphQL to a relatively dumb client. There are a lot of people looking into client-focused or end-to-end database abstractions right now, so the space exciting. What the upstarts should learn from Ent is: 1. Codegen is powerful, and often easier to understand than a mountain of typelevel magic. 2. A good ORM solution should really build in permissions and composing mutations at a deep level. The previous generation of ORMs like ActiveRecord left this stuff to user space to everyone’s detriment. 3. Embedded DSL as query language is a blessing and a curse. It’s better than forcing engineers to learn a weird alternative to SQL strings, but more frustrating to experts. Use these tradeoffs with caution!
- Fire-Dragon-DoL 4y agoI keep thinking that the query portion should be entirely split from the ORM portion. Query builder have specific usage, but are not the norm. None of them can model an advanced postgres query, but at the same time, they provide little (or nothing) utilities to map the data coming from a complex query, back to objects, which is funny, to me that would be the definition of "object relational mapper". I know why activerecord does this: it's way easier to do the join in memory, so that one row is obtained per object and not an enormous row representing many, but not providing any utility for raw queries seems a big missing feature. Edit: to be clear, by query I refer to read operations (select) that are not trivial (find one by id would be trivial)
- valenterry 4y ago> 100% statically typed and explicit API using code generation. Whenever I have to do code generation in my language of choice, I feel that the language should be able to do that without having to generate code. It's annoying to inspect and maintain and usually has quirks that I have to work around. How do Go developers feel about that?
- mbrodersen 4y agoThe advantage of Codegen is that it works for any language. Whether the language has macros or whatever. Another advantage is that you can generate code for multiple languages (say Typescript and C#) from the same spec, making sure the client/server code stays 100% in sync. Codegen is my favourite super productivity tool. I routinely write production code where only the core biz logic is hand coded. The rest is fully automated. Most Excellent!
- lordofgibbons 4y agoI've never had an issue with code generation in Go. However, they do tend to be verbose - hopefully they will start using generics to re-use the common bits and cut down on the amount of generated code! The only instance I've had issues with them has been when generating code for things like schema languages (like GraphQL) on large projects where if you click to check the definition of a function/struct, the editor struggles a little to open up a file with tens of thousands of lines of code.
- codeflo 4y agoORMs have the problem that the shape of the interface they provide mostly depends on configuration or data (the SQL schema). In static languages at least, this means that code has to be generated at some point. Tooling and convenience wise, there are differences between the approaches, but conceptually, does it really make a huge difference whether this step happens in a separate tool before compilation (seen here), or inside the compiler (in a macro system like Rust’s), or at runtime (as ORMs tend to do in JITted languages like Java and C#)?
- valenterry 4y ago
- syvolt 4y agoI've been using Ent for some time on a project and its been quite nice to just be able to write the schema in Go, testing has been a breeze with the enttest package, hooks work well, and everything feels intuitive to me unlike most other ORMs or ORM-adjacent tools. My preferred package before Ent was Squirrel [1] but I definitely plan to use Ent for future projects. [1] https://github.com/Masterminds/squirrel https://github.com/Masterminds/squirrel
- tantaman 4y agorelated -- an Ent inspired ORM in TypeScript: https://aphrodite.sh/ https://aphrodite.sh/, although with a focus on local software.
- jitl 4y agoVery cool and very similar to some of the ideas I’ve been playing with!
- bottlepalm 4y agoI'd kill for a port of the .Net Entity Framework to JavaScript - the current ORMs for JavaScript are really nothing compared to EF.. If you haven't used it, it is super powerful at selecting super complex structured data from a database super easy. :) It's query format is also the linq standard so you use the same extension methods to work with in-memory collections as you do with database tables.
- jitl 4y agoDo you have a link that quickly expresses the gist of what makes Entity Framework good/powerful? I tried using it in college for a little project, hit the docs like a brick wall, and went back to Rails. I always struggle “getting to the good parts” with Microsoft’s stuff.
- Foobar8568 4y agoMicrosoft documents are generally fairly good. I haven't touched much .Net these last 10years but i believe that it took at least EF6 in the original .Net framework to have the main good stuff and be productive, add the core version to the confusion...Latest few core versions should be good as well.
- jitl 4y ago“Go read the docs” is not helpful! I tried that 8 years ago based on HN sentiment about C# and my experience with the Microsoft docs was that they are very detailed but the level of detail makes it difficult to quickly pick up what it’s like to actually use one of their thingies. I walked into the docs for Entity Framework, spent 6 hours trying to get Visual Studio and their suggested development database set up, then gave up and went back to Rails which took 1 apt-get command and 2 Ruby commands.
- deleted 4y ago[deleted]
- Humphrey 4y agoHow would this go as a Django replacement? I still use Django for new projects, but find that I end up just putting a GraphQL API on top of the Django ORM. At face value, Ent seems to tick both those boxes, but with the efficiency of a compiled language.
- kbumsik 4y agoI have no experience in Django but in Ent with GraphQL. Ent is not a full-featured web framework so you need to implement many of features by your own or use other libraries (e.g. http server and session management). If you are only looking for ORM + GraphQL then I highly recommend trying Entgql, an Ent extension for GraphQL with Gqlgen library [1]. Once you define an ORM schema, it will generate GraphQL Query for Relay server. Still you need to implement GraphQL Mutations by your own but at least it will create Input types for you (both for Create/Update). [1]: https://github.com/99designs/gqlgen https://github.com/99designs/gqlgen
- rotemtam 4y agoHey @thunderbong One of Ent's maintainers here. Thanks a lot for sharing our project. To all others interested, Ariel (a8m) and I are here following this thread, so feel free to ask us anything and everything!
- DeathArrow 4y agoI see it was inspired by Entity Framework. Does it have migrations?
- a8m 4y agoYes. The migration is built on top of Atlas: https://github.com/ariga/atlas https://github.com/ariga/atlas
- winrid 4y agoEvery time I try to create a new project in some sort of actor/graph manner I end up just creating some DB indexes and crons instead. Maybe I'm just not thinking big enough, but I hope I get to work on something that justifies this architecture someday because it looks like fun
- ThinkBeat 4y agoI hear Entity framework and code as schema I get nervous The .Net entity framework has caused a lot of problems for clients in the past. I am not as familiar with the new one in .net core which is now just .net again I think. A huge mess managing deltas for the schema. Often used by people who do not know SQL nor relational databases that leads to enormously slow and resource intensive queries, (that can be done really easy and fast in raw Sql, and once you figure out how to EF is doing things) schemas that are suboptimal Once you start fighting EF to make it behave better you lose a lot of the benefit of the abstraction in the first place…
- zeroc8 4y agoWe ran into huge problems using Hibernate. Hibernate is such a complex beast and and includes so many traps that I think it's just not worth it, at least not when you don't have an absolute Hibernate expert on board. In our case, people don't care, as long as it "works" - which of course causes a lot of performance problems down the road. I'm not opposed to ORMs in general, but there has to be a middle ground.
- mmargerum 4y agoI can see how EF was useful in a stateful server setup (tracking changes, etc) but I just don't get why it's very useful for stateless API servers. Write some SQL and map it using dapper