Skip to content

Commit 9879416

Browse files
committed
docs: expand custom type casting
closes mysqljs#1990
1 parent 2a92398 commit 9879416

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

Readme.md

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,37 +1303,58 @@ var query = connection.query(options, function (error, results, fields) {
13031303
});
13041304
```
13051305

1306+
### Custom type casting
1307+
13061308
You can also pass a function and handle type casting yourself. You're given some
13071309
column information like database, table and name and also type and length. If you
13081310
just want to apply a custom type casting to a specific type you can do it and then
1309-
fallback to the default. Here's an example of converting `TINYINT(1)` to boolean:
1311+
fallback to the default.
1312+
1313+
The function is provided two arguments `field` and `next` and is expected to
1314+
return the value for the given field by invoking the parser functions through
1315+
the `field` object.
1316+
1317+
The `field` argument is a `Field` object and contains data about the field that
1318+
need to be parsed. The following are some of the properties on a `Field` object:
1319+
1320+
* `db` - a string of the database the field came from.
1321+
* `table` - a string of the table the field came from.
1322+
* `name` - a string of the field name.
1323+
* `type` - a string of the field type in all caps.
1324+
* `length` - a number of the field length, as given by the database.
1325+
1326+
The `next` argument is a `function` that, when called, will return the default
1327+
type conversaion for the given field.
1328+
1329+
When getting the field data, the following helper methods are present on the
1330+
`field` object:
1331+
1332+
* `.string()` - parse the field into a string.
1333+
* `.buffer()` - parse the field into a `Buffer`.
1334+
* `.geometry()` - parse the field as a geometry value.
1335+
1336+
The MySQL protocol is a text-based protocol. This means that over the wire, all
1337+
field types are represented as a string, which is why only string-like functions
1338+
are available on the `field` object. Based on the type information (like `INT`),
1339+
the type cast should convert the string field into a different JavaScript type
1340+
(like a `number`).
1341+
1342+
Here's an example of converting `TINYINT(1)` to boolean:
13101343

13111344
```js
1312-
connection.query({
1313-
sql: '...',
1345+
connection = mysql.createConnection({
13141346
typeCast: function (field, next) {
1315-
if (field.type == 'TINY' && field.length == 1) {
1316-
return (field.string() == '1'); // 1 = true, 0 = false
1347+
if (field.type === 'TINY' && field.length === 1) {
1348+
return (field.string() === '1'); // 1 = true, 0 = false
1349+
} else {
1350+
return next();
13171351
}
1318-
return next();
13191352
}
13201353
});
13211354
```
1322-
__WARNING: YOU MUST INVOKE the parser using one of these three field functions in your custom typeCast callback. They can only be called once. (see [#539](https://github.com/mysqljs/mysql/issues/539) for discussion)__
1323-
1324-
```
1325-
field.string()
1326-
field.buffer()
1327-
field.geometry()
1328-
```
1329-
are aliases for
1330-
```
1331-
parser.parseLengthCodedString()
1332-
parser.parseLengthCodedBuffer()
1333-
parser.parseGeometryValue()
1334-
```
1335-
__You can find which field function you need to use by looking at: [RowDataPacket.prototype._typeCast](https://github.com/mysqljs/mysql/blob/master/lib/protocol/packets/RowDataPacket.js#L41)__
13361355

1356+
__WARNING: YOU MUST INVOKE the parser using one of these three field functions
1357+
in your custom typeCast callback. They can only be called once.__
13371358

13381359
## Connection Flags
13391360

0 commit comments

Comments
 (0)