electrodb icon indicating copy to clipboard operation
electrodb copied to clipboard

[Feature Request] attributes of 'number' type, enumerated 'as const'.

Open ari-becker opened this issue 3 years ago • 7 comments
trafficstars

An example is easiest to explain:

{
  attributes: {
    ratingNumStars: {
      type: [0, 1, 2, 3, 4, 5] as const,
      default: 0
    }
  }
}

This would allow specifying the field as a number, with improved type safety (compared to using a validate method alone), while keeping numerical sorting.

ari-becker avatar Nov 12 '22 19:11 ari-becker

For this, I would recommend use of CustomAttributeType to accomplish this -- would that work in your case? I'd imagine given your example it'd be able to accomplish your needs and has the benefit of already being available.

My hesitancy around numeric enums is that Electro can't really enforce what values could end up in the table, when you consider atomic mutations like add and subtract, etc. A formal numeric enum might give users the impression that'd be handled by the library somehow.

tywalch avatar Nov 12 '22 21:11 tywalch

Got it. The following seems (at first glance) to work:

{
  attributes: {
    ratingNumStars: {
      type: CustomAttributeType<0 | 1 | 2 | 3 | 4 | 5>('number'),
      default: 0
    }
  }
}

although, for what it's worth, putting an invalid value in for the default (e.g. 6) doesn't cause a type error.

ari-becker avatar Nov 13 '22 07:11 ari-becker

Could it be your version of TypeScript? Here is an example in the playground, which uses the latest version of TypeScript:

https://electrodb.fun/?#code/PQKgBAsg9gJgpgGzARwK5wE4Es4GcA0YuccYGeqCALgUQBYCG5YA7llXWAGbZwB2MXGBDAAUKKwBbAA5QMVMAG8wAUT5V2AT0IBlTADcsAYziEAwqlxUokgIJUq2AEaoqcACqbppAL7cMNmAARIhwRo6wTkEA3OJGUHxWYFQMTgikALzBmlCoGAD6KWlw+XwMknAx4qBg7gy4ANZCahpUmsJi8YkKKY1CWXxwLKrqWgAUomBKk1NgkrCIAFzTs7P8rZrLQb1NQfgzq-qYuFgJWwCMewezxBiGJls7DNLSQdc++6sMDs6ueMuKa5TaQBGCocIASRgAKBsza3i2VmwfAA5ldVlMPrCyhUYRi4V44MsAORIrCo4mwrH4+C4IzYaQaM4rfHJQkkskUqmfDEYb7klF41nwolgCxWGz2RxYFxuTzeAA8AAYwAAfMDnNVgABMWoAzFqACxagCsAD4xsS+KhJE5MMSAJQ81Y+d7OsDk+AAD3+LK+uGIVCF+OkDWDrKmXBwCGhwVD6Ijs3iMigJzcywA2kEQbBwVQoUEALrul0lm5hv2sqOIWOkhrEsurZOyNOijOF2GYqnvGbUpTJVLpMCuh2xUQ7XAAOnpcG+cDGgOBoLzUJJLDnGDtMAAtEqlecGzMcaLiRxSABVahSOdgADqG63h6mtPpWEZpz4JPcdFI8yScC9Rw4AqVgHzgGAwDgI4MDmBh4CfMh+VRZYADZ9h8B1JxRKAxlHIA

tywalch avatar Nov 13 '22 22:11 tywalch

@ari-becker checking in, we're you able to resolve your typing issue?

tywalch avatar Dec 16 '22 00:12 tywalch

@ari-becker checking in, we're you able to resolve your typing issue?

Hey, this came up in a side project of mine, no further progress beyond my last comment 🙂

ari-becker avatar Dec 16 '22 15:12 ari-becker

Thanks for checking back in, if you ever pick things back up be sure to let me know and we can get to the bottom of what you're looking at 👍

tywalch avatar Dec 26 '22 16:12 tywalch

@tywalch I tried using CustomAttributeType per your example but I'm getting this error:

Exported variable 'MyEntity' has or is using name 'OpaquePrimitiveSymbol' from external module "/Users/me/Projects/app/node_modules/electrodb/index" but cannot be named.

Any ideas? Your playground link works fine though.

Typescript version: 4.9.4 (latest) ElectrodDB version: 2.3.5 (latest)

khuezy avatar Jan 10 '23 18:01 khuezy