tachybase_todo/packages/plugins/data-visualization/src/server/actions/formatter.ts

50 lines
1.5 KiB
TypeScript
Raw Normal View History

feat: data visualization (#2160) * feat(charts-v2): init * chore(charts-v2): init chart renderer * feat(chart-v2): add chart grid and initializer * feat(chart-v2): improve ui * feat(chart-v2): ui * feat(charts-v2): query sort ui * feat(charts-v2): field select component * feat(charts-v2): improve ui && add query action * feat(charts-v2): imporve ui, work in progress * fix(charts-v2): chart renderer request api twice * feat(charts-v2): add dimension formatter * feat(charts-v2): filter, sort, limit * feat(charts-v2): sql mode ui * feat(charts-v2): support duplicate & sql mode * fix(charts-v2): wrong defaultValue of json config * feat(charts-v2): transformer ui * feat(charts-v2): transformer * chore(charts-v2): rename transfromer to transform * feat(charts-v2): support cache * feat(charts-v2): add acl provider * chore(charts-v2): hide sql mode * refactor(charts-v2): add renderer provider * feat: collection permission check * feat(charts-v2): add antd statistic * test(charts-v2): backend * chore: improve code * test(charts-v2): add test * chore: add Chinese translation * fix(charts-v2): locale switch bug * chore: add dependency * feat(charts-v2): init chart config from query * feat: change layout * test: fix frontend test * feat: improve auto infer * fix: ui issues * chore: translation * fix: sql error * fix: some issues * feat: support table * fix: bug * chore: improve code and fix query * feat: add config reference * chore: add translation * fix: process data due to pg issue * test: fix parseBuilder * chore: upgrade formily to 2.2.25 * fix: some issues and import style * fix: bug when query with sort * feat: parse enum data * fix: yarn.lock * fix: type error * fix: infer bug and frontend test * test: fix frontend * fix: test * feat: improve preview * chore: downgrade formily * feat: support associations, draft, in testing * fix: typo * test: frontend & backend * fix: infer bug * feat: measure selection of statistics * fix: bug of group by alias * fix: some issues * fix: order issues * fix: yarn.lock * chore: fix filter include & 'data-visualization' * style: improve style * docs: add readme * chore: add translation --------- Co-authored-by: chenos <chenlinxh@gmail.com>
2023-06-30 20:49:44 +08:00
export const dateFormatFn = (sequelize: any, dialect: string, field: string, format: string) => {
switch (dialect) {
case 'sqlite':
format = format
.replace(/YYYY/g, '%Y')
.replace(/MM/g, '%m')
.replace(/DD/g, '%d')
.replace(/hh/g, '%H')
.replace(/mm/g, '%M')
.replace(/ss/g, '%S');
return sequelize.fn('strftime', format, sequelize.col(field));
case 'mysql':
format = format
.replace(/YYYY/g, '%Y')
.replace(/MM/g, '%m')
.replace(/DD/g, '%d')
.replace(/hh/g, '%H')
.replace(/mm/g, '%i')
.replace(/ss/g, '%S');
return sequelize.fn('date_format', sequelize.col(field), format);
case 'postgres':
format = format.replace(/hh/g, 'HH24').replace(/mm/g, 'MI').replace(/ss/g, 'SS');
return sequelize.fn('to_char', sequelize.col(field), format);
default:
return field;
}
};
export const formatFn = (sequelize: any, dialect: string, field: string, format: string) => {
switch (dialect) {
case 'sqlite':
case 'postgres':
return sequelize.fn('format', format, sequelize.col(field));
default:
return field;
}
};
export const formatter = (sequelize: any, type: string, field: string, format: string) => {
const dialect = sequelize.getDialect();
switch (type) {
case 'date':
case 'datetime':
case 'time':
return dateFormatFn(sequelize, dialect, field, format);
default:
return formatFn(sequelize, dialect, field, format);
}
};