lakutata
Version:
An IoC-based universal application framework.
449 lines (444 loc) • 19.1 kB
TypeScript
import { EntitySchema } from './TypeDef.internal.41.js';
import { LoggerOptions, Logger } from './TypeDef.internal.42.js';
import { NamingStrategyInterface } from './TypeDef.internal.43.js';
import { DatabaseType, IsolationLevel, ReplicationMode } from './TypeDef.internal.44.js';
import { Driver } from './TypeDef.internal.45.js';
import { Repository, TreeRepository, MongoRepository } from './TypeDef.internal.37.js';
import { EntitySubscriberInterface } from './TypeDef.internal.46.js';
import { MixedList, EntityTarget, ObjectLiteral, ObjectType } from './TypeDef.internal.36.js';
import { EntityManager, MongoEntityManager, SqljsEntityManager } from './TypeDef.internal.35.js';
import { EntityMetadata } from './TypeDef.internal.47.js';
import { MigrationInterface, Migration } from './TypeDef.internal.48.js';
import { QueryRunner } from './TypeDef.internal.40.js';
import { RelationLoader, RelationIdLoader, SelectQueryBuilder } from './TypeDef.internal.38.js';
import { QueryResultCache } from './TypeDef.internal.39.js';
import { CockroachConnectionOptions } from './TypeDef.internal.49.js';
import { MysqlConnectionOptions } from './TypeDef.internal.50.js';
import { PostgresConnectionOptions } from './TypeDef.internal.51.js';
import { SqliteConnectionOptions } from './TypeDef.internal.52.js';
import { SqlServerConnectionOptions } from './TypeDef.internal.53.js';
import { OracleConnectionOptions } from './TypeDef.internal.54.js';
import { MongoConnectionOptions } from './TypeDef.internal.55.js';
import { CordovaConnectionOptions } from './TypeDef.internal.56.js';
import { SqljsConnectionOptions } from './TypeDef.internal.57.js';
import { ReactNativeConnectionOptions } from './TypeDef.internal.58.js';
import { NativescriptConnectionOptions } from './TypeDef.internal.59.js';
import { ExpoConnectionOptions } from './TypeDef.internal.60.js';
import { AuroraMysqlConnectionOptions } from './TypeDef.internal.61.js';
import { SapConnectionOptions } from './TypeDef.internal.62.js';
import { AuroraPostgresConnectionOptions } from './TypeDef.internal.63.js';
import { BetterSqlite3ConnectionOptions } from './TypeDef.internal.64.js';
import { CapacitorConnectionOptions } from './TypeDef.internal.65.js';
import { SpannerConnectionOptions } from './TypeDef.internal.66.js';
import './TypeDef.internal.30.js';
/**
* BaseDataSourceOptions is set of DataSourceOptions shared by all database types.
*/
interface BaseDataSourceOptions {
/**
* Database type. This value is required.
*/
readonly type: DatabaseType;
/**
* Connection name. If connection name is not given then it will be called "default".
* Different connections must have different names.
*
* @deprecated
*/
readonly name?: string;
/**
* Entities to be loaded for this connection.
* Accepts both entity classes and directories where from entities need to be loaded.
* Directories support glob patterns.
*/
readonly entities?: MixedList<Function | string | EntitySchema>;
/**
* Subscribers to be loaded for this connection.
* Accepts both subscriber classes and directories where from subscribers need to be loaded.
* Directories support glob patterns.
*/
readonly subscribers?: MixedList<Function | string>;
/**
* Migrations to be loaded for this connection.
* Accepts both migration classes and glob patterns representing migration files.
*/
readonly migrations?: MixedList<Function | string>;
/**
* Migrations table name, in case of different name from "migrations".
* Accepts single string name.
*/
readonly migrationsTableName?: string;
/**
* Transaction mode for migrations to run in
*/
readonly migrationsTransactionMode?: "all" | "none" | "each";
/**
* Typeorm metadata table name, in case of different name from "typeorm_metadata".
* Accepts single string name.
*/
readonly metadataTableName?: string;
/**
* Naming strategy to be used to name tables and columns in the database.
*/
readonly namingStrategy?: NamingStrategyInterface;
/**
* Logging options.
*/
readonly logging?: LoggerOptions;
/**
* Logger instance used to log queries and events in the ORM.
*/
readonly logger?: "advanced-console" | "simple-console" | "formatted-console" | "file" | "debug" | Logger;
/**
* Maximum number of milliseconds query should be executed before logger log a warning.
*/
readonly maxQueryExecutionTime?: number;
/**
* Maximum number of clients the pool should contain.
*/
readonly poolSize?: number;
/**
* Indicates if database schema should be auto created on every application launch.
* Be careful with this option and don't use this in production - otherwise you can lose production data.
* This option is useful during debug and development.
* Alternative to it, you can use CLI and run schema:sync command.
*
* Note that for MongoDB database it does not create schema, because MongoDB is schemaless.
* Instead, it syncs just by creating indices.
*/
readonly synchronize?: boolean;
/**
* Indicates if migrations should be auto run on every application launch.
* Alternative to it, you can use CLI and run migrations:run command.
*/
readonly migrationsRun?: boolean;
/**
* Drops the schema each time connection is being established.
* Be careful with this option and don't use this in production - otherwise you'll lose all production data.
* This option is useful during debug and development.
*/
readonly dropSchema?: boolean;
/**
* Prefix to use on all tables (collections) of this connection in the database.
*/
readonly entityPrefix?: string;
/**
* When creating new Entity instances, skip all constructors when true.
*/
readonly entitySkipConstructor?: boolean;
/**
* Extra connection options to be passed to the underlying driver.
*
* todo: deprecate this and move all database-specific types into hts own connection options object.
*/
readonly extra?: any;
/**
* Specifies how relations must be loaded - using "joins" or separate queries.
* If you are loading too much data with nested joins it's better to load relations
* using separate queries.
*
* Default strategy is "join", but this default can be changed here.
* Also, strategy can be set per-query in FindOptions and QueryBuilder.
*/
readonly relationLoadStrategy?: "join" | "query";
/**
* Optionally applied "typename" to the model.
* If set, then each hydrated model will have this property with the target model / entity name inside.
*
* (works like a discriminator property).
*/
readonly typename?: string;
/**
* Allows to setup cache options.
*/
readonly cache?: boolean | {
/**
* Type of caching.
*
* - "database" means cached values will be stored in the separate table in database. This is default value.
* - "redis" means cached values will be stored inside redis. You must provide redis connection options.
*/
readonly type?: "database" | "redis" | "ioredis" | "ioredis/cluster";
/**
* Factory function for custom cache providers that implement QueryResultCache.
*/
readonly provider?: (connection: DataSource) => QueryResultCache;
/**
* Configurable table name for "database" type cache.
* Default value is "query-result-cache"
*/
readonly tableName?: string;
/**
* Used to provide redis connection options.
*/
readonly options?: any;
/**
* If set to true then queries (using find methods and QueryBuilder's methods) will always be cached.
*/
readonly alwaysEnabled?: boolean;
/**
* Time in milliseconds in which cache will expire.
* This can be setup per-query.
* Default value is 1000 which is equivalent to 1 second.
*/
readonly duration?: number;
/**
* Used to specify if cache errors should be ignored, and pass through the call to the Database.
*/
readonly ignoreErrors?: boolean;
};
/**
* Allows automatic isolation of where clauses
*/
readonly isolateWhereStatements?: boolean;
}
/**
* DataSourceOptions is an interface with settings and options for specific DataSource.
*/
type DataSourceOptions = MysqlConnectionOptions | PostgresConnectionOptions | CockroachConnectionOptions | SqliteConnectionOptions | SqlServerConnectionOptions | SapConnectionOptions | OracleConnectionOptions | CordovaConnectionOptions | NativescriptConnectionOptions | ReactNativeConnectionOptions | SqljsConnectionOptions | MongoConnectionOptions | AuroraMysqlConnectionOptions | AuroraPostgresConnectionOptions | ExpoConnectionOptions | BetterSqlite3ConnectionOptions | CapacitorConnectionOptions | SpannerConnectionOptions;
/**
* DataSource is a pre-defined connection configuration to a specific database.
* You can have multiple data sources connected (with multiple connections in it),
* connected to multiple databases in your application.
*
* Before, it was called `Connection`, but now `Connection` is deprecated
* because `Connection` isn't the best name for what it's actually is.
*/
declare class DataSource {
readonly "@instanceof": symbol;
/**
* Connection name.
*
* @deprecated we don't need names anymore since we are going to drop all related methods relying on this property.
*/
readonly name: string;
/**
* Connection options.
*/
readonly options: DataSourceOptions;
/**
* Indicates if DataSource is initialized or not.
*/
readonly isInitialized: boolean;
/**
* Database driver used by this connection.
*/
driver: Driver;
/**
* EntityManager of this connection.
*/
readonly manager: EntityManager;
/**
* Naming strategy used in the connection.
*/
namingStrategy: NamingStrategyInterface;
/**
* Name for the metadata table
*/
readonly metadataTableName: string;
/**
* Logger used to log orm events.
*/
logger: Logger;
/**
* Migration instances that are registered for this connection.
*/
readonly migrations: MigrationInterface[];
/**
* Entity subscriber instances that are registered for this connection.
*/
readonly subscribers: EntitySubscriberInterface<any>[];
/**
* All entity metadatas that are registered for this connection.
*/
readonly entityMetadatas: EntityMetadata[];
/**
* All entity metadatas that are registered for this connection.
* This is a copy of #.entityMetadatas property -> used for more performant searches.
*/
readonly entityMetadatasMap: Map<EntityTarget<any>, EntityMetadata>;
/**
* Used to work with query result cache.
*/
queryResultCache?: QueryResultCache;
/**
* Used to load relations and work with lazy relations.
*/
readonly relationLoader: RelationLoader;
readonly relationIdLoader: RelationIdLoader;
constructor(options: DataSourceOptions);
/**
Indicates if DataSource is initialized or not.
*
* @deprecated use .isInitialized instead
*/
get isConnected(): boolean;
/**
* Gets the mongodb entity manager that allows to perform mongodb-specific repository operations
* with any entity in this connection.
*
* Available only in mongodb connections.
*/
get mongoManager(): MongoEntityManager;
/**
* Gets a sql.js specific Entity Manager that allows to perform special load and save operations
*
* Available only in connection with the sqljs driver.
*/
get sqljsManager(): SqljsEntityManager;
/**
* Updates current connection options with provided options.
*/
setOptions(options: Partial<DataSourceOptions>): this;
/**
* Performs connection to the database.
* This method should be called once on application bootstrap.
* This method not necessarily creates database connection (depend on database type),
* but it also can setup a connection pool with database to use.
*/
initialize(): Promise<this>;
/**
* Performs connection to the database.
* This method should be called once on application bootstrap.
* This method not necessarily creates database connection (depend on database type),
* but it also can setup a connection pool with database to use.
*
* @deprecated use .initialize method instead
*/
connect(): Promise<this>;
/**
* Closes connection with the database.
* Once connection is closed, you cannot use repositories or perform any operations except opening connection again.
*/
destroy(): Promise<void>;
/**
* Closes connection with the database.
* Once connection is closed, you cannot use repositories or perform any operations except opening connection again.
*
* @deprecated use .destroy method instead
*/
close(): Promise<void>;
/**
* Creates database schema for all entities registered in this connection.
* Can be used only after connection to the database is established.
*
* @param dropBeforeSync If set to true then it drops the database with all its tables and data
*/
synchronize(dropBeforeSync?: boolean): Promise<void>;
/**
* Drops the database and all its data.
* Be careful with this method on production since this method will erase all your database tables and their data.
* Can be used only after connection to the database is established.
*/
dropDatabase(): Promise<void>;
/**
* Runs all pending migrations.
* Can be used only after connection to the database is established.
*/
runMigrations(options?: {
transaction?: "all" | "none" | "each";
fake?: boolean;
}): Promise<Migration[]>;
/**
* Reverts last executed migration.
* Can be used only after connection to the database is established.
*/
undoLastMigration(options?: {
transaction?: "all" | "none" | "each";
fake?: boolean;
}): Promise<void>;
/**
* Lists all migrations and whether they have been run.
* Returns true if there are pending migrations
*/
showMigrations(): Promise<boolean>;
/**
* Checks if entity metadata exist for the given entity class, target name or table name.
*/
hasMetadata(target: EntityTarget<any>): boolean;
/**
* Gets entity metadata for the given entity class or schema name.
*/
getMetadata(target: EntityTarget<any>): EntityMetadata;
/**
* Gets repository for the given entity.
*/
getRepository<Entity extends ObjectLiteral>(target: EntityTarget<Entity>): Repository<Entity>;
/**
* Gets tree repository for the given entity class or name.
* Only tree-type entities can have a TreeRepository, like ones decorated with @Tree decorator.
*/
getTreeRepository<Entity extends ObjectLiteral>(target: EntityTarget<Entity>): TreeRepository<Entity>;
/**
* Gets mongodb-specific repository for the given entity class or name.
* Works only if connection is mongodb-specific.
*/
getMongoRepository<Entity extends ObjectLiteral>(target: EntityTarget<Entity>): MongoRepository<Entity>;
/**
* Gets custom entity repository marked with @EntityRepository decorator.
*
* @deprecated use Repository.extend function to create a custom repository
*/
getCustomRepository<T>(customRepository: ObjectType<T>): T;
/**
* Wraps given function execution (and all operations made there) into a transaction.
* All database operations must be executed using provided entity manager.
*/
transaction<T>(runInTransaction: (entityManager: EntityManager) => Promise<T>): Promise<T>;
transaction<T>(isolationLevel: IsolationLevel, runInTransaction: (entityManager: EntityManager) => Promise<T>): Promise<T>;
/**
* Executes raw SQL query and returns raw database results.
*
* @see [Official docs](https://typeorm.io/data-source-api) for examples.
*/
query<T = any>(query: string, parameters?: any[], queryRunner?: QueryRunner): Promise<T>;
/**
* Tagged template function that executes raw SQL query and returns raw database results.
* Template expressions are automatically transformed into database parameters.
* Raw query execution is supported only by relational databases (MongoDB is not supported).
* Note: Don't call this as a regular function, it is meant to be used with backticks to tag a template literal.
* Example: dataSource.sql`SELECT * FROM table_name WHERE id = ${id}`
*/
sql<T = any>(strings: TemplateStringsArray, ...values: unknown[]): Promise<T>;
/**
* Creates a new query builder that can be used to build a SQL query.
*/
createQueryBuilder<Entity extends ObjectLiteral>(entityClass: EntityTarget<Entity>, alias: string, queryRunner?: QueryRunner): SelectQueryBuilder<Entity>;
/**
* Creates a new query builder that can be used to build a SQL query.
*/
createQueryBuilder(queryRunner?: QueryRunner): SelectQueryBuilder<any>;
/**
* Creates a query runner used for perform queries on a single database connection.
* Using query runners you can control your queries to execute using single database connection and
* manually control your database transaction.
*
* Mode is used in replication mode and indicates whatever you want to connect
* to master database or any of slave databases.
* If you perform writes you must use master database,
* if you perform reads you can use slave databases.
*/
createQueryRunner(mode?: ReplicationMode): QueryRunner;
/**
* Gets entity metadata of the junction table (many-to-many table).
*/
getManyToManyMetadata(entityTarget: EntityTarget<any>, relationPropertyPath: string): EntityMetadata | undefined;
/**
* Creates an Entity Manager for the current connection with the help of the EntityManagerFactory.
*/
createEntityManager(queryRunner?: QueryRunner): EntityManager;
/**
* Finds exist entity metadata by the given entity class, target name or table name.
*/
protected findMetadata(target: EntityTarget<any>): EntityMetadata | undefined;
/**
* Builds metadatas for all registered classes inside this connection.
*/
protected buildMetadatas(): Promise<void>;
/**
* Get the replication mode SELECT queries should use for this datasource by default
*/
defaultReplicationModeForReads(): ReplicationMode;
}
export { DataSource };
export type { BaseDataSourceOptions, DataSourceOptions };