@mikro-orm/core 7.0.3-dev.16 → 7.0.3-dev.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12,7 +12,7 @@ import type { Collection } from './Collection.js';
12
12
  import type { FilterOptions } from '../drivers/IDatabaseDriver.js';
13
13
  /** Union of all option keys supported across all property definition types (scalar, enum, embedded, relations). */
14
14
  export type UniversalPropertyKeys = keyof PropertyOptions<any> | keyof EnumOptions<any> | keyof EmbeddedOptions<any, any> | keyof ReferenceOptions<any, any> | keyof ManyToOneOptions<any, any> | keyof OneToManyOptions<any, any> | keyof OneToOneOptions<any, any> | keyof ManyToManyOptions<any, any>;
15
- type BuilderExtraKeys = '~options' | '~type' | '$type';
15
+ type BuilderExtraKeys = '~options' | '~type' | '$type' | 'strictNullable';
16
16
  type ExcludeKeys = 'entity' | 'items';
17
17
  type BuilderKeys = Exclude<UniversalPropertyKeys, ExcludeKeys> | BuilderExtraKeys;
18
18
  type IncludeKeysForProperty = Exclude<keyof PropertyOptions<any>, ExcludeKeys> | BuilderExtraKeys;
@@ -32,6 +32,10 @@ export interface PropertyChain<Value, Options> {
32
32
  nullable(): PropertyChain<Value, Omit<Options, 'nullable'> & {
33
33
  nullable: true;
34
34
  }>;
35
+ strictNullable(): PropertyChain<Value, Omit<Options, 'nullable' | 'strictNullable'> & {
36
+ nullable: true;
37
+ strictNullable: true;
38
+ }>;
35
39
  ref(): PropertyChain<Value, Omit<Options, 'ref'> & {
36
40
  ref: true;
37
41
  }>;
@@ -281,6 +285,14 @@ export declare class UniversalPropertyOptionsBuilder<Value, Options, IncludeKeys
281
285
  nullable(): Pick<UniversalPropertyOptionsBuilder<Value, Omit<Options, 'nullable'> & {
282
286
  nullable: true;
283
287
  }, IncludeKeys>, IncludeKeys>;
288
+ /**
289
+ * Set column as nullable without adding `| undefined` to the type.
290
+ * Use this when the property is always explicitly set (e.g. in constructor) but can be `null`.
291
+ */
292
+ strictNullable(): Pick<UniversalPropertyOptionsBuilder<Value, Omit<Options, 'nullable' | 'strictNullable'> & {
293
+ nullable: true;
294
+ strictNullable: true;
295
+ }, IncludeKeys>, IncludeKeys>;
284
296
  /**
285
297
  * Set column as unsigned for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
286
298
  */
@@ -673,7 +685,9 @@ type MaybeMapToPk<Value, Options> = Options extends {
673
685
  } ? Primary<Value> : Value;
674
686
  type MaybeNullable<Value, Options> = Options extends {
675
687
  nullable: true;
676
- } ? Value | null | undefined : Value;
688
+ } ? Options extends {
689
+ strictNullable: true;
690
+ } ? Value | null : Value | null | undefined : Value;
677
691
  type MaybeRelationRef<Value, Options> = Options extends {
678
692
  mapToPk: true;
679
693
  } ? Value : Options extends {
@@ -145,6 +145,13 @@ export class UniversalPropertyOptionsBuilder {
145
145
  nullable() {
146
146
  return this.assignOptions({ nullable: true });
147
147
  }
148
+ /**
149
+ * Set column as nullable without adding `| undefined` to the type.
150
+ * Use this when the property is always explicitly set (e.g. in constructor) but can be `null`.
151
+ */
152
+ strictNullable() {
153
+ return this.assignOptions({ nullable: true });
154
+ }
148
155
  /**
149
156
  * Set column as unsigned for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
150
157
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "7.0.3-dev.16",
3
+ "version": "7.0.3-dev.18",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "keywords": [
6
6
  "data-mapper",
@@ -1,4 +1,4 @@
1
- import type { ArrayElement, AutoPath, CleanTypeConfig, SerializeDTO, FromEntityType, TypeConfig, UnboxArray } from '../typings.js';
1
+ import type { ArrayElement, AutoPath, CleanTypeConfig, ExtractFieldsHint, FromEntityType, SerializeDTO, TypeConfig, UnboxArray } from '../typings.js';
2
2
  import { type PopulatePath } from '../enums.js';
3
3
  /** Converts entity instances to plain DTOs via `serialize()`, with fine-grained control over populate, exclude, and serialization groups. */
4
4
  export declare class EntitySerializer {
@@ -41,4 +41,4 @@ export interface SerializeOptions<T, P extends string = never, E extends string
41
41
  * const dto2 = wrap(user).serialize({ exclude: ['id', 'email'], forceObject: true });
42
42
  * ```
43
43
  */
44
- export declare function serialize<Entity extends object, Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Populate extends string = never, Exclude extends string = never, Config extends TypeConfig = never>(entity: Entity, options?: Config & SerializeOptions<UnboxArray<Entity>, Populate, Exclude>): Naked extends object[] ? SerializeDTO<ArrayElement<Naked>, Populate, Exclude, CleanTypeConfig<Config>>[] : SerializeDTO<Naked, Populate, Exclude, CleanTypeConfig<Config>>;
44
+ export declare function serialize<Entity extends object, Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Populate extends string = never, Exclude extends string = never, Config extends TypeConfig = never>(entity: Entity, options?: Config & SerializeOptions<UnboxArray<Entity>, Populate, Exclude>): Naked extends object[] ? SerializeDTO<ArrayElement<Naked>, Populate, Exclude, CleanTypeConfig<Config>>[] : SerializeDTO<Naked, Populate, Exclude, CleanTypeConfig<Config>, ExtractFieldsHint<Entity>>;
package/typings.d.ts CHANGED
@@ -201,6 +201,7 @@ export interface TypeConfig {
201
201
  declare const __selectedType: unique symbol;
202
202
  declare const __loadedType: unique symbol;
203
203
  declare const __loadHint: unique symbol;
204
+ declare const __fieldsHint: unique symbol;
204
205
  /**
205
206
  * Expands a populate hint into all its prefixes.
206
207
  * e.g., Prefixes<'a.b.c'> = 'a' | 'a.b' | 'a.b.c'
@@ -527,13 +528,18 @@ export type EntityDTOFlat<T, C extends TypeConfig = never> = {
527
528
  */
528
529
  type SerializeTopHints<H extends string> = H extends `${infer Top}.${string}` ? Top : H;
529
530
  type SerializeSubHints<K extends string, H extends string> = H extends `${K}.${infer Rest}` ? Rest : never;
531
+ type SerializeSubFields<K extends string, F extends string> = K extends F ? '*' : [F extends `${K & string}.${infer Rest}` ? Rest : never] extends [never] ? '*' : F extends `${K & string}.${infer Rest}` ? Rest : never;
532
+ type SerializeFieldsFilter<T, K extends keyof T, F extends string> = K extends Prefix<T, F> | PrimaryProperty<T> ? K : never;
530
533
  type SerializePropValue<T, K extends keyof T, H extends string, C extends TypeConfig = never> = K & string extends SerializeTopHints<H> ? NonNullable<T[K]> extends CollectionShape<infer U> ? SerializeDTO<U & object, SerializeSubHints<K & string, H>, never, C>[] : SerializeDTO<ExpandProperty<T[K]>, SerializeSubHints<K & string, H>, never, C> | Extract<T[K], null | undefined> : EntityDTOProp<T, T[K], C>;
534
+ type SerializePropValueWithFields<T, K extends keyof T, H extends string, C extends TypeConfig, F extends string> = K & string extends SerializeTopHints<H> ? NonNullable<T[K]> extends CollectionShape<infer U> ? SerializeDTO<U & object, SerializeSubHints<K & string, H>, never, C, SerializeSubFields<K & string, F>>[] : SerializeDTO<ExpandProperty<T[K]>, SerializeSubHints<K & string, H>, never, C, SerializeSubFields<K & string, F>> | Extract<T[K], null | undefined> : EntityDTOProp<T, T[K], C>;
531
535
  /**
532
536
  * Return type of `serialize()`. Combines Loaded + EntityDTO in a single pass for better performance.
533
- * Respects populate hints (`H`) and exclude hints (`E`).
537
+ * Respects populate hints (`H`), exclude hints (`E`), and fields hints (`F`).
534
538
  */
535
- export type SerializeDTO<T, H extends string = never, E extends string = never, C extends TypeConfig = never> = string extends H ? EntityDTOFlat<T, C> : {
539
+ export type SerializeDTO<T, H extends string = never, E extends string = never, C extends TypeConfig = never, F extends string = '*'> = string extends H ? EntityDTOFlat<T, C> : [F] extends ['*'] ? {
536
540
  [K in keyof T as ExcludeHidden<T, K> & CleanKeys<T, K> & (IsNever<E> extends true ? K : Exclude<K, E>)]: SerializePropValue<T, K, H, C> | Extract<T[K], null | undefined>;
541
+ } : {
542
+ [K in keyof T as ExcludeHidden<T, K> & CleanKeys<T, K> & (IsNever<E> extends true ? K : Exclude<K, E>) & SerializeFieldsFilter<T, K, F>]: SerializePropValueWithFields<T, K, H, C, F> | Extract<T[K], null | undefined>;
537
543
  };
538
544
  type TargetKeys<T> = T extends EntityClass<infer P> ? keyof P : keyof T;
539
545
  type PropertyName<T> = IsUnknown<T> extends false ? TargetKeys<T> : string;
@@ -1186,6 +1192,7 @@ export type Selected<T, L extends string = never, F extends string = '*'> = {
1186
1192
  [K in keyof T as IsPrefixed<T, K, L | F | AddEager<T>> | FunctionKeys<T, K>]: T[K] extends Function ? T[K] : NonNullable<T[K]> extends Scalar ? T[K] : LoadedProp<NonNullable<T[K]>, Suffix<K, L, true>, Suffix<K, F, true>> | AddOptional<T[K]>;
1187
1193
  } & {
1188
1194
  [__selectedType]?: T;
1195
+ [__fieldsHint]?: (hint: F) => void;
1189
1196
  };
1190
1197
  type LoadedEntityType<T> = {
1191
1198
  [__loadedType]?: T;
@@ -1196,6 +1203,10 @@ type LoadedEntityType<T> = {
1196
1203
  export type EntityType<T> = T | LoadedEntityType<T>;
1197
1204
  /** Extracts the base entity type from a `Loaded`/`Selected` wrapper, or returns `T` as-is. */
1198
1205
  export type FromEntityType<T> = T extends LoadedEntityType<infer U> ? U : T;
1206
+ /** Extracts the fields hint (`F`) from a `Loaded`/`Selected` type, or returns `'*'` (all fields) for unwrapped entities. */
1207
+ export type ExtractFieldsHint<T> = T extends {
1208
+ [__fieldsHint]?: (hint: infer F extends string) => void;
1209
+ } ? F : '*';
1199
1210
  type LoadedInternal<T, L extends string = never, F extends string = '*', E extends string = never> = [F] extends ['*'] ? IsNever<E> extends true ? T & {
1200
1211
  [K in keyof T as IsPrefixed<T, K, ExpandHint<T, L>>]: LoadedProp<NonNullable<T[K]>, Suffix<K, L>, Suffix<K, F>, Suffix<K, E>> | AddOptional<T[K]>;
1201
1212
  } : {
package/utils/Utils.js CHANGED
@@ -129,7 +129,7 @@ export function parseJsonSafe(value) {
129
129
  /** Collection of general-purpose utility methods used throughout the ORM. */
130
130
  export class Utils {
131
131
  static PK_SEPARATOR = '~~~';
132
- static #ORM_VERSION = '7.0.3-dev.16';
132
+ static #ORM_VERSION = '7.0.3-dev.18';
133
133
  /**
134
134
  * Checks if the argument is instance of `Object`. Returns false for arrays.
135
135
  */