Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define an NVARCHAR(MAX) field with sequelize?

Using node.js and sequelize vis-a-vis MSSQL, how do I define an NVARCHAR(MAX) field?

like image 958
joniba Avatar asked Aug 29 '17 10:08

joniba


People also ask

How do you define VARCHAR in Sequelize?

If you give the datatype as TEXT in Sequelize It will automatically convert that field to NVARCHAR(MAX ) for SQL Server database.

How is nvarchar Max stored?

VARCHAR(MAX) or NVARCHAR(MAX) is considered as a 'large value type'. Large value types are usually stored 'out of row'. It means that the data row will have a pointer to another location where the 'large value' is stored...

Is nvarchar 4000 the same as nvarchar Max?

The answers is: there is no different between nvarchar(7) and nvarchar(4000) in term of performance & storage size. There is an interesting thing is that: if you change nvarchar(7) or nvarchar(4000) to nvarchar(max). There is a difference in term of performance & storage size. Wow, Why is this happen?

When should I use nvarchar Max?

You can use SQL varchar when the sizes of the column vary considerably, use varchar(max) when there are chances that string length might exceed 8000 bytes, use char when the sizes of the column are fixed and use nvarchar if there is a requirement to store Unicode or multilingual data.


1 Answers

If you give the datatype as TEXT in Sequelize It will automatically convert that field to NVARCHAR(MAX) for SQL Server database.

I could not find this in the documentation

But it is mentioned in Sequelize github repository from line 79 to 90.

Pasting here the code snippet:

    TEXT.prototype.toSql = function toSql() {
    // TEXT is deprecated in mssql and it would normally be saved as a non-unicode string.
    // Using unicode is just future proof
    if (this._length) {
      if (this._length.toLowerCase() === 'tiny') { // tiny = 2^8
        warn('MSSQL does not support TEXT with the `length` = `tiny` option. `NVARCHAR(256)` will be used instead.');
        return 'NVARCHAR(256)';
      }
      warn('MSSQL does not support TEXT with the `length` option. `NVARCHAR(MAX)` will be used instead.');
    }
    return 'NVARCHAR(MAX)';
  };

As you can see SQL Server does not support TEXT field so it will convert it to NVARCHAR(MAX)

like image 186
Hemant Kumar Avatar answered Oct 13 '22 12:10

Hemant Kumar