Skip to content

Commit 5c80c04

Browse files
committed
Fix serializing
1 parent df129b9 commit 5c80c04

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

lib/index.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,18 @@ export default function Postgres(url, options) {
218218
}
219219
}
220220

221-
function array(value, type) {
221+
function array(value) {
222222
return {
223-
type,
223+
type: inferType(value),
224+
array: true,
224225
value
225226
}
226227
}
227228

228229
function json(value) {
229230
return {
230231
type: types.json.to,
231-
value: types.json.serialize(value)
232+
value
232233
}
233234
}
234235

@@ -249,11 +250,11 @@ export default function Postgres(url, options) {
249250

250251
function addArrayType(oid, typelem) {
251252
const parser = options.parsers[typelem]
252-
, serializer = options.serializers[typelem]
253+
253254
typeArrayMap[typelem] = oid
254255
options.parsers[oid] = (xs) => arrayParser(xs, parser)
255256
options.parsers[oid].array = true
256-
options.serializers[oid] = (xs) => arraySerializer(xs, serializer)
257+
options.serializers[oid] = (xs) => arraySerializer(xs, options.serializers[typelem])
257258
}
258259

259260
function addTypes(instance, connection) {
@@ -270,7 +271,7 @@ export default function Postgres(url, options) {
270271
if (name in instance)
271272
throw errors.generic({ message: name + ' is a reserved method name', code: 'RESERVED_METHOD_NAME' })
272273

273-
instance[name] = (x) => ({ type: type.to, value: type.serializer(x) })
274+
instance[name] = (x) => ({ type: type.to, value: x })
274275
})
275276
}
276277

@@ -371,14 +372,12 @@ export default function Postgres(url, options) {
371372
}
372373

373374
function getType(x) {
374-
const value = x.value ? x.value : x
375-
, type = x.type || (Array.isArray(value) ? typeArrayMap[inferType(value)] : inferType(value))
375+
const value = x.type ? x.value : x
376+
, type = x && x.array ? typeArrayMap[x.type || inferType(value)] : (x.type || inferType(value))
376377

377378
return {
378379
type,
379-
value: type
380-
? (options.serializers[type] || types.string.serialize)(value)
381-
: value
380+
value: (options.serializers[type] || types.string.serialize)(value)
382381
}
383382
}
384383
}

lib/types.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const types = {
1717
to: 3802,
1818
from: [114, 3802],
1919
serialize: x => JSON.stringify(x),
20-
parse: x => JSON.parse(JSON.parse(x))
20+
parse: x => JSON.parse(x)
2121
},
2222
boolean: {
2323
to: 16,
@@ -28,7 +28,7 @@ export const types = {
2828
date: {
2929
to: 1184,
3030
from: [1082, 1083, 1114, 1184],
31-
serialize: x => x.toJSON(),
31+
serialize: x => x.toISOString(),
3232
parse: x => new Date(x)
3333
},
3434
bytea: {
@@ -67,13 +67,13 @@ const type = {
6767
}
6868

6969
export function inferType(x) {
70-
return x instanceof Date
70+
return x.type || (x instanceof Date
7171
? 1184
7272
: Array.isArray(x)
7373
? inferType(x[0])
7474
: x instanceof Buffer
7575
? 17
76-
: type[typeof x] || 25
76+
: type[typeof x] || 25)
7777
}
7878

7979
const escapeBackslash = /\\/g
@@ -91,10 +91,10 @@ export function arraySerializer(xs, serializer) {
9191

9292
const first = xs[0]
9393

94-
if (Array.isArray(first))
94+
if (Array.isArray(first) && !first.type)
9595
return '{' + xs.map(x => arraySerializer(x, serializer)).join(',') + '}'
9696

97-
return '{' + xs.map(x => '"' + arrayEscape(serializer(x)) + '"').join(',') + '}'
97+
return '{' + xs.map(x => '"' + arrayEscape(serializer ? serializer(x.type ? x.value : x) : '' + x) + '"').join(',') + '}'
9898
}
9999

100100
const arrayParserState = {

0 commit comments

Comments
 (0)