Skip to content

WIP: Datatypes system refactor - #4186

Merged
mickhansen merged 10 commits into
masterfrom
datatypes
Nov 10, 2015
Merged

mickhansen merged 10 commits into
masterfrom
datatypes

Conversation

@janmeier

Copy link
Copy Markdown
Member

Phew - this turned out to be a bit more work than I had anticipated - but I think its for the better!

I still need to write the docs for the user facing parts (parse / stringify), but I want to get a review on the API before doing so.

Datatypes can now have parse and stringify functions. stringify is called in QueryGenerator.escape to convert the value into a DB representation. I've moved some of the code from sql-string directly into data-types. We might be able to move some more later.
Parse is called when values are recieved from the DB. The parse part is not tied up to any model, meaning it also works for raw queries. Instead it is connected to type id (postgres and mssql) or type string (mysql and sqlite). This means that if two types use the same backing type in the DB, they cannot have two different parse functions, because we can't tell the difference when parsing. Which types this is is different from dialect to dialect - but we throw an error up front if a parse function is not supported for a particular dialect to make the error obvious to the user.

While I was messing around I also rewrote how type identification works in sqlite - it's a bit smarter now, and I've fixed a couple of bugs where dates weren't parsed properly in includes. We still issue PRAGMA table_info before each query though - this could probably be cached - but I don't want to go into that now :)

Comment thread lib/data-types.js Outdated

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling util.inherits after each datatype, I added an inherits function to the abstract datatype, which inherits both prototype and static methods. Its also a utility method which creates the constructor for the new type, in cases where the constructor doesn't do anything interesting - saves a lot of copy-pasta

@janmeier

Copy link
Copy Markdown
Member Author

Ref #2042

@janmeier
janmeier force-pushed the datatypes branch 2 times, most recently from d961003 to 5da4ffc Compare July 24, 2015 21:53
Comment thread lib/data-types.js Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prints a deprecation notice but it's not really deprecated. It's just not the right type used. And the line provided by the module is the one from the DataTypes but it should be the line where the user inserted the wrong type. So this should probably be reconsidered.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, youre right ..

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, should be a warning more than a deprecation since we do want to normalize, but people should be aware that stuff might happen.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it haz suzh nize colorz!

Alright, I'll change it back to console.log...

This is a general problem with depd though - I actually thought it printed there line where the user did something wrong, but it always references a sequelize internal line number

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it did that aswell, otherwise it's quite useless.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not mistaken the issue with the call stack is that the warning is on the prototype and called from a internal function. That way depd is going to look at the next upper level and find the internal function that called it and prints that as the wrong line. So the depd call would have to happen in the function itself. It might work if you do something like that (I'm not sure though):

ABSTRACT.warn = function (text) {
  return text + '\n>> Check: ' + this.dialectTypes;
};
// And now somehwere where the warning is triggered:
function trigger (value) {
  if (typeof value !== 'number') {
    deprecate(this.warn(text));
  }
}

But it would not solve the fact that it's not really a deprecation :) (and you can't suppress that word). If you want the colors that's easy to implement without the library. Just use something like console.log('\x1b[36mCOLORED_WORDS\x1b[0m');

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BridgeAR Yep, you're right.

I just did a check, and everywhere we use depd is actually called from internally (e.g. here), so it never works as intended :D

I was thinking of writing my own version of depd, which basically traverses up the stack until it meets a filename that's not node_modules/sequelize/lib/**. Dunno how hard its going to be, but right now depd is not working any better than what we had before :D

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.npmjs.com/package/stack-trace looks promising, and we don't have to mess with the error object ourselves.

Anyhow, any review comments on the other million changes in this PR ;) ?

@BridgeAR

Copy link
Copy Markdown
Contributor

I only had a short glimpse over the changes but this is looking very promising and I really like the overall idea behind it.

@janmeier

Copy link
Copy Markdown
Member Author

@mikeringrose Just want to point your attention to this PR. Once its merged, all that needs to be done to add geometry support for sqlite and mssql is hopefully to add parse and stringify functions for the two dialects

@janmeier

janmeier commented Oct 8, 2015

Copy link
Copy Markdown
Member Author

This is going to be a bitch to rebase - looking at you @mickhansen !

@janmeier

janmeier commented Nov 9, 2015

Copy link
Copy Markdown
Member Author

@mickhansen This also fixes the issues mentioned by #4822, but by parsing the raw value instead of selecting it as text.

hint hint

Comment thread lib/data-types.js

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should $hexify not handle toString('hex')?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

@mickhansen

Copy link
Copy Markdown
Contributor

I can't seem to locate exactly where generic types are swapped for database specific types, can you point me to that?

@janmeier

janmeier commented Nov 9, 2015

Copy link
Copy Markdown
Member Author

@mickhansen

Copy link
Copy Markdown
Contributor

@janmeier Ah, so that didn't change.

Not sure i fully comprehend the entire changeset but it seems well reasoned and i didn't spot any obvious mistakes.

LGTM, lets rebase and i'll merge.

@janmeier

janmeier commented Nov 9, 2015

Copy link
Copy Markdown
Member Author

@mickhansen Rebased

Still a couple of loose ends to tie up with regards to documentation, and a couple more issues that couple be solved by this, but let's merge it and I'll look at the rest later

@mickhansen

Copy link
Copy Markdown
Contributor

@janmeier needs another one ;p

@janmeier

Copy link
Copy Markdown
Member Author

image

(Done)

mickhansen added a commit that referenced this pull request Nov 10, 2015
WIP: Datatypes system refactor
@mickhansen
mickhansen merged commit 6e13324 into master Nov 10, 2015
@janmeier

Copy link
Copy Markdown
Member Author

<3

@hendrul

hendrul commented Mar 14, 2016

Copy link
Copy Markdown
Contributor

Up/Down mecanism for datatypes is not included in the docs yet?, I'm having troubles with mysql when parsing a BLOB from the database, it triggers my parse method even for TEXT fields and is not passing a Buffer as value as I expected, instead, it is passing an specific Field object from the mysql driver on which I have to call value.parser._buffer in order to get the buffer and I have no clue about the type of the field from this object.

@janmeier
janmeier deleted the datatypes branch May 16, 2016 12:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants