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): strings should be nullable without casting as JSON #552

Merged
merged 1 commit into from
Feb 10, 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
19 changes: 19 additions & 0 deletions packages/mysql/tests/mysql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,3 +323,22 @@ test('unique constraint 1', async () => {
await expect(p).rejects.toBeInstanceOf(UniqueConstraintFailure);
}
});

test('string/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) {}
}

const database = await databaseFactory([Model]);
await database.persist(new Model('Peter'));
await database.persist(new Model('Christopher', 'Chris'));

const result = await database.query(Model).orderBy('id', 'asc').find();
expect(result).toMatchObject([
{name: 'Peter', nickName: null},
{name: 'Christopher', nickName: 'Chris'}
]);
});
2 changes: 1 addition & 1 deletion packages/sql/src/platform/default-platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function isNonUndefined(type: Type): boolean {
export function typeResolvesToString(type: Type): boolean {
if (type.kind === ReflectionKind.string) return true;
if (type.kind === ReflectionKind.literal && 'string' === typeof type.literal) return true;
if (type.kind === ReflectionKind.union) return type.types.every(v => typeResolvesToString(v));
if (type.kind === ReflectionKind.union) return type.types.filter(isNonUndefined).every(v => typeResolvesToString(v));
if (type.kind === ReflectionKind.enum) return typeResolvesToString(type.indexType);
return false;
}
Expand Down
Loading