Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions example_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@
"name": "created_at",
"type": "datetime",
"supportedOperations": ["lessThan", "greaterThan", "sortable"]
},
{
"name": "decimal_number",
"type": "decimal",
"supportedOperations": ["lessThan", "greaterThan", "sortable"],
"supportedAggregation": ["count"]
}
],
"indexes": [
Expand Down
8 changes: 7 additions & 1 deletion src/interfaces/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import {HTTPMethod} from './index';

export type DBEngine = 'sqlite' | 'pg';
export type CacheDbEngine = 'redis';
export type DataType = 'integer' | 'string' | 'boolean' | 'text' | 'datetime';
export type DataType =
| 'integer'
| 'string'
| 'boolean'
| 'text'
| 'datetime'
| 'decimal';
export type LogLevel =
| 'trace'
| 'debug'
Expand Down
6 changes: 6 additions & 0 deletions src/migrator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ function generateSchemaFile(config: ModelConfig, engine: DBEngine): string {
case 'datetime':
col = `integer('${f.name}', { mode: 'timestamp' })`;
break;
case 'decimal':
col = `real('${f.name}')`;
break;
default:
col = `text('${f.name}')`;
break; // fallback
Expand Down Expand Up @@ -96,6 +99,9 @@ ${columns}
case 'datetime':
col = `timestamp('${f.name}')`;
break;
case 'decimal':
col = `doublePrecision('${f.name}')`;
break;
default:
col = `text('${f.name}')`;
break; // fallback
Expand Down
2 changes: 2 additions & 0 deletions src/routes/custom-queries/custom-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ const cast = (value: unknown, type: DataType): unknown => {
return String(value);
case 'datetime':
return String(value);
case 'decimal':
return Number(value);
default:
return String(value);
}
Expand Down
2 changes: 2 additions & 0 deletions src/routes/schema-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export function mapDataTypeToJsonSchema(type: DataType): {
return {type: 'string'};
case 'datetime':
return {type: 'string', format: 'date-time'};
case 'decimal':
return {type: 'number'};
default:
return {type: 'string'};
}
Expand Down
2 changes: 1 addition & 1 deletion src/validators/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ const fieldSchema = {
},
type: {
type: 'string',
enum: ['integer', 'string', 'boolean', 'text', 'datetime'],
enum: ['integer', 'string', 'boolean', 'text', 'datetime', 'decimal'],
},
primaryKey: {type: 'boolean', default: false},
nullable: {type: 'boolean', default: true},
Expand Down
15 changes: 15 additions & 0 deletions src/validators/config/validate-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ const ALLOWED_OPERATIONS: Record<string, string[]> = {
'oneOf',
'indexable',
],
decimal: [
'sortable',
'editable',
'deletable',
'lessThan',
'lessThanEqual',
'greaterThan',
'greaterThanEqual',
'equal',
'oneOf',
'indexable',
],
string: [
'searchable',
'sortable',
Expand All @@ -43,6 +55,7 @@ const ALLOWED_OPERATIONS: Record<string, string[]> = {

const ALLOWED_AGGREGATIONS: Record<string, string[]> = {
integer: ['mean', 'max', 'min', 'count', 'sum'],
decimal: ['mean', 'max', 'min', 'count', 'sum'],
string: ['count'],
boolean: ['count', 'frequency'],
text: [],
Expand All @@ -53,6 +66,8 @@ function mapModelTypeToJsonSchema(type: string): string {
switch (type) {
case 'integer':
return 'integer';
case 'decimal':
return 'number';
case 'string':
case 'text':
return 'string';
Expand Down
3 changes: 2 additions & 1 deletion tests/routes/custom-queries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('test custom-queries api', () => {
method: 'POST',
path: '/all-types',
query:
'INSERT INTO test (b, t, d) VALUES (@@b:boolean@@, @@t:text@@, @@d:datetime@@);',
'INSERT INTO test (b, t, d, dec) VALUES (@@b:boolean@@, @@t:text@@, @@d:datetime@@, @@dec:decimal@@);',
},
],
};
Expand All @@ -106,6 +106,7 @@ describe('test custom-queries api', () => {
b: true,
t: 'some long text',
d: '2023-01-01T00:00:00Z',
dec: 12.34,
},
});

Expand Down
1 change: 1 addition & 0 deletions tests/routes/schema-helper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('test schema helper', () => {
dataType: 'datetime',
expectedSchema: {type: 'string', format: 'date-time'},
},
{dataType: 'decimal', expectedSchema: {type: 'number'}},
{dataType: 'array', expectedSchema: {type: 'string'}},
{dataType: 'null', expectedSchema: {type: 'string'}},
])('should map $dataType to JSON schema', ({dataType, expectedSchema}) => {
Expand Down
22 changes: 22 additions & 0 deletions tests/validators/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,17 @@ describe('validateInvalidModelFieldsConfig', () => {
expected:
'/models/0/fields/0/supportedOperations: "searchable" is not allowed for type "integer"',
},
{
name: 'field.supportedOperations contains invalid value for type=decimal',
patch: {
name: 'test',
fields: [
{name: 'test', type: 'decimal', supportedOperations: ['searchable']},
],
},
expected:
'/models/0/fields/0/supportedOperations: "searchable" is not allowed for type "decimal"',
},
{
name: 'field.supportedOperations contains invalid value for type=string',
patch: {
Expand Down Expand Up @@ -927,6 +938,17 @@ describe('validateInvalidModelFieldsConfig', () => {
expected:
'/models/0/fields/0/supportedAggregation: "frequency" is not allowed for type "integer"',
},
{
name: 'field.supportedAggregation contains invalid value for type=decimal',
patch: {
name: 'test',
fields: [
{name: 'test', type: 'decimal', supportedAggregation: ['frequency']},
],
},
expected:
'/models/0/fields/0/supportedAggregation: "frequency" is not allowed for type "decimal"',
},
{
name: 'field.supportedAggregation contains invalid value for type=string',
patch: {
Expand Down
Loading