@mikro-orm/core
Advanced tools
Comparing version 4.0.0-alpha.3 to 4.0.0-alpha.4
@@ -5,2 +5,3 @@ "use strict"; | ||
const metadata_1 = require("../metadata"); | ||
const events_1 = require("../events"); | ||
function hook(type) { | ||
@@ -16,19 +17,19 @@ return function (target, method) { | ||
function BeforeCreate() { | ||
return hook('beforeCreate'); | ||
return hook(events_1.EventType.beforeCreate); | ||
} | ||
exports.BeforeCreate = BeforeCreate; | ||
function AfterCreate() { | ||
return hook('afterCreate'); | ||
return hook(events_1.EventType.afterCreate); | ||
} | ||
exports.AfterCreate = AfterCreate; | ||
function BeforeUpdate() { | ||
return hook('beforeUpdate'); | ||
return hook(events_1.EventType.beforeUpdate); | ||
} | ||
exports.BeforeUpdate = BeforeUpdate; | ||
function AfterUpdate() { | ||
return hook('afterUpdate'); | ||
return hook(events_1.EventType.afterUpdate); | ||
} | ||
exports.AfterUpdate = AfterUpdate; | ||
function OnInit() { | ||
return hook('onInit'); | ||
return hook(events_1.EventType.onInit); | ||
} | ||
@@ -40,3 +41,3 @@ exports.OnInit = OnInit; | ||
function BeforeDelete() { | ||
return hook('beforeDelete'); | ||
return hook(events_1.EventType.beforeDelete); | ||
} | ||
@@ -48,4 +49,4 @@ exports.BeforeDelete = BeforeDelete; | ||
function AfterDelete() { | ||
return hook('afterDelete'); | ||
return hook(events_1.EventType.afterDelete); | ||
} | ||
exports.AfterDelete = AfterDelete; |
@@ -14,2 +14,3 @@ export * from './PrimaryKey'; | ||
export * from './Embedded'; | ||
export * from './Subscriber'; | ||
export * from './hooks'; |
@@ -27,2 +27,3 @@ "use strict"; | ||
__exportStar(require("./Embedded"), exports); | ||
__exportStar(require("./Subscriber"), exports); | ||
__exportStar(require("./hooks"), exports); |
@@ -11,3 +11,3 @@ "use strict"; | ||
const meta = metadata_1.MetadataStorage.getMetadataFromDecorator(target.constructor); | ||
entity_1.EntityValidator.validateSingleDecorator(meta, propertyName); | ||
metadata_1.MetadataValidator.validateSingleDecorator(meta, propertyName); | ||
const property = { name: propertyName, reference: entity_1.ReferenceType.MANY_TO_MANY }; | ||
@@ -14,0 +14,0 @@ meta.properties[propertyName] = Object.assign(property, options); |
@@ -11,3 +11,3 @@ "use strict"; | ||
const meta = metadata_1.MetadataStorage.getMetadataFromDecorator(target.constructor); | ||
entity_1.EntityValidator.validateSingleDecorator(meta, propertyName); | ||
metadata_1.MetadataValidator.validateSingleDecorator(meta, propertyName); | ||
const property = { name: propertyName, reference: entity_1.ReferenceType.MANY_TO_ONE }; | ||
@@ -14,0 +14,0 @@ meta.properties[propertyName] = Object.assign(property, options); |
@@ -11,3 +11,3 @@ "use strict"; | ||
const meta = metadata_1.MetadataStorage.getMetadataFromDecorator(target.constructor); | ||
entity_1.EntityValidator.validateSingleDecorator(meta, propertyName); | ||
metadata_1.MetadataValidator.validateSingleDecorator(meta, propertyName); | ||
const prop = { name: propertyName, reference }; | ||
@@ -14,0 +14,0 @@ Object.assign(prop, options); |
@@ -11,3 +11,3 @@ "use strict"; | ||
const desc = Object.getOwnPropertyDescriptor(target, propertyName) || {}; | ||
entity_1.EntityValidator.validateSingleDecorator(meta, propertyName); | ||
metadata_1.MetadataValidator.validateSingleDecorator(meta, propertyName); | ||
const name = options.name || propertyName; | ||
@@ -14,0 +14,0 @@ if (propertyName !== name && !(desc.value instanceof Function)) { |
@@ -114,7 +114,11 @@ "use strict"; | ||
runHooks(entity, meta) { | ||
if (meta.hooks && meta.hooks.onInit && meta.hooks.onInit.length > 0) { | ||
meta.hooks.onInit.forEach(hook => entity[hook]()); | ||
var _a; | ||
/* istanbul ignore next */ | ||
const hooks = ((_a = meta.hooks) === null || _a === void 0 ? void 0 : _a.onInit) || []; | ||
if (hooks.length > 0) { | ||
hooks.forEach(hook => entity[hook]()); | ||
} | ||
this.em.getEventManager().dispatchEvent(__1.EventType.onInit, entity, this.em); | ||
} | ||
} | ||
exports.EntityFactory = EntityFactory; |
@@ -5,3 +5,2 @@ import { EntityData, EntityMetadata, EntityProperty, FilterQuery, AnyEntity } from '../typings'; | ||
constructor(strict: boolean); | ||
static validateSingleDecorator(meta: EntityMetadata, propertyName: string): void; | ||
validate<T extends AnyEntity<T>>(entity: T, payload: any, meta: EntityMetadata): void; | ||
@@ -8,0 +7,0 @@ validateProperty<T extends AnyEntity<T>>(prop: EntityProperty, givenValue: any, entity: T): any; |
@@ -12,7 +12,2 @@ "use strict"; | ||
} | ||
static validateSingleDecorator(meta, propertyName) { | ||
if (meta.properties[propertyName] && meta.properties[propertyName].reference) { | ||
throw utils_1.ValidationError.multipleDecorators(meta.className, propertyName); | ||
} | ||
} | ||
validate(entity, payload, meta) { | ||
@@ -19,0 +14,0 @@ Object.values(meta.properties).forEach(prop => { |
@@ -0,1 +1,3 @@ | ||
/// <reference types="node" /> | ||
import { inspect } from 'util'; | ||
import { Configuration } from './utils'; | ||
@@ -9,2 +11,3 @@ import { EntityFactory, EntityRepository, EntityValidator, IdentifiedReference, Reference } from './entity'; | ||
import { Transaction } from './connections'; | ||
import { EventManager } from './events'; | ||
/** | ||
@@ -25,2 +28,3 @@ * The EntityManager is the central access point to ORM functionality. It is a facade to all different ORM subsystems | ||
private readonly entityFactory; | ||
private readonly eventManager; | ||
private transactionContext?; | ||
@@ -212,2 +216,3 @@ constructor(config: Configuration, driver: D, metadata: MetadataStorage, useContext?: boolean); | ||
getEntityFactory(): EntityFactory; | ||
getEventManager(): EventManager; | ||
/** | ||
@@ -229,2 +234,3 @@ * Checks whether this EntityManager is currently operating inside a database transaction. | ||
private preparePopulateObject; | ||
[inspect.custom](): string; | ||
} | ||
@@ -231,0 +237,0 @@ export interface FindOneOrFailOptions<T> extends FindOneOptions<T> { |
@@ -5,5 +5,7 @@ "use strict"; | ||
const uuid_1 = require("uuid"); | ||
const util_1 = require("util"); | ||
const utils_1 = require("./utils"); | ||
const entity_1 = require("./entity"); | ||
const unit_of_work_1 = require("./unit-of-work"); | ||
const events_1 = require("./events"); | ||
/** | ||
@@ -25,2 +27,3 @@ * The EntityManager is the central access point to ORM functionality. It is a facade to all different ORM subsystems | ||
this.entityFactory = new entity_1.EntityFactory(this.unitOfWork, this); | ||
this.eventManager = new events_1.EventManager(this.config.get('subscribers')); | ||
} | ||
@@ -369,2 +372,5 @@ /** | ||
} | ||
getEventManager() { | ||
return this.eventManager; | ||
} | ||
/** | ||
@@ -447,3 +453,6 @@ * Checks whether this EntityManager is currently operating inside a database transaction. | ||
} | ||
[util_1.inspect.custom]() { | ||
return `[EntityManager<${this.id}>]`; | ||
} | ||
} | ||
exports.EntityManager = EntityManager; |
@@ -6,2 +6,3 @@ export { Constructor, Dictionary, PrimaryKeyType, Primary, IPrimaryKey, FilterQuery, IWrappedEntity, EntityName, EntityData, AnyEntity, EntityProperty, EntityMetadata, QBFilterQuery, } from './typings'; | ||
export * from './entity'; | ||
export * from './events'; | ||
export * from './EntityManager'; | ||
@@ -8,0 +9,0 @@ export * from './unit-of-work'; |
@@ -20,2 +20,3 @@ "use strict"; | ||
__exportStar(require("./entity"), exports); | ||
__exportStar(require("./events"), exports); | ||
__exportStar(require("./EntityManager"), exports); | ||
@@ -22,0 +23,0 @@ __exportStar(require("./unit-of-work"), exports); |
@@ -18,3 +18,4 @@ import { Configuration } from '../utils'; | ||
private findEntities; | ||
private discoverDirectory; | ||
private discoverDirectories; | ||
private discoverReferences; | ||
private prepare; | ||
@@ -21,0 +22,0 @@ private getSchema; |
@@ -59,20 +59,21 @@ "use strict"; | ||
this.discovered.length = 0; | ||
if (this.config.get('discovery').requireEntitiesArray && this.config.get('entities').length === 0) { | ||
const key = (preferTsNode && this.config.get('tsNode', utils_1.Utils.detectTsNode()) && this.config.get('entitiesTs').length > 0) ? 'entitiesTs' : 'entities'; | ||
const paths = this.config.get(key).filter(item => utils_1.Utils.isString(item)); | ||
const refs = this.config.get(key).filter(item => !utils_1.Utils.isString(item)); | ||
if (this.config.get('discovery').requireEntitiesArray && paths.length > 0) { | ||
throw new Error(`[requireEntitiesArray] Explicit list of entities is required, please use the 'entities' option.`); | ||
} | ||
if (this.config.get('entities').length > 0) { | ||
await utils_1.Utils.runSerial(this.config.get('entities'), entity => this.discoverEntity(entity)); | ||
} | ||
else if (preferTsNode && this.config.get('tsNode', utils_1.Utils.detectTsNode())) { | ||
await utils_1.Utils.runSerial(this.config.get('entitiesDirsTs'), dir => this.discoverDirectory(dir)); | ||
} | ||
else { | ||
await utils_1.Utils.runSerial(this.config.get('entitiesDirs'), dir => this.discoverDirectory(dir)); | ||
} | ||
await this.discoverDirectories(paths); | ||
await this.discoverReferences(refs); | ||
this.validator.validateDiscovered(this.discovered, this.config.get('discovery').warnWhenNoEntities); | ||
return this.discovered; | ||
} | ||
async discoverDirectory(basePath) { | ||
const files = await globby_1.default(utils_1.Utils.normalizePath(basePath, '*'), { cwd: utils_1.Utils.normalizePath(this.config.get('baseDir')) }); | ||
this.logger.log('discovery', `- processing ${chalk_1.default.cyan(files.length)} files from directory ${chalk_1.default.cyan(basePath)}`); | ||
async discoverDirectories(paths) { | ||
if (paths.length === 0) { | ||
return; | ||
} | ||
paths = paths.map(path => utils_1.Utils.normalizePath(path)); | ||
const files = await globby_1.default(paths, { cwd: utils_1.Utils.normalizePath(this.config.get('baseDir')) }); | ||
this.logger.log('discovery', `- processing ${chalk_1.default.cyan(files.length)} files`); | ||
const found = []; | ||
for (const filepath of files) { | ||
@@ -96,5 +97,24 @@ const filename = path_1.basename(filepath); | ||
this.metadata.set(name, utils_1.Utils.copy(MetadataStorage_1.MetadataStorage.getMetadata(name, path))); | ||
await this.discoverEntity(target, path); | ||
const entity = this.prepare(target); | ||
const schema = this.getSchema(entity); | ||
const meta = schema.init().meta; | ||
this.metadata.set(meta.className, meta); | ||
found.push([entity, path]); | ||
} | ||
for (const [entity, path] of found) { | ||
await this.discoverEntity(entity, path); | ||
} | ||
} | ||
async discoverReferences(refs) { | ||
const found = []; | ||
for (const entity of refs) { | ||
const schema = this.getSchema(entity); | ||
const meta = schema.init().meta; | ||
this.metadata.set(meta.className, meta); | ||
found.push(entity); | ||
} | ||
for (const entity of found) { | ||
await this.discoverEntity(entity); | ||
} | ||
} | ||
prepare(entity) { | ||
@@ -113,2 +133,3 @@ if ('schema' in entity && entity.schema instanceof metadata_1.EntitySchema) { | ||
getSchema(entity) { | ||
var _a; | ||
if (entity instanceof metadata_1.EntitySchema) { | ||
@@ -123,3 +144,6 @@ return entity; | ||
} | ||
const schema = metadata_1.EntitySchema.fromMetadata(this.metadata.get(entity.name, true)); | ||
const exists = this.metadata.has(entity.name); | ||
const meta = this.metadata.get(entity.name, true); | ||
meta.abstract = (_a = meta.abstract) !== null && _a !== void 0 ? _a : !(exists && meta.name); | ||
const schema = metadata_1.EntitySchema.fromMetadata(meta); | ||
schema.setClass(entity); | ||
@@ -600,3 +624,3 @@ schema.meta.useCache = this.metadataProvider.useCache(); | ||
if (!target) { | ||
throw utils_1.ValidationError.entityNotFound(name, path.replace(this.config.get('baseDir'), '.')); | ||
throw utils_1.MetadataError.entityNotFound(name, path.replace(this.config.get('baseDir'), '.')); | ||
} | ||
@@ -603,0 +627,0 @@ return target; |
import { EntityMetadata, AnyEntity, Dictionary } from '../typings'; | ||
import { EntityManager } from '../EntityManager'; | ||
import { EventSubscriber } from '../events'; | ||
export declare class MetadataStorage { | ||
private static readonly metadata; | ||
private static readonly subscribers; | ||
private readonly metadata; | ||
@@ -10,2 +12,3 @@ constructor(metadata?: Dictionary<EntityMetadata>); | ||
static getMetadataFromDecorator<T = any>(target: T & Dictionary): EntityMetadata<T>; | ||
static getSubscriberMetadata(): Dictionary<EventSubscriber>; | ||
static init(): MetadataStorage; | ||
@@ -12,0 +15,0 @@ getAll(): Dictionary<EntityMetadata>; |
@@ -26,2 +26,5 @@ "use strict"; | ||
} | ||
static getSubscriberMetadata() { | ||
return MetadataStorage.subscribers; | ||
} | ||
static init() { | ||
@@ -35,3 +38,3 @@ return new MetadataStorage(MetadataStorage.metadata); | ||
if (entity && !this.metadata[entity] && validate && !init) { | ||
throw utils_1.ValidationError.missingMetadata(entity); | ||
throw utils_1.MetadataError.missingMetadata(entity); | ||
} | ||
@@ -59,2 +62,3 @@ if (!this.metadata[entity] && init) { | ||
exports.MetadataStorage = MetadataStorage; | ||
MetadataStorage.metadata = {}; | ||
MetadataStorage.metadata = utils_1.Utils.getGlobalStorage('metadata'); | ||
MetadataStorage.subscribers = utils_1.Utils.getGlobalStorage('subscribers'); |
import { EntityMetadata } from '../typings'; | ||
import { MetadataStorage } from './MetadataStorage'; | ||
export declare class MetadataValidator { | ||
static validateSingleDecorator(meta: EntityMetadata, propertyName: string): void; | ||
validateEntityDefinition(metadata: MetadataStorage, name: string): void; | ||
@@ -5,0 +6,0 @@ validateDiscovered(discovered: EntityMetadata[], warnWhenNoEntities: boolean): void; |
@@ -7,2 +7,8 @@ "use strict"; | ||
class MetadataValidator { | ||
static validateSingleDecorator(meta, propertyName) { | ||
var _a; | ||
if ((_a = meta.properties[propertyName]) === null || _a === void 0 ? void 0 : _a.reference) { | ||
throw utils_1.MetadataError.multipleDecorators(meta.className, propertyName); | ||
} | ||
} | ||
validateEntityDefinition(metadata, name) { | ||
@@ -12,3 +18,3 @@ const meta = metadata.get(name); | ||
if (!meta.embeddable && (!meta.primaryKeys || meta.primaryKeys.length === 0)) { | ||
throw utils_1.ValidationError.fromMissingPrimaryKey(meta); | ||
throw utils_1.MetadataError.fromMissingPrimaryKey(meta); | ||
} | ||
@@ -24,7 +30,7 @@ this.validateVersionField(meta); | ||
if (discovered.length === 0 && warnWhenNoEntities) { | ||
throw utils_1.ValidationError.noEntityDiscovered(); | ||
throw utils_1.MetadataError.noEntityDiscovered(); | ||
} | ||
const duplicates = utils_1.Utils.findDuplicates(discovered.map(meta => meta.className)); | ||
if (duplicates.length > 0) { | ||
throw utils_1.ValidationError.duplicateEntityDiscovered(duplicates); | ||
throw utils_1.MetadataError.duplicateEntityDiscovered(duplicates); | ||
} | ||
@@ -34,6 +40,6 @@ // validate base entities | ||
.filter(meta => meta.extends && !discovered.find(m => m.className === meta.extends)) | ||
.forEach(meta => { throw utils_1.ValidationError.fromUnknownBaseEntity(meta); }); | ||
.forEach(meta => { throw utils_1.MetadataError.fromUnknownBaseEntity(meta); }); | ||
// validate we found at least one entity (not just abstract/base entities) | ||
if (discovered.filter(meta => meta.name).length === 0 && warnWhenNoEntities) { | ||
throw utils_1.ValidationError.onlyAbstractEntitiesDiscovered(); | ||
throw utils_1.MetadataError.onlyAbstractEntitiesDiscovered(); | ||
} | ||
@@ -43,3 +49,3 @@ // check for not discovered entities | ||
if (prop.reference !== entity_1.ReferenceType.SCALAR && !discovered.find(m => m.className === prop.type)) { | ||
throw utils_1.ValidationError.fromUnknownEntity(prop.type, `${meta.className}.${prop.name}`); | ||
throw utils_1.MetadataError.fromUnknownEntity(prop.type, `${meta.className}.${prop.name}`); | ||
} | ||
@@ -51,7 +57,7 @@ })); | ||
if (!prop.type) { | ||
throw utils_1.ValidationError.fromWrongTypeDefinition(meta, prop); | ||
throw utils_1.MetadataError.fromWrongTypeDefinition(meta, prop); | ||
} | ||
// references do have type of known entity | ||
if (!metadata.get(prop.type, false, false)) { | ||
throw utils_1.ValidationError.fromWrongTypeDefinition(meta, prop); | ||
throw utils_1.MetadataError.fromWrongTypeDefinition(meta, prop); | ||
} | ||
@@ -72,11 +78,11 @@ } | ||
if (!inverse) { | ||
throw utils_1.ValidationError.fromWrongReference(meta, prop, 'inversedBy'); | ||
throw utils_1.MetadataError.fromWrongReference(meta, prop, 'inversedBy'); | ||
} | ||
// has correct `inversedBy` reference type | ||
if (inverse.type !== meta.name) { | ||
throw utils_1.ValidationError.fromWrongReference(meta, prop, 'inversedBy', inverse); | ||
if (inverse.type !== meta.className) { | ||
throw utils_1.MetadataError.fromWrongReference(meta, prop, 'inversedBy', inverse); | ||
} | ||
// inversed side is not defined as owner | ||
if (inverse.inversedBy) { | ||
throw utils_1.ValidationError.fromWrongOwnership(meta, prop, 'inversedBy'); | ||
throw utils_1.MetadataError.fromWrongOwnership(meta, prop, 'inversedBy'); | ||
} | ||
@@ -87,11 +93,11 @@ } | ||
if (prop.mappedBy && !owner) { | ||
throw utils_1.ValidationError.fromWrongReference(meta, prop, 'mappedBy'); | ||
throw utils_1.MetadataError.fromWrongReference(meta, prop, 'mappedBy'); | ||
} | ||
// has correct `mappedBy` reference type | ||
if (owner.type !== meta.name) { | ||
throw utils_1.ValidationError.fromWrongReference(meta, prop, 'mappedBy', owner); | ||
if (owner.type !== meta.className) { | ||
throw utils_1.MetadataError.fromWrongReference(meta, prop, 'mappedBy', owner); | ||
} | ||
// owning side is not defined as inverse | ||
if (owner.mappedBy) { | ||
throw utils_1.ValidationError.fromWrongOwnership(meta, prop, 'mappedBy'); | ||
throw utils_1.MetadataError.fromWrongOwnership(meta, prop, 'mappedBy'); | ||
} | ||
@@ -105,3 +111,3 @@ } | ||
if (props.length > 1) { | ||
throw utils_1.ValidationError.multipleVersionFields(meta, props.map(p => p.name)); | ||
throw utils_1.MetadataError.multipleVersionFields(meta, props.map(p => p.name)); | ||
} | ||
@@ -111,3 +117,3 @@ const prop = meta.properties[meta.versionProperty]; | ||
if (type !== 'number' && type !== 'date' && !type.startsWith('timestamp') && !type.startsWith('datetime')) { | ||
throw utils_1.ValidationError.invalidVersionFieldType(meta); | ||
throw utils_1.MetadataError.invalidVersionFieldType(meta); | ||
} | ||
@@ -114,0 +120,0 @@ } |
import { NamingStrategy } from './NamingStrategy'; | ||
export declare abstract class AbstractNamingStrategy implements NamingStrategy { | ||
getClassName(file: string, separator?: string): string; | ||
classToMigrationName(timestamp: string): string; | ||
abstract classToTableName(entityName: string): string; | ||
@@ -5,0 +6,0 @@ abstract joinColumnName(propertyName: string): string; |
@@ -10,3 +10,6 @@ "use strict"; | ||
} | ||
classToMigrationName(timestamp) { | ||
return `Migration${timestamp}`; | ||
} | ||
} | ||
exports.AbstractNamingStrategy = AbstractNamingStrategy; |
@@ -11,2 +11,6 @@ export interface NamingStrategy { | ||
/** | ||
* Return a migration name. This name should allow ordering. | ||
*/ | ||
classToMigrationName(timestamp: string): string; | ||
/** | ||
* Return a column name for a property | ||
@@ -13,0 +17,0 @@ */ |
{ | ||
"name": "@mikro-orm/core", | ||
"version": "4.0.0-alpha.3", | ||
"version": "4.0.0-alpha.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.", | ||
@@ -50,10 +50,10 @@ "main": "index.js", | ||
"dependencies": { | ||
"acorn-loose": "^7.0.0", | ||
"acorn-walk": "^7.1.1", | ||
"chalk": "^4.0.0", | ||
"acorn-loose": "^7.1.0", | ||
"acorn-walk": "^7.2.0", | ||
"chalk": "^4.1.0", | ||
"cli-highlight": "^2.1.4", | ||
"clone": "^2.1.2", | ||
"fast-deep-equal": "^3.1.1", | ||
"fs-extra": "^9.0.0", | ||
"globby": "^11.0.0", | ||
"fs-extra": "^9.0.1", | ||
"globby": "^11.0.1", | ||
"reflect-metadata": "^0.1.13", | ||
@@ -60,0 +60,0 @@ "uuid": "^8.1.0" |
import { QueryOrder } from './enums'; | ||
import { AssignOptions, Cascade, Collection, EntityRepository, EntityValidator, IdentifiedReference, Reference, ReferenceType, LoadStrategy } from './entity'; | ||
import { AssignOptions, Cascade, Collection, EntityRepository, EntityValidator, IdentifiedReference, LoadStrategy, Reference, ReferenceType } from './entity'; | ||
import { EntityManager } from './EntityManager'; | ||
@@ -8,2 +8,3 @@ import { LockMode } from './unit-of-work'; | ||
import { Type } from './types'; | ||
import { EventType } from './events'; | ||
export declare type Constructor<T> = new (...args: any[]) => T; | ||
@@ -178,3 +179,2 @@ export declare type Dictionary<T = any> = { | ||
} | ||
export declare type HookType = 'onInit' | 'beforeCreate' | 'afterCreate' | 'beforeUpdate' | 'afterUpdate' | 'beforeDelete' | 'afterDelete'; | ||
export interface EntityMetadata<T extends AnyEntity<T> = any> { | ||
@@ -213,3 +213,3 @@ name: string; | ||
customRepository: () => Constructor<EntityRepository<T>>; | ||
hooks: Partial<Record<HookType, (string & keyof T)[]>>; | ||
hooks: Partial<Record<keyof typeof EventType, (string & keyof T)[]>>; | ||
prototype: T; | ||
@@ -216,0 +216,0 @@ class: Constructor<T>; |
@@ -206,3 +206,3 @@ "use strict"; | ||
.forEach(prop => { | ||
const cs = this.changeSets.find(cs => cs.entity === changeSet.entity[prop.name]); | ||
const cs = this.changeSets.find(cs => cs.entity === utils_1.Utils.unwrapReference(changeSet.entity[prop.name])); | ||
const isScheduledForInsert = cs && cs.type === ChangeSet_1.ChangeSetType.CREATE && !cs.persisted; | ||
@@ -237,3 +237,3 @@ if (isScheduledForInsert) { | ||
if ((((_a = hooks === null || hooks === void 0 ? void 0 : hooks[type]) === null || _a === void 0 ? void 0 : _a.length) || 0) === 0) { | ||
return; | ||
return await this.em.getEventManager().dispatchEvent(type, entity, this.em); | ||
} | ||
@@ -245,2 +245,3 @@ const copy = utils_1.Utils.prepareEntity(entity, this.metadata, this.platform); | ||
} | ||
await this.em.getEventManager().dispatchEvent(type, entity, this.em); | ||
} | ||
@@ -247,0 +248,0 @@ /** |
@@ -5,8 +5,9 @@ import { Theme } from 'cli-highlight'; | ||
import { EntityFactory, EntityRepository } from '../entity'; | ||
import { Dictionary, EntityClass, EntityClassGroup, AnyEntity, IPrimaryKey, Constructor } from '../typings'; | ||
import { AnyEntity, Constructor, Dictionary, EntityClass, EntityClassGroup, IPrimaryKey } from '../typings'; | ||
import { Hydrator, ObjectHydrator } from '../hydration'; | ||
import { Logger, LoggerNamespace, ValidationError } from '../utils'; | ||
import { Logger, LoggerNamespace, NotFoundError } from '../utils'; | ||
import { EntityManager } from '../EntityManager'; | ||
import { EntityOptions, EntitySchema, IDatabaseDriver } from '..'; | ||
import { MetadataProvider, ReflectMetadataProvider } from '../metadata'; | ||
import { EventSubscriber } from '../events'; | ||
export declare class Configuration<D extends IDatabaseDriver = IDatabaseDriver> { | ||
@@ -16,4 +17,4 @@ static readonly DEFAULTS: { | ||
entities: never[]; | ||
entitiesDirs: never[]; | ||
entitiesDirsTs: never[]; | ||
entitiesTs: never[]; | ||
subscribers: never[]; | ||
discovery: { | ||
@@ -24,3 +25,2 @@ warnWhenNoEntities: boolean; | ||
disableDynamicFileAccess: boolean; | ||
tsConfigPath: string; | ||
}; | ||
@@ -32,3 +32,3 @@ strict: boolean; | ||
}; | ||
findOneOrFailHandler: (entityName: string, where: Dictionary | IPrimaryKey) => ValidationError<AnyEntity<any, string | number | symbol>>; | ||
findOneOrFailHandler: (entityName: string, where: Dictionary | IPrimaryKey) => NotFoundError<AnyEntity<any, string | number | symbol>>; | ||
baseDir: string; | ||
@@ -53,2 +53,3 @@ hydrator: typeof ObjectHydrator; | ||
emit: string; | ||
fileName: (timestamp: string) => string; | ||
}; | ||
@@ -157,2 +158,3 @@ cache: { | ||
emit?: 'js' | 'ts'; | ||
fileName?: (timestamp: string) => string; | ||
}; | ||
@@ -181,5 +183,5 @@ export interface PoolConfig { | ||
export interface MikroORMOptions<D extends IDatabaseDriver = IDatabaseDriver> extends ConnectionOptions { | ||
entities: (EntityClass<AnyEntity> | EntityClassGroup<AnyEntity> | EntitySchema<any>)[]; | ||
entitiesDirs: string[]; | ||
entitiesDirsTs: string[]; | ||
entities: (string | EntityClass<AnyEntity> | EntityClassGroup<AnyEntity> | EntitySchema<any>)[]; | ||
entitiesTs: (string | EntityClass<AnyEntity> | EntityClassGroup<AnyEntity> | EntitySchema<any>)[]; | ||
subscribers: EventSubscriber[]; | ||
discovery: { | ||
@@ -190,3 +192,2 @@ warnWhenNoEntities?: boolean; | ||
disableDynamicFileAccess?: boolean; | ||
tsConfigPath?: string; | ||
}; | ||
@@ -193,0 +194,0 @@ type?: keyof typeof Configuration.PLATFORMS; |
@@ -9,2 +9,3 @@ "use strict"; | ||
const utils_1 = require("../utils"); | ||
const __1 = require(".."); | ||
const metadata_1 = require("../metadata"); | ||
@@ -114,8 +115,7 @@ class Configuration { | ||
} | ||
if (this.options.entitiesDirsTs.length === 0) { | ||
this.options.entitiesDirsTs = this.options.entitiesDirs; | ||
} | ||
if (!this.options.charset) { | ||
this.options.charset = this.platform.getDefaultCharset(); | ||
} | ||
const subscribers = Object.values(__1.MetadataStorage.getSubscriberMetadata()); | ||
this.options.subscribers = [...new Set([...this.options.subscribers, ...subscribers])]; | ||
} | ||
@@ -129,9 +129,5 @@ validateOptions() { | ||
} | ||
if (this.options.entities.length === 0 && this.options.entitiesDirs.length === 0 && this.options.discovery.warnWhenNoEntities) { | ||
throw new Error('No entities found, please use `entities` or `entitiesDirs` option'); | ||
if (this.options.entities.length === 0 && this.options.discovery.warnWhenNoEntities) { | ||
throw new Error('No entities found, please use `entities` option'); | ||
} | ||
const notDirectory = this.options.entitiesDirs.find(dir => dir.match(/\.[jt]s$/)); | ||
if (notDirectory) { | ||
throw new Error(`Please provide path to directory in \`entitiesDirs\`, found: '${notDirectory}'`); | ||
} | ||
} | ||
@@ -158,4 +154,4 @@ initDriver() { | ||
entities: [], | ||
entitiesDirs: [], | ||
entitiesDirsTs: [], | ||
entitiesTs: [], | ||
subscribers: [], | ||
discovery: { | ||
@@ -166,3 +162,2 @@ warnWhenNoEntities: true, | ||
disableDynamicFileAccess: false, | ||
tsConfigPath: process.cwd() + '/tsconfig.json', | ||
}, | ||
@@ -172,3 +167,3 @@ strict: false, | ||
logger: console.log.bind(console), | ||
findOneOrFailHandler: (entityName, where) => utils_1.ValidationError.findOneFailed(entityName, where), | ||
findOneOrFailHandler: (entityName, where) => utils_1.NotFoundError.findOneFailed(entityName, where), | ||
baseDir: process.cwd(), | ||
@@ -193,2 +188,3 @@ hydrator: hydration_1.ObjectHydrator, | ||
emit: 'ts', | ||
fileName: (timestamp) => `Migration${timestamp}`, | ||
}, | ||
@@ -195,0 +191,0 @@ cache: { |
@@ -52,3 +52,6 @@ "use strict"; | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
require('ts-node').register({ project: tsConfigPath }); | ||
require('ts-node').register({ | ||
project: tsConfigPath, | ||
transpileOnly: true, | ||
}); | ||
if (await fs_extra_1.pathExists(tsConfigPath)) { | ||
@@ -58,3 +61,3 @@ // eslint-disable-next-line @typescript-eslint/no-var-requires | ||
/* istanbul ignore next */ | ||
const paths = (_a = tsConfig === null || tsConfig === void 0 ? void 0 : tsConfig.compilerOptions) === null || _a === void 0 ? void 0 : _a.paths; | ||
const paths = (_a = tsConfig.compilerOptions) === null || _a === void 0 ? void 0 : _a.paths; | ||
if (paths) { | ||
@@ -61,0 +64,0 @@ // eslint-disable-next-line @typescript-eslint/no-var-requires |
export * from './Configuration'; | ||
export * from './ConfigurationLoader'; | ||
export * from './ValidationError'; | ||
export * from './Logger'; | ||
@@ -8,1 +7,2 @@ export * from './Utils'; | ||
export * from './SmartQueryHelper'; | ||
export * from './errors'; |
@@ -15,3 +15,2 @@ "use strict"; | ||
__exportStar(require("./ConfigurationLoader"), exports); | ||
__exportStar(require("./ValidationError"), exports); | ||
__exportStar(require("./Logger"), exports); | ||
@@ -21,1 +20,2 @@ __exportStar(require("./Utils"), exports); | ||
__exportStar(require("./SmartQueryHelper"), exports); | ||
__exportStar(require("./errors"), exports); |
@@ -152,2 +152,3 @@ import { GlobbyOptions } from 'globby'; | ||
static isOperator(key: string, includeGroupOperators?: boolean): boolean; | ||
static getGlobalStorage(namespace: string): Dictionary; | ||
} |
@@ -224,3 +224,3 @@ "use strict"; | ||
} | ||
return Utils.isString(key) || typeof key === 'number' || Utils.isObjectID(key); | ||
return Utils.isString(key) || typeof key === 'number' || Utils.isObjectID(key) || key instanceof Date; | ||
} | ||
@@ -429,3 +429,3 @@ /** | ||
path = path_1.normalize(path).replace(/\\/g, '/'); | ||
return path.match(/^[/.]|[a-zA-Z]:/) ? path : './' + path; | ||
return (path.match(/^[/.]|[a-zA-Z]:/) || path.startsWith('!')) ? path : './' + path; | ||
} | ||
@@ -499,3 +499,8 @@ static relativePath(path, relativeTo) { | ||
} | ||
static getGlobalStorage(namespace) { | ||
const key = `mikro-orm-${namespace}`; | ||
global[key] = global[key] || {}; | ||
return global[key]; | ||
} | ||
} | ||
exports.Utils = Utils; |
405311
191
9272
Updatedacorn-loose@^7.1.0
Updatedacorn-walk@^7.2.0
Updatedchalk@^4.1.0
Updatedfs-extra@^9.0.1
Updatedglobby@^11.0.1