Skip to content

Commit 58fa7b5

Browse files
committed
build
1 parent 341370d commit 58fa7b5

File tree

8 files changed

+90
-13
lines changed

8 files changed

+90
-13
lines changed

cjs/src/connection.js

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ function createJsonTransform(fn) {
328328
return typeof x === 'object' && x !== null && (column.type === 114 || column.type === 3802)
329329
? Array.isArray(x)
330330
? x.map(x => jsonTransform(x, column))
331-
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: v }), {})
331+
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: jsonTransform(v, column) }), {})
332332
: x
333333
}
334334
}

cjs/tests/index.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ t('Connection ended timeout', async() => {
532532

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

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

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

deno/src/connection.js

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ function createJsonTransform(fn) {
329329
return typeof x === 'object' && x !== null && (column.type === 114 || column.type === 3802)
330330
? Array.isArray(x)
331331
? x.map(x => jsonTransform(x, column))
332-
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: v }), {})
332+
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: jsonTransform(v, column) }), {})
333333
: x
334334
}
335335
}

deno/tests/index.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ t('Connection ended timeout', async() => {
534534

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

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

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

src/types.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,7 @@ function createJsonTransform(fn) {
328328
return typeof x === 'object' && x !== null && (column.type === 114 || column.type === 3802)
329329
? Array.isArray(x)
330330
? x.map(x => jsonTransform(x, column))
331-
: Object.entries(x).reduce((acc, [k, v]) => {
332-
const transformedKey = fn(k)
333-
return Object.assign(acc, { [transformedKey]: jsonTransform(v, column) })
334-
}, {})
331+
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: jsonTransform(v, column) }), {})
335332
: x
336333
}
337334
}

tests/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ t('Connection ended timeout', async() => {
532532

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

0 commit comments

Comments
 (0)