Skip to content

Commit be0e604

Browse files
committed
build
1 parent 4aa19d1 commit be0e604

File tree

8 files changed

+90
-13
lines changed

8 files changed

+90
-13
lines changed

cjs/src/connection.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -745,8 +745,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
745745
return (
746746
(x === 'read-write' && xs.default_transaction_read_only === 'on') ||
747747
(x === 'read-only' && xs.default_transaction_read_only === 'off') ||
748-
(x === 'primary' && xs.in_hot_standby === 'off') ||
749-
(x === 'standby' && xs.in_hot_standby === 'on') ||
748+
(x === 'primary' && xs.in_hot_standby === 'on') ||
749+
(x === 'standby' && xs.in_hot_standby === 'off') ||
750750
(x === 'prefer-standby' && xs.in_hot_standby === 'off' && options.host[retries])
751751
)
752752
}

cjs/src/types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ function createJsonTransform(fn) {
332332
return typeof x === 'object' && x !== null && (column.type === 114 || column.type === 3802)
333333
? Array.isArray(x)
334334
? x.map(x => jsonTransform(x, column))
335-
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: v }), {})
335+
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: jsonTransform(v, column) }), {})
336336
: x
337337
}
338338
}

cjs/tests/index.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ t('Connection ended timeout', async() => {
531531

532532
t('Connection ended error', async() => {
533533
const sql = postgres(options)
534-
sql.end()
534+
await sql.end()
535535
return ['CONNECTION_ENDED', (await sql``.catch(x => x.code))]
536536
})
537537

@@ -611,6 +611,46 @@ t('Transform nested json in arrays', async() => {
611611
return ['aBcD', (await sql`select '[{"a_b":1},{"c_d":2}]'::jsonb as x`)[0].x.map(Object.keys).join('')]
612612
})
613613

614+
t('Transform deeply nested json object in arrays', async() => {
615+
const sql = postgres({
616+
...options,
617+
transform: postgres.camel
618+
})
619+
return ['childObj_deeplyNestedObj_grandchildObj', (await sql`select '[{"nested_obj": {"child_obj": 2, "deeply_nested_obj": {"grandchild_obj": 3}}}]'::jsonb as x`)[0].x
620+
.map((x) => {
621+
let result;
622+
for (const key in x) {
623+
const result1 = Object.keys(x[key]);
624+
const result2 = Object.keys(x[key].deeplyNestedObj);
625+
626+
result = [...result1, ...result2];
627+
}
628+
629+
return result;
630+
})[0]
631+
.join('_')]
632+
})
633+
634+
t('Transform deeply nested json array in arrays', async() => {
635+
const sql = postgres({
636+
...options,
637+
transform: postgres.camel
638+
})
639+
return ['childArray_deeplyNestedArray_grandchildArray', (await sql`select '[{"nested_array": [{"child_array": 2, "deeply_nested_array": [{"grandchild_array":3}]}]}]'::jsonb AS x`)[0].x
640+
.map((x) => {
641+
let result;
642+
for (const key in x) {
643+
const result1 = Object.keys(x[key][0]);
644+
const result2 = Object.keys(x[key][0].deeplyNestedArray[0]);
645+
646+
result = [...result1, ...result2];
647+
}
648+
649+
return result;
650+
})[0]
651+
.join('_')]
652+
})
653+
614654
t('Bypass transform for json primitive', async() => {
615655
const sql = postgres({
616656
...options,

deno/src/connection.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -749,8 +749,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
749749
return (
750750
(x === 'read-write' && xs.default_transaction_read_only === 'on') ||
751751
(x === 'read-only' && xs.default_transaction_read_only === 'off') ||
752-
(x === 'primary' && xs.in_hot_standby === 'off') ||
753-
(x === 'standby' && xs.in_hot_standby === 'on') ||
752+
(x === 'primary' && xs.in_hot_standby === 'on') ||
753+
(x === 'standby' && xs.in_hot_standby === 'off') ||
754754
(x === 'prefer-standby' && xs.in_hot_standby === 'off' && options.host[retries])
755755
)
756756
}

deno/src/types.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ function createJsonTransform(fn) {
333333
return typeof x === 'object' && x !== null && (column.type === 114 || column.type === 3802)
334334
? Array.isArray(x)
335335
? x.map(x => jsonTransform(x, column))
336-
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: v }), {})
336+
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: jsonTransform(v, column) }), {})
337337
: x
338338
}
339339
}

deno/tests/index.js

+41-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ t('Connection ended timeout', async() => {
533533

534534
t('Connection ended error', async() => {
535535
const sql = postgres(options)
536-
sql.end()
536+
await sql.end()
537537
return ['CONNECTION_ENDED', (await sql``.catch(x => x.code))]
538538
})
539539

@@ -613,6 +613,46 @@ t('Transform nested json in arrays', async() => {
613613
return ['aBcD', (await sql`select '[{"a_b":1},{"c_d":2}]'::jsonb as x`)[0].x.map(Object.keys).join('')]
614614
})
615615

616+
t('Transform deeply nested json object in arrays', async() => {
617+
const sql = postgres({
618+
...options,
619+
transform: postgres.camel
620+
})
621+
return ['childObj_deeplyNestedObj_grandchildObj', (await sql`select '[{"nested_obj": {"child_obj": 2, "deeply_nested_obj": {"grandchild_obj": 3}}}]'::jsonb as x`)[0].x
622+
.map((x) => {
623+
let result;
624+
for (const key in x) {
625+
const result1 = Object.keys(x[key]);
626+
const result2 = Object.keys(x[key].deeplyNestedObj);
627+
628+
result = [...result1, ...result2];
629+
}
630+
631+
return result;
632+
})[0]
633+
.join('_')]
634+
})
635+
636+
t('Transform deeply nested json array in arrays', async() => {
637+
const sql = postgres({
638+
...options,
639+
transform: postgres.camel
640+
})
641+
return ['childArray_deeplyNestedArray_grandchildArray', (await sql`select '[{"nested_array": [{"child_array": 2, "deeply_nested_array": [{"grandchild_array":3}]}]}]'::jsonb AS x`)[0].x
642+
.map((x) => {
643+
let result;
644+
for (const key in x) {
645+
const result1 = Object.keys(x[key][0]);
646+
const result2 = Object.keys(x[key][0].deeplyNestedArray[0]);
647+
648+
result = [...result1, ...result2];
649+
}
650+
651+
return result;
652+
})[0]
653+
.join('_')]
654+
})
655+
616656
t('Bypass transform for json primitive', async() => {
617657
const sql = postgres({
618658
...options,

src/types.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -332,10 +332,7 @@ function createJsonTransform(fn) {
332332
return typeof x === 'object' && x !== null && (column.type === 114 || column.type === 3802)
333333
? Array.isArray(x)
334334
? x.map(x => jsonTransform(x, column))
335-
: Object.entries(x).reduce((acc, [k, v]) => {
336-
const transformedKey = fn(k)
337-
return Object.assign(acc, { [transformedKey]: jsonTransform(v, column) })
338-
}, {})
335+
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: jsonTransform(v, column) }), {})
339336
: x
340337
}
341338
}

tests/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ t('Connection ended timeout', async() => {
531531

532532
t('Connection ended error', async() => {
533533
const sql = postgres(options)
534-
sql.end()
534+
await sql.end()
535535
return ['CONNECTION_ENDED', (await sql``.catch(x => x.code))]
536536
})
537537

0 commit comments

Comments
 (0)