Skip to content

feat: add Set and Map support to ? placeholder expansion#27

Open
mdierolf wants to merge 4 commits into
mysqljs:mainfrom
mdierolf:feature/add_set_and_map_support
Open

feat: add Set and Map support to ? placeholder expansion#27
mdierolf wants to merge 4 commits into
mysqljs:mainfrom
mdierolf:feature/add_set_and_map_support

Conversation

@mdierolf
Copy link
Copy Markdown

Summary

This PR adds first-class support for Set and Map to the ?
placeholder expansion in format().

Previously, passing a Set or Map as a parameter fell back to the
default object coercion, producing output like '[object Set]' or
'[object Map]', which forced callers to convert these structures to
arrays/objects manually before formatting.

With this change:

  • Set is treated like an Array — useful for IN (?) clauses
    and any other list context.
  • Map is treated like a plain object in UPDATE ... SET ?
    context — useful when the column set is built dynamically and
    insertion order / non-string-safe keys matter.

Behavior

Input Context Result
new Set([1, 2, 3]) IN (?) IN (1, 2, 3)
new Set([42]) SELECT ? SELECT 42
new Map([['name','foo'],['count',7]]) UPDATE t SET ? UPDATE t SET `name` = 'foo', `count` = 7
new Map([['x', 1]]) WHERE data = ? WHERE data = '[object Map]' (unchanged fallback)
new Set() IN (?) IN ()
new Map() UPDATE t SET ? UPDATE t SET

Motivation

Set and Map are increasingly common in modern TypeScript codebases
(deduped IDs, ordered key/value config, etc.). Requiring callers to
Array.from(set) or Object.fromEntries(map) at every call site is
boilerplate and error-prone.

These types also have the additional benefit of requiring explicit creation, as opposed to array or object types, which may be received unexpectedly when user input is not properly sanitized.

Notes / Compatibility

  • This is purely additive: existing code paths for arrays, plain
    objects, and primitives are unchanged.
  • The Map fallback in non-SET positions intentionally preserves the
    current '[object Map]' behavior

Tests

Added a new describe block covering:

  • Set in IN (?) with numbers and strings
  • Set in SELECT ?
  • Set in UPDATE ... SET ? (list expansion)
  • Map in UPDATE ... SET ? (key/value expansion)
  • Map in a regular ? position (fallback)
  • Empty Set and empty Map

Checklist

  • Tests added
  • All existing tests still pass
  • No public API removed or renamed
  • No new runtime dependencies

Extend the `?` placeholder handling so that `Set` instances are
expanded like arrays and `Map` instances are expanded like plain
objects.

- `Set` values used inside `IN (?)` or `SELECT ?` are expanded as a
  comma-separated list of escaped values, matching existing Array
  behavior.
- `Map` values used in `UPDATE ... SET ?` are expanded as
  `` `key` = value `` pairs, matching existing Object behavior.
- A `Map` used in a regular `?` position (non-SET) falls back to the
  default string coercion (`'[object Map]'`), consistent with how
  non-plain objects are handled today.
- Empty `Set` and empty `Map` produce empty expansions, mirroring the
  behavior of empty arrays and empty objects.

Tests covering each of these cases are included.
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 23, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (92edf51) to head (dfeb823).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #27   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines          401       414   +13     
=========================================
+ Hits           401       414   +13     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@wellwelwel wellwelwel linked an issue May 23, 2026 that may be closed by this pull request
@wellwelwel
Copy link
Copy Markdown
Member

wellwelwel commented May 23, 2026

Thanks, @mdierolf!

In theory, the option is called stringifyObjects, so the behavior requiring the "conversion" of Map and Sets for non-{} seems like the correct behavior to me, but it doesn't have to mean a limitation.

Since the implementation doesn't affect those who don't use these types/instances, besides the performance remaining the same, it's fine by me. @sidorares, what do you think?

Note

To fix the lint, you can run the command npm run lint:fix 🙋🏻‍♂️

@wellwelwel wellwelwel changed the title feat: add Set and Map support to ? placeholder expansion feat: add Set and Map support to ? placeholder expansion May 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support Set and Map as parameters for the ? placeholder

3 participants