Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sql): dates should be nullable without casting as JSON #553

Merged
merged 1 commit into from
Mar 24, 2024
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
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
Loading