typeorm 0.3.0 → 0.3.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/browser/connection/options-reader/ConnectionOptionsEnvReader.js +0 -5
- package/browser/connection/options-reader/ConnectionOptionsEnvReader.js.map +1 -1
- package/browser/data-source/BaseDataSourceOptions.d.ts +0 -17
- package/browser/data-source/BaseDataSourceOptions.js.map +1 -1
- package/browser/data-source/DataSource.js +7 -9
- package/browser/data-source/DataSource.js.map +1 -1
- package/browser/driver/postgres/PostgresDriver.js +1 -1
- package/browser/driver/postgres/PostgresDriver.js.map +1 -1
- package/browser/entity-manager/EntityManager.js +4 -1
- package/browser/entity-manager/EntityManager.js.map +1 -1
- package/browser/find-options/FindOperatorType.d.ts +1 -1
- package/browser/find-options/FindOperatorType.js.map +1 -1
- package/browser/find-options/operator/ArrayContainedBy.d.ts +6 -0
- package/browser/find-options/operator/ArrayContainedBy.js +10 -0
- package/browser/find-options/operator/ArrayContainedBy.js.map +1 -0
- package/browser/find-options/operator/ArrayContains.d.ts +6 -0
- package/browser/find-options/operator/ArrayContains.js +10 -0
- package/browser/find-options/operator/ArrayContains.js.map +1 -0
- package/browser/find-options/operator/ArrayOverlap.d.ts +6 -0
- package/browser/find-options/operator/ArrayOverlap.js +10 -0
- package/browser/find-options/operator/ArrayOverlap.js.map +1 -0
- package/browser/query-builder/QueryBuilder.js +6 -0
- package/browser/query-builder/QueryBuilder.js.map +1 -1
- package/browser/query-builder/WhereClause.d.ts +1 -1
- package/browser/query-builder/WhereClause.js.map +1 -1
- package/browser/repository/BaseEntity.d.ts +101 -38
- package/browser/repository/BaseEntity.js +34 -40
- package/browser/repository/BaseEntity.js.map +1 -1
- package/browser/util/InstanceChecker.d.ts +2 -0
- package/browser/util/InstanceChecker.js +6 -0
- package/browser/util/InstanceChecker.js.map +1 -1
- package/connection/options-reader/ConnectionOptionsEnvReader.js +0 -5
- package/connection/options-reader/ConnectionOptionsEnvReader.js.map +1 -1
- package/data-source/BaseDataSourceOptions.d.ts +0 -17
- package/data-source/BaseDataSourceOptions.js.map +1 -1
- package/data-source/DataSource.js +7 -9
- package/data-source/DataSource.js.map +1 -1
- package/driver/postgres/PostgresDriver.js +1 -1
- package/driver/postgres/PostgresDriver.js.map +1 -1
- package/entity-manager/EntityManager.js +4 -1
- package/entity-manager/EntityManager.js.map +1 -1
- package/find-options/FindOperatorType.d.ts +1 -1
- package/find-options/FindOperatorType.js.map +1 -1
- package/find-options/operator/ArrayContainedBy.d.ts +6 -0
- package/find-options/operator/ArrayContainedBy.js +14 -0
- package/find-options/operator/ArrayContainedBy.js.map +1 -0
- package/find-options/operator/ArrayContains.d.ts +6 -0
- package/find-options/operator/ArrayContains.js +14 -0
- package/find-options/operator/ArrayContains.js.map +1 -0
- package/find-options/operator/ArrayOverlap.d.ts +6 -0
- package/find-options/operator/ArrayOverlap.js +14 -0
- package/find-options/operator/ArrayOverlap.js.map +1 -0
- package/package.json +1 -1
- package/query-builder/QueryBuilder.js +6 -0
- package/query-builder/QueryBuilder.js.map +1 -1
- package/query-builder/WhereClause.d.ts +1 -1
- package/query-builder/WhereClause.js.map +1 -1
- package/repository/BaseEntity.d.ts +101 -38
- package/repository/BaseEntity.js +34 -40
- package/repository/BaseEntity.js.map +1 -1
- package/util/InstanceChecker.d.ts +2 -0
- package/util/InstanceChecker.js +6 -0
- package/util/InstanceChecker.js.map +1 -1
|
@@ -5,8 +5,7 @@ import { SaveOptions } from "./SaveOptions";
|
|
|
5
5
|
import { FindOneOptions } from "../find-options/FindOneOptions";
|
|
6
6
|
import { RemoveOptions } from "./RemoveOptions";
|
|
7
7
|
import { FindManyOptions } from "../find-options/FindManyOptions";
|
|
8
|
-
import { DataSource } from "../data-source
|
|
9
|
-
import { ObjectType } from "../common/ObjectType";
|
|
8
|
+
import { DataSource } from "../data-source";
|
|
10
9
|
import { SelectQueryBuilder } from "../query-builder/SelectQueryBuilder";
|
|
11
10
|
import { InsertResult } from "../query-builder/result/InsertResult";
|
|
12
11
|
import { UpdateResult } from "../query-builder/result/UpdateResult";
|
|
@@ -20,9 +19,9 @@ import { EntityTarget } from "../common/EntityTarget";
|
|
|
20
19
|
*/
|
|
21
20
|
export declare class BaseEntity {
|
|
22
21
|
/**
|
|
23
|
-
*
|
|
22
|
+
* DataSource used in all static methods of the BaseEntity.
|
|
24
23
|
*/
|
|
25
|
-
private static
|
|
24
|
+
private static dataSource?;
|
|
26
25
|
/**
|
|
27
26
|
* Checks if entity has an id.
|
|
28
27
|
* If entity composite compose ids, it will check them all.
|
|
@@ -50,13 +49,15 @@ export declare class BaseEntity {
|
|
|
50
49
|
*/
|
|
51
50
|
reload(): Promise<void>;
|
|
52
51
|
/**
|
|
53
|
-
* Sets
|
|
52
|
+
* Sets DataSource to be used by entity.
|
|
54
53
|
*/
|
|
55
|
-
static
|
|
54
|
+
static useDataSource(dataSource: DataSource): void;
|
|
56
55
|
/**
|
|
57
56
|
* Gets current entity's Repository.
|
|
58
57
|
*/
|
|
59
|
-
static getRepository<T extends BaseEntity>(this:
|
|
58
|
+
static getRepository<T extends BaseEntity>(this: {
|
|
59
|
+
new (): T;
|
|
60
|
+
} & typeof BaseEntity): Repository<T>;
|
|
60
61
|
/**
|
|
61
62
|
* Returns object that is managed by this repository.
|
|
62
63
|
* If this repository manages entity from schema,
|
|
@@ -71,29 +72,41 @@ export declare class BaseEntity {
|
|
|
71
72
|
/**
|
|
72
73
|
* Gets entity mixed id.
|
|
73
74
|
*/
|
|
74
|
-
static getId<T extends BaseEntity>(this:
|
|
75
|
+
static getId<T extends BaseEntity>(this: {
|
|
76
|
+
new (): T;
|
|
77
|
+
} & typeof BaseEntity, entity: T): any;
|
|
75
78
|
/**
|
|
76
79
|
* Creates a new query builder that can be used to build a SQL query.
|
|
77
80
|
*/
|
|
78
|
-
static createQueryBuilder<T extends BaseEntity>(this:
|
|
81
|
+
static createQueryBuilder<T extends BaseEntity>(this: {
|
|
82
|
+
new (): T;
|
|
83
|
+
} & typeof BaseEntity, alias?: string): SelectQueryBuilder<T>;
|
|
79
84
|
/**
|
|
80
85
|
* Creates a new entity instance.
|
|
81
86
|
*/
|
|
82
|
-
static create<T extends BaseEntity>(this:
|
|
87
|
+
static create<T extends BaseEntity>(this: {
|
|
88
|
+
new (): T;
|
|
89
|
+
} & typeof BaseEntity): T;
|
|
83
90
|
/**
|
|
84
91
|
* Creates a new entities and copies all entity properties from given objects into their new entities.
|
|
85
92
|
* Note that it copies only properties that present in entity schema.
|
|
86
93
|
*/
|
|
87
|
-
static create<T extends BaseEntity>(this:
|
|
94
|
+
static create<T extends BaseEntity>(this: {
|
|
95
|
+
new (): T;
|
|
96
|
+
} & typeof BaseEntity, entityLikeArray: DeepPartial<T>[]): T[];
|
|
88
97
|
/**
|
|
89
98
|
* Creates a new entity instance and copies all entity properties from this object into a new entity.
|
|
90
99
|
* Note that it copies only properties that present in entity schema.
|
|
91
100
|
*/
|
|
92
|
-
static create<T extends BaseEntity>(this:
|
|
101
|
+
static create<T extends BaseEntity>(this: {
|
|
102
|
+
new (): T;
|
|
103
|
+
} & typeof BaseEntity, entityLike: DeepPartial<T>): T;
|
|
93
104
|
/**
|
|
94
105
|
* Merges multiple entities (or entity-like objects) into a given entity.
|
|
95
106
|
*/
|
|
96
|
-
static merge<T extends BaseEntity>(this:
|
|
107
|
+
static merge<T extends BaseEntity>(this: {
|
|
108
|
+
new (): T;
|
|
109
|
+
} & typeof BaseEntity, mergeIntoEntity: T, ...entityLikes: DeepPartial<T>[]): T;
|
|
97
110
|
/**
|
|
98
111
|
* Creates a new entity from the given plain javascript object. If entity already exist in the database, then
|
|
99
112
|
* it loads it (and everything related to it), replaces all values with the new ones from the given object
|
|
@@ -103,88 +116,122 @@ export declare class BaseEntity {
|
|
|
103
116
|
* Note that given entity-like object must have an entity id / primary key to find entity by.
|
|
104
117
|
* Returns undefined if entity with given id was not found.
|
|
105
118
|
*/
|
|
106
|
-
static preload<T extends BaseEntity>(this:
|
|
119
|
+
static preload<T extends BaseEntity>(this: {
|
|
120
|
+
new (): T;
|
|
121
|
+
} & typeof BaseEntity, entityLike: DeepPartial<T>): Promise<T | undefined>;
|
|
107
122
|
/**
|
|
108
123
|
* Saves all given entities in the database.
|
|
109
124
|
* If entities do not exist in the database then inserts, otherwise updates.
|
|
110
125
|
*/
|
|
111
|
-
static save<T extends BaseEntity>(this:
|
|
126
|
+
static save<T extends BaseEntity>(this: {
|
|
127
|
+
new (): T;
|
|
128
|
+
} & typeof BaseEntity, entities: DeepPartial<T>[], options?: SaveOptions): Promise<T[]>;
|
|
112
129
|
/**
|
|
113
130
|
* Saves a given entity in the database.
|
|
114
131
|
* If entity does not exist in the database then inserts, otherwise updates.
|
|
115
132
|
*/
|
|
116
|
-
static save<T extends BaseEntity>(this:
|
|
133
|
+
static save<T extends BaseEntity>(this: {
|
|
134
|
+
new (): T;
|
|
135
|
+
} & typeof BaseEntity, entity: DeepPartial<T>, options?: SaveOptions): Promise<T>;
|
|
117
136
|
/**
|
|
118
137
|
* Removes a given entities from the database.
|
|
119
138
|
*/
|
|
120
|
-
static remove<T extends BaseEntity>(this:
|
|
139
|
+
static remove<T extends BaseEntity>(this: {
|
|
140
|
+
new (): T;
|
|
141
|
+
} & typeof BaseEntity, entities: T[], options?: RemoveOptions): Promise<T[]>;
|
|
121
142
|
/**
|
|
122
143
|
* Removes a given entity from the database.
|
|
123
144
|
*/
|
|
124
|
-
static remove<T extends BaseEntity>(this:
|
|
145
|
+
static remove<T extends BaseEntity>(this: {
|
|
146
|
+
new (): T;
|
|
147
|
+
} & typeof BaseEntity, entity: T, options?: RemoveOptions): Promise<T>;
|
|
125
148
|
/**
|
|
126
149
|
* Records the delete date of all given entities.
|
|
127
150
|
*/
|
|
128
|
-
static softRemove<T extends BaseEntity>(this:
|
|
151
|
+
static softRemove<T extends BaseEntity>(this: {
|
|
152
|
+
new (): T;
|
|
153
|
+
} & typeof BaseEntity, entities: T[], options?: SaveOptions): Promise<T[]>;
|
|
129
154
|
/**
|
|
130
155
|
* Records the delete date of a given entity.
|
|
131
156
|
*/
|
|
132
|
-
static softRemove<T extends BaseEntity>(this:
|
|
157
|
+
static softRemove<T extends BaseEntity>(this: {
|
|
158
|
+
new (): T;
|
|
159
|
+
} & typeof BaseEntity, entity: T, options?: SaveOptions): Promise<T>;
|
|
133
160
|
/**
|
|
134
161
|
* Inserts a given entity into the database.
|
|
135
162
|
* Unlike save method executes a primitive operation without cascades, relations and other operations included.
|
|
136
163
|
* Executes fast and efficient INSERT query.
|
|
137
164
|
* Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted.
|
|
138
165
|
*/
|
|
139
|
-
static insert<T extends BaseEntity>(this:
|
|
166
|
+
static insert<T extends BaseEntity>(this: {
|
|
167
|
+
new (): T;
|
|
168
|
+
} & typeof BaseEntity, entity: QueryDeepPartialEntity<T> | QueryDeepPartialEntity<T>[]): Promise<InsertResult>;
|
|
140
169
|
/**
|
|
141
170
|
* Updates entity partially. Entity can be found by a given conditions.
|
|
142
171
|
* Unlike save method executes a primitive operation without cascades, relations and other operations included.
|
|
143
172
|
* Executes fast and efficient UPDATE query.
|
|
144
173
|
* Does not check if entity exist in the database.
|
|
145
174
|
*/
|
|
146
|
-
static update<T extends BaseEntity>(this:
|
|
175
|
+
static update<T extends BaseEntity>(this: {
|
|
176
|
+
new (): T;
|
|
177
|
+
} & typeof BaseEntity, criteria: string | string[] | number | number[] | Date | Date[] | ObjectID | ObjectID[] | FindOptionsWhere<T>, partialEntity: QueryDeepPartialEntity<T>): Promise<UpdateResult>;
|
|
147
178
|
/**
|
|
148
179
|
* Inserts a given entity into the database, unless a unique constraint conflicts then updates the entity
|
|
149
180
|
* Unlike save method executes a primitive operation without cascades, relations and other operations included.
|
|
150
181
|
* Executes fast and efficient INSERT ... ON CONFLICT DO UPDATE/ON DUPLICATE KEY UPDATE query.
|
|
151
182
|
*/
|
|
152
|
-
static upsert<T extends BaseEntity>(this:
|
|
183
|
+
static upsert<T extends BaseEntity>(this: {
|
|
184
|
+
new (): T;
|
|
185
|
+
} & typeof BaseEntity, entityOrEntities: QueryDeepPartialEntity<T> | QueryDeepPartialEntity<T>[], conflictPathsOrOptions: string[] | UpsertOptions<T>): Promise<InsertResult>;
|
|
153
186
|
/**
|
|
154
187
|
* Deletes entities by a given criteria.
|
|
155
188
|
* Unlike remove method executes a primitive operation without cascades, relations and other operations included.
|
|
156
189
|
* Executes fast and efficient DELETE query.
|
|
157
190
|
* Does not check if entity exist in the database.
|
|
158
191
|
*/
|
|
159
|
-
static delete<T extends BaseEntity>(this:
|
|
192
|
+
static delete<T extends BaseEntity>(this: {
|
|
193
|
+
new (): T;
|
|
194
|
+
} & typeof BaseEntity, criteria: string | string[] | number | number[] | Date | Date[] | ObjectID | ObjectID[] | FindOptionsWhere<T>): Promise<DeleteResult>;
|
|
160
195
|
/**
|
|
161
196
|
* Counts entities that match given options.
|
|
162
197
|
*/
|
|
163
|
-
static count<T extends BaseEntity>(this:
|
|
198
|
+
static count<T extends BaseEntity>(this: {
|
|
199
|
+
new (): T;
|
|
200
|
+
} & typeof BaseEntity, options?: FindManyOptions<T>): Promise<number>;
|
|
164
201
|
/**
|
|
165
202
|
* Counts entities that match given WHERE conditions.
|
|
166
203
|
*/
|
|
167
|
-
static countBy<T extends BaseEntity>(this:
|
|
204
|
+
static countBy<T extends BaseEntity>(this: {
|
|
205
|
+
new (): T;
|
|
206
|
+
} & typeof BaseEntity, where: FindOptionsWhere<T>): Promise<number>;
|
|
168
207
|
/**
|
|
169
208
|
* Finds entities that match given options.
|
|
170
209
|
*/
|
|
171
|
-
static find<T extends BaseEntity>(this:
|
|
210
|
+
static find<T extends BaseEntity>(this: {
|
|
211
|
+
new (): T;
|
|
212
|
+
} & typeof BaseEntity, options?: FindManyOptions<T>): Promise<T[]>;
|
|
172
213
|
/**
|
|
173
214
|
* Finds entities that match given WHERE conditions.
|
|
174
215
|
*/
|
|
175
|
-
static findBy<T extends BaseEntity>(this:
|
|
216
|
+
static findBy<T extends BaseEntity>(this: {
|
|
217
|
+
new (): T;
|
|
218
|
+
} & typeof BaseEntity, where: FindOptionsWhere<T>): Promise<T[]>;
|
|
176
219
|
/**
|
|
177
220
|
* Finds entities that match given find options.
|
|
178
221
|
* Also counts all entities that match given conditions,
|
|
179
222
|
* but ignores pagination settings (from and take options).
|
|
180
223
|
*/
|
|
181
|
-
static findAndCount<T extends BaseEntity>(this:
|
|
224
|
+
static findAndCount<T extends BaseEntity>(this: {
|
|
225
|
+
new (): T;
|
|
226
|
+
} & typeof BaseEntity, options?: FindManyOptions<T>): Promise<[T[], number]>;
|
|
182
227
|
/**
|
|
183
228
|
* Finds entities that match given WHERE conditions.
|
|
184
229
|
* Also counts all entities that match given conditions,
|
|
185
230
|
* but ignores pagination settings (from and take options).
|
|
186
231
|
*/
|
|
187
|
-
static findAndCountBy<T extends BaseEntity>(this:
|
|
232
|
+
static findAndCountBy<T extends BaseEntity>(this: {
|
|
233
|
+
new (): T;
|
|
234
|
+
} & typeof BaseEntity, where: FindOptionsWhere<T>): Promise<[T[], number]>;
|
|
188
235
|
/**
|
|
189
236
|
* Finds entities by ids.
|
|
190
237
|
* Optionally find options can be applied.
|
|
@@ -195,15 +242,21 @@ export declare class BaseEntity {
|
|
|
195
242
|
* id: In([1, 2, 3])
|
|
196
243
|
* })
|
|
197
244
|
*/
|
|
198
|
-
static findByIds<T extends BaseEntity>(this:
|
|
245
|
+
static findByIds<T extends BaseEntity>(this: {
|
|
246
|
+
new (): T;
|
|
247
|
+
} & typeof BaseEntity, ids: any[]): Promise<T[]>;
|
|
199
248
|
/**
|
|
200
249
|
* Finds first entity that matches given conditions.
|
|
201
250
|
*/
|
|
202
|
-
static findOne<T extends BaseEntity>(this:
|
|
251
|
+
static findOne<T extends BaseEntity>(this: {
|
|
252
|
+
new (): T;
|
|
253
|
+
} & typeof BaseEntity, options: FindOneOptions<T>): Promise<T | null>;
|
|
203
254
|
/**
|
|
204
255
|
* Finds first entity that matches given conditions.
|
|
205
256
|
*/
|
|
206
|
-
static findOneBy<T extends BaseEntity>(this:
|
|
257
|
+
static findOneBy<T extends BaseEntity>(this: {
|
|
258
|
+
new (): T;
|
|
259
|
+
} & typeof BaseEntity, where: FindOptionsWhere<T>): Promise<T | null>;
|
|
207
260
|
/**
|
|
208
261
|
* Finds first entity that matches given options.
|
|
209
262
|
*
|
|
@@ -213,22 +266,32 @@ export declare class BaseEntity {
|
|
|
213
266
|
* id: 1 // where "id" is your primary column name
|
|
214
267
|
* })
|
|
215
268
|
*/
|
|
216
|
-
static findOneById<T extends BaseEntity>(this:
|
|
269
|
+
static findOneById<T extends BaseEntity>(this: {
|
|
270
|
+
new (): T;
|
|
271
|
+
} & typeof BaseEntity, id: string | number | Date | ObjectID): Promise<T | null>;
|
|
217
272
|
/**
|
|
218
273
|
* Finds first entity that matches given conditions.
|
|
219
274
|
*/
|
|
220
|
-
static findOneOrFail<T extends BaseEntity>(this:
|
|
275
|
+
static findOneOrFail<T extends BaseEntity>(this: {
|
|
276
|
+
new (): T;
|
|
277
|
+
} & typeof BaseEntity, options: FindOneOptions<T>): Promise<T>;
|
|
221
278
|
/**
|
|
222
279
|
* Finds first entity that matches given conditions.
|
|
223
280
|
*/
|
|
224
|
-
static findOneByOrFail<T extends BaseEntity>(this:
|
|
281
|
+
static findOneByOrFail<T extends BaseEntity>(this: {
|
|
282
|
+
new (): T;
|
|
283
|
+
} & typeof BaseEntity, where: FindOptionsWhere<T>): Promise<T>;
|
|
225
284
|
/**
|
|
226
285
|
* Executes a raw SQL query and returns a raw database results.
|
|
227
286
|
* Raw query execution is supported only by relational databases (MongoDB is not supported).
|
|
228
287
|
*/
|
|
229
|
-
static query<T extends BaseEntity>(this:
|
|
288
|
+
static query<T extends BaseEntity>(this: {
|
|
289
|
+
new (): T;
|
|
290
|
+
} & typeof BaseEntity, query: string, parameters?: any[]): Promise<any>;
|
|
230
291
|
/**
|
|
231
292
|
* Clears all the data from the given table/collection (truncates/drops it).
|
|
232
293
|
*/
|
|
233
|
-
static clear<T extends BaseEntity>(this:
|
|
294
|
+
static clear<T extends BaseEntity>(this: {
|
|
295
|
+
new (): T;
|
|
296
|
+
} & typeof BaseEntity): Promise<void>;
|
|
234
297
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { getConnection } from "../globals";
|
|
2
1
|
import { ObjectUtils } from "../util/ObjectUtils";
|
|
3
2
|
/**
|
|
4
3
|
* Base abstract entity for all entities, used in ActiveRecord patterns.
|
|
@@ -12,60 +11,65 @@ export class BaseEntity {
|
|
|
12
11
|
* If entity composite compose ids, it will check them all.
|
|
13
12
|
*/
|
|
14
13
|
hasId() {
|
|
15
|
-
|
|
14
|
+
const baseEntity = this.constructor;
|
|
15
|
+
return baseEntity.getRepository().hasId(this);
|
|
16
16
|
}
|
|
17
17
|
/**
|
|
18
18
|
* Saves current entity in the database.
|
|
19
19
|
* If entity does not exist in the database then inserts, otherwise updates.
|
|
20
20
|
*/
|
|
21
21
|
save(options) {
|
|
22
|
-
|
|
22
|
+
const baseEntity = this.constructor;
|
|
23
|
+
return baseEntity.getRepository().save(this, options);
|
|
23
24
|
}
|
|
24
25
|
/**
|
|
25
26
|
* Removes current entity from the database.
|
|
26
27
|
*/
|
|
27
28
|
remove(options) {
|
|
28
|
-
|
|
29
|
+
const baseEntity = this.constructor;
|
|
30
|
+
return baseEntity.getRepository().remove(this, options);
|
|
29
31
|
}
|
|
30
32
|
/**
|
|
31
33
|
* Records the delete date of current entity.
|
|
32
34
|
*/
|
|
33
35
|
softRemove(options) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
.softRemove(this, options);
|
|
36
|
+
const baseEntity = this.constructor;
|
|
37
|
+
return baseEntity.getRepository().softRemove(this, options);
|
|
37
38
|
}
|
|
38
39
|
/**
|
|
39
40
|
* Recovers a given entity in the database.
|
|
40
41
|
*/
|
|
41
42
|
recover(options) {
|
|
42
|
-
|
|
43
|
+
const baseEntity = this.constructor;
|
|
44
|
+
return baseEntity.getRepository().recover(this, options);
|
|
43
45
|
}
|
|
44
46
|
/**
|
|
45
47
|
* Reloads entity data from the database.
|
|
46
48
|
*/
|
|
47
49
|
async reload() {
|
|
48
|
-
const
|
|
49
|
-
const newestEntity = await
|
|
50
|
+
const baseEntity = this.constructor;
|
|
51
|
+
const newestEntity = await baseEntity
|
|
50
52
|
.getRepository()
|
|
51
|
-
.findOneOrFail(
|
|
53
|
+
.findOneOrFail(baseEntity.getId(this));
|
|
52
54
|
ObjectUtils.assign(this, newestEntity);
|
|
53
55
|
}
|
|
54
56
|
// -------------------------------------------------------------------------
|
|
55
57
|
// Public Static Methods
|
|
56
58
|
// -------------------------------------------------------------------------
|
|
57
59
|
/**
|
|
58
|
-
* Sets
|
|
60
|
+
* Sets DataSource to be used by entity.
|
|
59
61
|
*/
|
|
60
|
-
static
|
|
61
|
-
this.
|
|
62
|
+
static useDataSource(dataSource) {
|
|
63
|
+
this.dataSource = dataSource;
|
|
62
64
|
}
|
|
63
65
|
/**
|
|
64
66
|
* Gets current entity's Repository.
|
|
65
67
|
*/
|
|
66
68
|
static getRepository() {
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
+
const dataSource = this.dataSource;
|
|
70
|
+
if (!dataSource)
|
|
71
|
+
throw new Error(`DataSource is not set for this entity.`);
|
|
72
|
+
return dataSource.getRepository(this);
|
|
69
73
|
}
|
|
70
74
|
/**
|
|
71
75
|
* Returns object that is managed by this repository.
|
|
@@ -105,9 +109,7 @@ export class BaseEntity {
|
|
|
105
109
|
* Merges multiple entities (or entity-like objects) into a given entity.
|
|
106
110
|
*/
|
|
107
111
|
static merge(mergeIntoEntity, ...entityLikes) {
|
|
108
|
-
return this
|
|
109
|
-
.getRepository()
|
|
110
|
-
.merge(mergeIntoEntity, ...entityLikes);
|
|
112
|
+
return this.getRepository().merge(mergeIntoEntity, ...entityLikes);
|
|
111
113
|
}
|
|
112
114
|
/**
|
|
113
115
|
* Creates a new entity from the given plain javascript object. If entity already exist in the database, then
|
|
@@ -126,25 +128,19 @@ export class BaseEntity {
|
|
|
126
128
|
* Saves one or many given entities.
|
|
127
129
|
*/
|
|
128
130
|
static save(entityOrEntities, options) {
|
|
129
|
-
return this
|
|
130
|
-
.getRepository()
|
|
131
|
-
.save(entityOrEntities, options);
|
|
131
|
+
return this.getRepository().save(entityOrEntities, options);
|
|
132
132
|
}
|
|
133
133
|
/**
|
|
134
134
|
* Removes one or many given entities.
|
|
135
135
|
*/
|
|
136
136
|
static remove(entityOrEntities, options) {
|
|
137
|
-
return this
|
|
138
|
-
.getRepository()
|
|
139
|
-
.remove(entityOrEntities, options);
|
|
137
|
+
return this.getRepository().remove(entityOrEntities, options);
|
|
140
138
|
}
|
|
141
139
|
/**
|
|
142
140
|
* Records the delete date of one or many given entities.
|
|
143
141
|
*/
|
|
144
142
|
static softRemove(entityOrEntities, options) {
|
|
145
|
-
return this
|
|
146
|
-
.getRepository()
|
|
147
|
-
.softRemove(entityOrEntities, options);
|
|
143
|
+
return this.getRepository().softRemove(entityOrEntities, options);
|
|
148
144
|
}
|
|
149
145
|
/**
|
|
150
146
|
* Inserts a given entity into the database.
|
|
@@ -152,8 +148,8 @@ export class BaseEntity {
|
|
|
152
148
|
* Executes fast and efficient INSERT query.
|
|
153
149
|
* Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted.
|
|
154
150
|
*/
|
|
155
|
-
static insert(entity
|
|
156
|
-
return this.getRepository().insert(entity
|
|
151
|
+
static insert(entity) {
|
|
152
|
+
return this.getRepository().insert(entity);
|
|
157
153
|
}
|
|
158
154
|
/**
|
|
159
155
|
* Updates entity partially. Entity can be found by a given conditions.
|
|
@@ -161,10 +157,8 @@ export class BaseEntity {
|
|
|
161
157
|
* Executes fast and efficient UPDATE query.
|
|
162
158
|
* Does not check if entity exist in the database.
|
|
163
159
|
*/
|
|
164
|
-
static update(criteria, partialEntity
|
|
165
|
-
return this
|
|
166
|
-
.getRepository()
|
|
167
|
-
.update(criteria, partialEntity, options);
|
|
160
|
+
static update(criteria, partialEntity) {
|
|
161
|
+
return this.getRepository().update(criteria, partialEntity);
|
|
168
162
|
}
|
|
169
163
|
/**
|
|
170
164
|
* Inserts a given entity into the database, unless a unique constraint conflicts then updates the entity
|
|
@@ -180,8 +174,8 @@ export class BaseEntity {
|
|
|
180
174
|
* Executes fast and efficient DELETE query.
|
|
181
175
|
* Does not check if entity exist in the database.
|
|
182
176
|
*/
|
|
183
|
-
static delete(criteria
|
|
184
|
-
return this.getRepository().delete(criteria
|
|
177
|
+
static delete(criteria) {
|
|
178
|
+
return this.getRepository().delete(criteria);
|
|
185
179
|
}
|
|
186
180
|
/**
|
|
187
181
|
* Counts entities that match given options.
|
|
@@ -193,7 +187,7 @@ export class BaseEntity {
|
|
|
193
187
|
* Counts entities that match given WHERE conditions.
|
|
194
188
|
*/
|
|
195
189
|
static countBy(where) {
|
|
196
|
-
return this.getRepository().
|
|
190
|
+
return this.getRepository().countBy(where);
|
|
197
191
|
}
|
|
198
192
|
/**
|
|
199
193
|
* Finds entities that match given options.
|
|
@@ -205,7 +199,7 @@ export class BaseEntity {
|
|
|
205
199
|
* Finds entities that match given WHERE conditions.
|
|
206
200
|
*/
|
|
207
201
|
static findBy(where) {
|
|
208
|
-
return this.getRepository().
|
|
202
|
+
return this.getRepository().findBy(where);
|
|
209
203
|
}
|
|
210
204
|
/**
|
|
211
205
|
* Finds entities that match given find options.
|
|
@@ -221,7 +215,7 @@ export class BaseEntity {
|
|
|
221
215
|
* but ignores pagination settings (from and take options).
|
|
222
216
|
*/
|
|
223
217
|
static findAndCountBy(where) {
|
|
224
|
-
return this.getRepository().
|
|
218
|
+
return this.getRepository().findAndCountBy(where);
|
|
225
219
|
}
|
|
226
220
|
/**
|
|
227
221
|
* Finds entities by ids.
|
|
@@ -234,7 +228,7 @@ export class BaseEntity {
|
|
|
234
228
|
* })
|
|
235
229
|
*/
|
|
236
230
|
static findByIds(ids) {
|
|
237
|
-
return this.getRepository().
|
|
231
|
+
return this.getRepository().findByIds(ids);
|
|
238
232
|
}
|
|
239
233
|
/**
|
|
240
234
|
* Finds first entity that matches given conditions.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../browser/src/repository/BaseEntity.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAc1C,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAA;AAKjD;;GAEG;AACH,MAAM,OAAO,UAAU;IAWnB,4EAA4E;IAC5E,iBAAiB;IACjB,4EAA4E;IAE5E;;;OAGG;IACH,KAAK;QACD,OAAQ,IAAI,CAAC,WAAmB,CAAC,aAAa,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;IAChE,CAAC;IAED;;;OAGG;IACH,IAAI,CAAC,OAAqB;QACtB,OAAQ,IAAI,CAAC,WAAmB,CAAC,aAAa,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;IACxE,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,OAAuB;QAC1B,OAAQ,IAAI,CAAC,WAAmB,CAAC,aAAa,EAAE,CAAC,MAAM,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;IAC1E,CAAC;IAED;;OAEG;IACH,UAAU,CAAC,OAAqB;QAC5B,OAAQ,IAAI,CAAC,WAAmB;aAC3B,aAAa,EAAE;aACf,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;IAClC,CAAC;IAED;;OAEG;IACH,OAAO,CAAC,OAAqB;QACzB,OAAQ,IAAI,CAAC,WAAmB,CAAC,aAAa,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;IAC3E,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,MAAM;QACR,MAAM,IAAI,GAAQ,IAAI,CAAC,WAAW,CAAA;QAClC,MAAM,YAAY,GAAe,MAAM,IAAI;aACtC,aAAa,EAAE;aACf,aAAa,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAA;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E3B,OAAQ,IAAY,CAAC,aAAa,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,CAAA;IACzD,CAAC;IAED;;;;;;;;OAQG;IACH,MAAM,CAAC,WAAW,CAEd,EAAsC;QAEtC,OAAQ,IAAY,CAAC,aAAa,EAAE,CAAC,WAAW,CAAC,EAAE,CAAC,CAAA;IACxD,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,aAAa,CAEhB,OAA0B;QAE1B,OAAQ,IAAY,CAAC,aAAa,EAAE,CAAC,aAAa,CAAC,OAAO,CAAC,CAAA;IAC/D,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,eAAe,CAElB,KAA2B;QAE3B,OAAQ,IAAY,CAAC,aAAa,EAAE,CAAC,eAAe,CAAC,KAAK,CAAC,CAAA;IAC/D,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,KAAK,CAER,KAAa,EACb,UAAkB;QAElB,OAAQ,IAAY,CAAC,aAAa,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,UAAU,CAAC,CAAA;IACjE,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,KAAK;QACR,OAAQ,IAAY,CAAC,aAAa,EAAE,CAAC,KAAK,EAAE,CAAA;IAChD,CAAC;CACJ","file":"BaseEntity.js","sourcesContent":["import { Repository } from \"./Repository\"\nimport { getConnection } from \"../globals\"\nimport { FindOptionsWhere } from \"../find-options/FindOptionsWhere\"\nimport { DeepPartial } from \"../common/DeepPartial\"\nimport { SaveOptions } from \"./SaveOptions\"\nimport { FindOneOptions } from \"../find-options/FindOneOptions\"\nimport { RemoveOptions } from \"./RemoveOptions\"\nimport { FindManyOptions } from \"../find-options/FindManyOptions\"\nimport { DataSource } from \"../data-source/DataSource\"\nimport { ObjectType } from \"../common/ObjectType\"\nimport { SelectQueryBuilder } from \"../query-builder/SelectQueryBuilder\"\nimport { InsertResult } from \"../query-builder/result/InsertResult\"\nimport { UpdateResult } from \"../query-builder/result/UpdateResult\"\nimport { DeleteResult } from \"../query-builder/result/DeleteResult\"\nimport { ObjectID } from \"../driver/mongodb/typings\"\nimport { ObjectUtils } from \"../util/ObjectUtils\"\nimport { QueryDeepPartialEntity } from \"../query-builder/QueryPartialEntity\"\nimport { UpsertOptions } from \"./UpsertOptions\"\nimport { EntityTarget } from \"../common/EntityTarget\"\n\n/**\n * Base abstract entity for all entities, used in ActiveRecord patterns.\n */\nexport class BaseEntity {\n // -------------------------------------------------------------------------\n // Private Static Properties\n // -------------------------------------------------------------------------\n\n /**\n * Connection used in all static methods of the BaseEntity.\n */\n // @ts-ignore: Unused variable which is actually used\n private static usedConnection?: DataSource\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n /**\n * Checks if entity has an id.\n * If entity composite compose ids, it will check them all.\n */\n hasId(): boolean {\n return (this.constructor as any).getRepository().hasId(this)\n }\n\n /**\n * Saves current entity in the database.\n * If entity does not exist in the database then inserts, otherwise updates.\n */\n save(options?: SaveOptions): Promise<this> {\n return (this.constructor as any).getRepository().save(this, options)\n }\n\n /**\n * Removes current entity from the database.\n */\n remove(options?: RemoveOptions): Promise<this> {\n return (this.constructor as any).getRepository().remove(this, options)\n }\n\n /**\n * Records the delete date of current entity.\n */\n softRemove(options?: SaveOptions): Promise<this> {\n return (this.constructor as any)\n .getRepository()\n .softRemove(this, options)\n }\n\n /**\n * Recovers a given entity in the database.\n */\n recover(options?: SaveOptions): Promise<this> {\n return (this.constructor as any).getRepository().recover(this, options)\n }\n\n /**\n * Reloads entity data from the database.\n */\n async reload(): Promise<void> {\n const base: any = this.constructor\n const newestEntity: BaseEntity = await base\n .getRepository()\n .findOneOrFail(base.getId(this))\n\n ObjectUtils.assign(this, newestEntity)\n }\n\n // -------------------------------------------------------------------------\n // Public Static Methods\n // -------------------------------------------------------------------------\n\n /**\n * Sets connection to be used by entity.\n */\n static useConnection(connection: DataSource) {\n this.usedConnection = connection\n }\n\n /**\n * Gets current entity's Repository.\n */\n static getRepository<T extends BaseEntity>(\n this: ObjectType<T>,\n ): Repository<T> {\n const connection: DataSource =\n (this as any).usedConnection || getConnection()\n return connection.getRepository<T>(this)\n }\n\n /**\n * Returns object that is managed by this repository.\n * If this repository manages entity from schema,\n * then it returns a name of that schema instead.\n */\n static get target(): EntityTarget<any> {\n return this.getRepository().target\n }\n\n /**\n * Checks entity has an id.\n * If entity composite compose ids, it will check them all.\n */\n static hasId(entity: BaseEntity): boolean {\n return this.getRepository().hasId(entity)\n }\n\n /**\n * Gets entity mixed id.\n */\n static getId<T extends BaseEntity>(this: ObjectType<T>, entity: T): any {\n return (this as any).getRepository().getId(entity)\n }\n\n /**\n * Creates a new query builder that can be used to build a SQL query.\n */\n static createQueryBuilder<T extends BaseEntity>(\n this: ObjectType<T>,\n alias?: string,\n ): SelectQueryBuilder<T> {\n return (this as any).getRepository().createQueryBuilder(alias)\n }\n\n /**\n * Creates a new entity instance.\n */\n static create<T extends BaseEntity>(\n this: ObjectType<T> & typeof BaseEntity,\n ): T\n\n /**\n * Creates a new entities and copies all entity properties from given objects into their new entities.\n * Note that it copies only properties that present in entity schema.\n */\n static create<T extends BaseEntity>(\n this: ObjectType<T> & typeof BaseEntity,\n entityLikeArray: DeepPartial<T>[],\n ): T[]\n\n /**\n * Creates a new entity instance and copies all entity properties from this object into a new entity.\n * Note that it copies only properties that present in entity schema.\n */\n static create<T extends BaseEntity>(\n this: ObjectType<T> & typeof BaseEntity,\n entityLike: DeepPartial<T>,\n ): T\n /**\n * Creates a new entity instance and copies all entity properties from this object into a new entity.\n * Note that it copies only properties that present in entity schema.\n */\n static create<T extends BaseEntity>(\n this: ObjectType<T> & typeof BaseEntity,\n entityOrEntities?: any,\n ): T {\n return (this as any).getRepository().create(entityOrEntities)\n }\n\n /**\n * Merges multiple entities (or entity-like objects) into a given entity.\n */\n static merge<T extends BaseEntity>(\n this: ObjectType<T>,\n mergeIntoEntity: T,\n ...entityLikes: DeepPartial<T>[]\n ): T {\n return (this as any)\n .getRepository()\n .merge(mergeIntoEntity, ...entityLikes)\n }\n\n /**\n * Creates a new entity from the given plain javascript object. If entity already exist in the database, then\n * it loads it (and everything related to it), replaces all values with the new ones from the given object\n * and returns this new entity. This new entity is actually a loaded from the db entity with all properties\n * replaced from the new object.\n *\n * Note that given entity-like object must have an entity id / primary key to find entity by.\n * Returns undefined if entity with given id was not found.\n */\n static preload<T extends BaseEntity>(\n this: ObjectType<T>,\n entityLike: DeepPartial<T>,\n ): Promise<T | undefined> {\n const thisRepository = (this as any).getRepository() as Repository<T>\n return thisRepository.preload(entityLike)\n }\n\n /**\n * Saves all given entities in the database.\n * If entities do not exist in the database then inserts, otherwise updates.\n */\n static save<T extends BaseEntity>(\n this: ObjectType<T>,\n entities: T[],\n options?: SaveOptions,\n ): Promise<T[]>\n\n /**\n * Saves a given entity in the database.\n * If entity does not exist in the database then inserts, otherwise updates.\n */\n static save<T extends BaseEntity>(\n this: ObjectType<T>,\n entity: T,\n options?: SaveOptions,\n ): Promise<T>\n\n /**\n * Saves one or many given entities.\n */\n static save<T extends BaseEntity>(\n this: ObjectType<T>,\n entityOrEntities: T | T[],\n options?: SaveOptions,\n ): Promise<T | T[]> {\n return (this as any)\n .getRepository()\n .save(entityOrEntities as any, options)\n }\n\n /**\n * Removes a given entities from the database.\n */\n static remove<T extends BaseEntity>(\n this: ObjectType<T>,\n entities: T[],\n options?: RemoveOptions,\n ): Promise<T[]>\n\n /**\n * Removes a given entity from the database.\n */\n static remove<T extends BaseEntity>(\n this: ObjectType<T>,\n entity: T,\n options?: RemoveOptions,\n ): Promise<T>\n\n /**\n * Removes one or many given entities.\n */\n static remove<T extends BaseEntity>(\n this: ObjectType<T>,\n entityOrEntities: T | T[],\n options?: RemoveOptions,\n ): Promise<T | T[]> {\n return (this as any)\n .getRepository()\n .remove(entityOrEntities as any, options)\n }\n\n /**\n * Records the delete date of all given entities.\n */\n static softRemove<T extends BaseEntity>(\n this: ObjectType<T>,\n entities: T[],\n options?: SaveOptions,\n ): Promise<T[]>\n\n /**\n * Records the delete date of a given entity.\n */\n static softRemove<T extends BaseEntity>(\n this: ObjectType<T>,\n entity: T,\n options?: SaveOptions,\n ): Promise<T>\n\n /**\n * Records the delete date of one or many given entities.\n */\n static softRemove<T extends BaseEntity>(\n this: ObjectType<T>,\n entityOrEntities: T | T[],\n options?: SaveOptions,\n ): Promise<T | T[]> {\n return (this as any)\n .getRepository()\n .softRemove(entityOrEntities as any, options)\n }\n\n /**\n * Inserts a given entity into the database.\n * Unlike save method executes a primitive operation without cascades, relations and other operations included.\n * Executes fast and efficient INSERT query.\n * Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted.\n */\n static insert<T extends BaseEntity>(\n this: ObjectType<T>,\n entity: QueryDeepPartialEntity<T> | QueryDeepPartialEntity<T>[],\n options?: SaveOptions,\n ): Promise<InsertResult> {\n return (this as any).getRepository().insert(entity, options)\n }\n\n /**\n * Updates entity partially. Entity can be found by a given conditions.\n * Unlike save method executes a primitive operation without cascades, relations and other operations included.\n * Executes fast and efficient UPDATE query.\n * Does not check if entity exist in the database.\n */\n static update<T extends BaseEntity>(\n this: ObjectType<T>,\n criteria:\n | string\n | string[]\n | number\n | number[]\n | Date\n | Date[]\n | ObjectID\n | ObjectID[]\n | FindOptionsWhere<T>,\n partialEntity: QueryDeepPartialEntity<T>,\n options?: SaveOptions,\n ): Promise<UpdateResult> {\n return (this as any)\n .getRepository()\n .update(criteria, partialEntity, options)\n }\n\n /**\n * Inserts a given entity into the database, unless a unique constraint conflicts then updates the entity\n * Unlike save method executes a primitive operation without cascades, relations and other operations included.\n * Executes fast and efficient INSERT ... ON CONFLICT DO UPDATE/ON DUPLICATE KEY UPDATE query.\n */\n static upsert<T extends BaseEntity>(\n this: ObjectType<T> & typeof BaseEntity,\n entityOrEntities:\n | QueryDeepPartialEntity<T>\n | QueryDeepPartialEntity<T>[],\n conflictPathsOrOptions: string[] | UpsertOptions<T>,\n ): Promise<InsertResult> {\n return this.getRepository<T>().upsert(\n entityOrEntities,\n conflictPathsOrOptions,\n )\n }\n\n /**\n * Deletes entities by a given criteria.\n * Unlike remove method executes a primitive operation without cascades, relations and other operations included.\n * Executes fast and efficient DELETE query.\n * Does not check if entity exist in the database.\n */\n static delete<T extends BaseEntity>(\n this: ObjectType<T>,\n criteria:\n | string\n | string[]\n | number\n | number[]\n | Date\n | Date[]\n | ObjectID\n | ObjectID[]\n | FindOptionsWhere<T>,\n options?: RemoveOptions,\n ): Promise<DeleteResult> {\n return (this as any).getRepository().delete(criteria, options)\n }\n\n /**\n * Counts entities that match given options.\n */\n static count<T extends BaseEntity>(\n this: ObjectType<T>,\n options?: FindManyOptions<T>,\n ): Promise<number> {\n return (this as any).getRepository().count(options)\n }\n\n /**\n * Counts entities that match given WHERE conditions.\n */\n static countBy<T extends BaseEntity>(\n this: ObjectType<T>,\n where: FindOptionsWhere<T>,\n ): Promise<number> {\n return (this as any).getRepository().count(where)\n }\n\n /**\n * Finds entities that match given options.\n */\n static find<T extends BaseEntity>(\n this: ObjectType<T>,\n options?: FindManyOptions<T>,\n ): Promise<T[]> {\n return (this as any).getRepository().find(options)\n }\n\n /**\n * Finds entities that match given WHERE conditions.\n */\n static findBy<T extends BaseEntity>(\n this: ObjectType<T>,\n where: FindOptionsWhere<T>,\n ): Promise<T[]> {\n return (this as any).getRepository().find(where)\n }\n\n /**\n * Finds entities that match given find options.\n * Also counts all entities that match given conditions,\n * but ignores pagination settings (from and take options).\n */\n static findAndCount<T extends BaseEntity>(\n this: ObjectType<T>,\n options?: FindManyOptions<T>,\n ): Promise<[T[], number]> {\n return (this as any).getRepository().findAndCount(options)\n }\n\n /**\n * Finds entities that match given WHERE conditions.\n * Also counts all entities that match given conditions,\n * but ignores pagination settings (from and take options).\n */\n static findAndCountBy<T extends BaseEntity>(\n this: ObjectType<T>,\n where: FindOptionsWhere<T>,\n ): Promise<[T[], number]> {\n return (this as any).getRepository().findAndCount(where)\n }\n\n /**\n * Finds entities by ids.\n * Optionally find options can be applied.\n *\n * @deprecated use `findBy` method instead in conjunction with `In` operator, for example:\n *\n * .findBy({\n * id: In([1, 2, 3])\n * })\n */\n static findByIds<T extends BaseEntity>(\n this: ObjectType<T>,\n ids: any[],\n ): Promise<T[]> {\n return (this as any).getRepository().findByIdsBy(ids)\n }\n\n /**\n * Finds first entity that matches given conditions.\n */\n static findOne<T extends BaseEntity>(\n this: ObjectType<T>,\n options: FindOneOptions<T>,\n ): Promise<T | null> {\n return (this as any).getRepository().findOne(options)\n }\n\n /**\n * Finds first entity that matches given conditions.\n */\n static findOneBy<T extends BaseEntity>(\n this: ObjectType<T>,\n where?: FindOptionsWhere<T>,\n ): Promise<T | null> {\n return (this as any).getRepository().findOneBy(where)\n }\n\n /**\n * Finds first entity that matches given options.\n *\n * @deprecated use `findOneBy` method instead in conjunction with `In` operator, for example:\n *\n * .findOneBy({\n * id: 1 // where \"id\" is your primary column name\n * })\n */\n static findOneById<T extends BaseEntity>(\n this: ObjectType<T>,\n id?: string | number | Date | ObjectID,\n ): Promise<T | null> {\n return (this as any).getRepository().findOneById(id)\n }\n\n /**\n * Finds first entity that matches given conditions.\n */\n static findOneOrFail<T extends BaseEntity>(\n this: ObjectType<T>,\n options: FindOneOptions<T>,\n ): Promise<T> {\n return (this as any).getRepository().findOneOrFail(options)\n }\n\n /**\n * Finds first entity that matches given conditions.\n */\n static findOneByOrFail<T extends BaseEntity>(\n this: ObjectType<T>,\n where?: FindOptionsWhere<T>,\n ): Promise<T> {\n return (this as any).getRepository().findOneByOrFail(where)\n }\n\n /**\n * Executes a raw SQL query and returns a raw database results.\n * Raw query execution is supported only by relational databases (MongoDB is not supported).\n */\n static query<T extends BaseEntity>(\n this: ObjectType<T>,\n query: string,\n parameters?: any[],\n ): Promise<any> {\n return (this as any).getRepository().query(query, parameters)\n }\n\n /**\n * Clears all the data from the given table/collection (truncates/drops it).\n */\n static clear<T extends BaseEntity>(this: ObjectType<T>): Promise<void> {\n return (this as any).getRepository().clear()\n }\n}\n"],"sourceRoot":".."}
|
|
1
|
+
{"version":3,"s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;QAE1B,OAAO,IAAI,CAAC,aAAa,EAAK,CAAC,SAAS,CAAC,KAAK,CAAC,CAAA;IACnD,CAAC;IAED;;;;;;;;OAQG;IACH,MAAM,CAAC,WAAW,CAEd,EAAqC;QAErC,OAAO,IAAI,CAAC,aAAa,EAAK,CAAC,WAAW,CAAC,EAAE,CAAC,CAAA;IAClD,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,aAAa,CAEhB,OAA0B;QAE1B,OAAO,IAAI,CAAC,aAAa,EAAK,CAAC,aAAa,CAAC,OAAO,CAAC,CAAA;IACzD,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,eAAe,CAElB,KAA0B;QAE1B,OAAO,IAAI,CAAC,aAAa,EAAK,CAAC,eAAe,CAAC,KAAK,CAAC,CAAA;IACzD,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,KAAK,CAER,KAAa,EACb,UAAkB;QAElB,OAAO,IAAI,CAAC,aAAa,EAAK,CAAC,KAAK,CAAC,KAAK,EAAE,UAAU,CAAC,CAAA;IAC3D,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,KAAK;QAGR,OAAO,IAAI,CAAC,aAAa,EAAK,CAAC,KAAK,EAAE,CAAA;IAC1C,CAAC;CACJ","file":"BaseEntity.js","sourcesContent":["import { Repository } from \"./Repository\"\nimport { FindOptionsWhere } from \"../find-options/FindOptionsWhere\"\nimport { DeepPartial } from \"../common/DeepPartial\"\nimport { SaveOptions } from \"./SaveOptions\"\nimport { FindOneOptions } from \"../find-options/FindOneOptions\"\nimport { RemoveOptions } from \"./RemoveOptions\"\nimport { FindManyOptions } from \"../find-options/FindManyOptions\"\nimport { DataSource } from \"../data-source\"\nimport { SelectQueryBuilder } from \"../query-builder/SelectQueryBuilder\"\nimport { InsertResult } from \"../query-builder/result/InsertResult\"\nimport { UpdateResult } from \"../query-builder/result/UpdateResult\"\nimport { DeleteResult } from \"../query-builder/result/DeleteResult\"\nimport { ObjectID } from \"../driver/mongodb/typings\"\nimport { ObjectUtils } from \"../util/ObjectUtils\"\nimport { QueryDeepPartialEntity } from \"../query-builder/QueryPartialEntity\"\nimport { UpsertOptions } from \"./UpsertOptions\"\nimport { EntityTarget } from \"../common/EntityTarget\"\n\n/**\n * Base abstract entity for all entities, used in ActiveRecord patterns.\n */\nexport class BaseEntity {\n // -------------------------------------------------------------------------\n // Private Static Properties\n // -------------------------------------------------------------------------\n\n /**\n * DataSource used in all static methods of the BaseEntity.\n */\n // @ts-ignore: Unused variable which is actually used\n private static dataSource?: DataSource\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n /**\n * Checks if entity has an id.\n * If entity composite compose ids, it will check them all.\n */\n hasId(): boolean {\n const baseEntity = this.constructor as typeof BaseEntity\n return baseEntity.getRepository().hasId(this)\n }\n\n /**\n * Saves current entity in the database.\n * If entity does not exist in the database then inserts, otherwise updates.\n */\n save(options?: SaveOptions): Promise<this> {\n const baseEntity = this.constructor as typeof BaseEntity\n return baseEntity.getRepository().save(this, options)\n }\n\n /**\n * Removes current entity from the database.\n */\n remove(options?: RemoveOptions): Promise<this> {\n const baseEntity = this.constructor as typeof BaseEntity\n return baseEntity.getRepository().remove(this, options) as Promise<this>\n }\n\n /**\n * Records the delete date of current entity.\n */\n softRemove(options?: SaveOptions): Promise<this> {\n const baseEntity = this.constructor as typeof BaseEntity\n return baseEntity.getRepository().softRemove(this, options)\n }\n\n /**\n * Recovers a given entity in the database.\n */\n recover(options?: SaveOptions): Promise<this> {\n const baseEntity = this.constructor as typeof BaseEntity\n return baseEntity.getRepository().recover(this, options)\n }\n\n /**\n * Reloads entity data from the database.\n */\n async reload(): Promise<void> {\n const baseEntity = this.constructor as typeof BaseEntity\n const newestEntity: BaseEntity = await baseEntity\n .getRepository()\n .findOneOrFail(baseEntity.getId(this))\n\n ObjectUtils.assign(this, newestEntity)\n }\n\n // -------------------------------------------------------------------------\n // Public Static Methods\n // -------------------------------------------------------------------------\n\n /**\n * Sets DataSource to be used by entity.\n */\n static useDataSource(dataSource: DataSource) {\n this.dataSource = dataSource\n }\n\n /**\n * Gets current entity's Repository.\n */\n static getRepository<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n ): Repository<T> {\n const dataSource = (this as typeof BaseEntity).dataSource\n if (!dataSource)\n throw new Error(`DataSource is not set for this entity.`)\n return dataSource.getRepository<T>(this)\n }\n\n /**\n * Returns object that is managed by this repository.\n * If this repository manages entity from schema,\n * then it returns a name of that schema instead.\n */\n static get target(): EntityTarget<any> {\n return this.getRepository().target\n }\n\n /**\n * Checks entity has an id.\n * If entity composite compose ids, it will check them all.\n */\n static hasId(entity: BaseEntity): boolean {\n return this.getRepository().hasId(entity)\n }\n\n /**\n * Gets entity mixed id.\n */\n static getId<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entity: T,\n ): any {\n return this.getRepository<T>().getId(entity)\n }\n\n /**\n * Creates a new query builder that can be used to build a SQL query.\n */\n static createQueryBuilder<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n alias?: string,\n ): SelectQueryBuilder<T> {\n return this.getRepository<T>().createQueryBuilder(alias)\n }\n\n /**\n * Creates a new entity instance.\n */\n static create<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n ): T\n\n /**\n * Creates a new entities and copies all entity properties from given objects into their new entities.\n * Note that it copies only properties that present in entity schema.\n */\n static create<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entityLikeArray: DeepPartial<T>[],\n ): T[]\n\n /**\n * Creates a new entity instance and copies all entity properties from this object into a new entity.\n * Note that it copies only properties that present in entity schema.\n */\n static create<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entityLike: DeepPartial<T>,\n ): T\n\n /**\n * Creates a new entity instance and copies all entity properties from this object into a new entity.\n * Note that it copies only properties that present in entity schema.\n */\n static create<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entityOrEntities?: any,\n ) {\n return this.getRepository<T>().create(entityOrEntities)\n }\n\n /**\n * Merges multiple entities (or entity-like objects) into a given entity.\n */\n static merge<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n mergeIntoEntity: T,\n ...entityLikes: DeepPartial<T>[]\n ): T {\n return this.getRepository<T>().merge(\n mergeIntoEntity,\n ...entityLikes,\n ) as T\n }\n\n /**\n * Creates a new entity from the given plain javascript object. If entity already exist in the database, then\n * it loads it (and everything related to it), replaces all values with the new ones from the given object\n * and returns this new entity. This new entity is actually a loaded from the db entity with all properties\n * replaced from the new object.\n *\n * Note that given entity-like object must have an entity id / primary key to find entity by.\n * Returns undefined if entity with given id was not found.\n */\n static preload<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entityLike: DeepPartial<T>,\n ): Promise<T | undefined> {\n const thisRepository = this.getRepository() as Repository<T>\n return thisRepository.preload(entityLike)\n }\n\n /**\n * Saves all given entities in the database.\n * If entities do not exist in the database then inserts, otherwise updates.\n */\n static save<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entities: DeepPartial<T>[],\n options?: SaveOptions,\n ): Promise<T[]>\n\n /**\n * Saves a given entity in the database.\n * If entity does not exist in the database then inserts, otherwise updates.\n */\n static save<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entity: DeepPartial<T>,\n options?: SaveOptions,\n ): Promise<T>\n\n /**\n * Saves one or many given entities.\n */\n static save<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entityOrEntities: DeepPartial<T> | DeepPartial<T>[],\n options?: SaveOptions,\n ) {\n return this.getRepository<T>().save(entityOrEntities as any, options)\n }\n\n /**\n * Removes a given entities from the database.\n */\n static remove<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entities: T[],\n options?: RemoveOptions,\n ): Promise<T[]>\n\n /**\n * Removes a given entity from the database.\n */\n static remove<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entity: T,\n options?: RemoveOptions,\n ): Promise<T>\n\n /**\n * Removes one or many given entities.\n */\n static remove<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entityOrEntities: T | T[],\n options?: RemoveOptions,\n ) {\n return this.getRepository<T>().remove(entityOrEntities as any, options)\n }\n\n /**\n * Records the delete date of all given entities.\n */\n static softRemove<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entities: T[],\n options?: SaveOptions,\n ): Promise<T[]>\n\n /**\n * Records the delete date of a given entity.\n */\n static softRemove<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entity: T,\n options?: SaveOptions,\n ): Promise<T>\n\n /**\n * Records the delete date of one or many given entities.\n */\n static softRemove<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entityOrEntities: T | T[],\n options?: SaveOptions,\n ) {\n return this.getRepository<T>().softRemove(\n entityOrEntities as any,\n options,\n )\n }\n\n /**\n * Inserts a given entity into the database.\n * Unlike save method executes a primitive operation without cascades, relations and other operations included.\n * Executes fast and efficient INSERT query.\n * Does not check if entity exist in the database, so query will fail if duplicate entity is being inserted.\n */\n static insert<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entity: QueryDeepPartialEntity<T> | QueryDeepPartialEntity<T>[],\n ): Promise<InsertResult> {\n return this.getRepository<T>().insert(entity)\n }\n\n /**\n * Updates entity partially. Entity can be found by a given conditions.\n * Unlike save method executes a primitive operation without cascades, relations and other operations included.\n * Executes fast and efficient UPDATE query.\n * Does not check if entity exist in the database.\n */\n static update<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n criteria:\n | string\n | string[]\n | number\n | number[]\n | Date\n | Date[]\n | ObjectID\n | ObjectID[]\n | FindOptionsWhere<T>,\n partialEntity: QueryDeepPartialEntity<T>,\n ): Promise<UpdateResult> {\n return this.getRepository<T>().update(criteria, partialEntity)\n }\n\n /**\n * Inserts a given entity into the database, unless a unique constraint conflicts then updates the entity\n * Unlike save method executes a primitive operation without cascades, relations and other operations included.\n * Executes fast and efficient INSERT ... ON CONFLICT DO UPDATE/ON DUPLICATE KEY UPDATE query.\n */\n static upsert<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n entityOrEntities:\n | QueryDeepPartialEntity<T>\n | QueryDeepPartialEntity<T>[],\n conflictPathsOrOptions: string[] | UpsertOptions<T>,\n ): Promise<InsertResult> {\n return this.getRepository<T>().upsert(\n entityOrEntities,\n conflictPathsOrOptions,\n )\n }\n\n /**\n * Deletes entities by a given criteria.\n * Unlike remove method executes a primitive operation without cascades, relations and other operations included.\n * Executes fast and efficient DELETE query.\n * Does not check if entity exist in the database.\n */\n static delete<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n criteria:\n | string\n | string[]\n | number\n | number[]\n | Date\n | Date[]\n | ObjectID\n | ObjectID[]\n | FindOptionsWhere<T>,\n ): Promise<DeleteResult> {\n return this.getRepository<T>().delete(criteria)\n }\n\n /**\n * Counts entities that match given options.\n */\n static count<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n options?: FindManyOptions<T>,\n ): Promise<number> {\n return this.getRepository<T>().count(options)\n }\n\n /**\n * Counts entities that match given WHERE conditions.\n */\n static countBy<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n where: FindOptionsWhere<T>,\n ): Promise<number> {\n return this.getRepository<T>().countBy(where)\n }\n\n /**\n * Finds entities that match given options.\n */\n static find<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n options?: FindManyOptions<T>,\n ): Promise<T[]> {\n return this.getRepository<T>().find(options)\n }\n\n /**\n * Finds entities that match given WHERE conditions.\n */\n static findBy<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n where: FindOptionsWhere<T>,\n ): Promise<T[]> {\n return this.getRepository<T>().findBy(where)\n }\n\n /**\n * Finds entities that match given find options.\n * Also counts all entities that match given conditions,\n * but ignores pagination settings (from and take options).\n */\n static findAndCount<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n options?: FindManyOptions<T>,\n ): Promise<[T[], number]> {\n return this.getRepository<T>().findAndCount(options)\n }\n\n /**\n * Finds entities that match given WHERE conditions.\n * Also counts all entities that match given conditions,\n * but ignores pagination settings (from and take options).\n */\n static findAndCountBy<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n where: FindOptionsWhere<T>,\n ): Promise<[T[], number]> {\n return this.getRepository<T>().findAndCountBy(where)\n }\n\n /**\n * Finds entities by ids.\n * Optionally find options can be applied.\n *\n * @deprecated use `findBy` method instead in conjunction with `In` operator, for example:\n *\n * .findBy({\n * id: In([1, 2, 3])\n * })\n */\n static findByIds<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n ids: any[],\n ): Promise<T[]> {\n return this.getRepository<T>().findByIds(ids)\n }\n\n /**\n * Finds first entity that matches given conditions.\n */\n static findOne<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n options: FindOneOptions<T>,\n ): Promise<T | null> {\n return this.getRepository<T>().findOne(options)\n }\n\n /**\n * Finds first entity that matches given conditions.\n */\n static findOneBy<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n where: FindOptionsWhere<T>,\n ): Promise<T | null> {\n return this.getRepository<T>().findOneBy(where)\n }\n\n /**\n * Finds first entity that matches given options.\n *\n * @deprecated use `findOneBy` method instead in conjunction with `In` operator, for example:\n *\n * .findOneBy({\n * id: 1 // where \"id\" is your primary column name\n * })\n */\n static findOneById<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n id: string | number | Date | ObjectID,\n ): Promise<T | null> {\n return this.getRepository<T>().findOneById(id)\n }\n\n /**\n * Finds first entity that matches given conditions.\n */\n static findOneOrFail<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n options: FindOneOptions<T>,\n ): Promise<T> {\n return this.getRepository<T>().findOneOrFail(options)\n }\n\n /**\n * Finds first entity that matches given conditions.\n */\n static findOneByOrFail<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n where: FindOptionsWhere<T>,\n ): Promise<T> {\n return this.getRepository<T>().findOneByOrFail(where)\n }\n\n /**\n * Executes a raw SQL query and returns a raw database results.\n * Raw query execution is supported only by relational databases (MongoDB is not supported).\n */\n static query<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n query: string,\n parameters?: any[],\n ): Promise<any> {\n return this.getRepository<T>().query(query, parameters)\n }\n\n /**\n * Clears all the data from the given table/collection (truncates/drops it).\n */\n static clear<T extends BaseEntity>(\n this: { new (): T } & typeof BaseEntity,\n ): Promise<void> {\n return this.getRepository<T>().clear()\n }\n}\n"],"sourceRoot":".."}
|
|
@@ -27,6 +27,7 @@ import type { EntityMetadata } from "../metadata/EntityMetadata";
|
|
|
27
27
|
import type { ColumnMetadata } from "../metadata/ColumnMetadata";
|
|
28
28
|
import type { MssqlParameter } from "../driver/sqlserver/MssqlParameter";
|
|
29
29
|
import { DataSource } from "../data-source";
|
|
30
|
+
import { BaseEntity } from "../repository/BaseEntity";
|
|
30
31
|
export declare class InstanceChecker {
|
|
31
32
|
static isMssqlParameter(obj: unknown): obj is MssqlParameter;
|
|
32
33
|
static isEntityMetadata(obj: unknown): obj is EntityMetadata;
|
|
@@ -45,6 +46,7 @@ export declare class InstanceChecker {
|
|
|
45
46
|
static isMongoEntityManager(obj: unknown): obj is MongoEntityManager;
|
|
46
47
|
static isSqljsEntityManager(obj: unknown): obj is SqljsEntityManager;
|
|
47
48
|
static isEntitySchema(obj: unknown): obj is EntitySchema;
|
|
49
|
+
static isBaseEntityConstructor(obj: unknown): obj is typeof BaseEntity;
|
|
48
50
|
static isFindOperator(obj: unknown): obj is FindOperator<any>;
|
|
49
51
|
static isEqualOperator(obj: unknown): obj is EqualOperator<any>;
|
|
50
52
|
static isQuery(obj: unknown): obj is Query;
|
|
@@ -56,6 +56,12 @@ export class InstanceChecker {
|
|
|
56
56
|
static isEntitySchema(obj) {
|
|
57
57
|
return this.check(obj, "EntitySchema");
|
|
58
58
|
}
|
|
59
|
+
static isBaseEntityConstructor(obj) {
|
|
60
|
+
return (typeof obj === "function" &&
|
|
61
|
+
typeof obj.hasId === "function" &&
|
|
62
|
+
typeof obj.save === "function" &&
|
|
63
|
+
typeof obj.useDataSource === "function");
|
|
64
|
+
}
|
|
59
65
|
static isFindOperator(obj) {
|
|
60
66
|
return (this.check(obj, "FindOperator") || this.check(obj, "EqualOperator"));
|
|
61
67
|
}
|