Skip to content

Commit

Permalink
fix(types): add missing support for json
Browse files Browse the repository at this point in the history
closes #83
  • Loading branch information
uladkasach committed Oct 26, 2023
1 parent a6e45d2 commit 7d6014a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ describe('extractDataTypeFromColumnOrArgumentDefinitionSql', () => {
sql: 'in_photo_ids bigint[]',
type: DataType.NUMBER_ARRAY,
},
{
sql: 'verified boolean',
type: DataType.BOOLEAN,
},
{
sql: 'in_adhoc_data jsonb',
type: DataType.JSON,
},
{
sql: 'in_adhoc_data JSON',
type: DataType.JSON,
},
];
examples.forEach((example) => {
it(`should be able to determine type accurately for this example: "${example.sql}"`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ const postgresBooleanTypes = ['BOOLEAN'];
// combine the types so we can search over the union
const dbBooleanTypes = [...mysqlBooleanTypes, ...postgresBooleanTypes];

// https://dev.mysql.com/doc/refman/8.0/en/json.html
const mysqlJsonTypes = ['JSON'];

// https://www.postgresql.org/docs/9.5/datatype-json.html
const postgresJsonTypes = ['JSON', 'JSONB'];

// combine the types so we can search over the union
const dbJsonTypes = [...mysqlJsonTypes, ...postgresJsonTypes];

export const extractDataTypeFromColumnOrArgumentDefinitionSql = ({
sql,
}: {
Expand All @@ -121,5 +130,7 @@ export const extractDataTypeFromColumnOrArgumentDefinitionSql = ({
return DataType.BUFFER;
if (dbBooleanTypes.some((dbType) => sqlUpper.includes(` ${dbType}`)))
return DataType.BOOLEAN;
if (dbJsonTypes.some((dbType) => sqlUpper.includes(` ${dbType}`)))
return DataType.JSON;
throw new Error(`could not extract data type from '${sql}'`);
};
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ describe('extractTypeDefinitionFromFunctionInputSql', () => {
type: [DataType.BOOLEAN, DataType.NULL],
}),
},
{
sql: 'in_adhoc_data jsonb',
def: new TypeDefinitionOfResourceColumn({
name: 'in_adhoc_data',
type: [DataType.JSON, DataType.NULL],
}),
},
];
examples.forEach((example) => {
it(`should be able to determine types accurately for this example: "${example.sql}"`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ describe('extractTypeDefinitionFromColumnSql', () => {
type: [DataType.BOOLEAN, DataType.NULL],
}),
},
{
sql: 'adhoc_data JSONB NOT NULL',
def: new TypeDefinitionOfResourceColumn({
name: 'adhoc_data',
type: [DataType.JSON],
}),
},
];
examples.forEach((example) => {
it(`should be able to determine types accurately for this example: "${example.sql}"`, () => {
Expand Down

0 comments on commit 7d6014a

Please sign in to comment.