@yandjin-mikro-orm/core 6.1.4-rc-sti-changes-1
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.
- package/EntityManager.d.ts +553 -0
- package/EntityManager.js +1736 -0
- package/LICENSE +21 -0
- package/MikroORM.d.ts +102 -0
- package/MikroORM.js +258 -0
- package/README.md +383 -0
- package/cache/CacheAdapter.d.ts +40 -0
- package/cache/CacheAdapter.js +2 -0
- package/cache/FileCacheAdapter.d.ts +31 -0
- package/cache/FileCacheAdapter.js +90 -0
- package/cache/GeneratedCacheAdapter.d.ts +25 -0
- package/cache/GeneratedCacheAdapter.js +38 -0
- package/cache/MemoryCacheAdapter.d.ts +24 -0
- package/cache/MemoryCacheAdapter.js +44 -0
- package/cache/NullCacheAdapter.d.ts +19 -0
- package/cache/NullCacheAdapter.js +30 -0
- package/cache/index.d.ts +5 -0
- package/cache/index.js +21 -0
- package/connections/Connection.d.ts +85 -0
- package/connections/Connection.js +128 -0
- package/connections/index.d.ts +1 -0
- package/connections/index.js +17 -0
- package/decorators/Check.d.ts +3 -0
- package/decorators/Check.js +17 -0
- package/decorators/CreateRequestContext.d.ts +2 -0
- package/decorators/CreateRequestContext.js +31 -0
- package/decorators/Embeddable.d.ts +8 -0
- package/decorators/Embeddable.js +15 -0
- package/decorators/Embedded.d.ts +13 -0
- package/decorators/Embedded.js +21 -0
- package/decorators/EnsureRequestContext.d.ts +2 -0
- package/decorators/EnsureRequestContext.js +26 -0
- package/decorators/Entity.d.ts +18 -0
- package/decorators/Entity.js +17 -0
- package/decorators/Enum.d.ts +9 -0
- package/decorators/Enum.js +20 -0
- package/decorators/Filter.d.ts +2 -0
- package/decorators/Filter.js +12 -0
- package/decorators/Formula.d.ts +5 -0
- package/decorators/Formula.js +19 -0
- package/decorators/Indexed.d.ts +12 -0
- package/decorators/Indexed.js +25 -0
- package/decorators/ManyToMany.d.ts +21 -0
- package/decorators/ManyToMany.js +17 -0
- package/decorators/ManyToOne.d.ts +15 -0
- package/decorators/ManyToOne.js +17 -0
- package/decorators/OneToMany.d.ts +18 -0
- package/decorators/OneToMany.js +21 -0
- package/decorators/OneToOne.d.ts +12 -0
- package/decorators/OneToOne.js +11 -0
- package/decorators/PrimaryKey.d.ts +8 -0
- package/decorators/PrimaryKey.js +24 -0
- package/decorators/Property.d.ts +218 -0
- package/decorators/Property.js +35 -0
- package/decorators/Subscriber.d.ts +1 -0
- package/decorators/Subscriber.js +2 -0
- package/decorators/hooks.d.ts +16 -0
- package/decorators/hooks.js +60 -0
- package/decorators/index.d.ts +17 -0
- package/decorators/index.js +36 -0
- package/drivers/DatabaseDriver.d.ts +72 -0
- package/drivers/DatabaseDriver.js +358 -0
- package/drivers/IDatabaseDriver.d.ts +193 -0
- package/drivers/IDatabaseDriver.js +4 -0
- package/drivers/index.d.ts +2 -0
- package/drivers/index.js +18 -0
- package/entity/ArrayCollection.d.ts +113 -0
- package/entity/ArrayCollection.js +386 -0
- package/entity/BaseEntity.d.ts +22 -0
- package/entity/BaseEntity.js +47 -0
- package/entity/Collection.d.ts +104 -0
- package/entity/Collection.js +373 -0
- package/entity/EntityAssigner.d.ts +28 -0
- package/entity/EntityAssigner.js +226 -0
- package/entity/EntityFactory.d.ts +41 -0
- package/entity/EntityFactory.js +302 -0
- package/entity/EntityHelper.d.ts +29 -0
- package/entity/EntityHelper.js +250 -0
- package/entity/EntityIdentifier.d.ts +10 -0
- package/entity/EntityIdentifier.js +19 -0
- package/entity/EntityLoader.d.ts +65 -0
- package/entity/EntityLoader.js +579 -0
- package/entity/EntityRepository.d.ts +161 -0
- package/entity/EntityRepository.js +207 -0
- package/entity/EntityValidator.d.ts +19 -0
- package/entity/EntityValidator.js +152 -0
- package/entity/Reference.d.ts +89 -0
- package/entity/Reference.js +242 -0
- package/entity/WrappedEntity.d.ts +67 -0
- package/entity/WrappedEntity.js +146 -0
- package/entity/index.d.ts +13 -0
- package/entity/index.js +29 -0
- package/entity/wrap.d.ts +15 -0
- package/entity/wrap.js +26 -0
- package/enums.d.ts +160 -0
- package/enums.js +169 -0
- package/errors.d.ts +65 -0
- package/errors.js +222 -0
- package/events/EventManager.d.ts +17 -0
- package/events/EventManager.js +77 -0
- package/events/EventSubscriber.d.ts +39 -0
- package/events/EventSubscriber.js +2 -0
- package/events/TransactionEventBroadcaster.d.ts +11 -0
- package/events/TransactionEventBroadcaster.js +17 -0
- package/events/index.d.ts +3 -0
- package/events/index.js +19 -0
- package/exceptions.d.ts +104 -0
- package/exceptions.js +130 -0
- package/hydration/Hydrator.d.ts +23 -0
- package/hydration/Hydrator.js +51 -0
- package/hydration/ObjectHydrator.d.ts +24 -0
- package/hydration/ObjectHydrator.js +332 -0
- package/hydration/index.d.ts +2 -0
- package/hydration/index.js +18 -0
- package/index.d.ts +25 -0
- package/index.js +50 -0
- package/index.mjs +192 -0
- package/logging/DefaultLogger.d.ts +32 -0
- package/logging/DefaultLogger.js +90 -0
- package/logging/Logger.d.ts +56 -0
- package/logging/Logger.js +2 -0
- package/logging/SimpleLogger.d.ts +17 -0
- package/logging/SimpleLogger.js +31 -0
- package/logging/colors.d.ts +9 -0
- package/logging/colors.js +20 -0
- package/logging/index.d.ts +4 -0
- package/logging/index.js +20 -0
- package/metadata/EntitySchema.d.ts +74 -0
- package/metadata/EntitySchema.js +293 -0
- package/metadata/MetadataDiscovery.d.ts +71 -0
- package/metadata/MetadataDiscovery.js +1244 -0
- package/metadata/MetadataProvider.d.ts +11 -0
- package/metadata/MetadataProvider.js +23 -0
- package/metadata/MetadataStorage.d.ts +22 -0
- package/metadata/MetadataStorage.js +87 -0
- package/metadata/MetadataValidator.d.ts +24 -0
- package/metadata/MetadataValidator.js +213 -0
- package/metadata/ReflectMetadataProvider.d.ts +8 -0
- package/metadata/ReflectMetadataProvider.js +48 -0
- package/metadata/index.d.ts +6 -0
- package/metadata/index.js +22 -0
- package/naming-strategy/AbstractNamingStrategy.d.ts +18 -0
- package/naming-strategy/AbstractNamingStrategy.js +48 -0
- package/naming-strategy/EntityCaseNamingStrategy.d.ts +12 -0
- package/naming-strategy/EntityCaseNamingStrategy.js +32 -0
- package/naming-strategy/MongoNamingStrategy.d.ts +9 -0
- package/naming-strategy/MongoNamingStrategy.js +25 -0
- package/naming-strategy/NamingStrategy.d.ts +52 -0
- package/naming-strategy/NamingStrategy.js +2 -0
- package/naming-strategy/UnderscoreNamingStrategy.d.ts +10 -0
- package/naming-strategy/UnderscoreNamingStrategy.js +28 -0
- package/naming-strategy/index.d.ts +5 -0
- package/naming-strategy/index.js +21 -0
- package/package.json +70 -0
- package/platforms/ExceptionConverter.d.ts +5 -0
- package/platforms/ExceptionConverter.js +11 -0
- package/platforms/Platform.d.ts +201 -0
- package/platforms/Platform.js +452 -0
- package/platforms/index.d.ts +2 -0
- package/platforms/index.js +18 -0
- package/serialization/EntitySerializer.d.ts +34 -0
- package/serialization/EntitySerializer.js +206 -0
- package/serialization/EntityTransformer.d.ts +8 -0
- package/serialization/EntityTransformer.js +192 -0
- package/serialization/SerializationContext.d.ts +30 -0
- package/serialization/SerializationContext.js +111 -0
- package/serialization/index.d.ts +3 -0
- package/serialization/index.js +19 -0
- package/types/ArrayType.d.ts +13 -0
- package/types/ArrayType.js +47 -0
- package/types/BigIntType.d.ts +16 -0
- package/types/BigIntType.js +45 -0
- package/types/BlobType.d.ts +10 -0
- package/types/BlobType.js +27 -0
- package/types/BooleanType.d.ts +8 -0
- package/types/BooleanType.js +16 -0
- package/types/DateTimeType.d.ts +8 -0
- package/types/DateTimeType.js +16 -0
- package/types/DateType.d.ts +8 -0
- package/types/DateType.js +16 -0
- package/types/DecimalType.d.ts +11 -0
- package/types/DecimalType.js +22 -0
- package/types/DoubleType.d.ts +11 -0
- package/types/DoubleType.js +22 -0
- package/types/EnumArrayType.d.ts +9 -0
- package/types/EnumArrayType.js +32 -0
- package/types/EnumType.d.ts +8 -0
- package/types/EnumType.js +16 -0
- package/types/FloatType.d.ts +8 -0
- package/types/FloatType.js +16 -0
- package/types/IntegerType.d.ts +8 -0
- package/types/IntegerType.js +16 -0
- package/types/IntervalType.d.ts +8 -0
- package/types/IntervalType.js +16 -0
- package/types/JsonType.d.ts +13 -0
- package/types/JsonType.js +34 -0
- package/types/MediumIntType.d.ts +6 -0
- package/types/MediumIntType.js +10 -0
- package/types/SmallIntType.d.ts +8 -0
- package/types/SmallIntType.js +16 -0
- package/types/StringType.d.ts +8 -0
- package/types/StringType.js +16 -0
- package/types/TextType.d.ts +8 -0
- package/types/TextType.js +16 -0
- package/types/TimeType.d.ts +9 -0
- package/types/TimeType.js +23 -0
- package/types/TinyIntType.d.ts +8 -0
- package/types/TinyIntType.js +16 -0
- package/types/Type.d.ts +64 -0
- package/types/Type.js +84 -0
- package/types/Uint8ArrayType.d.ts +11 -0
- package/types/Uint8ArrayType.js +37 -0
- package/types/UnknownType.d.ts +7 -0
- package/types/UnknownType.js +13 -0
- package/types/UuidType.d.ts +7 -0
- package/types/UuidType.js +13 -0
- package/types/index.d.ts +75 -0
- package/types/index.js +77 -0
- package/typings.d.ts +767 -0
- package/typings.js +198 -0
- package/unit-of-work/ChangeSet.d.ts +34 -0
- package/unit-of-work/ChangeSet.js +62 -0
- package/unit-of-work/ChangeSetComputer.d.ts +26 -0
- package/unit-of-work/ChangeSetComputer.js +153 -0
- package/unit-of-work/ChangeSetPersister.d.ts +50 -0
- package/unit-of-work/ChangeSetPersister.js +361 -0
- package/unit-of-work/CommitOrderCalculator.d.ts +62 -0
- package/unit-of-work/CommitOrderCalculator.js +113 -0
- package/unit-of-work/IdentityMap.d.ts +17 -0
- package/unit-of-work/IdentityMap.js +84 -0
- package/unit-of-work/UnitOfWork.d.ts +124 -0
- package/unit-of-work/UnitOfWork.js +1013 -0
- package/unit-of-work/index.d.ts +6 -0
- package/unit-of-work/index.js +22 -0
- package/utils/AbstractSchemaGenerator.d.ts +38 -0
- package/utils/AbstractSchemaGenerator.js +101 -0
- package/utils/Configuration.d.ts +390 -0
- package/utils/Configuration.js +357 -0
- package/utils/ConfigurationLoader.d.ts +29 -0
- package/utils/ConfigurationLoader.js +282 -0
- package/utils/Cursor.d.ts +77 -0
- package/utils/Cursor.js +169 -0
- package/utils/DataloaderUtils.d.ts +43 -0
- package/utils/DataloaderUtils.js +194 -0
- package/utils/EntityComparator.d.ts +73 -0
- package/utils/EntityComparator.js +568 -0
- package/utils/NullHighlighter.d.ts +4 -0
- package/utils/NullHighlighter.js +9 -0
- package/utils/QueryHelper.d.ts +28 -0
- package/utils/QueryHelper.js +228 -0
- package/utils/RawQueryFragment.d.ts +96 -0
- package/utils/RawQueryFragment.js +188 -0
- package/utils/RequestContext.d.ts +34 -0
- package/utils/RequestContext.js +54 -0
- package/utils/TransactionContext.d.ts +19 -0
- package/utils/TransactionContext.js +34 -0
- package/utils/Utils.d.ts +274 -0
- package/utils/Utils.js +1073 -0
- package/utils/clone.d.ts +6 -0
- package/utils/clone.js +127 -0
- package/utils/index.d.ts +13 -0
- package/utils/index.js +29 -0
- package/utils/upsert-utils.d.ts +6 -0
- package/utils/upsert-utils.js +33 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import type { CreateOptions, EntityManager, MergeOptions } from '../EntityManager';
|
|
2
|
+
import type { AssignOptions } from './EntityAssigner';
|
|
3
|
+
import type { EntityData, EntityName, Primary, Loaded, FilterQuery, EntityDictionary, AutoPath, RequiredEntityData, Ref, EntityType, EntityDTO, MergeSelected, FromEntityType, IsSubset, MergeLoaded, ArrayElement } from '../typings';
|
|
4
|
+
import type { CountOptions, DeleteOptions, FindAllOptions, FindByCursorOptions, FindOneOptions, FindOneOrFailOptions, FindOptions, GetReferenceOptions, NativeInsertUpdateOptions, UpdateOptions } from '../drivers/IDatabaseDriver';
|
|
5
|
+
import type { EntityLoaderOptions } from './EntityLoader';
|
|
6
|
+
import type { Cursor } from '../utils/Cursor';
|
|
7
|
+
export declare class EntityRepository<Entity extends object> {
|
|
8
|
+
protected readonly em: EntityManager;
|
|
9
|
+
protected readonly entityName: EntityName<Entity>;
|
|
10
|
+
constructor(em: EntityManager, entityName: EntityName<Entity>);
|
|
11
|
+
/**
|
|
12
|
+
* Finds first entity matching your `where` query.
|
|
13
|
+
*/
|
|
14
|
+
findOne<Hint extends string = never, Fields extends string = '*', Excludes extends string = never>(where: FilterQuery<Entity>, options?: FindOneOptions<Entity, Hint, Fields, Excludes>): Promise<Loaded<Entity, Hint, Fields, Excludes> | null>;
|
|
15
|
+
/**
|
|
16
|
+
* Finds first entity matching your `where` query. If nothing is found, it will throw an error.
|
|
17
|
+
* You can override the factory for creating this method via `options.failHandler` locally
|
|
18
|
+
* or via `Configuration.findOneOrFailHandler` globally.
|
|
19
|
+
*/
|
|
20
|
+
findOneOrFail<Hint extends string = never, Fields extends string = '*', Excludes extends string = never>(where: FilterQuery<Entity>, options?: FindOneOrFailOptions<Entity, Hint, Fields, Excludes>): Promise<Loaded<Entity, Hint, Fields, Excludes>>;
|
|
21
|
+
/**
|
|
22
|
+
* Creates or updates the entity, based on whether it is already present in the database.
|
|
23
|
+
* This method performs an `insert on conflict merge` query ensuring the database is in sync, returning a managed
|
|
24
|
+
* entity instance. The method accepts either `entityName` together with the entity `data`, or just entity instance.
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* // insert into "author" ("age", "email") values (33, 'foo@bar.com') on conflict ("email") do update set "age" = 41
|
|
28
|
+
* const author = await em.getRepository(Author).upsert({ email: 'foo@bar.com', age: 33 });
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* The entity data needs to contain either the primary key, or any other unique property. Let's consider the following example, where `Author.email` is a unique property:
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* // insert into "author" ("age", "email") values (33, 'foo@bar.com') on conflict ("email") do update set "age" = 41
|
|
35
|
+
* // select "id" from "author" where "email" = 'foo@bar.com'
|
|
36
|
+
* const author = await em.getRepository(Author).upsert({ email: 'foo@bar.com', age: 33 });
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* Depending on the driver support, this will either use a returning query, or a separate select query, to fetch the primary key if it's missing from the `data`.
|
|
40
|
+
*
|
|
41
|
+
* If the entity is already present in current context, there won't be any queries - instead, the entity data will be assigned and an explicit `flush` will be required for those changes to be persisted.
|
|
42
|
+
*/
|
|
43
|
+
upsert(entityOrData?: EntityData<Entity> | Entity, options?: NativeInsertUpdateOptions<Entity>): Promise<Entity>;
|
|
44
|
+
/**
|
|
45
|
+
* Creates or updates the entity, based on whether it is already present in the database.
|
|
46
|
+
* This method performs an `insert on conflict merge` query ensuring the database is in sync, returning a managed
|
|
47
|
+
* entity instance.
|
|
48
|
+
*
|
|
49
|
+
* ```ts
|
|
50
|
+
* // insert into "author" ("age", "email") values (33, 'foo@bar.com') on conflict ("email") do update set "age" = 41
|
|
51
|
+
* const authors = await em.getRepository(Author).upsertMany([{ email: 'foo@bar.com', age: 33 }, ...]);
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* The entity data needs to contain either the primary key, or any other unique property. Let's consider the following example, where `Author.email` is a unique property:
|
|
55
|
+
*
|
|
56
|
+
* ```ts
|
|
57
|
+
* // insert into "author" ("age", "email") values (33, 'foo@bar.com'), (666, 'lol@lol.lol') on conflict ("email") do update set "age" = excluded."age"
|
|
58
|
+
* // select "id" from "author" where "email" = 'foo@bar.com'
|
|
59
|
+
* const author = await em.getRepository(Author).upsertMany([
|
|
60
|
+
* { email: 'foo@bar.com', age: 33 },
|
|
61
|
+
* { email: 'lol@lol.lol', age: 666 },
|
|
62
|
+
* ]);
|
|
63
|
+
* ```
|
|
64
|
+
*
|
|
65
|
+
* Depending on the driver support, this will either use a returning query, or a separate select query, to fetch the primary key if it's missing from the `data`.
|
|
66
|
+
*
|
|
67
|
+
* If the entity is already present in current context, there won't be any queries - instead, the entity data will be assigned and an explicit `flush` will be required for those changes to be persisted.
|
|
68
|
+
*/
|
|
69
|
+
upsertMany(entitiesOrData?: EntityData<Entity>[] | Entity[], options?: NativeInsertUpdateOptions<Entity>): Promise<Entity[]>;
|
|
70
|
+
/**
|
|
71
|
+
* Finds all entities matching your `where` query. You can pass additional options via the `options` parameter.
|
|
72
|
+
*/
|
|
73
|
+
find<Hint extends string = never, Fields extends string = '*', Excludes extends string = never>(where: FilterQuery<Entity>, options?: FindOptions<Entity, Hint, Fields, Excludes>): Promise<Loaded<Entity, Hint, Fields, Excludes>[]>;
|
|
74
|
+
/**
|
|
75
|
+
* Calls `em.find()` and `em.count()` with the same arguments (where applicable) and returns the results as tuple
|
|
76
|
+
* where first element is the array of entities, and the second is the count.
|
|
77
|
+
*/
|
|
78
|
+
findAndCount<Hint extends string = never, Fields extends string = '*', Excludes extends string = never>(where: FilterQuery<Entity>, options?: FindOptions<Entity, Hint, Fields, Excludes>): Promise<[Loaded<Entity, Hint, Fields, Excludes>[], number]>;
|
|
79
|
+
/**
|
|
80
|
+
* @inheritDoc EntityManager.findByCursor
|
|
81
|
+
*/
|
|
82
|
+
findByCursor<Hint extends string = never, Fields extends string = '*', Excludes extends string = never>(where: FilterQuery<Entity>, options: FindByCursorOptions<Entity, Hint, Fields, Excludes>): Promise<Cursor<Entity, Hint, Fields, Excludes>>;
|
|
83
|
+
/**
|
|
84
|
+
* Finds all entities of given type. You can pass additional options via the `options` parameter.
|
|
85
|
+
*/
|
|
86
|
+
findAll<Hint extends string = never, Fields extends string = '*', Excludes extends string = never>(options?: FindAllOptions<Entity, Hint, Fields, Excludes>): Promise<Loaded<Entity, Hint, Fields, Excludes>[]>;
|
|
87
|
+
/**
|
|
88
|
+
* @inheritDoc EntityManager.insert
|
|
89
|
+
*/
|
|
90
|
+
insert(data: Entity | RequiredEntityData<Entity>, options?: NativeInsertUpdateOptions<Entity>): Promise<Primary<Entity>>;
|
|
91
|
+
/**
|
|
92
|
+
* @inheritDoc EntityManager.insert
|
|
93
|
+
*/
|
|
94
|
+
insertMany(data: Entity[] | RequiredEntityData<Entity>[], options?: NativeInsertUpdateOptions<Entity>): Promise<Primary<Entity>[]>;
|
|
95
|
+
/**
|
|
96
|
+
* Fires native update query. Calling this has no side effects on the context (identity map).
|
|
97
|
+
*/
|
|
98
|
+
nativeUpdate(where: FilterQuery<Entity>, data: EntityData<Entity>, options?: UpdateOptions<Entity>): Promise<number>;
|
|
99
|
+
/**
|
|
100
|
+
* Fires native delete query. Calling this has no side effects on the context (identity map).
|
|
101
|
+
*/
|
|
102
|
+
nativeDelete(where: FilterQuery<Entity>, options?: DeleteOptions<Entity>): Promise<number>;
|
|
103
|
+
/**
|
|
104
|
+
* Maps raw database result to an entity and merges it to this EntityManager.
|
|
105
|
+
*/
|
|
106
|
+
map(result: EntityDictionary<Entity>, options?: {
|
|
107
|
+
schema?: string;
|
|
108
|
+
}): Entity;
|
|
109
|
+
/**
|
|
110
|
+
* Gets a reference to the entity identified by the given type and identifier without actually loading it, if the entity is not yet loaded
|
|
111
|
+
*/
|
|
112
|
+
getReference(id: Primary<Entity>, options: Omit<GetReferenceOptions, 'wrapped'> & {
|
|
113
|
+
wrapped: true;
|
|
114
|
+
}): Ref<Entity>;
|
|
115
|
+
/**
|
|
116
|
+
* Gets a reference to the entity identified by the given type and identifier without actually loading it, if the entity is not yet loaded
|
|
117
|
+
*/
|
|
118
|
+
getReference(id: Primary<Entity> | Primary<Entity>[]): Entity;
|
|
119
|
+
/**
|
|
120
|
+
* Gets a reference to the entity identified by the given type and identifier without actually loading it, if the entity is not yet loaded
|
|
121
|
+
*/
|
|
122
|
+
getReference(id: Primary<Entity>, options: Omit<GetReferenceOptions, 'wrapped'> & {
|
|
123
|
+
wrapped: false;
|
|
124
|
+
}): Entity;
|
|
125
|
+
/**
|
|
126
|
+
* Checks whether given property can be populated on the entity.
|
|
127
|
+
*/
|
|
128
|
+
canPopulate(property: string): boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Loads specified relations in batch. This will execute one query for each relation, that will populate it on all the specified entities.
|
|
131
|
+
*/
|
|
132
|
+
populate<Ent extends Entity | Entity[], Hint extends string = never, Naked extends FromEntityType<Entity> = FromEntityType<Entity>, Fields extends string = '*', Excludes extends string = never>(entities: Ent, populate: AutoPath<Entity, Hint, '*'>[] | false, options?: EntityLoaderOptions<Entity, Fields, Excludes>): Promise<Ent extends object[] ? MergeLoaded<ArrayElement<Ent>, Naked, Hint, Fields, Excludes>[] : MergeLoaded<Ent, Naked, Hint, Fields, Excludes>>;
|
|
133
|
+
/**
|
|
134
|
+
* Creates new instance of given entity and populates it with given data.
|
|
135
|
+
* The entity constructor will be used unless you provide `{ managed: true }` in the `options` parameter.
|
|
136
|
+
* The constructor will be given parameters based on the defined constructor of the entity. If the constructor
|
|
137
|
+
* parameter matches a property name, its value will be extracted from `data`. If no matching property exists,
|
|
138
|
+
* the whole `data` parameter will be passed. This means we can also define `constructor(data: Partial<Entity>)` and
|
|
139
|
+
* `em.create()` will pass the data into it (unless we have a property named `data` too).
|
|
140
|
+
*/
|
|
141
|
+
create(data: RequiredEntityData<Entity>, options?: CreateOptions): Entity;
|
|
142
|
+
/**
|
|
143
|
+
* Shortcut for `wrap(entity).assign(data, { em })`
|
|
144
|
+
*/
|
|
145
|
+
assign<Ent extends EntityType<Entity>, Naked extends FromEntityType<Ent> = FromEntityType<Ent>, Data extends EntityData<Naked> | Partial<EntityDTO<Naked>> = EntityData<Naked> | Partial<EntityDTO<Naked>>>(entity: Ent | Partial<Ent>, data: Data & IsSubset<EntityData<Naked>, Data>, options?: AssignOptions): MergeSelected<Ent, Naked, keyof Data & string>;
|
|
146
|
+
/**
|
|
147
|
+
* Merges given entity to this EntityManager so it becomes managed. You can force refreshing of existing entities
|
|
148
|
+
* via second parameter. By default it will return already loaded entities without modifying them.
|
|
149
|
+
*/
|
|
150
|
+
merge(data: Entity | EntityData<Entity>, options?: MergeOptions): Entity;
|
|
151
|
+
/**
|
|
152
|
+
* Returns total number of entities matching your `where` query.
|
|
153
|
+
*/
|
|
154
|
+
count<Hint extends string = never>(where?: FilterQuery<Entity>, options?: CountOptions<Entity, Hint>): Promise<number>;
|
|
155
|
+
getEntityName(): string;
|
|
156
|
+
/**
|
|
157
|
+
* Returns the underlying EntityManager instance
|
|
158
|
+
*/
|
|
159
|
+
getEntityManager(): EntityManager;
|
|
160
|
+
protected validateRepositoryType(entities: Entity[] | Entity, method: string): void;
|
|
161
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EntityRepository = void 0;
|
|
4
|
+
const errors_1 = require("../errors");
|
|
5
|
+
const Utils_1 = require("../utils/Utils");
|
|
6
|
+
class EntityRepository {
|
|
7
|
+
em;
|
|
8
|
+
entityName;
|
|
9
|
+
constructor(em, entityName) {
|
|
10
|
+
this.em = em;
|
|
11
|
+
this.entityName = entityName;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Finds first entity matching your `where` query.
|
|
15
|
+
*/
|
|
16
|
+
async findOne(where, options) {
|
|
17
|
+
return this.getEntityManager().findOne(this.entityName, where, options);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Finds first entity matching your `where` query. If nothing is found, it will throw an error.
|
|
21
|
+
* You can override the factory for creating this method via `options.failHandler` locally
|
|
22
|
+
* or via `Configuration.findOneOrFailHandler` globally.
|
|
23
|
+
*/
|
|
24
|
+
async findOneOrFail(where, options) {
|
|
25
|
+
return this.getEntityManager().findOneOrFail(this.entityName, where, options);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Creates or updates the entity, based on whether it is already present in the database.
|
|
29
|
+
* This method performs an `insert on conflict merge` query ensuring the database is in sync, returning a managed
|
|
30
|
+
* entity instance. The method accepts either `entityName` together with the entity `data`, or just entity instance.
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* // insert into "author" ("age", "email") values (33, 'foo@bar.com') on conflict ("email") do update set "age" = 41
|
|
34
|
+
* const author = await em.getRepository(Author).upsert({ email: 'foo@bar.com', age: 33 });
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* The entity data needs to contain either the primary key, or any other unique property. Let's consider the following example, where `Author.email` is a unique property:
|
|
38
|
+
*
|
|
39
|
+
* ```ts
|
|
40
|
+
* // insert into "author" ("age", "email") values (33, 'foo@bar.com') on conflict ("email") do update set "age" = 41
|
|
41
|
+
* // select "id" from "author" where "email" = 'foo@bar.com'
|
|
42
|
+
* const author = await em.getRepository(Author).upsert({ email: 'foo@bar.com', age: 33 });
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* Depending on the driver support, this will either use a returning query, or a separate select query, to fetch the primary key if it's missing from the `data`.
|
|
46
|
+
*
|
|
47
|
+
* If the entity is already present in current context, there won't be any queries - instead, the entity data will be assigned and an explicit `flush` will be required for those changes to be persisted.
|
|
48
|
+
*/
|
|
49
|
+
async upsert(entityOrData, options) {
|
|
50
|
+
return this.getEntityManager().upsert(this.entityName, entityOrData, options);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Creates or updates the entity, based on whether it is already present in the database.
|
|
54
|
+
* This method performs an `insert on conflict merge` query ensuring the database is in sync, returning a managed
|
|
55
|
+
* entity instance.
|
|
56
|
+
*
|
|
57
|
+
* ```ts
|
|
58
|
+
* // insert into "author" ("age", "email") values (33, 'foo@bar.com') on conflict ("email") do update set "age" = 41
|
|
59
|
+
* const authors = await em.getRepository(Author).upsertMany([{ email: 'foo@bar.com', age: 33 }, ...]);
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* The entity data needs to contain either the primary key, or any other unique property. Let's consider the following example, where `Author.email` is a unique property:
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* // insert into "author" ("age", "email") values (33, 'foo@bar.com'), (666, 'lol@lol.lol') on conflict ("email") do update set "age" = excluded."age"
|
|
66
|
+
* // select "id" from "author" where "email" = 'foo@bar.com'
|
|
67
|
+
* const author = await em.getRepository(Author).upsertMany([
|
|
68
|
+
* { email: 'foo@bar.com', age: 33 },
|
|
69
|
+
* { email: 'lol@lol.lol', age: 666 },
|
|
70
|
+
* ]);
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* Depending on the driver support, this will either use a returning query, or a separate select query, to fetch the primary key if it's missing from the `data`.
|
|
74
|
+
*
|
|
75
|
+
* If the entity is already present in current context, there won't be any queries - instead, the entity data will be assigned and an explicit `flush` will be required for those changes to be persisted.
|
|
76
|
+
*/
|
|
77
|
+
async upsertMany(entitiesOrData, options) {
|
|
78
|
+
return this.getEntityManager().upsertMany(this.entityName, entitiesOrData, options);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Finds all entities matching your `where` query. You can pass additional options via the `options` parameter.
|
|
82
|
+
*/
|
|
83
|
+
async find(where, options) {
|
|
84
|
+
return this.getEntityManager().find(this.entityName, where, options);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Calls `em.find()` and `em.count()` with the same arguments (where applicable) and returns the results as tuple
|
|
88
|
+
* where first element is the array of entities, and the second is the count.
|
|
89
|
+
*/
|
|
90
|
+
async findAndCount(where, options) {
|
|
91
|
+
return this.getEntityManager().findAndCount(this.entityName, where, options);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* @inheritDoc EntityManager.findByCursor
|
|
95
|
+
*/
|
|
96
|
+
async findByCursor(where, options) {
|
|
97
|
+
return this.getEntityManager().findByCursor(this.entityName, where, options);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Finds all entities of given type. You can pass additional options via the `options` parameter.
|
|
101
|
+
*/
|
|
102
|
+
async findAll(options) {
|
|
103
|
+
return this.getEntityManager().findAll(this.entityName, options);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* @inheritDoc EntityManager.insert
|
|
107
|
+
*/
|
|
108
|
+
async insert(data, options) {
|
|
109
|
+
return this.getEntityManager().insert(this.entityName, data, options);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* @inheritDoc EntityManager.insert
|
|
113
|
+
*/
|
|
114
|
+
async insertMany(data, options) {
|
|
115
|
+
return this.getEntityManager().insertMany(this.entityName, data, options);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Fires native update query. Calling this has no side effects on the context (identity map).
|
|
119
|
+
*/
|
|
120
|
+
async nativeUpdate(where, data, options) {
|
|
121
|
+
return this.getEntityManager().nativeUpdate(this.entityName, where, data, options);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Fires native delete query. Calling this has no side effects on the context (identity map).
|
|
125
|
+
*/
|
|
126
|
+
async nativeDelete(where, options) {
|
|
127
|
+
return this.getEntityManager().nativeDelete(this.entityName, where, options);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Maps raw database result to an entity and merges it to this EntityManager.
|
|
131
|
+
*/
|
|
132
|
+
map(result, options) {
|
|
133
|
+
return this.getEntityManager().map(this.entityName, result, options);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Gets a reference to the entity identified by the given type and identifier without actually loading it, if the entity is not yet loaded
|
|
137
|
+
*/
|
|
138
|
+
getReference(id, options) {
|
|
139
|
+
return this.getEntityManager().getReference(this.entityName, id, options);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Checks whether given property can be populated on the entity.
|
|
143
|
+
*/
|
|
144
|
+
canPopulate(property) {
|
|
145
|
+
return this.getEntityManager().canPopulate(this.entityName, property);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Loads specified relations in batch. This will execute one query for each relation, that will populate it on all the specified entities.
|
|
149
|
+
*/
|
|
150
|
+
async populate(entities, populate, options) {
|
|
151
|
+
this.validateRepositoryType(entities, 'populate');
|
|
152
|
+
// @ts-ignore hard to type
|
|
153
|
+
return this.getEntityManager().populate(entities, populate, options);
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Creates new instance of given entity and populates it with given data.
|
|
157
|
+
* The entity constructor will be used unless you provide `{ managed: true }` in the `options` parameter.
|
|
158
|
+
* The constructor will be given parameters based on the defined constructor of the entity. If the constructor
|
|
159
|
+
* parameter matches a property name, its value will be extracted from `data`. If no matching property exists,
|
|
160
|
+
* the whole `data` parameter will be passed. This means we can also define `constructor(data: Partial<Entity>)` and
|
|
161
|
+
* `em.create()` will pass the data into it (unless we have a property named `data` too).
|
|
162
|
+
*/
|
|
163
|
+
create(data, options) {
|
|
164
|
+
return this.getEntityManager().create(this.entityName, data, options);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Shortcut for `wrap(entity).assign(data, { em })`
|
|
168
|
+
*/
|
|
169
|
+
assign(entity, data, options) {
|
|
170
|
+
this.validateRepositoryType(entity, 'assign');
|
|
171
|
+
return this.getEntityManager().assign(entity, data, options);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Merges given entity to this EntityManager so it becomes managed. You can force refreshing of existing entities
|
|
175
|
+
* via second parameter. By default it will return already loaded entities without modifying them.
|
|
176
|
+
*/
|
|
177
|
+
merge(data, options) {
|
|
178
|
+
return this.getEntityManager().merge(this.entityName, data, options);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Returns total number of entities matching your `where` query.
|
|
182
|
+
*/
|
|
183
|
+
async count(where = {}, options = {}) {
|
|
184
|
+
return this.getEntityManager().count(this.entityName, where, options);
|
|
185
|
+
}
|
|
186
|
+
getEntityName() {
|
|
187
|
+
return Utils_1.Utils.className(this.entityName);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Returns the underlying EntityManager instance
|
|
191
|
+
*/
|
|
192
|
+
getEntityManager() {
|
|
193
|
+
return this.em;
|
|
194
|
+
}
|
|
195
|
+
validateRepositoryType(entities, method) {
|
|
196
|
+
entities = Utils_1.Utils.asArray(entities);
|
|
197
|
+
if (entities.length === 0) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const entityName = entities[0].constructor.name;
|
|
201
|
+
const repoType = Utils_1.Utils.className(this.entityName);
|
|
202
|
+
if (entityName && repoType !== entityName) {
|
|
203
|
+
throw errors_1.ValidationError.fromWrongRepositoryType(entityName, repoType, method);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
exports.EntityRepository = EntityRepository;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { EntityData, EntityMetadata, EntityProperty, FilterQuery } from '../typings';
|
|
2
|
+
export declare class EntityValidator {
|
|
3
|
+
private strict;
|
|
4
|
+
KNOWN_TYPES: Set<string>;
|
|
5
|
+
constructor(strict: boolean);
|
|
6
|
+
validate<T extends object>(entity: T, payload: any, meta: EntityMetadata<T>): void;
|
|
7
|
+
validateRequired<T extends object>(entity: T): void;
|
|
8
|
+
validateProperty<T extends object>(prop: EntityProperty, givenValue: any, entity: T): any;
|
|
9
|
+
validateParams(params: any, type?: string, field?: string): void;
|
|
10
|
+
validatePrimaryKey<T>(entity: EntityData<T>, meta: EntityMetadata<T>): void;
|
|
11
|
+
validateEmptyWhere<T>(where: FilterQuery<T>): void;
|
|
12
|
+
private getValue;
|
|
13
|
+
private setValue;
|
|
14
|
+
private validateCollection;
|
|
15
|
+
private fixTypes;
|
|
16
|
+
private fixDateType;
|
|
17
|
+
private fixNumberType;
|
|
18
|
+
private fixBooleanType;
|
|
19
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EntityValidator = void 0;
|
|
4
|
+
const enums_1 = require("../enums");
|
|
5
|
+
const Utils_1 = require("../utils/Utils");
|
|
6
|
+
const errors_1 = require("../errors");
|
|
7
|
+
const wrap_1 = require("./wrap");
|
|
8
|
+
class EntityValidator {
|
|
9
|
+
strict;
|
|
10
|
+
KNOWN_TYPES = new Set(['string', 'number', 'boolean', 'bigint', 'Uint8Array', 'Date', 'Buffer', 'RegExp']);
|
|
11
|
+
constructor(strict) {
|
|
12
|
+
this.strict = strict;
|
|
13
|
+
}
|
|
14
|
+
validate(entity, payload, meta) {
|
|
15
|
+
meta.props.forEach(prop => {
|
|
16
|
+
if (prop.inherited) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
if ([enums_1.ReferenceKind.ONE_TO_MANY, enums_1.ReferenceKind.MANY_TO_MANY].includes(prop.kind)) {
|
|
20
|
+
this.validateCollection(entity, prop);
|
|
21
|
+
}
|
|
22
|
+
const SCALAR_TYPES = ['string', 'number', 'boolean', 'Date'];
|
|
23
|
+
if (prop.kind !== enums_1.ReferenceKind.SCALAR || !SCALAR_TYPES.includes(prop.type)) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const newValue = this.validateProperty(prop, this.getValue(payload, prop), entity);
|
|
27
|
+
if (this.getValue(payload, prop) === newValue) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
this.setValue(payload, prop, newValue);
|
|
31
|
+
/* istanbul ignore else */
|
|
32
|
+
if (entity[prop.name]) {
|
|
33
|
+
entity[prop.name] = payload[prop.name];
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
validateRequired(entity) {
|
|
38
|
+
const wrapped = (0, wrap_1.helper)(entity);
|
|
39
|
+
for (const prop of wrapped.__meta.props) {
|
|
40
|
+
if (!prop.nullable &&
|
|
41
|
+
!prop.autoincrement &&
|
|
42
|
+
!prop.default &&
|
|
43
|
+
!prop.defaultRaw &&
|
|
44
|
+
!prop.onCreate &&
|
|
45
|
+
!prop.generated &&
|
|
46
|
+
!prop.embedded &&
|
|
47
|
+
![enums_1.ReferenceKind.ONE_TO_MANY, enums_1.ReferenceKind.MANY_TO_MANY].includes(prop.kind) &&
|
|
48
|
+
prop.name !== wrapped.__meta.root.discriminatorColumn &&
|
|
49
|
+
prop.type.toLowerCase() !== 'objectid' &&
|
|
50
|
+
prop.persist !== false &&
|
|
51
|
+
entity[prop.name] == null) {
|
|
52
|
+
throw errors_1.ValidationError.propertyRequired(entity, prop);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
validateProperty(prop, givenValue, entity) {
|
|
57
|
+
if (givenValue === null || givenValue === undefined) {
|
|
58
|
+
return givenValue;
|
|
59
|
+
}
|
|
60
|
+
const expectedType = prop.runtimeType;
|
|
61
|
+
let givenType = Utils_1.Utils.getObjectType(givenValue);
|
|
62
|
+
let ret = givenValue;
|
|
63
|
+
if (!this.strict) {
|
|
64
|
+
ret = this.fixTypes(expectedType, givenType, givenValue);
|
|
65
|
+
givenType = Utils_1.Utils.getObjectType(ret);
|
|
66
|
+
}
|
|
67
|
+
if (prop.enum && prop.items) {
|
|
68
|
+
if (!prop.items.some(it => it === givenValue)) {
|
|
69
|
+
throw errors_1.ValidationError.fromWrongPropertyType(entity, prop.name, expectedType, givenType, givenValue);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
if (givenType !== expectedType && this.KNOWN_TYPES.has(expectedType)) {
|
|
74
|
+
throw errors_1.ValidationError.fromWrongPropertyType(entity, prop.name, expectedType, givenType, givenValue);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return ret;
|
|
78
|
+
}
|
|
79
|
+
validateParams(params, type = 'search condition', field) {
|
|
80
|
+
if (Utils_1.Utils.isPrimaryKey(params) || Utils_1.Utils.isEntity(params)) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (Array.isArray(params)) {
|
|
84
|
+
return params.forEach(item => this.validateParams(item, type, field));
|
|
85
|
+
}
|
|
86
|
+
if (Utils_1.Utils.isPlainObject(params)) {
|
|
87
|
+
Object.keys(params).forEach(k => {
|
|
88
|
+
this.validateParams(params[k], type, k);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
validatePrimaryKey(entity, meta) {
|
|
93
|
+
const pkExists = meta.primaryKeys.every(pk => entity[pk] != null) || entity[meta.serializedPrimaryKey] != null;
|
|
94
|
+
if (!entity || !pkExists) {
|
|
95
|
+
throw errors_1.ValidationError.fromMergeWithoutPK(meta);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
validateEmptyWhere(where) {
|
|
99
|
+
if (Utils_1.Utils.isEmpty(where)) {
|
|
100
|
+
throw new Error(`You cannot call 'EntityManager.findOne()' with empty 'where' parameter`);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
getValue(o, prop) {
|
|
104
|
+
if (prop.embedded && prop.embedded[0] in o) {
|
|
105
|
+
return o[prop.embedded[0]]?.[prop.embedded[1]];
|
|
106
|
+
}
|
|
107
|
+
return o[prop.name];
|
|
108
|
+
}
|
|
109
|
+
setValue(o, prop, v) {
|
|
110
|
+
/* istanbul ignore next */
|
|
111
|
+
if (prop.embedded && prop.embedded[0] in o) {
|
|
112
|
+
return o[prop.embedded[0]][prop.embedded[1]] = v;
|
|
113
|
+
}
|
|
114
|
+
o[prop.name] = v;
|
|
115
|
+
}
|
|
116
|
+
validateCollection(entity, prop) {
|
|
117
|
+
if (prop.hydrate !== false && (0, wrap_1.helper)(entity).__initialized && !entity[prop.name]) {
|
|
118
|
+
throw errors_1.ValidationError.fromCollectionNotInitialized(entity, prop);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
fixTypes(expectedType, givenType, givenValue) {
|
|
122
|
+
if (expectedType === 'Date' && ['string', 'number'].includes(givenType)) {
|
|
123
|
+
givenValue = this.fixDateType(givenValue);
|
|
124
|
+
}
|
|
125
|
+
if (expectedType === 'number' && givenType === 'string') {
|
|
126
|
+
givenValue = this.fixNumberType(givenValue);
|
|
127
|
+
}
|
|
128
|
+
if (expectedType === 'boolean' && givenType === 'number') {
|
|
129
|
+
givenValue = this.fixBooleanType(givenValue);
|
|
130
|
+
}
|
|
131
|
+
return givenValue;
|
|
132
|
+
}
|
|
133
|
+
fixDateType(givenValue) {
|
|
134
|
+
let date;
|
|
135
|
+
if (Utils_1.Utils.isString(givenValue) && givenValue.match(/^-?\d+(\.\d+)?$/)) {
|
|
136
|
+
date = new Date(+givenValue);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
date = new Date(givenValue);
|
|
140
|
+
}
|
|
141
|
+
return date.toString() !== 'Invalid Date' ? date : givenValue;
|
|
142
|
+
}
|
|
143
|
+
fixNumberType(givenValue) {
|
|
144
|
+
const num = +givenValue;
|
|
145
|
+
return '' + num === givenValue ? num : givenValue;
|
|
146
|
+
}
|
|
147
|
+
fixBooleanType(givenValue) {
|
|
148
|
+
const bool = !!givenValue;
|
|
149
|
+
return +bool === givenValue ? bool : givenValue;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
exports.EntityValidator = EntityValidator;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { inspect } from 'util';
|
|
3
|
+
import type { AddEager, Dictionary, EntityClass, EntityKey, EntityProperty, Loaded, LoadedReference, Primary, Ref } from '../typings';
|
|
4
|
+
import type { FindOneOptions, FindOneOrFailOptions } from '../drivers/IDatabaseDriver';
|
|
5
|
+
export declare class Reference<T extends object> {
|
|
6
|
+
private entity;
|
|
7
|
+
constructor(entity: T);
|
|
8
|
+
static create<T extends object>(entity: T | Ref<T>): Ref<T>;
|
|
9
|
+
static createFromPK<T extends object>(entityType: EntityClass<T>, pk: Primary<T>, options?: {
|
|
10
|
+
schema?: string;
|
|
11
|
+
}): Ref<T>;
|
|
12
|
+
static createNakedFromPK<T extends object>(entityType: EntityClass<T>, pk: Primary<T>, options?: {
|
|
13
|
+
schema?: string;
|
|
14
|
+
}): T;
|
|
15
|
+
/**
|
|
16
|
+
* Checks whether the argument is instance of `Reference` wrapper.
|
|
17
|
+
*/
|
|
18
|
+
static isReference<T extends object>(data: any): data is Reference<T>;
|
|
19
|
+
/**
|
|
20
|
+
* Wraps the entity in a `Reference` wrapper if the property is defined as `ref`.
|
|
21
|
+
*/
|
|
22
|
+
static wrapReference<T extends object, O extends object>(entity: T | Reference<T>, prop: EntityProperty<O, T>): Reference<T> | T;
|
|
23
|
+
/**
|
|
24
|
+
* Returns wrapped entity.
|
|
25
|
+
*/
|
|
26
|
+
static unwrapReference<T extends object>(ref: T | Reference<T> | ScalarReference<T> | Ref<T>): T;
|
|
27
|
+
/**
|
|
28
|
+
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded). Returns the entity.
|
|
29
|
+
* If the entity is not found in the database (e.g. it was deleted in the meantime, or currently active filters disallow loading of it)
|
|
30
|
+
* the method returns `null`. Use `loadOrFail()` if you want an error to be thrown in such a case.
|
|
31
|
+
*/
|
|
32
|
+
load<TT extends T, P extends string = never, F extends string = '*', E extends string = never>(options?: LoadReferenceOptions<TT, P, F, E>): Promise<Loaded<TT, P, F, E> | null>;
|
|
33
|
+
/**
|
|
34
|
+
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
|
|
35
|
+
* Returns the entity or throws an error just like `em.findOneOrFail()` (and respects the same config options).
|
|
36
|
+
*/
|
|
37
|
+
loadOrFail<TT extends T, P extends string = never, F extends string = '*', E extends string = never>(options?: LoadReferenceOrFailOptions<TT, P, F, E>): Promise<Loaded<TT, P, F, E>>;
|
|
38
|
+
private set;
|
|
39
|
+
unwrap(): T;
|
|
40
|
+
getEntity(): T;
|
|
41
|
+
getProperty<K extends keyof T>(prop: K): T[K];
|
|
42
|
+
loadProperty<TT extends T, P extends string = never, K extends keyof TT = keyof TT>(prop: K, options?: LoadReferenceOrFailOptions<TT, P>): Promise<Loaded<TT, P>[K]>;
|
|
43
|
+
isInitialized(): boolean;
|
|
44
|
+
populated(populated?: boolean): void;
|
|
45
|
+
toJSON(...args: any[]): Dictionary;
|
|
46
|
+
/** @ignore */
|
|
47
|
+
[inspect.custom](depth: number): string;
|
|
48
|
+
}
|
|
49
|
+
export declare class ScalarReference<Value> {
|
|
50
|
+
private value?;
|
|
51
|
+
private initialized;
|
|
52
|
+
private entity?;
|
|
53
|
+
private property?;
|
|
54
|
+
constructor(value?: Value | undefined, initialized?: boolean);
|
|
55
|
+
/**
|
|
56
|
+
* Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
|
|
57
|
+
* Returns either the whole entity, or the requested property.
|
|
58
|
+
*/
|
|
59
|
+
load(options?: Omit<LoadReferenceOptions<any, any>, 'populate' | 'fields' | 'exclude'>): Promise<Value | undefined>;
|
|
60
|
+
set(value: Value): void;
|
|
61
|
+
bind<Entity extends object>(entity: Entity, property: EntityKey<Entity>): void;
|
|
62
|
+
unwrap(): Value | undefined;
|
|
63
|
+
isInitialized(): boolean;
|
|
64
|
+
/** @ignore */
|
|
65
|
+
[inspect.custom](): string;
|
|
66
|
+
}
|
|
67
|
+
export interface LoadReferenceOptions<T extends object, P extends string = never, F extends string = '*', E extends string = never> extends FindOneOptions<T, P, F, E> {
|
|
68
|
+
dataloader?: boolean;
|
|
69
|
+
}
|
|
70
|
+
export interface LoadReferenceOrFailOptions<T extends object, P extends string = never, F extends string = '*', E extends string = never> extends FindOneOrFailOptions<T, P, F, E> {
|
|
71
|
+
dataloader?: boolean;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* shortcut for `wrap(entity).toReference()`
|
|
75
|
+
*/
|
|
76
|
+
export declare function ref<T extends object>(entity: T | Ref<T>): Ref<T> & LoadedReference<Loaded<T, AddEager<T>>>;
|
|
77
|
+
/**
|
|
78
|
+
* shortcut for `Reference.createFromPK(entityType, pk)`
|
|
79
|
+
*/
|
|
80
|
+
export declare function ref<T extends object, PKV extends Primary<T> = Primary<T>>(entityType: EntityClass<T>, pk?: T | PKV): Ref<T>;
|
|
81
|
+
/**
|
|
82
|
+
* shortcut for `wrap(entity).toReference()`
|
|
83
|
+
*/
|
|
84
|
+
export declare function ref<T>(value: T | Ref<T>): Ref<T> & LoadedReference<Loaded<T, AddEager<T>>>;
|
|
85
|
+
/**
|
|
86
|
+
* shortcut for `Reference.createNakedFromPK(entityType, pk)`
|
|
87
|
+
*/
|
|
88
|
+
export declare function rel<T, PK extends Primary<T>>(entityType: EntityClass<T>, pk: T | PK): T;
|
|
89
|
+
export { Reference as Ref };
|