Skip to content

Commit

Permalink
fix(sql): dates should be nullable without casting as JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
fergusean committed Feb 11, 2024
1 parent 0014074 commit 88dd4a9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/mysql/src/mysql-platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import { Pool } from 'mariadb';
import { mySqlSerializer } from './mysql-serializer.js';
import { isDateType, isReferenceType, isUUIDType, ReflectionClass, ReflectionKind, ReflectionProperty, Serializer, Type, TypeNumberBrand } from '@deepkit/type';
import { isReferenceType, isUUIDType, ReflectionClass, ReflectionKind, ReflectionProperty, Serializer, Type, TypeNumberBrand } from '@deepkit/type';
import {
Column,
DefaultPlatform,
Expand All @@ -19,6 +19,7 @@ import {
noopSqlTypeCaster,
typeResolvesToBigInt,
typeResolvesToBoolean,
typeResolvesToDate,
typeResolvesToInteger,
typeResolvesToNumber,
typeResolvesToString,
Expand Down Expand Up @@ -66,7 +67,7 @@ export class MySQLPlatform extends DefaultPlatform {
this.addType(type => type.kind === ReflectionKind.number && type.brand === TypeNumberBrand.float64, 'double');
this.addType(type => type.kind === ReflectionKind.number && type.brand === TypeNumberBrand.float, 'double');

this.addType(isDateType, 'datetime');
this.addType(typeResolvesToDate, 'datetime');
this.addType(isUUIDType, 'binary', 16);

this.addBinaryType('longblob');
Expand Down
8 changes: 5 additions & 3 deletions packages/mysql/tests/mysql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,21 +324,23 @@ test('unique constraint 1', async () => {
}
});

test('string/null unions should not render as JSON', async () => {
test('non-object null unions should not render as JSON', async () => {
@entity.name('model6')
class Model {
id: number & PrimaryKey & AutoIncrement = 0;

constructor(public name: string, public nickName: string | null = null) {}
constructor(public name: string, public nickName: string | null = null, public birthdate: Date | null = null) {}
}

const database = await databaseFactory([Model]);
await database.persist(new Model('Peter'));
await database.persist(new Model('Christopher', 'Chris'));
await database.persist(new Model('Thomas', 'Tom', new Date('1960-02-10T00:00:00Z')));

const result = await database.query(Model).orderBy('id', 'asc').find();
expect(result).toMatchObject([
{name: 'Peter', nickName: null},
{name: 'Christopher', nickName: 'Chris'}
{name: 'Christopher', nickName: 'Chris'},
{name: 'Thomas', nickName: 'Tom', birthdate: new Date('1960-02-10T00:00:00Z')},
]);
});
7 changes: 6 additions & 1 deletion packages/sql/src/platform/default-platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { sqlSerializer } from '../serializer/sql-serializer.js';
import { parseType, SchemaParser } from '../reverse/schema-parser.js';
import { SQLFilterBuilder } from '../sql-filter-builder.js';
import { Sql } from '../sql-builder.js';
import { binaryTypes, databaseAnnotation, isCustomTypeClass, isIntegerType, ReflectionClass, ReflectionKind, ReflectionProperty, Serializer, Type } from '@deepkit/type';
import { binaryTypes, databaseAnnotation, isCustomTypeClass, isDateType, isIntegerType, ReflectionClass, ReflectionKind, ReflectionProperty, Serializer, Type } from '@deepkit/type';
import { DatabaseEntityRegistry, MigrateOptions } from '@deepkit/orm';
import { splitDotPath } from '../sql-adapter.js';

Expand Down Expand Up @@ -78,6 +78,11 @@ export function typeResolvesToBoolean(type: Type): boolean {
return false;
}

export function typeResolvesToDate(type: Type): boolean {
if (type.kind === ReflectionKind.union) return type.types.filter(isNonUndefined).every(isDateType);
return isDateType(type);
}

export function noopSqlTypeCaster(placeholder: string): string {
return placeholder;
}
Expand Down

0 comments on commit 88dd4a9

Please sign in to comment.