Skip to content

Commit 611bac5

Browse files
committed
Serialize unknown types according to js type
1 parent 71d085e commit 611bac5

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/connection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import tls from 'tls'
33
import crypto from 'crypto'
44
import Stream from 'stream'
55

6-
import { stringify, handleValue, arrayParser, arraySerializer } from './types.js'
6+
import { serialize, stringify, handleValue, arrayParser, arraySerializer } from './types.js'
77
import { Errors } from './errors.js'
88
import Result from './result.js'
99
import Queue from './queue.js'
@@ -912,7 +912,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
912912
type = types[i]
913913
parameters[i] = x = type in options.serializers
914914
? options.serializers[type](x)
915-
: '' + x
915+
: serialize(x)
916916

917917
prev = b.i
918918
b.inc(4).str(x).i32(b.i - prev - 4, prev)

src/types.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import { Query } from './query.js'
22
import { Errors } from './errors.js'
33

4+
export const serialize = function serialize(x) {
5+
return typeof x === 'string' ? x :
6+
x instanceof Date ? types.date.serialize(x) :
7+
x instanceof Uint8Array ? types.bytea.serialize(x) :
8+
(x === true || x === false) ? types.boolean.serialize(x) :
9+
'' + x
10+
}
11+
412
export const types = {
513
string: {
614
to: 25,
715
from: null, // defaults to string
8-
serialize: x => '' + x
16+
serialize
917
},
1018
number: {
1119
to: 0,

0 commit comments

Comments
 (0)