back to json extract

This commit is contained in:
Charlie Kolb 2025-10-28 18:16:57 +01:00
parent f8a3137a40
commit 24ea57fbc3
No known key found for this signature in database
4 changed files with 20 additions and 10 deletions

View File

@ -3551,11 +3551,7 @@ describe('dataTable', () => {
// in face of V2 we decided to accept these breaking
// [null, null, 'eq', true],
// [null, null, 'neq', false],
[11, 3, 'gt', true],
[11, 3, 'lt', false],
[11, '3', 'gt', false],
[11, '3', 'eq', false],
[11, '3', 'lt', false],
[true, true, 'eq', true],
[true, false, 'eq', false],
[true, false, 'neq', true],

View File

@ -24,7 +24,10 @@ export class DataTableDDLService {
throw new UnexpectedError('QueryRunner is not available');
}
const dslColumns = [new DslColumn('id').int.autoGenerate2.primary, ...toDslColumns(columns)];
const dslColumns = [
new DslColumn('id').int.autoGenerate2.primary,
...toDslColumns(columns, this.dataSource.options.type),
];
const createTable = new CreateTable(toTableName(dataTableId), '', em.queryRunner).withColumns(
...dslColumns,
).withTimestamps;

View File

@ -80,12 +80,14 @@ function getConditionAndParams(
}
// For filters, we let TypeORM handle date conversion through parameterized queries.
const value = filter.value;
let value = filter.value;
let postfix = '';
if (typeof value === 'object' && !(value instanceof Date)) {
// value = JSON.stringify(value);
if (dbType === 'postgres') {
postfix = '::jsonb';
} else {
value = JSON.stringify(value);
}
}

View File

@ -22,7 +22,10 @@ import type { DataTableUserTableName } from '../data-table.types';
import { NotFoundError } from '@/errors/response-errors/not-found.error';
import { parsePath, toPostgresPath, toSQLitePath } from './path-utils';
export function toDslColumns(columns: DataTableCreateColumnSchema[]): DslColumn[] {
export function toDslColumns(
columns: DataTableCreateColumnSchema[],
dbType: DataSourceOptions['type'],
): DslColumn[] {
return columns.map((col) => {
const name = new DslColumn(col.name.trim());
@ -36,7 +39,10 @@ export function toDslColumns(columns: DataTableCreateColumnSchema[]): DslColumn[
case 'date':
return name.timestampTimezone();
case 'json':
return name.jsonb;
if (dbType === 'postgres') {
return name.jsonb;
}
return name.json;
default:
return name.text;
}
@ -70,7 +76,10 @@ export function dataTableColumnTypeToSql(
}
return 'DATETIME';
case 'json':
return 'JSONB';
if (dbType === 'postgres') {
return 'JSONB';
}
return 'JSON';
default:
throw new NotFoundError(`Unsupported field type: ${type as string}`);
}
@ -417,7 +426,7 @@ export function resolvePath(
// this is mostly for sqlite, behavior in MariaDB and MySQL mostly aligns though there are subtle
// difference we don't care for in the face of imminent removal of support
const path = toSQLitePath(pathArray);
const base = `jsonb_extract(${ref}, '${path.replaceAll("'", "\\'")}')`;
const base = `json_extract(${ref}, '${path.replaceAll("'", "\\'")}')`;
// if (typeof value === 'number') {
// return `CAST(${base} as ${dataTableColumnTypeToSql('number', dbType)})`;