TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Postgres to TypeScript Interfaces and Enums

105 点作者 yasserf将近 4 年前

18 条评论

jbaudanza将近 4 年前
I really like the idea of building TypeScript off of SQL types. But my ideal tool would be able to infer types from queries, not schemas. For example.. if I did:<p><pre><code> await db.query(&quot;SELECT user_id, COUNT(*) AS count FROM comments GROUP BY user_id&quot;); </code></pre> I would like a TypeScript object that looks like:<p><pre><code> Array&lt;{ user_id: number, count: number }&gt;</code></pre>
评论 #27575583 未加载
评论 #27572393 未加载
评论 #27571761 未加载
评论 #27590265 未加载
评论 #27572869 未加载
评论 #27574747 未加载
评论 #27575551 未加载
评论 #27572160 未加载
评论 #27575104 未加载
评论 #27571737 未加载
kristiandupont将近 4 年前
I am making a competitor to this and I am really happy that the approach seems to be gaining traction. Having the database as the source of truth and migrating it with SQL feels very right for me.<p>It does mean that architectural decisions start in the database as well, which I am trying to govern with &quot;schema linting&quot; ([link redacted]). If you are using Postgres, I would love help with building this.
评论 #27571377 未加载
评论 #27571411 未加载
评论 #27575157 未加载
iddan将近 4 年前
Prisma is a really mature project that does just and much more <a href="https:&#x2F;&#x2F;www.prisma.io&#x2F;docs&#x2F;concepts&#x2F;components&#x2F;introspection" rel="nofollow">https:&#x2F;&#x2F;www.prisma.io&#x2F;docs&#x2F;concepts&#x2F;components&#x2F;introspection</a>
评论 #27575102 未加载
评论 #27573386 未加载
taylorlapeyre将近 4 年前
We are using Kanel[1] for this right now — is there an advantage to using this instead?<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;kristiandupont&#x2F;kanel" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kristiandupont&#x2F;kanel</a>
评论 #27571825 未加载
tengbretson将近 4 年前
Ive been dreaming up a TS SQL library that would use the new template literal features to implement a full postgres SQL parser <i>in the type system</i>. I imagine this would allow you to define a series of db migration statements (alter table, add column, etc) and the net result of that series of migrations would be rolled up into a schema definition as a type.<p>Then you could take this schema type and feed it into some function as a generic to generate a raw query executor that could again fully parse the SQL and perfectly infer the result type.<p>I believe all this is possible but I don&#x27;t really know where to start.
评论 #27572711 未加载
评论 #27572758 未加载
评论 #27572717 未加载
评论 #27572855 未加载
评论 #27574767 未加载
gmac将近 4 年前
If you like this idea, Zapatos[1] does something similar but provides more comprehensive types (differentiated into what you get from a SELECT, what you can INSERT, etc.), plus some help using these types to interact with the database.<p>[1] <a href="https:&#x2F;&#x2F;jawj.github.io&#x2F;zapatos&#x2F;" rel="nofollow">https:&#x2F;&#x2F;jawj.github.io&#x2F;zapatos&#x2F;</a>
评论 #27574129 未加载
sa46将近 4 年前
I wrote a similar tool for Go and Postgres but it also generates the serialization code. [1]<p>- I prefer generating code from queries instead of generating active record style models based on tables.<p>- I think it’s worth it to spin up Postgres to get type info instead of trying to parse queries. Reimplementing Postgres’ type inference is a Herculean task even with an AST. Sqlc, which directly inspired pggen does this.<p>- It’s hard to support customization of generated code in a structured way. The way I’m leaning towards is if you need customization, you need to use the SDK which provides plugin hooks. Using flags for customization is the road to madness.<p>- What I would like to do is serialize directly to protobuf to avoid the serialization dance of Go structs to Proto structs.<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;jschaf&#x2F;pggen" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jschaf&#x2F;pggen</a>
ht85将近 4 年前
I&#x27;ve been using a fork of schemats for a while, it is a great tool.<p>We quickly figured that using the table definitions as our app layer models wouldn&#x27;t cut it, notably to handle polymorphism and non ideal (sic) table design.<p>We decided to &quot;hand write&quot; (mostly codegen) the types we use in the business logic and have a translation layer between those and the interfaces generated by schemats. For simple tables the mapping is 1:1, but for complicated tables we centralize type checks and defensive assertions there. For polymorphism it would look like this:<p><pre><code> CREATE TYPE invoice_line_type AS ENUM(&#x27;product&#x27;, &#x27;shipment&#x27;); CREATE TABLE invoice_lines AS ( id SERIAL PRIMARY KEY, type invoice_line_type NOT NULL, product_id INT REFERENCES products(id), shipment_id INT REFERENCES shipments(id), amount NUMERIC NOT NULL, CHECK (CASE WHEN type = &#x27;product&#x27; THEN product_id IS NOT NULL ELSE TRUE END), CHECK (CASE WHEN type = &#x27;shipment&#x27; THEN shipment_id IS NOT NULL ELSE TRUE END) ); </code></pre> The translation layer:<p><pre><code> import { invoice_lines, invoice_line_type } from &#x27;src&#x2F;schema&#x27; export type ProductInvoiceLine = { id: number type: invoice_line_type.product product_id: number amount: number } export type ShipmentInvoiceLine = { id: number type: invoice_line_type.shipment shipment_id: number amount: number } export const parse = (row: invoice_lines): ProductInvoiceLine | ShipmentInvoiceLine =&gt; { const base = { id: row.id, amount: row.amount, } switch (row.type) { case invoice_line_type.product: return { ...base, type: invoice_line_type.product, product_id: ensureNumber(row.product_id), } case invoice_line_type.shipment: return { ...base, type: invoice_line_type.shipment, shipment_id: ensureNumber(row.shipment_id), } } }</code></pre>
edem将近 4 年前
How does this relate to Prisma? <a href="https:&#x2F;&#x2F;www.prisma.io&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.prisma.io&#x2F;</a> Do any of you have some experience with it? Also, I think it would be even better if there was a library like JOOQ for Java: <a href="https:&#x2F;&#x2F;www.jooq.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.jooq.org&#x2F;</a> It not only generates your entity classes, but also gives a full-blown type-safe query DSL!
danvk将近 4 年前
There are many forks of schemats out there that add various features. Here’s mine: <a href="https:&#x2F;&#x2F;github.com&#x2F;danvk&#x2F;pg-to-ts" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;danvk&#x2F;pg-to-ts</a><p>It copies Postgres comments over to JSDoc&#x2F;TSDoc comments, emits some data about foreign key relationships and supports TS types for json&#x2F;jsonb columns via @type comments. Feel free to copy any of those feature if you think they’re good ideas :)
评论 #27572674 未加载
PhilipTrauner将近 4 年前
Quoting the issue that the creator opened on the original repo (<a href="https:&#x2F;&#x2F;github.com&#x2F;SweetIQ&#x2F;schemats&#x2F;issues&#x2F;127" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;SweetIQ&#x2F;schemats&#x2F;issues&#x2F;127</a>):<p>&gt; I figured having one that upgrades all the dependencies and uses some of the newer JS features would be a nice win.<p>Is this &quot;fork&quot; also going to address all the other Postgres-related issues of the original?
评论 #27571306 未加载
yashap将近 4 年前
Seems like it generates TS types, but doesn’t help with data access? i.e. not trying to compete with tools that generate full data access layers, like Prisma <a href="https:&#x2F;&#x2F;www.prisma.io&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.prisma.io&#x2F;</a> ?<p>If so, out of curiosity, what’s the use case for generating just the types? How are you actually getting data to&#x2F;from the DB?
评论 #27571999 未加载
stadium将近 4 年前
This is really interesting.<p>What are the tradeoffs of generating the unique ID uuid&#x27;s from the database vs from the code?<p>Compared to ORM&#x27;s, is one of the main differences that the db objects define the types and constraints and those propagate out to the code? And ORM&#x27;s start with code and propagate out to the DB?
评论 #27572124 未加载
krzkaczor将近 4 年前
It seems like it only generates types for entities.<p>My dream library would be basically a generated client that looks like knex or objection but with full type-safety and mapping.<p>I don&#x27;t like prisma that much as it takes control&#x2F;power from the developer. But it can be great if you don&#x27;t know SQL.
评论 #27573713 未加载
random_savv将近 4 年前
One alternative approach: one could write SQL queries to generate the interfaces for a set of tables (or views), and customize those to your codebase&#x2F;needs.
peter_neumark将近 4 年前
I’ve used vramework, and it’s an incredible tool for rapid development. Really good stuff!
评论 #27571765 未加载
topicseed将近 4 年前
How does it compare to zapatos?
oauea将近 4 年前
I&#x27;ve been generating Java code from postgres (created by liquibase migrations) using Jooq, then that Java code is transformed into a OpenAPI spec using Springfox, which in turn is turned into any language of choice (such as typescript types). It&#x27;s really good.