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.

Public API for using custom data type parsers

See original GitHub issue

Having specific data types returned as custom types is a common use case. For example, while it makes sense for sequelize to return DECIMALs as strings by default to keep the dependencies small, any application that handles any kind of complex monetary values that need to be manipulated will likely want to use something like bignumber.js to handle those.

const user = await User.findOne();
user.balance instanceof BigNumber // true

await user.update({balance: user.balance.plus('5000000000.10')});

Currently, there are two methods, $stringify (which is private?) and parse. These partly sit on the base data types and then sometimes get overridden by the dialect data-types.

There should be a way to define a custom parser and stringifier for a data type that

  • works with all dialects
  • does not require to use private methods
  • has an easy API (the user should not be concerned about the nature of the data types being static, classes and factory functions). #1498 Proposal

Allow to set two methods on each data type, parse and stringify. parse at the moment is a static method, because it needs to be set in the libraries before a model is defined. stringify() is an instance method and can therefor access this.options. NOTE: It would be nice if parse() could be an instance method aswell for consistency. This may be changed before v4 is stable.

The internal parse and stringify methods will be renamed to _parse and _stringify to make them explicitely private. These may be overridden by dialects. They always first check for the presence of a parse and/or stringify function on the data type that may have been provided by the user and use it to convert the value.

The default implementation of parse() and stringify() always delegate to the dialect-specific data-types with .call()/.apply(). The dialect that should be used when stringifying is given as a dialect option in the options argument that some data types already accept. Data types are not replaced anymore at Model normalization with the dialect specific data types.

A user can choose to fallback to the default behaviour by calling super.stringify().

Sequelize.DECIMAL.parse = value => new BigNumber(value);
Sequelize.DECIMAL.stringify = value => value instanceof BigNumber ? value.toString() : value;

Interface:

stringify?: (value: any, options: { dialect: string}) => string;
parse?: (value: string, options: { dialect: string }) => any;

Questions about the current codebase

  • why is $stringify() private, but parse() not? No reason
  • why is parse() static, but $stringify() an instance method? Parsers need to be set at connection-level before Model creation
  • why is there a second stringify() method? No reason
  • why is there no default implementation for parse()? E.g. why are there some data types that don’t have this method defined, and how is this handled No reason

ToDo

  • Remove ParserStore #6473
  • Don’t export closures in dialect data types
  • Don’t patch BaseTypes with types property - just use dialect objects
  • Remove DataType.extend (we have inheritance for that and will use delegation anyway)
  • Remove DataType.key (we have DataType.name and DataType.prototype.toSql() for that)
  • Introduce dialects/abstract/data-types.js (dialects need a base implementation that they can fallback to even when the user overrides parsers/stringifiers)
  • Use method delegation

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:15 (12 by maintainers)

github_iconTop GitHub Comments

6reactions
shtse8commented, Oct 29, 2017

any updates of this issues? I want to extend some datatypes properly.

4reactions
kingjerodcommented, Feb 20, 2018

Currently trying to figure out how to deal with DECIMAL being strings in Sequelize v4, really need something like this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parsers - Django REST framework
REST framework includes a number of built in Parser classes, that allow you to accept requests with various media types. There is also...
Read more >
Implementing the custom parser - Flexmonster
This guide describes how to create and use a parser for the custom data source ... public string Type { get; private set;...
Read more >
haskell - (Generically) Build Parsers from custom data types?
In this answer I use the ReadP parser which comes with base, ... means "this type has only one constructor, the list of...
Read more >
REST API Guide | Parse
The REST API lets you interact with Parse Server from anything that can send an HTTP request. There are many things you can...
Read more >
Custom Data Types in ASP.NET Core Web APIs
If you have issues with how Swagger generates the documentation for custom types in your ASP.NET Core Web APIs, you should read this...
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