Numbers returned as strings
Explains why @planetscale/database returns BIGINT and unsigned 64-bit columns as strings (deliberate, to avoid silent type changes past MAX_SAFE_INTEGER) and how to convert them with a custom cast function. Use when numeric columns come back as strings in Node. Not for other drivers, or for columns you actually want as strings.
Thats deliberate. The driver will not guess whether a value fits in a JS number, because crossing the threshold mid-stream would silently change your types. Supply a custom cast function to convert them how you like.
- Add a custom cast function in your connection config. See the README's custom type casting section for the hook.
- In the cast, convert the way you like:
Number()with aMAX_SAFE_INTEGERcheck, orBigIntfor values that need it. Expected: the columns come back as numbers (or BigInt) instead of strings.
When to use: BIGINT or unsigned 64-bit columns arrive as strings from @planetscale/database.
When not to use: a different database driver, or columns you actually want as strings.
Compatibility: @planetscale/database (Node.js). Reported in planetscale/database-js#175.
Variants:
- "planetscale bigint string"
- "planetscale numbers returned as strings nodejs"
- "planetscale custom type casting"
Root cause: deliberate driver behavior. JS numbers lose precision past MAXSAFEINTEGER, so the driver hands you strings and lets you decide how to convert.
Edge cases: values above Number.MAXSAFEINTEGER need BigInt. Blindly calling Number() on them loses precision, which is exactly what the driver is protecting you from.