UNPKG

@mikro-orm/core

Version:

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.

704 lines (703 loc) • 31.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OneToOneOptionsBuilder = exports.OneToManyOptionsBuilderOnlyMappedBy = exports.OneToManyOptionsBuilder = exports.ManyToOneOptionsBuilder = exports.ManyToManyOptionsBuilder = exports.ReferenceOptionsBuilder = exports.EmbeddedOptionsBuilder = exports.EnumOptionsBuilder = exports.PropertyOptionsBuilder = void 0; exports.defineEntity = defineEntity; const types_1 = require("../types"); const EntitySchema_1 = require("../metadata/EntitySchema"); /** @internal */ class PropertyOptionsBuilder { '~options'; '~type'; constructor(options) { this['~options'] = options; } /** * Alias for `fieldName`. */ name(name) { return new PropertyOptionsBuilder({ ...this['~options'], name }); } /** * Specify database column name for this property. * * @see https://mikro-orm.io/docs/naming-strategy */ fieldName(fieldName) { return new PropertyOptionsBuilder({ ...this['~options'], fieldName }); } /** * Specify database column names for this property. * Same as `fieldName` but for composite FKs. * * @see https://mikro-orm.io/docs/naming-strategy */ fieldNames(...fieldNames) { return new PropertyOptionsBuilder({ ...this['~options'], fieldNames }); } /** * Specify an exact database column type for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. This option is only for simple properties represented by a single column. (SQL only) */ columnType(columnType) { return new PropertyOptionsBuilder({ ...this['~options'], columnType }); } /** * Specify an exact database column type for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. This option is suitable for composite keys, where one property is represented by multiple columns. (SQL only) */ columnTypes(...columnTypes) { return new PropertyOptionsBuilder({ ...this['~options'], columnTypes }); } /** * Explicitly specify the runtime type. * * @see https://mikro-orm.io/docs/metadata-providers * @see https://mikro-orm.io/docs/custom-types */ type(type) { return new PropertyOptionsBuilder({ ...this['~options'], type }); } /** * Runtime type of the property. This is the JS type that your property is mapped to, e.g. `string` or `number`, and is normally inferred automatically via `reflect-metadata`. * In some cases, the inference won't work, and you might need to specify the `runtimeType` explicitly - the most common one is when you use a union type with null like `foo: number | null`. */ runtimeType(runtimeType) { return new PropertyOptionsBuilder({ ...this['~options'], runtimeType }); } /** * Set length of database column, used for datetime/timestamp/varchar column types for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only) */ length(length) { return new PropertyOptionsBuilder({ ...this['~options'], length }); } /** * Set precision of database column to represent the number of significant digits. (SQL only) */ precision(precision) { return new PropertyOptionsBuilder({ ...this['~options'], precision }); } /** * Set scale of database column to represents the number of digits after the decimal point. (SQL only) */ scale(scale) { return new PropertyOptionsBuilder({ ...this['~options'], scale }); } /** * Explicitly specify the auto increment of the primary key. */ autoincrement(autoincrement = true) { return new PropertyOptionsBuilder({ ...this['~options'], autoincrement }); } /** * Add the property to the `returning` statement. */ returning(returning = true) { return new PropertyOptionsBuilder({ ...this['~options'], returning }); } /** * Automatically set the property value when entity gets created, executed during flush operation. * @param entity */ onCreate(onCreate) { return new PropertyOptionsBuilder({ ...this['~options'], onCreate }); } /** * Automatically update the property value every time entity gets updated, executed during flush operation. * @param entity */ onUpdate(onUpdate) { return new PropertyOptionsBuilder({ ...this['~options'], onUpdate }); } /** * Specify default column value for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. * This is a runtime value, assignable to the entity property. (SQL only) */ default(defaultValue) { return new PropertyOptionsBuilder({ ...this['~options'], default: defaultValue }); } /** * Specify SQL functions for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only) * Since v4 you should use defaultRaw for SQL functions. e.g. now() */ defaultRaw(defaultRaw) { return new PropertyOptionsBuilder({ ...this['~options'], defaultRaw }); } /** * Set to map some SQL snippet for the entity. * * @see https://mikro-orm.io/docs/defining-entities#formulas Formulas */ formula(formula) { return new PropertyOptionsBuilder({ ...this['~options'], formula }); } /** * For generated columns. This will be appended to the column type after the `generated always` clause. */ generated(generated) { return new PropertyOptionsBuilder({ ...this['~options'], generated }); } /** * Set column as nullable for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. */ nullable(nullable = true) { return new PropertyOptionsBuilder({ ...this['~options'], nullable }); } /** * Set column as unsigned for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only) */ unsigned(unsigned = true) { return new PropertyOptionsBuilder({ ...this['~options'], unsigned }); } /** * Set false to define {@link https://mikro-orm.io/docs/serializing#shadow-properties Shadow Property}. */ persist(persist = true) { return new PropertyOptionsBuilder({ ...this['~options'], persist }); } /** * Set false to disable hydration of this property. Useful for persisted getters. */ hydrate(hydrate = true) { return new PropertyOptionsBuilder({ ...this['~options'], hydrate }); } /** * Enable `ScalarReference` wrapper for lazy values. Use this in combination with `lazy: true` to have a type-safe accessor object in place of the value. */ ref(ref = true) { return new PropertyOptionsBuilder({ ...this['~options'], ref }); } /** * Set false to disable change tracking on a property level. * * @see https://mikro-orm.io/docs/unit-of-work#change-tracking-and-performance-considerations */ trackChanges(trackChanges = true) { return new PropertyOptionsBuilder({ ...this['~options'], trackChanges }); } /** * Set to true to omit the property when {@link https://mikro-orm.io/docs/serializing Serializing}. */ hidden(hidden = true) { return new PropertyOptionsBuilder({ ...this['~options'], hidden }); } /** * Set to true to enable {@link https://mikro-orm.io/docs/transactions#optimistic-locking Optimistic Locking} via version field. (SQL only) */ version(version = true) { return new PropertyOptionsBuilder({ ...this['~options'], version }); } /** * Set to true to enable {@link https://mikro-orm.io/docs/transactions#optimistic-locking Optimistic Locking} via concurrency fields. */ concurrencyCheck(concurrencyCheck = true) { return new PropertyOptionsBuilder({ ...this['~options'], concurrencyCheck }); } /** * Explicitly specify index on a property. */ index(index = true) { return new PropertyOptionsBuilder({ ...this['~options'], index }); } /** * Set column as unique for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only) */ unique(unique = true) { return new PropertyOptionsBuilder({ ...this['~options'], unique }); } /** * Specify column with check constraints. (Postgres driver only) * * @see https://mikro-orm.io/docs/defining-entities#check-constraints */ check(check) { return new PropertyOptionsBuilder({ ...this['~options'], check }); } /** * Set to omit the property from the select clause for lazy loading. * * @see https://mikro-orm.io/docs/defining-entities#lazy-scalar-properties */ lazy(lazy = true, ref = true) { return new PropertyOptionsBuilder({ ...this['~options'], lazy, ref }); } /** * Set true to define entity's unique primary key identifier. * * @see https://mikro-orm.io/docs/decorators#primarykey */ primary(primary = true) { return new PropertyOptionsBuilder({ ...this['~options'], primary }); } /** * Set true to define the properties as setter. (virtual) * * @example * ``` * @Property({ setter: true }) * set address(value: string) { * this._address = value.toLocaleLowerCase(); * } * ``` */ setter(setter = true) { return new PropertyOptionsBuilder({ ...this['~options'], setter }); } /** * Set true to define the properties as getter. (virtual) * * @example * ``` * @Property({ getter: true }) * get fullName() { * return this.firstName + this.lastName; * } * ``` */ getter(getter = true) { return new PropertyOptionsBuilder({ ...this['~options'], getter }); } /** * When defining a property over a method (not a getter, a regular function), you can use this option to point * to the method name. * * @example * ``` * @Property({ getter: true }) * getFullName() { * return this.firstName + this.lastName; * } * ``` */ getterName(getterName) { return new PropertyOptionsBuilder({ ...this['~options'], getterName }); } /** * Set to define serialized primary key for MongoDB. (virtual) * Alias for `@SerializedPrimaryKey()` decorator. * * @see https://mikro-orm.io/docs/decorators#serializedprimarykey */ serializedPrimaryKey(serializedPrimaryKey = true) { return new PropertyOptionsBuilder({ ...this['~options'], serializedPrimaryKey }); } /** * Set to use serialize property. Allow to specify a callback that will be used when serializing a property. * * @see https://mikro-orm.io/docs/serializing#property-serializers */ serializer(serializer) { return new PropertyOptionsBuilder({ ...this['~options'], serializer }); } /** * Specify name of key for the serialized value. */ serializedName(serializedName) { return new PropertyOptionsBuilder({ ...this['~options'], serializedName }); } /** * Specify serialization groups for `serialize()` calls. If a property does not specify any group, it will be included, * otherwise only properties with a matching group are included. */ groups(...groups) { return new PropertyOptionsBuilder({ ...this['~options'], groups }); } /** * Specify a custom order based on the values. (SQL only) */ customOrder(...customOrder) { return new PropertyOptionsBuilder({ ...this['~options'], customOrder }); } /** * Specify comment of column for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only) */ comment(comment) { return new PropertyOptionsBuilder({ ...this['~options'], comment }); } /** mysql only */ extra(extra) { return new PropertyOptionsBuilder({ ...this['~options'], extra }); } /** * Set to avoid a perpetual diff from the {@link https://mikro-orm.io/docs/schema-generator Schema Generator} when columns are generated. * * @see https://mikro-orm.io/docs/defining-entities#sql-generated-columns */ ignoreSchemaChanges(...ignoreSchemaChanges) { return new PropertyOptionsBuilder({ ...this['~options'], ignoreSchemaChanges }); } $type() { return new PropertyOptionsBuilder({ ...this['~options'] }); } } exports.PropertyOptionsBuilder = PropertyOptionsBuilder; /** @internal */ class EnumOptionsBuilder extends PropertyOptionsBuilder { constructor(options) { super(options); this['~options'] = options; } array(array = true) { return new EnumOptionsBuilder({ ...this['~options'], array }); } /** for postgres, by default it uses text column with check constraint */ nativeEnumName(nativeEnumName) { return new EnumOptionsBuilder({ ...this['~options'], nativeEnumName }); } } exports.EnumOptionsBuilder = EnumOptionsBuilder; /** @internal */ class EmbeddedOptionsBuilder extends PropertyOptionsBuilder { constructor(options) { super(options); this['~options'] = options; } prefix(prefix) { return new EmbeddedOptionsBuilder({ ...this['~options'], prefix }); } prefixMode(prefixMode) { return new EmbeddedOptionsBuilder({ ...this['~options'], prefixMode }); } object(object = true) { return new EmbeddedOptionsBuilder({ ...this['~options'], object }); } array(array = true) { return new EmbeddedOptionsBuilder({ ...this['~options'], array }); } } exports.EmbeddedOptionsBuilder = EmbeddedOptionsBuilder; /** @internal */ class ReferenceOptionsBuilder extends PropertyOptionsBuilder { constructor(options) { super(options); this['~options'] = options; } /** Set what actions on owning entity should be cascaded to the relationship. Defaults to [Cascade.PERSIST, Cascade.MERGE] (see {@doclink cascading}). */ cascade(...cascade) { return new ReferenceOptionsBuilder({ ...this['~options'], cascade }); } /** Always load the relationship. Discouraged for use with to-many relations for performance reasons. */ eager(eager = true) { return new ReferenceOptionsBuilder({ ...this['~options'], eager }); } /** Override the default loading strategy for this property. This option has precedence over the global `loadStrategy`, but can be overridden by `FindOptions.strategy`. */ strategy(strategy) { return new ReferenceOptionsBuilder({ ...this['~options'], strategy }); } /** * @internal * re-declare to override type inference */ /* istanbul ignore next */ ref(ref = true) { return new ReferenceOptionsBuilder({ ...this['~options'], ref }); } /** * @internal * re-declare to override type inference */ /* istanbul ignore next */ primary(primary = true) { return new ReferenceOptionsBuilder({ ...this['~options'], primary }); } } exports.ReferenceOptionsBuilder = ReferenceOptionsBuilder; /** @internal */ class ManyToManyOptionsBuilder extends ReferenceOptionsBuilder { constructor(options) { super(options); this['~options'] = options; } /** Set this side as owning. Owning side is where the foreign key is defined. This option is not required if you use `inversedBy` or `mappedBy` to distinguish owning and inverse side. */ owner(owner = true) { return new ManyToManyOptionsBuilder({ ...this['~options'], owner }); } /** Point to the inverse side property name. */ inversedBy(inversedBy) { return new ManyToManyOptionsBuilder({ ...this['~options'], inversedBy }); } /** Point to the owning side property name. */ mappedBy(mappedBy) { return new ManyToManyOptionsBuilder({ ...this['~options'], mappedBy }); } /** Condition for {@doclink collections#declarative-partial-loading | Declarative partial loading}. */ where(...where) { return new ManyToManyOptionsBuilder({ ...this['~options'], where: where }); } /** Set default ordering. */ orderBy(...orderBy) { return new ManyToManyOptionsBuilder({ ...this['~options'], orderBy }); } /** Force stable insertion order of items in the collection (see {@doclink collections | Collections}). */ fixedOrder(fixedOrder = true) { return new ManyToManyOptionsBuilder({ ...this['~options'], fixedOrder }); } /** Override default order column name (`id`) for fixed ordering. */ fixedOrderColumn(fixedOrderColumn) { return new ManyToManyOptionsBuilder({ ...this['~options'], fixedOrderColumn }); } /** Override default name for pivot table (see {@doclink naming-strategy | Naming Strategy}). */ pivotTable(pivotTable) { return new ManyToManyOptionsBuilder({ ...this['~options'], pivotTable }); } /** Set pivot entity for this relation (see {@doclink collections#custom-pivot-table-entity | Custom pivot table entity}). */ pivotEntity(pivotEntity) { return new ManyToManyOptionsBuilder({ ...this['~options'], pivotEntity }); } /** Override the default database column name on the owning side (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */ joinColumn(joinColumn) { return new ManyToManyOptionsBuilder({ ...this['~options'], joinColumn }); } /** Override the default database column name on the owning side (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */ joinColumns(...joinColumns) { return new ManyToManyOptionsBuilder({ ...this['~options'], joinColumns }); } /** Override the default database column name on the inverse side (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */ inverseJoinColumn(inverseJoinColumn) { return new ManyToManyOptionsBuilder({ ...this['~options'], inverseJoinColumn }); } /** Override the default database column name on the inverse side (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */ inverseJoinColumns(...inverseJoinColumns) { return new ManyToManyOptionsBuilder({ ...this['~options'], inverseJoinColumns }); } /** Override the default database column name on the target entity (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */ referenceColumnName(referenceColumnName) { return new ManyToManyOptionsBuilder({ ...this['~options'], referenceColumnName }); } /** Override the default database column name on the target entity (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */ referencedColumnNames(...referencedColumnNames) { return new ManyToManyOptionsBuilder({ ...this['~options'], referencedColumnNames }); } /** What to do when the target entity gets deleted. */ deleteRule(deleteRule) { return new ManyToManyOptionsBuilder({ ...this['~options'], deleteRule }); } /** What to do when the reference to the target entity gets updated. */ updateRule(updateRule) { return new ManyToManyOptionsBuilder({ ...this['~options'], updateRule }); } } exports.ManyToManyOptionsBuilder = ManyToManyOptionsBuilder; /** @internal */ class ManyToOneOptionsBuilder extends ReferenceOptionsBuilder { constructor(options) { super(options); this['~options'] = options; } /** Point to the inverse side property name. */ inversedBy(inversedBy) { return new ManyToOneOptionsBuilder({ ...this['~options'], inversedBy }); } /** Wrap the entity in {@apilink Reference} wrapper. */ ref(ref = true) { return new ManyToOneOptionsBuilder({ ...this['~options'], ref }); } /** Use this relation as a primary key. */ primary(primary = true) { return new ManyToOneOptionsBuilder({ ...this['~options'], primary }); } /** Map this relation to the primary key value instead of an entity. */ mapToPk(mapToPk = true) { return new ManyToOneOptionsBuilder({ ...this['~options'], mapToPk }); } /** Override the default database column name on the owning side (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */ joinColumn(joinColumn) { return new ManyToOneOptionsBuilder({ ...this['~options'], joinColumn }); } /** Override the default database column name on the owning side (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */ joinColumns(...joinColumns) { return new ManyToOneOptionsBuilder({ ...this['~options'], joinColumns }); } /** When a part of a composite column is shared in other properties, use this option to specify what columns are considered as owned by this property. This is useful when your composite property is nullable, but parts of it are not. */ ownColumns(...ownColumns) { return new ManyToOneOptionsBuilder({ ...this['~options'], ownColumns }); } /** Override the default database column name on the target entity (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */ referenceColumnName(referenceColumnName) { return new ManyToOneOptionsBuilder({ ...this['~options'], referenceColumnName }); } /** Override the default database column name on the target entity (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */ referencedColumnNames(...referencedColumnNames) { return new ManyToOneOptionsBuilder({ ...this['~options'], referencedColumnNames }); } /** What to do when the target entity gets deleted. */ deleteRule(deleteRule) { return new ManyToOneOptionsBuilder({ ...this['~options'], deleteRule }); } /** What to do when the reference to the target entity gets updated. */ updateRule(updateRule) { return new ManyToOneOptionsBuilder({ ...this['~options'], updateRule }); } /** Set the constraint type. Immediate constraints are checked for each statement, while deferred ones are only checked at the end of the transaction. Only for postgres unique constraints. */ deferMode(deferMode) { return new ManyToOneOptionsBuilder({ ...this['~options'], deferMode }); } } exports.ManyToOneOptionsBuilder = ManyToOneOptionsBuilder; /** @internal */ class OneToManyOptionsBuilder extends ReferenceOptionsBuilder { constructor(options) { super(options); this['~options'] = options; } /** Remove the entity when it gets disconnected from the relationship (see {@doclink cascading | Cascading}). */ orphanRemoval(orphanRemoval = true) { return new OneToManyOptionsBuilder({ ...this['~options'], orphanRemoval }); } /** Set default ordering. */ orderBy(orderBy) { return new OneToManyOptionsBuilder({ ...this['~options'], orderBy }); } /** Condition for {@doclink collections#declarative-partial-loading | Declarative partial loading}. */ where(where) { return new OneToManyOptionsBuilder({ ...this['~options'], where }); } /** Override the default database column name on the owning side (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */ joinColumn(joinColumn) { return new OneToManyOptionsBuilder({ ...this['~options'], joinColumn }); } /** Override the default database column name on the owning side (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */ joinColumns(...joinColumns) { return new OneToManyOptionsBuilder({ ...this['~options'], joinColumns }); } /** Override the default database column name on the inverse side (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */ inverseJoinColumn(inverseJoinColumn) { return new OneToManyOptionsBuilder({ ...this['~options'], inverseJoinColumn }); } /** Override the default database column name on the inverse side (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */ inverseJoinColumns(...inverseJoinColumns) { return new OneToManyOptionsBuilder({ ...this['~options'], inverseJoinColumns }); } /** Override the default database column name on the target entity (see {@doclink naming-strategy | Naming Strategy}). This option is only for simple properties represented by a single column. */ referenceColumnName(referenceColumnName) { return new OneToManyOptionsBuilder({ ...this['~options'], referenceColumnName }); } /** Override the default database column name on the target entity (see {@doclink naming-strategy | Naming Strategy}). This option is suitable for composite keys, where one property is represented by multiple columns. */ referencedColumnNames(...referencedColumnNames) { return new OneToManyOptionsBuilder({ ...this['~options'], referencedColumnNames }); } } exports.OneToManyOptionsBuilder = OneToManyOptionsBuilder; /** @internal */ class OneToManyOptionsBuilderOnlyMappedBy { constructor(options) { this['~options'] = options; } /** Point to the owning side property name. */ mappedBy(mappedBy) { return new OneToManyOptionsBuilder({ ...this['~options'], mappedBy }); } } exports.OneToManyOptionsBuilderOnlyMappedBy = OneToManyOptionsBuilderOnlyMappedBy; /** @internal */ class OneToOneOptionsBuilder extends ReferenceOptionsBuilder { constructor(options) { super(options); this['~options'] = options; } /** Set this side as owning. Owning side is where the foreign key is defined. This option is not required if you use `inversedBy` or `mappedBy` to distinguish owning and inverse side. */ owner(owner = true) { return new OneToOneOptionsBuilder({ ...this['~options'], owner }); } /** Point to the inverse side property name. */ inversedBy(inversedBy) { return new OneToOneOptionsBuilder({ ...this['~options'], inversedBy }); } /** Wrap the entity in {@apilink Reference} wrapper. */ ref(ref = true) { return new OneToOneOptionsBuilder({ ...this['~options'], ref }); } /** Use this relation as a primary key. */ primary(primary = true) { return new OneToOneOptionsBuilder({ ...this['~options'], primary }); } /** Map this relation to the primary key value instead of an entity. */ mapToPk(mapToPk = true) { return new OneToOneOptionsBuilder({ ...this['~options'], mapToPk }); } /** When a part of a composite column is shared in other properties, use this option to specify what columns are considered as owned by this property. This is useful when your composite property is nullable, but parts of it are not. */ ownColumns(...ownColumns) { return new OneToOneOptionsBuilder({ ...this['~options'], ownColumns }); } /** What to do when the target entity gets deleted. */ deleteRule(deleteRule) { return new OneToOneOptionsBuilder({ ...this['~options'], deleteRule }); } /** What to do when the reference to the target entity gets updated. */ updateRule(updateRule) { return new OneToOneOptionsBuilder({ ...this['~options'], updateRule }); } /** Set the constraint type. Immediate constraints are checked for each statement, while deferred ones are only checked at the end of the transaction. Only for postgres unique constraints. */ deferMode(deferMode) { return new OneToOneOptionsBuilder({ ...this['~options'], deferMode }); } } exports.OneToOneOptionsBuilder = OneToOneOptionsBuilder; function createPropertyBuilders(options) { return Object.fromEntries(Object.entries(options).map(([key, value]) => [key, () => new PropertyOptionsBuilder({ type: value })])); } const propertyBuilders = { ...createPropertyBuilders(types_1.types), json: () => new PropertyOptionsBuilder({ type: types_1.types.json }), formula: (formula) => new PropertyOptionsBuilder({ formula }), type: (type) => new PropertyOptionsBuilder({ type }), enum: (items) => new EnumOptionsBuilder({ enum: true, items, }), embedded: (target) => new EmbeddedOptionsBuilder({ entity: () => target, kind: 'embedded', }), manyToMany: (target) => new ManyToManyOptionsBuilder({ entity: () => target, kind: 'm:n', }), manyToOne: (target) => new ManyToOneOptionsBuilder({ entity: () => target, kind: 'm:1', ref: true, }), oneToMany: (target) => new OneToManyOptionsBuilderOnlyMappedBy({ entity: () => target, kind: '1:m', }), oneToOne: (target) => new OneToOneOptionsBuilder({ entity: () => target, kind: '1:1', ref: true, }), }; function getBuilderOptions(builder) { return '~options' in builder ? builder['~options'] : builder; } function defineEntity(meta) { const { properties: propertiesOrGetter, ...options } = meta; const propertyOptions = typeof propertiesOrGetter === 'function' ? propertiesOrGetter(propertyBuilders) : propertiesOrGetter; const properties = {}; for (const [key, builder] of Object.entries(propertyOptions)) { const values = new Map(); if (typeof builder === 'function') { Object.defineProperty(properties, key, { get: () => { let value = values.get(key); if (value === undefined) { value = getBuilderOptions(builder()); values.set(key, value); } return value; }, set: (value) => { values.set(key, value); }, enumerable: true, }); } else { Object.defineProperty(properties, key, { value: getBuilderOptions(builder), writable: true, enumerable: true, }); } } return new EntitySchema_1.EntitySchema({ properties, ...options }); } defineEntity.properties = propertyBuilders;