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
3 changes: 2 additions & 1 deletion src/interfaces/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export type DataType =
| 'boolean'
| 'text'
| 'datetime'
| 'decimal';
| 'decimal'
| 'date';
export type LogLevel =
| 'trace'
| 'debug'
Expand Down
8 changes: 7 additions & 1 deletion src/migrator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ function generateSchemaFile(config: ModelConfig, engine: DBEngine): string {
case 'decimal':
col = `real('${f.name}')`;
break;
case 'date':
col = `text('${f.name}')`;
break;
default:
col = `text('${f.name}')`;
break; // fallback
Expand Down Expand Up @@ -102,6 +105,9 @@ ${columns}
case 'decimal':
col = `doublePrecision('${f.name}')`;
break;
case 'date':
col = `date('${f.name}')`;
break;
default:
col = `text('${f.name}')`;
break; // fallback
Expand Down Expand Up @@ -132,7 +138,7 @@ ${columns}
const extras = [indexes, foreignKeys].filter(Boolean).join(',\n');

return `
import { pgTable, serial, integer, text, boolean, doublePrecision, index, uniqueIndex, timestamp, foreignKey } from 'drizzle-orm/pg-core';
import { pgTable, serial, integer, text, boolean, doublePrecision, index, uniqueIndex, timestamp, date, foreignKey } from 'drizzle-orm/pg-core';

export const ${config.name} = pgTable('${config.name}', {
${columns}
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 'date':
return String(value);
case 'decimal':
return Number(value);
default:
Expand Down
6 changes: 6 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 'date':
return {type: 'string', format: 'date'};
case 'decimal':
return {type: 'number'};
default:
Expand Down Expand Up @@ -141,6 +143,10 @@ function normalizeSchemaForAjv(schema: JsonSchemaObject): JsonSchemaObject {
prop.type = 'string';
prop.format = 'date-time';
}
if (prop && prop.type === 'date') {
prop.type = 'string';
prop.format = 'date';
}
});
}
return normalized;
Expand Down
10 changes: 9 additions & 1 deletion src/validators/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,15 @@ const fieldSchema = {
},
type: {
type: 'string',
enum: ['integer', 'string', 'boolean', 'text', 'datetime', 'decimal'],
enum: [
'integer',
'string',
'boolean',
'text',
'datetime',
'decimal',
'date',
],
},
primaryKey: {type: 'boolean', default: false},
nullable: {type: 'boolean', default: true},
Expand Down
21 changes: 20 additions & 1 deletion src/validators/config/validate-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ const ALLOWED_OPERATIONS: Record<string, string[]> = {
'equal',
'oneOf',
],
date: [
'sortable',
'lessThan',
'lessThanEqual',
'greaterThan',
'greaterThanEqual',
'equal',
'oneOf',
],
};

const ALLOWED_AGGREGATIONS: Record<string, string[]> = {
Expand All @@ -60,6 +69,7 @@ const ALLOWED_AGGREGATIONS: Record<string, string[]> = {
boolean: ['count', 'frequency'],
text: [],
datetime: ['mean', 'max', 'min', 'count'],
date: ['mean', 'max', 'min', 'count'],
};

function mapModelTypeToJsonSchema(type: string): string {
Expand All @@ -75,6 +85,8 @@ function mapModelTypeToJsonSchema(type: string): string {
return 'boolean';
case 'datetime':
return 'date-time';
case 'date':
return 'date';
/* istanbul ignore next */
default:
return 'string';
Expand All @@ -92,6 +104,10 @@ function normalizeSchemaForAjv(schema: JsonSchemaObject): JsonSchemaObject {
prop.type = 'string';
prop.format = 'date-time';
}
if (prop && prop.type === 'date') {
prop.type = 'string';
prop.format = 'date';
}
});
}
return normalized;
Expand Down Expand Up @@ -201,7 +217,10 @@ function validateModelValidation(config: AppConfig, ajv: Ajv): string[] {
(schemaType === 'datetime' || schemaType === 'date-time') &&
(expectedType === 'datetime' || expectedType === 'date-time');

if (!isDateMatch) {
const isJustDateMatch =
schemaType === 'date' && expectedType === 'date';

if (!isDateMatch && !isJustDateMatch) {
errors.push(
`${propPath}: type mismatch (model=${modelType}, schema=${schemaType})`,
);
Expand Down
2 changes: 1 addition & 1 deletion tests/migrator/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ describe('migrateDatabase', () => {

const schemaContent = writeFileSyncMock.mock.calls[0][1] as string;
expect(schemaContent).toContain(
"import { pgTable, serial, integer, text, boolean, doublePrecision, index, uniqueIndex, timestamp, foreignKey } from 'drizzle-orm/pg-core'",
"import { pgTable, serial, integer, text, boolean, doublePrecision, index, uniqueIndex, timestamp, date, foreignKey } from 'drizzle-orm/pg-core'",
);
expect(schemaContent).toContain("export const posts = pgTable('posts'");
expect(schemaContent).toContain("id: serial('id').primaryKey()");
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, dec) VALUES (@@b:boolean@@, @@t:text@@, @@d:datetime@@, @@dec:decimal@@);',
'INSERT INTO test (b, t, d, dec, dt) VALUES (@@b:boolean@@, @@t:text@@, @@d:datetime@@, @@dec:decimal@@, @@dt:date@@);',
},
],
};
Expand All @@ -107,6 +107,7 @@ describe('test custom-queries api', () => {
t: 'some long text',
d: '2023-01-01T00:00:00Z',
dec: 12.34,
dt: '2023-01-01',
},
});

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 @@ -23,6 +23,7 @@ describe('test schema helper', () => {
expectedSchema: {type: 'string', format: 'date-time'},
},
{dataType: 'decimal', expectedSchema: {type: 'number'}},
{dataType: 'date', expectedSchema: {type: 'string', format: 'date'}},
{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 @@ -570,6 +570,17 @@ describe('validateInvalidModelFieldsConfig', () => {
expected:
'/models/0/fields/0/supportedOperations: "searchable" is not allowed for type "decimal"',
},
{
name: 'field.supportedOperations contains invalid value for type=date',
patch: {
name: 'test',
fields: [
{name: 'test', type: 'date', supportedOperations: ['searchable']},
],
},
expected:
'/models/0/fields/0/supportedOperations: "searchable" is not allowed for type "date"',
},
{
name: 'field.supportedOperations contains invalid value for type=string',
patch: {
Expand Down Expand Up @@ -949,6 +960,17 @@ describe('validateInvalidModelFieldsConfig', () => {
expected:
'/models/0/fields/0/supportedAggregation: "frequency" is not allowed for type "decimal"',
},
{
name: 'field.supportedAggregation contains invalid value for type=date',
patch: {
name: 'test',
fields: [
{name: 'test', type: 'date', supportedAggregation: ['frequency']},
],
},
expected:
'/models/0/fields/0/supportedAggregation: "frequency" is not allowed for type "date"',
},
{
name: 'field.supportedAggregation contains invalid value for type=string',
patch: {
Expand Down
Loading