Repository navigation
feat(mssql): add useUnicodeStrings dialect option - #18548
Conversation
The mssql dialect unconditionally escaped & bound every string value as a
Unicode value: string literals were prefixed with `N`, and bound parameters
used the `NVarChar` type. This was hardcoded, with no way to opt out.
For schemas that use non-Unicode `VARCHAR` columns this causes a well-known
SQL Server performance problem: when a `VARCHAR` column is compared against
an `NVARCHAR` literal or parameter, SQL Server must implicitly convert the
column (not just the parameter) to satisfy type precedence rules. That
conversion is applied per-row, which prevents the query optimizer from using
an index seek on the column - it falls back to a full index/table scan.
Add a new `useUnicodeStrings` dialect option (default `true`, preserving the
current behavior) so applications can generate non-Unicode string literals and
bind string parameters as `VarChar` instead:
```ts
new Sequelize({
dialect: MsSqlDialect,
useUnicodeStrings: false,
});
```
When disabled, `dialect.escapeString('ABC-123')` returns `'ABC-123'` instead of
`N'ABC-123'`, and `MsSqlQuery#getSQLTypeFromJsType` binds strings as
`TYPES.VarChar` instead of `TYPES.NVarChar`.
Fixes sequelize#18260
|
Important Review skippedReview was skipped as selected files did not have any reviewable changes. ⚙️ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
* origin/main: (54 commits) fix: use bind parameters in the Oracle catalog queries (sequelize#18498) meta: update mysql docker tag to v8.4.13 (sequelize#18577) meta: update dependency eslint-plugin-jsdoc to v65.4.0 (sequelize#18575) meta: update dependency node to v24.22.1 (sequelize#18574) meta: update dependency @oclif/core to ^5.1.9 (sequelize#18573) fix(core): skip removing foreign keys in dropAllTables when dropping with cascade (sequelize#18497) fix(core): drop each model only once in Sequelize#drop (sequelize#18572) fix(postgres): drop the enum types of a model when its table is dropped (sequelize#18554) fix(core): avoid lodash omit in the Model constructor (sequelize#18556) meta(core): support async functions in expectPerDialect (sequelize#18569) meta: update dependency ibm_db to ^4.1.0 (sequelize#18571) fix(core): make rejectInvalidOptions iterate supportable options and accept undefined (sequelize#18557) fix(core): respect the omitNull option of calls and models, fix query generator typings (sequelize#18565) meta(core): convert unit/model tests to TypeScript (sequelize#18564) meta(core): convert unit/instance tests to TypeScript (sequelize#18563) meta: update sequelize AUTHORS (sequelize#18549) feat(mssql): add useUnicodeStrings dialect option (sequelize#18548) meta: retry image pulls in the database start scripts (sequelize#18340) meta(core): follow-ups to the isModelStatic cache from sequelize#18234 (sequelize#18559) meta: update dependency uuid to v14 (sequelize#18544) ... # Conflicts: # packages/postgres/src/query-interface.js
* origin/main: (26 commits) fix: use bind parameters in the Oracle catalog queries (sequelize#18498) meta: update mysql docker tag to v8.4.13 (sequelize#18577) meta: update dependency eslint-plugin-jsdoc to v65.4.0 (sequelize#18575) meta: update dependency node to v24.22.1 (sequelize#18574) meta: update dependency @oclif/core to ^5.1.9 (sequelize#18573) fix(core): skip removing foreign keys in dropAllTables when dropping with cascade (sequelize#18497) fix(core): drop each model only once in Sequelize#drop (sequelize#18572) fix(postgres): drop the enum types of a model when its table is dropped (sequelize#18554) fix(core): avoid lodash omit in the Model constructor (sequelize#18556) meta(core): support async functions in expectPerDialect (sequelize#18569) meta: update dependency ibm_db to ^4.1.0 (sequelize#18571) fix(core): make rejectInvalidOptions iterate supportable options and accept undefined (sequelize#18557) fix(core): respect the omitNull option of calls and models, fix query generator typings (sequelize#18565) meta(core): convert unit/model tests to TypeScript (sequelize#18564) meta(core): convert unit/instance tests to TypeScript (sequelize#18563) meta: update sequelize AUTHORS (sequelize#18549) feat(mssql): add useUnicodeStrings dialect option (sequelize#18548) meta: retry image pulls in the database start scripts (sequelize#18340) meta(core): follow-ups to the isModelStatic cache from sequelize#18234 (sequelize#18559) meta: update dependency uuid to v14 (sequelize#18544) ... # Conflicts: # packages/core/src/abstract-dialect/query-generator.js # packages/core/src/abstract-dialect/query-interface-typescript.ts # packages/core/test/unit/model/bulk-create.test.js # packages/core/test/unit/sql/insert.test.js
Description
The
mssqldialect unconditionally escaped & bound every string value as a Unicode value:MsSqlDialect#escapeStringprefixed every string literal withN(packages/mssql/src/dialect.ts)MsSqlQuery#getSQLTypeFromJsTypedefaulted every string bind parameter toTYPES.NVarChar(packages/mssql/src/query.js)Both were hardcoded, with no way to opt out. For schemas that use non-Unicode
VARCHARcolumns, this causes a well-known SQL Server performance problem: when aVARCHARcolumn is compared against anNVARCHARliteral or parameter, SQL Server must implicitly convert the column (not just the parameter) to satisfy type precedence rules. That conversion is applied per-row, which prevents the query optimizer from using an index seek on the column — it falls back to a full index/table scan.Many production schemas (codes, IDs, statuses; anything migrated from an older system or designed for ASCII-only data) use plain
VARCHARcolumns, so the slowdown is silent and hard to diagnose.Solution
Add a new
useUnicodeStringsdialect option (defaulttrue, so existing behavior is preserved):When disabled:
dialect.escapeString('ABC-123')returns'ABC-123'instead ofN'ABC-123'MsSqlQuery#getSQLTypeFromJsTypebinds strings asTYPES.VarCharinstead ofTYPES.NVarCharThe issue also asks whether the type could be derived from the model attribute's column type rather than an option. That is a much larger change (it requires the escaping/binding layer to know the target column's SQL type, which is not currently threaded through), and it would still need an escape hatch for cases where the column type is unknown. This option delivers the fix in a small, reviewable, backward-compatible change; the per-attribute derivation can be built on top of it later.
Changes
packages/mssql/src/dialect.tsuseUnicodeStringstoMsSqlDialectOptions, register it inDIALECT_OPTION_NAMES, expose auseUnicodeStringsgetter (defaults totrue), and makeescapeStringomit theNprefix when disabledpackages/mssql/src/query.jsTYPES.VarCharwhenuseUnicodeStringsis disabledpackages/core/test/unit/dialects/mssql/query-generator.test.jsnew Sequelize(), andescapeStringoutput in both modespackages/core/test/unit/dialects/mssql/query.test.jsNVarCharby default andVarCharwhen disabledTests
Run locally on Windows 11 / Node v22.23.3:
I verified the new tests fail without the implementation (5 failures — 3 option round-trips plus the 2 behavior assertions), so they do exercise the new code path:
can be disabled through the dialect optionscan be enabled through the dialect optionsdefaults to trueescapes strings as non-Unicode literals when useUnicodeStrings is disabledshould bind strings as VarChar when the dialect is configured to not use unicode stringsI did not run the MSSQL integration suite (
test-integration-mssql) — it requires a live SQL Server instance (Docker), which is not available in this environment. I'd appreciate it if a maintainer could confirm the integration suite in CI.I also could not verify the actual execution-plan improvement (index seek vs. scan) against a real database for the same reason. The change is behaviorally straightforward — it stops emitting the
Nprefix and stops bindingNVarChar— but an execution-plan confirmation would strengthen the report.Notes
true→ no behavior change for existing users.sequelize/websiterepo) would need a follow-up to document the new option.Fixes #18260
Summary by CodeRabbit