1
/*!
2
 */
3 5
import "reflect-metadata";
4 5
import {ConnectionManager} from "./connection/ConnectionManager";
5
import {Connection} from "./connection/Connection";
6 5
import {MetadataArgsStorage} from "./metadata-args/MetadataArgsStorage";
7
import {ConnectionOptions} from "./connection/ConnectionOptions";
8 5
import {getFromContainer} from "./container";
9
import {ObjectType} from "./common/ObjectType";
10
import {Repository} from "./repository/Repository";
11
import {EntityManager} from "./entity-manager/EntityManager";
12 5
import {PlatformTools} from "./platform/PlatformTools";
13
import {TreeRepository} from "./repository/TreeRepository";
14
import {MongoRepository} from "./repository/MongoRepository";
15 5
import {ConnectionOptionsReader} from "./connection/ConnectionOptionsReader";
16
import {MongoEntityManager} from "./entity-manager/MongoEntityManager";
17
import {SqljsEntityManager} from "./entity-manager/SqljsEntityManager";
18
import {SelectQueryBuilder} from "./query-builder/SelectQueryBuilder";
19
import {EntityTarget} from "./common/EntityTarget";
20

21
// -------------------------------------------------------------------------
22
// Commonly Used exports
23
// -------------------------------------------------------------------------
24

25 5
export * from "./container";
26
export * from "./common/EntityTarget";
27
export * from "./common/ObjectType";
28
export * from "./common/ObjectLiteral";
29
export * from "./common/DeepPartial";
30 5
export * from "./error/QueryFailedError";
31 5
export * from "./decorator/columns/Column";
32 5
export * from "./decorator/columns/CreateDateColumn";
33 5
export * from "./decorator/columns/DeleteDateColumn";
34 5
export * from "./decorator/columns/PrimaryGeneratedColumn";
35 5
export * from "./decorator/columns/PrimaryColumn";
36 5
export * from "./decorator/columns/UpdateDateColumn";
37 5
export * from "./decorator/columns/VersionColumn";
38 5
export * from "./decorator/columns/ViewColumn";
39 5
export * from "./decorator/columns/ObjectIdColumn";
40 5
export * from "./decorator/listeners/AfterInsert";
41 5
export * from "./decorator/listeners/AfterLoad";
42 5
export * from "./decorator/listeners/AfterRemove";
43 5
export * from "./decorator/listeners/AfterUpdate";
44 5
export * from "./decorator/listeners/BeforeInsert";
45 5
export * from "./decorator/listeners/BeforeRemove";
46 5
export * from "./decorator/listeners/BeforeUpdate";
47 5
export * from "./decorator/listeners/EventSubscriber";
48
export * from "./decorator/options/ColumnOptions";
49
export * from "./decorator/options/IndexOptions";
50
export * from "./decorator/options/JoinColumnOptions";
51
export * from "./decorator/options/JoinTableOptions";
52
export * from "./decorator/options/RelationOptions";
53
export * from "./decorator/options/EntityOptions";
54
export * from "./decorator/options/ValueTransformer";
55 5
export * from "./decorator/relations/JoinColumn";
56 5
export * from "./decorator/relations/JoinTable";
57 5
export * from "./decorator/relations/ManyToMany";
58 5
export * from "./decorator/relations/ManyToOne";
59 5
export * from "./decorator/relations/OneToMany";
60 5
export * from "./decorator/relations/OneToOne";
61 5
export * from "./decorator/relations/RelationCount";
62 5
export * from "./decorator/relations/RelationId";
63 5
export * from "./decorator/entity/Entity";
64 5
export * from "./decorator/entity/ChildEntity";
65 5
export * from "./decorator/entity/TableInheritance";
66 5
export * from "./decorator/entity-view/ViewEntity";
67 5
export * from "./decorator/transaction/Transaction";
68 5
export * from "./decorator/transaction/TransactionManager";
69 5
export * from "./decorator/transaction/TransactionRepository";
70 5
export * from "./decorator/tree/TreeLevelColumn";
71 5
export * from "./decorator/tree/TreeParent";
72 5
export * from "./decorator/tree/TreeChildren";
73 5
export * from "./decorator/tree/Tree";
74 5
export * from "./decorator/Index";
75 5
export * from "./decorator/Unique";
76 5
export * from "./decorator/Check";
77 5
export * from "./decorator/Exclusion";
78 5
export * from "./decorator/Generated";
79 5
export * from "./decorator/EntityRepository";
80 5
export * from "./find-options/operator/Any";
81 5
export * from "./find-options/operator/Between";
82 5
export * from "./find-options/operator/Equal";
83 5
export * from "./find-options/operator/In";
84 5
export * from "./find-options/operator/IsNull";
85 5
export * from "./find-options/operator/LessThan";
86 5
export * from "./find-options/operator/LessThanOrEqual";
87 5
export * from "./find-options/operator/ILike";
88 5
export * from "./find-options/operator/Like";
89 5
export * from "./find-options/operator/MoreThan";
90 5
export * from "./find-options/operator/MoreThanOrEqual";
91 5
export * from "./find-options/operator/Not";
92 5
export * from "./find-options/operator/Raw";
93
export * from "./find-options/FindConditions";
94
export * from "./find-options/FindManyOptions";
95
export * from "./find-options/FindOneOptions";
96 5
export * from "./find-options/FindOperator";
97
export * from "./find-options/FindOperatorType";
98
export * from "./find-options/JoinOptions";
99
export * from "./find-options/OrderByCondition";
100 5
export * from "./find-options/FindOptionsUtils";
101
export * from "./logger/Logger";
102
export * from "./logger/LoggerOptions";
103 5
export * from "./logger/AdvancedConsoleLogger";
104 5
export * from "./logger/SimpleConsoleLogger";
105 5
export * from "./logger/FileLogger";
106 5
export * from "./metadata/EntityMetadata";
107 5
export * from "./entity-manager/EntityManager";
108 5
export * from "./repository/AbstractRepository";
109 5
export * from "./repository/Repository";
110 5
export * from "./repository/BaseEntity";
111 5
export * from "./repository/TreeRepository";
112 5
export * from "./repository/MongoRepository";
113
export * from "./repository/RemoveOptions";
114
export * from "./repository/SaveOptions";
115 5
export * from "./schema-builder/table/TableCheck";
116 5
export * from "./schema-builder/table/TableColumn";
117 5
export * from "./schema-builder/table/TableExclusion";
118 5
export * from "./schema-builder/table/TableForeignKey";
119 5
export * from "./schema-builder/table/TableIndex";
120 5
export * from "./schema-builder/table/TableUnique";
121 5
export * from "./schema-builder/table/Table";
122 5
export * from "./driver/mongodb/typings";
123
export * from "./driver/types/DatabaseType";
124
export * from "./driver/types/ReplicationMode";
125 5
export * from "./driver/sqlserver/MssqlParameter";
126

127 5
export {ConnectionOptionsReader} from "./connection/ConnectionOptionsReader";
128 5
export {Connection} from "./connection/Connection";
129 5
export {ConnectionManager} from "./connection/ConnectionManager";
130
export {ConnectionOptions} from "./connection/ConnectionOptions";
131
export {Driver} from "./driver/Driver";
132 5
export {QueryBuilder} from "./query-builder/QueryBuilder";
133 5
export {SelectQueryBuilder} from "./query-builder/SelectQueryBuilder";
134 5
export {DeleteQueryBuilder} from "./query-builder/DeleteQueryBuilder";
135 5
export {InsertQueryBuilder} from "./query-builder/InsertQueryBuilder";
136 5
export {UpdateQueryBuilder} from "./query-builder/UpdateQueryBuilder";
137 5
export {RelationQueryBuilder} from "./query-builder/RelationQueryBuilder";
138 5
export {Brackets} from "./query-builder/Brackets";
139
export {WhereExpression} from "./query-builder/WhereExpression";
140 5
export {InsertResult} from "./query-builder/result/InsertResult";
141 5
export {UpdateResult} from "./query-builder/result/UpdateResult";
142 5
export {DeleteResult} from "./query-builder/result/DeleteResult";
143
export {QueryRunner} from "./query-runner/QueryRunner";
144 5
export {MongoEntityManager} from "./entity-manager/MongoEntityManager";
145 5
export {Migration} from "./migration/Migration";
146 5
export {MigrationExecutor} from "./migration/MigrationExecutor";
147
export {MigrationInterface} from "./migration/MigrationInterface";
148 5
export {DefaultNamingStrategy} from "./naming-strategy/DefaultNamingStrategy";
149
export {NamingStrategyInterface} from "./naming-strategy/NamingStrategyInterface";
150
export {FindOneOptions} from "./find-options/FindOneOptions";
151
export {FindManyOptions} from "./find-options/FindManyOptions";
152
export {InsertEvent} from "./subscriber/event/InsertEvent";
153
export {LoadEvent} from "./subscriber/event/LoadEvent";
154
export {UpdateEvent} from "./subscriber/event/UpdateEvent";
155
export {RemoveEvent} from "./subscriber/event/RemoveEvent";
156
export {EntitySubscriberInterface} from "./subscriber/EntitySubscriberInterface";
157 5
export {EntitySchema} from "./entity-schema/EntitySchema";
158
export {EntitySchemaColumnOptions} from "./entity-schema/EntitySchemaColumnOptions";
159
export {EntitySchemaIndexOptions} from "./entity-schema/EntitySchemaIndexOptions";
160
export {EntitySchemaRelationOptions} from "./entity-schema/EntitySchemaRelationOptions";
161
export {ColumnType} from "./driver/types/ColumnTypes";
162

163
// -------------------------------------------------------------------------
164
// Deprecated
165
// -------------------------------------------------------------------------
166

167
// -------------------------------------------------------------------------
168
// Commonly used functionality
169
// -------------------------------------------------------------------------
170

171
/**
172
 * Gets metadata args storage.
173
 */
174 5
export function getMetadataArgsStorage(): MetadataArgsStorage {
175
    // we should store metadata storage in a global variable otherwise it brings too much problems
176
    // one of the problem is that if any entity (or any other) will be imported before consumer will call
177
    // useContainer method with his own container implementation, that entity will be registered in the
178
    // old old container (default one post probably) and consumer will his entity.
179
    // calling useContainer before he imports any entity (or any other) is not always convenient.
180
    // another reason is that when we run migrations typeorm is being called from a global package
181
    // and it may load entities which register decorators in typeorm of local package
182
    // this leads to impossibility of usage of entities in migrations and cli related operations
183 5
    const globalScope = PlatformTools.getGlobalVariable();
184 5
    if (!globalScope.typeormMetadataArgsStorage)
185 5
        globalScope.typeormMetadataArgsStorage = new MetadataArgsStorage();
186

187 5
    return globalScope.typeormMetadataArgsStorage;
188
}
189

190
/**
191
 * Reads connection options stored in ormconfig configuration file.
192
 */
193 5
export async function getConnectionOptions(connectionName: string = "default"): Promise<ConnectionOptions> {
194 5
    return new ConnectionOptionsReader().get(connectionName);
195
}
196

197
/**
198
 * Gets a ConnectionManager which creates connections.
199
 */
200 5
export function getConnectionManager(): ConnectionManager {
201 5
    return getFromContainer(ConnectionManager);
202
}
203

204
/**
205
 * Creates a new connection and registers it in the manager.
206
 * Only one connection from ormconfig will be created (name "default" or connection without name).
207
 */
208
export async function createConnection(): Promise<Connection>;
209

210
/**
211
 * Creates a new connection from the ormconfig file with a given name.
212
 */
213
export async function createConnection(name: string): Promise<Connection>;
214

215
/**
216
 * Creates a new connection and registers it in the manager.
217
 */
218
export async function createConnection(options: ConnectionOptions): Promise<Connection>;
219

220
/**
221
 * Creates a new connection and registers it in the manager.
222
 *
223
 * If connection options were not specified, then it will try to create connection automatically,
224
 * based on content of ormconfig (json/js/yml/xml/env) file or environment variables.
225
 * Only one connection from ormconfig will be created (name "default" or connection without name).
226
 */
227 5
export async function createConnection(optionsOrName?: any): Promise<Connection> {
228 5
    const connectionName = typeof optionsOrName === "string" ? optionsOrName : "default";
229 5
    const options = optionsOrName instanceof Object ? optionsOrName : await getConnectionOptions(connectionName);
230 5
    return getConnectionManager().create(options).connect();
231
}
232

233
/**
234
 * Creates new connections and registers them in the manager.
235
 *
236
 * If connection options were not specified, then it will try to create connection automatically,
237
 * based on content of ormconfig (json/js/yml/xml/env) file or environment variables.
238
 * All connections from the ormconfig will be created.
239
 */
240 5
export async function createConnections(options?: ConnectionOptions[]): Promise<Connection[]> {
241 5
    if (!options)
242 0
        options = await new ConnectionOptionsReader().all();
243 5
    const connections = options.map(options => getConnectionManager().create(options));
244 5
    return Promise.all(connections.map(connection => connection.connect()));
245
}
246

247
/**
248
 * Gets connection from the connection manager.
249
 * If connection name wasn't specified, then "default" connection will be retrieved.
250
 */
251 5
export function getConnection(connectionName: string = "default"): Connection {
252 3
    return getConnectionManager().get(connectionName);
253
}
254

255
/**
256
 * Gets entity manager from the connection.
257
 * If connection name wasn't specified, then "default" connection will be retrieved.
258
 */
259 5
export function getManager(connectionName: string = "default"): EntityManager {
260 0
    return getConnectionManager().get(connectionName).manager;
261
}
262

263
/**
264
 * Gets MongoDB entity manager from the connection.
265
 * If connection name wasn't specified, then "default" connection will be retrieved.
266
 */
267 5
export function getMongoManager(connectionName: string = "default"): MongoEntityManager {
268 0
    return getConnectionManager().get(connectionName).manager as MongoEntityManager;
269
}
270

271
/**
272
 * Gets Sqljs entity manager from connection name.
273
 * "default" connection is used, when no name is specified.
274
 * Only works when Sqljs driver is used.
275
 */
276 5
export function getSqljsManager(connectionName: string = "default"): SqljsEntityManager {
277 3
    return getConnectionManager().get(connectionName).manager as SqljsEntityManager;
278
}
279

280
/**
281
 * Gets repository for the given entity class.
282
 */
283 5
export function getRepository<Entity>(entityClass: EntityTarget<Entity>, connectionName: string = "default"): Repository<Entity> {
284 0
    return getConnectionManager().get(connectionName).getRepository<Entity>(entityClass);
285
}
286

287
/**
288
 * Gets tree repository for the given entity class.
289
 */
290 5
export function getTreeRepository<Entity>(entityClass: EntityTarget<Entity>, connectionName: string = "default"): TreeRepository<Entity> {
291 0
    return getConnectionManager().get(connectionName).getTreeRepository<Entity>(entityClass);
292
}
293

294
/**
295
 * Gets tree repository for the given entity class.
296
 */
297 5
export function getCustomRepository<T>(customRepository: ObjectType<T>, connectionName: string = "default"): T {
298 0
    return getConnectionManager().get(connectionName).getCustomRepository(customRepository);
299
}
300

301
/**
302
 * Gets mongodb repository for the given entity class or name.
303
 */
304 5
export function getMongoRepository<Entity>(entityClass: EntityTarget<Entity>, connectionName: string = "default"): MongoRepository<Entity> {
305 0
    return getConnectionManager().get(connectionName).getMongoRepository<Entity>(entityClass);
306
}
307

308
/**
309
 * Creates a new query builder.
310
 */
311 5
export function createQueryBuilder<Entity>(entityClass?: EntityTarget<Entity>, alias?: string, connectionName: string = "default"): SelectQueryBuilder<Entity> {
312 0
    if (entityClass) {
313 0
        return getRepository(entityClass, connectionName).createQueryBuilder(alias);
314
    }
315

316 0
    return getConnection(connectionName).createQueryBuilder();
317
}

Read our documentation on viewing source code .

Loading