Golang后端和Angular前端与Prisma生成的模型导入

huangapple go评论78阅读模式
英文:

Golang Back-end and Angular Front-end with Prisma Generated Models import

问题

我计划开发一个应用程序,后端使用Golang API,前端使用Angular,数据库使用PostgreSQL。几天前我发现了Prisma,对我来说很有用;如果我首先使用Prisma-Go-Client创建数据库模式,那么我就可以在不手动创建任何数据库bean/模型的情况下编写前端和后端代码,特别是对于前端,我可以直接开始编码,而无需任何数据库连接和API,只需使用由Prisma生成的Typescript模型。因此,我可以使用导入的模型模拟一些临时数据,当后端准备好时,一致的数据类型将流动,我只需从模拟数据切换到将其绑定到所需数据的API请求。

现在的问题是,对于后端的Golang部分,没问题,我会导入prisma-go-client并生成模型,然后将其用于任何模式更改和CRUD,然后将数据作为结果返回等等。但是前端呢?我应该将prisma TS客户端集成到现有的Angular前端并生成模型,还是这将在构建代码到dist后暴露数据库?或者,如果我为此目的创建另一个项目,该项目将创建TS prisma模型,并仅获取和导入这些模型到现有的Angular项目,这样会起作用吗?还是它仍然包含数据库暴露?

最后,如果prisma-go-clientGoLang创建模型,并将数据库结果解析为该模型并将其作为JSON返回给前端,TS生成的prisma模型能够解析该响应吗?还是它们不一致,因为它们在两种不同的语言之间生成,并且它们将有所不同(我希望它们是相同的)?

英文:

I'm planning to have an application that will be having a Golang API Back-End with Angular Front-End and PostgreSQL Database. Few days ago I discovered Prisma and it rang a bell for me; if I create the DB schema first with Prisma-Go-Client then I'd be able to write Front-End and Back-End without creating any beans/models for DB manually and especially for the Front-End I'd directly start coding without any Database connection and API just using prisma generated imports that are Typescript models. So basically I could mock some temporary data with imported models and when Back-End would be ready consistent data types would flow and all I have to do it switch from mock data to bind it to the API request of the required data.

Now the question is; for Golang Back-End side its fine, I'll import prisma-go-client and generate the models and use them for any schema change and CRUD then return that data as a result etc. but what about Front-End? Should I create prisma TS client into existing Angular Front-End and generate models or this will result of a database reveal after the build of the code into the dist? Or if I create another project only for this purpose that will create TS prisma models and only get and import that models into the existing angular project will that work or will it still contain DB reveal?

Lastly, if prima-go-client creates models for GoLang and I parse the DB result into that model and return it as JSON to the Front-End, will TS generated prisma models be able to parse that response? or they will not be consistent since they are generated between two different languages and they will have difference (which I expect them to be same)?

答案1

得分: 4

这不会起作用,因为Prisma TS Client不适用于前端。它是一个后端库,就像Prisma Go Client一样。

你可以在类似https://github.com/99designs/gqlgen的类型安全的GraphQL服务器中使用Go Client,然后在前端使用类型安全的GraphQL客户端。

请注意,Go Client不再得到官方维护:https://github.com/prisma/prisma-client-go/issues/707。如果可能的话,我们建议您使用TS客户端。

英文:

This wouldn't work because the Prisma TS Client is not meant to be used on the frontend. It's a backend library, just like the Prisma Go Client.

You could use the Go Client within a type-safe GraphQL server like https://github.com/99designs/gqlgen and then use a type-safe GraphQL client on the frontend.

Just a note that the Go Client is no longer officially maintained: https://github.com/prisma/prisma-client-go/issues/707. We'd recommend you use the TS client, if possible.

huangapple
  • 本文由 发表于 2022年1月28日 03:38:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/70884725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定