tsedio / tsed

:triangular_ruler: Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone. ⭐️ Star to support our work!
https://tsed.io/
MIT License
2.84k stars 286 forks source link

[FEAT] Add value transformer to de/serializer #572

Closed edgesite closed 5 years ago

edgesite commented 5 years ago

Information

Description

Add value transformer to des/serializer can help apply Hashids like mechanism much easier.

Example

const SALT = 'SALT';

class Entity {
  @Property({encode: v => hashids.encode(SALT, v), decode: e => hashids.decode(SALT, e)})
  @PrimaryColumn()
  id: number;
}
Romakita commented 5 years ago

Good Idea :). I suggest this implementation:

const SALT = 'SALT';

class Entity {
  @PropertySerialize(v =>  hashids.encode(SALT, v)) 
  @PropertyDeserialize(e =>  hashids.decode(SALT, e))
  @PrimaryColumn()
  id: number;
}

What do you think ?

edgesite commented 5 years ago

Great. Maybe better also add it to JsonPropertyOptions? I just made an example https://github.com/edgesite/ts-express-decorators/commit/5f6f439e38b7fc909fbab2258f8e9499153ef7ca

Also, I noticed the https://github.com/TypedProject/ts-express-decorators/pull/539 but it's seems stalled. Split ser/des to a standalone package is a great idea, the existing converter system is a bit tricky. It would be nice to redesign the API of the converter system to keep it clean and elegant.

Romakita commented 5 years ago

@edgesite I don't understand in your code why you change the IConverterOptions with decode and encode, because decode/encode are applied on a specific prop.

Yes #539 is a test. Problem: It's a breaking change for developers, because it require to add this dependency in package.json for each projects using Ts.ED (like @tsed/di and @tsed/common). But in future the converters and jsonschema will be splited. Redesign jsonschema API is planed also. For the converter, isn't planed. The tricky code is over JsonSchema / PropertyMetada / PropertyRegistry. This part is really confusing and redundante.

Right know, I'll work on your feature ;). Refactoring jsonschema/converter will be done in another PR in future.

See you Romain

edgesite commented 5 years ago

@Romakita oh I just miss push -f when I amend the commit. Check https://github.com/edgesite/ts-express-decorators/commit/5837227a9a4e27b3224cb55de025308ed8ae6663

Also, can we document *Registry to show users how to create custom filters? It's really a pain feature and needs looking into the code right now.