Skip to content

Commit c3b742f

Browse files
authored
Merge pull request #853 from constructive-io/devin/1773967756-export-meta-jsonb-array
feat: add jsonb[] type support in export-meta.ts and csv-to-pg parser
2 parents 71058a2 + aec7dac commit c3b742f

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

packages/csv-to-pg/src/parse.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,39 @@ const getCoercionFunc = (type: string, from: string[], opts: FieldOptions, field
458458
});
459459
return wrapValue(val, opts);
460460
};
461+
case 'jsonb[]':
462+
return (record: Record<string, unknown>): Node => {
463+
const rawValue = record[from[0]];
464+
if (isNullToken(rawValue)) {
465+
return makeNullOrThrow(fieldName, rawValue, type, required, 'value is empty or null');
466+
}
467+
if (Array.isArray(rawValue)) {
468+
if (rawValue.length === 0) {
469+
return makeNullOrThrow(fieldName, rawValue, type, required, 'array is empty');
470+
}
471+
const elements = rawValue.map(el => JSON.stringify(el));
472+
const arrayLiteral = psqlArray(elements);
473+
if (isEmpty(arrayLiteral)) {
474+
return makeNullOrThrow(fieldName, rawValue, type, required, 'failed to format array');
475+
}
476+
const val = nodes.aConst({
477+
sval: ast.string({ sval: String(arrayLiteral) })
478+
});
479+
return wrapValue(val, opts);
480+
}
481+
// If it's a string, try to parse as JSON array
482+
if (typeof rawValue === 'string') {
483+
const parsed = parseJson(cleanseEmptyStrings(rawValue));
484+
if (isEmpty(parsed)) {
485+
return makeNullOrThrow(fieldName, rawValue, type, required, 'value is empty or null');
486+
}
487+
const val = nodes.aConst({
488+
sval: ast.string({ sval: String(parsed) })
489+
});
490+
return wrapValue(val, opts);
491+
}
492+
return makeNullOrThrow(fieldName, rawValue, type, required, 'value is not an array');
493+
};
461494
default:
462495
return (record: Record<string, unknown>): Node => {
463496
const rawValue = record[from[0]];

pgpm/core/src/export/export-meta.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Parser } from 'csv-to-pg';
33
import { getPgPool } from 'pg-cache';
44
import type { Pool } from 'pg';
55

6-
type FieldType = 'uuid' | 'uuid[]' | 'text' | 'text[]' | 'boolean' | 'image' | 'upload' | 'url' | 'jsonb' | 'int' | 'interval' | 'timestamptz';
6+
type FieldType = 'uuid' | 'uuid[]' | 'text' | 'text[]' | 'boolean' | 'image' | 'upload' | 'url' | 'jsonb' | 'jsonb[]' | 'int' | 'interval' | 'timestamptz';
77

88
interface TableConfig {
99
schema: string;
@@ -35,6 +35,8 @@ const mapPgTypeToFieldType = (udtName: string): FieldType => {
3535
case 'jsonb':
3636
case 'json':
3737
return 'jsonb';
38+
case '_jsonb':
39+
return 'jsonb[]';
3840
case 'int4':
3941
case 'int8':
4042
case 'int2':
@@ -855,7 +857,8 @@ const config: Record<string, TableConfig> = {
855857
use_rls: 'boolean',
856858
node_data: 'jsonb',
857859
grant_roles: 'text[]',
858-
grant_privileges: 'jsonb',
860+
fields: 'jsonb[]',
861+
grant_privileges: 'jsonb[]',
859862
policy_type: 'text',
860863
policy_privileges: 'text[]',
861864
policy_role: 'text',

0 commit comments

Comments
 (0)