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 Mar 23, 2024
1 parent 51b9b36 commit c5760c1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 26 deletions.
15 changes: 3 additions & 12 deletions packages/mysql/src/mysql-platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +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 @@ -30,6 +20,7 @@ import {
PreparedAdapter,
typeResolvesToBigInt,
typeResolvesToBoolean,
typeResolvesToDate,
typeResolvesToInteger,
typeResolvesToNumber,
typeResolvesToString,
Expand Down Expand Up @@ -78,7 +69,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')},
]);
});
17 changes: 6 additions & 11 deletions packages/sql/src/platform/default-platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,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';
import { PreparedAdapter } from '../prepare.js';
Expand Down Expand Up @@ -98,6 +88,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 c5760c1

Please sign in to comment.