Skip to content

Commit

Permalink
fix(sql): strings should be nullable without casting as JSON (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
fergusean committed Feb 10, 2024
1 parent 3e09ddf commit fe55b7f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
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

0 comments on commit fe55b7f

Please sign in to comment.