Skip to content

Commit

Permalink
fix(core): make PK properties non-nullable in EntityDTO
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Jan 25, 2024
1 parent c0c6812 commit dc4fc6f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
13 changes: 8 additions & 5 deletions packages/core/src/typings.ts
Expand Up @@ -281,8 +281,8 @@ export type EntityDataNested<T> = T extends undefined
type EntityDataItem<T> = T | EntityDataProp<T> | null;

export type RequiredEntityDataNested<T, O> = T extends any[]
? Readonly<T>
: RequiredEntityData<T, O> | ExpandRequiredEntityProp<T, O>;
? Readonly<T>
: RequiredEntityData<T, O> | ExpandRequiredEntityProp<T, O>;

type ExplicitlyOptionalProps<T> = (T extends { [OptionalProps]?: infer K } ? K : never) | ({ [K in keyof T]: T[K] extends Opt ? K : never }[keyof T] & {});
type NullableKeys<T> = { [K in keyof T]: null extends T[K] ? K : never }[keyof T];
Expand Down Expand Up @@ -362,11 +362,14 @@ export type EntityDTOProp<E, T, C extends TypeConfig = never> = T extends Scalar
: T;

// ideally this should also mark not populated collections as optional, but that would be breaking
type DTOProbablyOptionalProps<T> = ExplicitlyOptionalProps<T> | Defined<NullableKeys<T>>;
type DTOIsOptional<T, K extends keyof T> = T[K] extends LoadedCollection<any>
? false
: K extends ProbablyOptionalProps<T>
? true
: false;
: K extends PrimaryProperty<T>
? false
: K extends DTOProbablyOptionalProps<T>
? true
: false;
type DTORequiredKeys<T, K extends keyof T> = DTOIsOptional<T, K> extends false ? ExcludeHidden<T, K> & CleanKeys<T, K> : never;
type DTOOptionalKeys<T, K extends keyof T> = DTOIsOptional<T, K> extends false ? never : ExcludeHidden<T, K> & CleanKeys<T, K>;

Expand Down
7 changes: 6 additions & 1 deletion tests/entities-sql/Author2.ts
Expand Up @@ -24,7 +24,9 @@ import {
Opt,
Hidden,
Embeddable,
Embedded, sql,
Embedded,
sql,
OptionalProps,
} from '@mikro-orm/core';

import { Book2 } from './Book2';
Expand Down Expand Up @@ -58,6 +60,9 @@ export class Identity {
@Unique({ properties: ['name', 'email'] })
export class Author2 extends BaseEntity2 {

// just for testing the types, this is not needed
[OptionalProps]?: 'id';

static beforeDestroyCalled = 0;
static afterDestroyCalled = 0;

Expand Down
Expand Up @@ -205,6 +205,7 @@ test('explicit serialization with not initialized properties', async () => {
const jon = await orm.em.findOneOrFail(Author2, author)!;

const o = serialize(jon, { populate: ['*'] });
expect(o.id.toFixed()).toBe('2'); // PKs should be non-nullable even if defined in `OptionalProps` explicitly
expect(o).toMatchObject({
id: jon.id,
createdAt: jon.createdAt,
Expand Down
1 change: 0 additions & 1 deletion tests/issues/GH1111.test.ts
Expand Up @@ -54,7 +54,6 @@ describe('GH issue 1111', () => {
await orm.schema.ensureDatabase();
});


beforeEach(async () => {
await orm.schema.dropSchema();
await orm.schema.createSchema();
Expand Down

0 comments on commit dc4fc6f

Please sign in to comment.