question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow providing DB schema instead of querying a dummy database for it

See original GitHub issue

First - I love this library! It looks like the only one that actually lets me write plain SQL but with type safety! For me, this is the killer feature of this library.

But PgTyped also sells itself on another feature: “No need to map or translate your DB schema to TypeScript, PgTyped automatically generates types and interfaces for your SQL queries by using your running Postgres database as the source of type information.”

That might be what others want, but I actually want to define my DB schema in TypeScript. This is the feature I’m requesting: a way to, instead of providing DB connection details, instead provide a schema definition for type-checking to use.

Reasons I want this:

  • There are constraints in my database that are not expressed in the database schema. For example, I have columns of type jsonb, but the contents here follow a strict JSON schema that PgTyped cannot know about by just looking at the DB.
  • I want to have an in-repo definition of what the database should look like. My dodgy development DB should not be the authority on this.
  • I don’t want the build process to depend on a database connection. Everything necessary for the build should be in the repo. Potentially, there should be a deploy-time check that the configured database matches the in-repo schema definition, although that’s another story.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:6
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
bradleyayerscommented, Feb 25, 2022

This would be a gigantic architectural shift for this library. This library doesn’t really know anything about your DB schema, instead it’s leveraging PostgreSQL’s prepared statements to ask for type information from the database itself. I’d be interested to know the strategy sqlc takes, I assume it’s fundamentally different if it doesn’t spin up a database?

0reactions
adelszcommented, Apr 27, 2022

Thanks for the kind words @jameshfisher. Unfortunately, pgTyped can’t do query analysis by itself for now, it offloads that to Postgres.

I want to have an in-repo definition of what the database should look like. My dodgy development DB should not be the authority on this.

I understand it might be a bit frustrating, but for now you can put a DB schema dump in your repo and use docker to spin up the DB as part of the build process.

@bradleyayers

I’d be interested to know the strategy sqlc takes, I assume it’s fundamentally different if it doesn’t spin up a database?

It looks like sqlc went the hard route of implementing a database schema and query analysis engine. It is quite impressive what they have achieved. Can’t speak on the quality of their query analysis unfortunately, especially on complicated queries. It would be great if postgres would just do proper type inference in the engine without requiring library authors to build their own parsing engines.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Database Schema Structure - DbSchema
Databases store data based on the schema definition, so understanding it is a key part of designing databases. In this chapter, we will...
Read more >
Create a Database Schema - SQL Server | Microsoft Learn
Learn how to create a schema in SQL Server by using SQL Server Management Studio or Transact-SQL, including limitations and restrictions.
Read more >
What are database schemas? 5 minute guide with examples
A database schema is an abstract design that represents the storage of your data in a database. It describes both the organization of...
Read more >
5 Database Design Schema Example: Critical Practices ...
Importance of Database Schema Design​​ A Schema organizes data into Tables with appropriate Attributes, shows the interrelationships between ...
Read more >
MySQL Sample Databases
This is a rather simple database with 6 tables but with millions of records. 1.1 Database and Tables. There are 6 tables as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found