Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opt-in for branded types (TableId rather than - number). #2

Open
Newbie012 opened this issue Sep 10, 2022 · 3 comments
Open

Opt-in for branded types (TableId rather than - number). #2

Newbie012 opened this issue Sep 10, 2022 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@Newbie012
Copy link
Collaborator

No description provided.

@Newbie012 Newbie012 added the enhancement New feature or request label Sep 10, 2022
@Newbie012 Newbie012 self-assigned this Sep 10, 2022
@karlhorky
Copy link
Collaborator

I guess this is also already possible now without any new SafeQL feature if using something like A.Type<> in ts-toolbelt or similar?

import {A} from 'ts-toolbelt'

type EUR = A.Type<number, 'eur'>
type USD = A.Type<number, 'usd'>

let eurWallet = 10 as EUR
let usdWallet = 15 as USD

eurWallet = usdWallet // error

@Newbie012
Copy link
Collaborator Author

Not exactly:

function getById(userId: UserId) {
  return sql`select name from posts where id = ${userId}
}

How do you tell Postgres that UserId is a reference of users (id) and not posts (id)? by modifying the database (should be only possible in shadow database).

  1. Drop all constraints
  2. Drop all default values
  3. CREATE TYPE for each key.
  4. Map the created types to the TS types.

As a result, when auto-fix will be invoked, you'll receive id: UserId rather than id: number. It's a bit complicated, and I don't think there would be much adoption from the community, so I'm putting this as a low priority. But I will definitely get back to it!

@karlhorky
Copy link
Collaborator

Oh I see what you mean now, you're talking about the PostgreSQL data types created by CREATE TYPE, right ok.

So these in combination with TypeScript types - and maybe the TS types are also branded / opaque types - got it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants