Prisma 2.0 Beta: Type-safe Database Access
prisma.io
prisma.io
With Prisma2, honestly disappointed as it is too little to show for. This is an ORM like library that supports only couple of databases with database migrations still in experimental state with broken links for workarounds[1]
Also Prisma2 has half of the features at 'No / Not yet' supported state - https://www.prisma.io/docs/reference/database-connectors/dat...
Well makes me wonder what's wrong with TypeORM which supports 8 different databases with full feature support and migrations that work!
[1] https://www.prisma.io/docs/reference/tools-and-interfaces/pr...
Data Validation
Cascading delete
Cascading update
Renaming existing table
Renaming existing columnWhile I can understand your sentiment, I don't think this is quite true! We've seen a number of companies very successfully adopt Prisma 1 – ultimately we realized though that the vision of building a new way for application developers to access their database can't be built on top of the JVM. At that point, we needed to make a tough judgement call and decided to rewrite Prisma in Rust. Needless to say that this was quite a challenge, but our engineers have done an absolutely outstanding job of managing this rewrite.
We certainly had a number of tough learnings, especially when it came to the variety of different DB schemas out there that we needed to support with our introspection. I'm really proud of the result though, we've built a strong and stable foundation that allows us to expand the functionality of our tools over time – this would not have been possible without the rewrite!
> With Prisma2, honestly disappointed as it is too little to show for. This is an ORM like library that supports only couple of databases with database migrations still in experimental state with broken links for workarounds[1]
Please recall that we're just launching into Beta. As I said, we now have the foundation to expand the functionality of our tools! You'll see a lot of exciting improvements and new tools from Prisma over the next few months!
Sorry about the broken links, I'm in the process of fixing most of them! In this case, the links point to the "Guides > Database workflows"-section which contains a number of helpful step-by-step tutorials for performing certain schema migrations (e.g. cascading deletes [1]) and using those with Prisma Client. (While these features are not supported by Prisma Migrate yet, you can still configure them in your DB and use Prisma Client for DB access).
[1] https://www.prisma.io/docs/guides/database-workflows/cascadi...
On the other side my experience with Prisma 2 so far has been excellent and I found a very good community around it.
Im using TypeORM and happy to learn if it is so. Can you please show any relevant issues for it ?
It would be interesting to understand how data corruption and lost happens at ORM level!
The gist of this conversation is - passionate open source devs outdo many folds in their work yet fail to monetise their work. And a VC backed company here has little to show for apart from they've managed to trend in HN!
https://github.com/typeorm/typeorm/blob/master/docs/query-ru...
See my response above:
> Also note that you can actually generate Prisma Client into a different location [4], so if you prefer not to have it in node_modules, that's totally possible as well.
[4] https://www.prisma.io/docs/reference/tools-and-interfaces/pr...
Edit: it did at first, now it's loading. I've seen other 404s just clicking around the docs. It's possible you've got some CDN caching issues?
TLDR: Generally deployment should "just work", but there can be cases when you might have to do some extra configuration to get your app deployed.
Also note that you can actually generate Prisma Client into a different location [4], so if you prefer not to have it in node_modules, that's totally possible as well.
[1] https://www.prisma.io/docs/reference/tools-and-interfaces/pr...
[2] https://www.prisma.io/docs/reference/tools-and-interfaces/pr...
[3] https://www.prisma.io/docs/reference/tools-and-interfaces/pr...
[4] https://www.prisma.io/docs/reference/tools-and-interfaces/pr...
For example, we use yarn pnp in our codebase so I have absolutely no hope that it will ever work with Prisma 2.
“Don’t try to be smart” is a very useful mantra for programmers, but Prisma seems to ignore this.
But hey I don’t want to use it anyway because what I want is a graphql endpoint not an ORM. We used Prisma 1 and now moved on to graphile.
Also please read this Twitter thread between the lead Yarn maintainer and our CEO [2]. The gist is that we're very open to other approaches but this was the best we've come up with so far after 9 months of experimentation and lots of feedback from the community. This doesn't get easier by the variety of package managers out there and the custom workflows they sometimes employ (e.g. wrt pruning and caching). To put it in the words of our CEO: we'd be more than happy to embrace whatever package manager mechanism would enable this use case "more cleanly".
[1] https://www.prisma.io/docs/reference/tools-and-interfaces/pr... [2] https://twitter.com/FredKSchott/status/1245053210852618246
I'm secretly rewriting an API written in Prisma 1 into something lower level that actually allows me to write database queries, optimise what is going on, look into the simple underlying abstractions if things go wrong and essentially produce something I can reason about without just hoping nothing goes wrong under the hood.
Apollo codegen is what you want if you'd like your Typescript frontend to be consuming the API in a typesafe way.
At first it may seem like yet another attempt to make database access easy while really making it more complex and harder to maintain.
I haven’t tried this yet, but after reading a couple pages it looks more interesting than that. For example the fact that it relies on typescript knowledge which is already widespread, and the fact that it doesn’t require any server side technology to be installed is a huge advantage.
I’m not sure how it feels in practice, if anyone has experience with it please chime in.
Focus on your data – not on complex SQL queries
Looks like just another SQL DSL trying to make you not learn SQL. TS's structural typing and plain SQL can get you far into a simpler/robust system IMO.
Also, the "introspection" part of Prisma seems very crippled, Postgres has so many useful features for writing application code that it seems wasteful to just treat it as dumb data store.
For "small" and "large" queries, just use the ORM. For "complex" queries, just use SQL.
Next generation “serverless” databases will fully abstract object mapping away. It’s tedious and arbitrary. Even indexing is relatively easy to automate. I am incredibly excited for what Apple is brewing up right now for developers.
It kind of flew under the radar, because it's not super clear how to use it from the landing page.
The DB and the code are not packaged together so is there even such a thing as compile time type safety?
Bringing this type information at "compile-time" into your language of choice where you embed your queries can also be done but is a harder problem. There is good progress in that sphere but it is still in it is infancy.
The GraphQL eco-system for example, has this figured out using code generators. A similar thing can be done with DBs.
def do(s: int) -> str:
return 5
print(do("123"))
Python is duck-typed. Wrong types aren't detected until they fail to quack correctly.Except versioning, maintainability, testability, source control, migration, or just about 80% of what an application developers expects from the thing that runs their code.
I mean, I really really love the idea of stored procedures and triggers, it's so much more elegant than putting all the code in some Node app and just doing read-modify-write cycles into the DB, but until DBs get their ass together wrt being a properly useable platform to run code on, it's not gonna happen.
The smart module bit, it's a bit scary.
basically all I want is a lightweight layer that exposes a promise api to deal with different databases in a uniform manner.
1) sync to schema.json (this json file contains info of all tables, their columns, indices and foreign keys). I can sync to db or from db. This lets me have my state of schema committed to git, and easily move from dev -> staging -> prod. Django migrations are a giant pain in the ass. Don't do smart fancy node modules either.
2) autogenerated typescript types, this are nice. But again don't do fancy node modules, have the typescript types committable to git repo. It makes it easy to diff and bisect when things fo awry.
3) Super light weight Object -> sql. Access foreign relationships as promises (always). Make it very easy to debug the generated sql for perf reasons. Django does some bits well but it's insanely slow at times. Doing things with raw sql should be just as fast as using a library.
Prisma became relevant during their Prisma 1 time, it was one of the first solutions to automatically generate GraphQL endpoints from DB schemas. It only had to keep going this way to lead the whole market. But they chose not too, and competing solutions such as Graphile beats prisma 1 so much now.
Meanwhile they have been stuck for 2 years in their new weird direction of Prisma 2, with no results to show for, and no clear vision. The GraphQL server is gone, even though it was central to their product. Who cares about yet another ORM? I can generate types from any GraphQL endpoint automatically anyway!
For developers not keen on adding a thick indirection layer between the app and the DB, I have been trying to make raw SQL easier and more reliable to use directly in TS with PgTyped[1].
In general, I think most people are more than glad to use raw SQL solution instead of a mapper as long as it has:
1) Realtime parameter/result type inference in their language of choice.
2) Has autocomplete and validation in their IDE
3) Is composable, allowing to join queries/sub-queries..
So far I have been able to implement SQL-in-TS type inference in PgTyped[1] with autocomplete and validation on the way.
I think one thing that distinguishes Prisma (on the longer run) from a library like pgtyped is that Prisma will enable more database workflows than just database access. With Prisma Migrate, you can alter your DB schema and keep a migration history, Prisma Studio will enable lots of DB workflows on a GUI level, and Prisma Cloud will have great integrations for teams and larger scale organizations eventually.
All that being said, because Prisma is a DB toolkit, you can also pick and choose any tools you like from it. Nothing would e.g. speak against using Prisma Migrate to run your schema migrations but keep accessing your DB with pgtyped, in fact I think this would be an awesome combination :)
[1] https://github.com/phiresky/ts-typed-sql [2] https://github.com/Ff00ff/mammoth [3] https://github.com/AnyhowStep/tsql [4] https://github.com/travigd/vulcyn
A query in one of those looks like the following:
const rows = await select(list.id, list.createdAt)
.from(list)
.where(list.createdAt.gt(now().minus(`2 days`)).or(list.value.eq(0)))
.limit(10);
The resulting type of `rows` is inferred by TypeScript to the corresponding correct type (`{id: string, createdAt: Date}`)Prisma solves the problem by generating code, while all of these do it completely in type-level within typescript. Prisma also seems to introduce its own query language, while the above linked libraries stay closer to SQL.
[1] was written by a friend of mine and I'm using it in production to great success. I wouldn't recommend it though because it has repeatedly broken with new TypeScript updates since it uses very complex type structures and the dev has basically given up on it due to the complexity.
[2] is probably the most production-ready, though it has some design choices I'm not sure about.
[3] is the most well-designed, and can handle complex scenarios (including differentiating between join types etc). But it's incomplete so far.
[4] I have not used.
Pure type-level has the advantage of not adding a compile-step and keeping everything in a tool you already know. But all of the above have the disadvantage that the resulting type structures are very complex - most TS devs would have a hard time understanding the internals of the libraries and the resulting type errors can be unreadable. They can also cause the TS compiler to slow down significantly (in the past [1] had exponential compile time when adding a new column, though this has been fixed).
Also, since the TS compiler uses heuristics in multiple places and the type system is not sound, Microsoft has introduced regressions that have broken [1] in every other release.
Get me a friend of a friend of a friend who joined 2 days ago.
https://github.com/facebookexperimental/fquery/blob/master/t...
I'm in the avoid code generation camp as well as typically my build processes are complex enough and don't need another step.
But I am not quite satisfied by the existing query generators. Also after having read https://github.com/jawj/mostly-ormless/blob/master/README.md I wanted to put Postgres' `json_agg` function into use as it looks like it is simplifying a lot of the nested objects stuff one serves over REST APIs.
So I designed my own https://github.com/hoeck/typesafe-query-builder - with special support for `json_agg` and `json_build_object`. Its working already in production and for me, the dev UX is unprecedented in working with database code. Typechecks and autocompletions really boost it. But to be honest, I'm a bit worried about Typescript compiler bugs and compilation times when the project grows.
I keep seeing Prisma pop it's head up every now and then and I've been curious. I looks pretty nice, maybe I'll start a toy project in Redwood and try them both out!
Sorry to say, above comment is mostly cringe worthy! Having tried Redwoodjs, I should say at the moment Nextjs offers everything better.
I'm working on integrating PgTyped into Huncwot [2]
[1]: https://github.com/adelsz/pgtyped [2]: https://github.com/huncwotjs/huncwot
My original post:
This looked interesting at first but this reads like someone either don't know a thing about JPA and Entity Framework or are deliberately misrepresenting them:
> ORMs are libraries that map tables in your database to classes in your programming language. Prisma on the other hand is an auto-generated query builder that exposes queries which are tailored to your models. All Prisma Client queries return plain old JavaScript objects.
Yep, tables maps to classes, but guess what gets returned?
Plain Old Java (or CLR) Objects. And guess what: Thanks to those languages well thought out design and extensive metaprogramming abilities this can all be done without making up a new language also.
This is probably the harshest I've been here on HN, but smearing good solutions really doesn't sit well with me.
What I'm mostly trying to refer to are ActiveRecord style ORMs in the Node.js ecosystem (such as Sequelize and TypeORM). With those libraries, you often have model objects that can get really complicated to work with, these are not plain objects but implement lots of additional behaviour for storage and retrieval of data, often times business logic as well. Compared to that, working with plain objects (which are statically and structurally typed) is a fundamentally different approach that's hopefully easier to reason (at least that's what I've found from my personal experience).
I guess your explanation here makes a lot of sense - I don't know much about Javascript ORMs.
There's also stuff like Objection.js (which works well in TypeScript), too.
I don't dislike Prisma, but the framing is really strange to me.
- I think that the core team is bad at and refuses to improve at communicating with its community via roadmaps. Huge changes just appear, including literally breaking changes within a given major release, with little to no warning.
- I think they don’t respect what community they do have, building completely new, blessed-by-the-core-team libraries that step on existing open-source solutions in the NestJS ecosystem mostly just so the package can say “@nestjs/“ at the front of it. (Top of mind example is “@nestjs/schedule”, which is a worse take on “nest-schedule.") There isn't much personal incentive to build community tooling if the core team will swoop in and go "mine now!" without so much as a prior-work credit and it's disrespectful besides.
- They continue to build their own tooling that steps on the accepted norms and systems in the Node community. The NestJS CLI now incorporates "monorepos" that aren't Lerna or Yarn ones; they're also pushing developers to use it as their primary way of building applications, rather than invoking tsc, with the dangly carrot of "we'll write plugins for it [rather than using one of the existing ways to better handle extended compilation]". It feels like a lock-in play for future monetization (there's open rumors that Kamil is very focused on monetization, which--you do you, but as Rails and Django and even Spring showed us this stuff seems to really work better if you care about it existing to exist, and you make money off of using it) and the lack of community roadmap makes assuaging those concerns very much not a priority of the core team.
I was a big booster of NestJS a year ago and I’ve written a bunch of modules for it. I was even a personal donor to the project's Open Collective, it's the only project I've ever done that for. But I do not use NestJS for new projects because I have no confidence in the design or the leadership there and I don’t think it’s a wise use of my time anymore.
If someone’s tried both, would love a quick comparison :)
The level of type-safety when using Prisma Client with TypeScript is indeed a major factor that sets Prisma apart from other DB libraries in general (not Massive in particular).
Would also love to hear what other people think of the two in comparison! :)
The most important thing to note here is that Prisma Client is not an ORM! [1]
Prisma doesn't map classes to tables as ORMs do and therefore doesn't suffer from the object-relational impedance mismatch. Prisma Client queries return plain old JS objects, which makes it really easy to reason about queries and returned data.
Prisma also has a query engine [2] that's implemented in Rust. This is being developed separately from the TS/JS layer above (so that in the future Prisma can support other languages as well).
Prisma also generally provides stronger type safety than TypeORM. For example, you get full type safety even for partial queries [3], you can find an example for what this looks like in this video [4].
Let me know if you have more questions about this :)
[1] https://www.prisma.io/docs/understand-prisma/prisma-in-your-...
[2] https://www.prisma.io/docs/reference/tools-and-interfaces/pr...
[3] https://www.prisma.io/docs/reference/tools-and-interfaces/pr...
Is that really true? the data models are still different, relations vs js objects. Maybe you meant to say that Prisma hides and abstracts away the relational model, but the impedance mismatch is still there.
- Prisma maps queries to structural types.
This is a key distinction that the community will need to spend some time to really get an intuitive understanding for. I think you will realise that many of the problems we discovered when using classic ORMs (often described as impedance mismatch) does not apply to Prisma. But there are sure to be other problems that we will discover over time. I am looking forward to shake this out together with the broader community over the coming years.
But with a few workarounds it was possible to make it work and is quite nice to use.
Example of missing features:
Primary key as a combination of two relational models: https://github.com/prisma/prisma1/issues/3553
Database Constraints https://github.com/prisma/prisma1/issues/728
Cascading Deletes https://github.com/prisma/prisma1/issues/1262
Upsert https://github.com/prisma/prisma1/issues/2194
On delete restrict https://github.com/prisma/prisma1/issues/2701
Mutation CreateOrConnect https://github.com/prisma/prisma1/issues/3724
Improving performance with Indexes https://github.com/prisma/prisma1/issues/1300
Multiple field unique constraint https://github.com/prisma/prisma1/issues/171
Interface types https://github.com/prisma/prisma1/issues/83
Embedded Types https://github.com/prisma/prisma1/issues/2836
Declarative support for multi-field unique constraints https://github.com/prisma/prisma1/issues/1237
Support union types https://github.com/prisma/prisma1/issues/165
Transactions for multiple mutations https://github.com/prisma/prisma1/issues/74
Prisma is a database toolkit that's used by application developers to develop server-side applications in Node.js and TypeScript (e.g. REST APIs, microservices, gRPC calls, GraphQL APIs, ..., anything that talks to a database). The main tool Prisma Client is a query builder that's used to programmatically send queries to a database from Node.js/TS.
Hasura is a "GraphQL-as-a-Service" provider that generates a GraphQL API for your database. This GraphQL API is typically accessed by frontend developers. That setup can be great when your application doesn't require a lot of business logic and the CRUD capabilities that are exposed in the GraphQL API fit your needs (though I believe you can add business logic in Hasura by integrating serverless functions).
With Prisma, you're still in full control of your own backend application and can choose whatever tech stack you like for developing it (as long as it's Node.js-based, though Prisma Client will be in available in more languages the future)!
By the way, we also love GraphQL. We're currently brewing a new "GraphQL application framework" that can be used on top of Prisma. That way it will be possible to auto-generate resolvers for Prisma models to reduce the boilerplate you need to write, while still keeping the full control of your GraphQL schema.
You can learn more about this here: https://www.nexusjs.org/#/
While Hasura does work to auto-generate a GraphQL API from an existing database, it also has a web console that lets you build your database models and relationships, plus permission logic and authorization point-and-click.
Custom business-logic outside of the CRUD it generates is done by writing HTTP endpoints that you can wrap as "Actions", which it generates GraphQL types and resolvers for as part of it's schema. IE, you will want to define an API endpoint for login/signup, and declare these as "Actions", which you can then query from Hasura's schema.
An SDK/toolkit can be auto-generated from the GraphQL schema Hasura produces using graphql-code-generator, giving you strongly-typed query/mutation/subscription components in your desired frontend framework (IE React/Vue/Angular + Apollo/urql etc.)
Scheduled tasks/cronjob functionality is under development as well.
Prisma gives you a "finer grained" set of tools to "build" an application with, where Hasura kind of just hands you the whole thing (though in an opinionated fashion).
Prisma and others like Apollo server are libraries you need to add to your Node app, ie not standalone. They give you more customization because you control the entire server and what functions to run.
The difference is just in how custom you want it and how much setup you don't want to do.
My own take of interfacing with an SQL database with Typescript: a query generator that uses mapped types a lot to infer result set types from queries and a schema file.
https://github.com/hoeck/typesafe-query-builder
Not sure if thats a good idea and if the Typescript inference even scales that well when using lots of tables.
Any feedback and criticism would be welcome.
Some ideas from a recently open sourced project:
https://github.com/facebookexperimental/fquery/blob/master/t...
Any production ready experience with Prisma?
Also what breaking changes or not-yet-implemented features of prisma1 should I be aware of.
----
Edit: I see there's a "I currently use Prisma 1, what should I do?" section. Any other info would be greatly appreciated.
You can find more context on isprisma2ready.com.
If you have a specific DB in mind that you'd like to see supported, please open a feature request here: https://github.com/prisma/prisma/issues/new?assignees=&label...
More info here: https://www.prisma.io/docs/more/faq#how-much-does-prisma-20-...
However, using Nexus [1] on top of Prisma, you'll be then be able to turn Prisma's realtime API into GraphQL subscriptions. All of this is quite a bit further down the road, but certainly on our radar and part of future plans!
We have an open feature request for that here: https://github.com/prisma/prisma/issues/298