refactored connection options - created interface for each type of database driver (for future database-specific connection options).

This commit is contained in:
Umed Khudoiberdiev 2017-06-09 01:42:39 +05:00
parent d2ff2cbd0a
commit 34bbd9a3ad
65 changed files with 828 additions and 751 deletions

View File

@ -30,6 +30,9 @@ each for its own `findOne*` or `find*` methods
Now naming strategy should be registered by passing naming strategy instance directly
* `driver` section in connection options now deprecated. All settings should go directly to connection options root.
* removed `fromTable` from the `QueryBuilder`. Now use regular `from` to select from tables
* removed `usePool` option from the connection options
* connection options interface has changed and now each platform has its own set of connection options
* `storage` in sqlite options has been renamed to `database`
### OTHER API CHANGES

View File

@ -2,14 +2,12 @@
{
"skip": false,
"name": "mysql",
"driver": {
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "test",
"password": "test",
"database": "test"
},
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "test",
"password": "test",
"database": "test",
"logging": {
"logQueries": false
}
@ -17,14 +15,12 @@
{
"skip": false,
"name": "mariadb",
"driver": {
"type": "mariadb",
"host": "localhost",
"port": 3307,
"username": "test",
"password": "test",
"database": "test"
},
"type": "mariadb",
"host": "localhost",
"port": 3307,
"username": "test",
"password": "test",
"database": "test",
"logging": {
"logQueries": false
}
@ -32,10 +28,8 @@
{
"skip": false,
"name": "sqlite",
"driver": {
"type": "sqlite",
"storage": "temp/sqlitedb.db"
},
"type": "sqlite",
"database": "temp/sqlitedb.db",
"logging": {
"logQueries": false
}
@ -43,14 +37,12 @@
{
"skip": false,
"name": "postgres",
"driver": {
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "root",
"password": "test",
"database": "test"
},
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "root",
"password": "test",
"database": "test",
"logging": {
"logQueries": false
}
@ -58,13 +50,11 @@
{
"skip": true,
"name": "mssql",
"driver": {
"type": "mssql",
"host": "localhost",
"username": "sa",
"password": "admin",
"database": "test"
},
"type": "mssql",
"host": "localhost",
"username": "sa",
"password": "admin",
"database": "test",
"logging": {
"logQueries": false
}
@ -72,14 +62,12 @@
{
"skip": true,
"name": "oracle",
"driver": {
"type": "oracle",
"host": "localhost",
"username": "system",
"password": "oracle",
"port": 1521,
"sid": "xe.oracle.docker"
},
"type": "oracle",
"host": "localhost",
"username": "system",
"password": "oracle",
"port": 1521,
"sid": "xe.oracle.docker",
"logging": {
"logQueries": false
}
@ -88,10 +76,8 @@
"skip": false,
"disabledIfNotEnabledImplicitly": true,
"name": "mongodb",
"driver": {
"type": "mongodb",
"database": "test"
},
"type": "mongodb",
"database": "test",
"logging": {
"logQueries": false
}

View File

@ -2,78 +2,64 @@
{
"skip": false,
"name": "mysql",
"driver": {
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "test",
"password": "test",
"database": "test"
}
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "test",
"password": "test",
"database": "test"
},
{
"skip": false,
"name": "mariadb",
"driver": {
"type": "mariadb",
"host": "localhost",
"port": 3307,
"username": "test",
"password": "test",
"database": "test"
}
"type": "mariadb",
"host": "localhost",
"port": 3307,
"username": "test",
"password": "test",
"database": "test"
},
{
"skip": false,
"name": "sqlite",
"driver": {
"type": "sqlite",
"storage": "temp/sqlitedb.db"
}
"type": "sqlite",
"database": "temp/sqlitedb.db"
},
{
"skip": false,
"name": "postgres",
"driver": {
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "test",
"password": "test",
"database": "test"
}
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "test",
"password": "test",
"database": "test"
},
{
"skip": true,
"name": "mssql",
"driver": {
"type": "mssql",
"host": "192.168.1.10",
"username": "sa",
"password": "admin12345",
"database": "test"
}
"type": "mssql",
"host": "192.168.1.10",
"username": "sa",
"password": "admin12345",
"database": "test"
},
{
"skip": true,
"name": "oracle",
"driver": {
"type": "oracle",
"host": "localhost",
"username": "system",
"password": "oracle",
"port": 1521,
"sid": "xe.oracle.docker"
}
"type": "oracle",
"host": "localhost",
"username": "system",
"password": "oracle",
"port": 1521,
"sid": "xe.oracle.docker"
},
{
"skip": false,
"disabledIfNotEnabledImplicitly": true,
"name": "mongodb",
"driver": {
"type": "mongodb",
"database": "test"
},
"type": "mongodb",
"database": "test",
"logging": {
"logQueries": false
}

View File

@ -3,28 +3,26 @@ import {createConnection, ConnectionOptions} from "../../src/index";
import {Post} from "./entity/Post";
const options: ConnectionOptions = {
driver: {
// type: "postgres",
// host: "localhost",
// port: 5432,
// username: "root",
// password: "admin",
// database: "test"
type: "oracle",
host: "localhost",
username: "system",
password: "oracle",
port: 1521,
sid: "xe.oracle.docker",
// type: "mssql",
// host: "192.168.1.10",
// username: "sa",
// password: "admin12345",
// database: "test",
// port: 1521
// type: "sqlite",
// storage: "temp/sqlitedb.db"
},
type: "oracle",
host: "localhost",
username: "system",
password: "oracle",
port: 1521,
sid: "xe.oracle.docker",
// type: "postgres",
// host: "localhost",
// port: 5432,
// username: "root",
// password: "admin",
// database: "test"
// type: "mssql",
// host: "192.168.1.10",
// username: "sa",
// password: "admin12345",
// database: "test",
// port: 1521
// type: "sqlite",
// storage: "temp/sqlitedb.db"
logging: {
logQueries: true,
logSchemaCreation: true

View File

@ -7,14 +7,12 @@ import {Cover} from "./entity/Cover";
import {Category} from "./entity/Category";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaSync: true,
entities: [__dirname + "/entity/*"]
};

View File

@ -3,14 +3,12 @@ import {createConnection, ConnectionOptions} from "../../src/index";
import {EverythingEntity} from "./entity/EverythingEntity";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
logOnlyFailedQueries: true
},

View File

@ -4,14 +4,12 @@ import {Post} from "./entity/Post";
import {CustomNamingStrategy} from "./naming-strategy/CustomNamingStrategy";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaSync: true,
namingStrategy: new CustomNamingStrategy(),
entities: [Post]

View File

@ -6,14 +6,12 @@ import {PostAuthor} from "./entity/PostAuthor";
import {Blog} from "./entity/Blog";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true

View File

@ -4,14 +4,12 @@ import {Post} from "./entity/Post";
import {PostAuthor} from "./entity/PostAuthor";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaSync: true,
entities: [Post, PostAuthor]
};

View File

@ -4,14 +4,12 @@ import {Post} from "./entity/Post";
import {BasePost} from "./entity/BasePost";
const options: ConnectionOptions = {
driver: {
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "test",
"password": "test",
"database": "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "test",
password: "test",
database: "test",
logging: {
logQueries: true,
logFailedQueryError: true,

View File

@ -3,14 +3,12 @@ import {createConnection, ConnectionOptions} from "../../src/index";
import {Post} from "./entity/Post";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true

View File

@ -5,14 +5,12 @@ import {Author} from "./entity/Author";
import {Category} from "./entity/Category";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true

View File

@ -6,14 +6,12 @@ import {Category} from "./entity/Category";
import {PostMetadata} from "./entity/PostMetadata";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true

View File

@ -9,14 +9,12 @@ import {PostInformation} from "./entity/PostInformation";
import {PostAuthor} from "./entity/PostAuthor";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
// logQueries: true
},

View File

@ -5,14 +5,12 @@ import {Author} from "./entity/Author";
import {Category} from "./entity/Category";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true

View File

@ -5,14 +5,12 @@ import {Author} from "./entity/Author";
import {Category} from "./entity/Category";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true

View File

@ -3,14 +3,12 @@ import {createConnection, ConnectionOptions} from "../../src/index";
import {Category} from "./entity/Category";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true

View File

@ -5,14 +5,12 @@ import {Author} from "./entity/Author";
import {Category} from "./entity/Category";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true

View File

@ -5,14 +5,12 @@ import {Post} from "./entity/Post";
// NOTE: this example is not working yet, only concepts of how this feature must work described here
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaSync: true,
// entitySchemaDirectories: [__dirname + "/schemas"],
entitySchemas: [

View File

@ -4,14 +4,12 @@ import {Post} from "./entity/Post";
import {Author} from "./entity/Author";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true

View File

@ -5,14 +5,12 @@ import {Question} from "./entity/Question";
import {Counters} from "./entity/Counters";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true

View File

@ -3,14 +3,12 @@ import {createConnection, ConnectionOptions} from "../../src/index";
import {Post} from "./entity/Post";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
logOnlyFailedQueries: true,
logFailedQueryError: true

View File

@ -6,14 +6,12 @@ import {Student} from "./entity/Student";
import {Person} from "./entity/Person";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
// logQueries: true,
logOnlyFailedQueries: true,

View File

@ -6,14 +6,12 @@ import {Student} from "./entity/Student";
import {Person} from "./entity/Person";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
logging: {
// logQueries: true,
logOnlyFailedQueries: true,

View File

@ -9,19 +9,17 @@ import {PostInformation} from "./entity/PostInformation";
import {PostAuthor} from "./entity/PostAuthor";
const options: ConnectionOptions = {
driver: {
// type: "mssql",
// host: "192.168.1.10",
// username: "sa",
// password: "admin12345",
// database: "test",
type: "oracle",
host: "localhost",
username: "system",
password: "oracle",
port: 1521,
sid: "xe.oracle.docker",
},
// type: "mssql",
// host: "192.168.1.10",
// username: "sa",
// password: "admin12345",
// database: "test",
type: "oracle",
host: "localhost",
username: "system",
password: "oracle",
port: 1521,
sid: "xe.oracle.docker",
autoSchemaSync: true,
logging: {
logQueries: true,

View File

@ -4,10 +4,8 @@ import {Post} from "./entity/Post";
import {Category} from "./entity/Category";
const options: ConnectionOptions = {
driver: {
type: "sqlite",
storage: "temp/sqlitedb.db"
},
type: "sqlite",
database: "temp/sqlitedb.db",
logging: {
logQueries: true,
logSchemaCreation: true

View File

@ -5,11 +5,9 @@ import {Author} from "./entity/Author";
import {Category} from "./entity/Category";
const options: ConnectionOptions = {
driver: {
type: "sqlite",
storage: "temp/sqlitedb.db",
tablesPrefix: "samples_" // pay attention on this prefix
},
type: "sqlite",
database: "temp/sqlitedb.db",
tablesPrefix: "samples_", // pay attention on this prefix
autoSchemaSync: true,
logging: {
logQueries: true,

View File

@ -5,14 +5,12 @@ import {Author} from "./entity/Author";
import {MigrationExecutor} from "../../src/migration/MigrationExecutor";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaSync: true,
logging: {
logQueries: true,
@ -41,15 +39,13 @@ createConnection(options).then(async connection => {
// now create a new connection
connection = await createConnection({
type: "mysql",
name: "mysql",
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "test",
password: "test",
database: "test"
},
host: "localhost",
port: 3306,
username: "test",
password: "test",
database: "test",
logging: {
logQueries: true
},

View File

@ -9,14 +9,12 @@ import {UserRepository} from "./repository/UserRepository";
import {User} from "./entity/User";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaSync: true,
logging: {
logQueries: true,

View File

@ -3,11 +3,9 @@ import {createConnection, ConnectionOptions} from "../../src/index";
import {Post} from "./entity/Post";
const options: ConnectionOptions = {
driver: {
type: "mongodb",
host: "localhost",
database: "test",
},
type: "mongodb",
host: "localhost",
database: "test",
logging: {
logQueries: true,
logSchemaCreation: true

View File

@ -4,13 +4,11 @@ import {Post} from "./entity/Post";
import {PostDetails} from "./entity/PostDetails";
const options: ConnectionOptions = {
driver: {
type: "mssql",
host: "192.168.1.10",
username: "sa",
password: "admin12345",
database: "test",
},
type: "mssql",
host: "192.168.1.10",
username: "sa",
password: "admin12345",
database: "test",
logging: {
logFailedQueryError: true,
// logQueries: true

View File

@ -7,14 +7,12 @@ import {EverythingSubscriber} from "./subscriber/EverythingSubscriber";
// first create a connection
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaSync: true,
entities: [Post, PostAuthor, PostCategory],
subscribers: [EverythingSubscriber]

View File

@ -6,14 +6,12 @@ import {PostAuthor} from "./entity/PostAuthor";
import {Blog} from "./entity/Blog";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaSync: true,
entities: [__dirname + "/entity/*"]
};

View File

@ -5,14 +5,12 @@ import {PostCategory} from "./entity/PostCategory";
import {PostAuthor} from "./entity/PostAuthor";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaSync: true,
entities: [__dirname + "/entity/*"]
};

View File

@ -3,14 +3,12 @@ import {createConnection, ConnectionOptions} from "../../src/index";
import {Category} from "./entity/Category";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaSync: true,
entities: [__dirname + "/entity/*"]
};

View File

@ -5,14 +5,12 @@ import {PostCategory} from "./entity/PostCategory";
import {PostAuthor} from "./entity/PostAuthor";
const options: ConnectionOptions = {
driver: {
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test"
},
type: "mysql",
host: "localhost",
port: 3306,
username: "root",
password: "admin",
database: "test",
autoSchemaSync: true,
entities: [__dirname + "/entity/*"],
subscribers: [__dirname + "/subscriber/*"]

View File

@ -0,0 +1,141 @@
import {DriverOptions} from "../driver/DriverOptions";
import {EntitySchema} from "../entity-schema/EntitySchema";
import {LoggerOptions} from "../logger/LoggerOptions";
import {NamingStrategyInterface} from "../naming-strategy/NamingStrategyInterface";
import {DatabaseType} from "../driver/DatabaseType";
/**
* BaseConnectionOptions is set of connection options shared by all database types.
*/
export interface BaseConnectionOptions {
/**
* 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.
*/
readonly name?: string;
/**
* Database options of this connection.
*
* @deprecated Define options right in the connection options section.
*/
readonly driver?: DriverOptions;
/**
* 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;
/**
* Prefix to use on all tables (collections) of this connection in the database.
*
* todo: rename to entityPrefix
*/
readonly tablesPrefix?: string;
/**
* Naming strategy to be used to name tables and columns in the database.
*/
readonly namingStrategy?: NamingStrategyInterface;
/**
* 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?: Function[]|string[];
/**
* 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?: Function[]|string[];
/**
* Entity schemas to be loaded for this connection.
* Accepts both entity schema classes and directories where from entity schemas need to be loaded.
* Directories support glob patterns.
*/
readonly entitySchemas?: EntitySchema[]|string[];
/**
* Migrations to be loaded for this connection.
* Accepts both migration classes and directories where from migrations need to be loaded.
* Directories support glob patterns.
*/
readonly migrations?: Function[]|string[];
/**
* Logging options.
*/
readonly logging?: LoggerOptions;
/**
* Drops the schema each time connection is being established.
* Be careful with this option and don't use this in production - otherwise you'll loose all production data.
* This option is useful during debug and development.
*/
readonly dropSchemaOnConnection?: boolean;
/**
* 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 loose 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.
*
* todo: rename it simply to synchronize: boolean ?
*/
readonly autoSchemaSync?: boolean;
/**
* Indicates if migrations should be auto run on every application launch.
* Alternative to it, you can use CLI and run migration:create command.
*
* todo: rename it simply to runMigrations: boolean ?
*/
readonly autoMigrationsRun?: boolean;
/**
* Environment in which connection will run.
* Current environment is determined from the environment NODE_ENV variable's value.
* For example, if NODE_ENV is "test" and this property is set to "test",
* then this connection will be created. On any other NODE_ENV value it will be skipped.
* This option is specific to the configuration in the ormconfig.json file.
*/
readonly environment?: string;
/**
* CLI settings.
*/
readonly cli?: {
/**
* Directory where entities should be created by default.
*/
readonly entitiesDir?: string;
/**
* Directory where migrations should be created by default.
*/
readonly migrationsDir?: string;
/**
* Directory where subscribers should be created by default.
*/
readonly subscribersDir?: string;
};
}

View File

@ -279,19 +279,15 @@ export class ConnectionManager {
*/
protected async createFromEnvAndConnect(): Promise<Connection> {
return this.createAndConnectByConnectionOptions({
driver: {
type: PlatformTools.getEnvVariable("TYPEORM_DRIVER_TYPE"),
url: PlatformTools.getEnvVariable("TYPEORM_URL"),
host: PlatformTools.getEnvVariable("TYPEORM_HOST"),
port: PlatformTools.getEnvVariable("TYPEORM_PORT"),
username: PlatformTools.getEnvVariable("TYPEORM_USERNAME"),
password: PlatformTools.getEnvVariable("TYPEORM_PASSWORD"),
database: PlatformTools.getEnvVariable("TYPEORM_DATABASE"),
sid: PlatformTools.getEnvVariable("TYPEORM_SID"),
storage: PlatformTools.getEnvVariable("TYPEORM_STORAGE"),
usePool: PlatformTools.getEnvVariable("TYPEORM_USE_POOL") !== undefined ? OrmUtils.toBoolean(PlatformTools.getEnvVariable("TYPEORM_USE_POOL")) : undefined, // special check for defined is required here
extra: PlatformTools.getEnvVariable("TYPEORM_DRIVER_EXTRA") ? JSON.parse(PlatformTools.getEnvVariable("TYPEORM_DRIVER_EXTRA")) : undefined
},
type: PlatformTools.getEnvVariable("TYPEORM_DRIVER_TYPE"),
url: PlatformTools.getEnvVariable("TYPEORM_URL"),
host: PlatformTools.getEnvVariable("TYPEORM_HOST"),
port: PlatformTools.getEnvVariable("TYPEORM_PORT"),
username: PlatformTools.getEnvVariable("TYPEORM_USERNAME"),
password: PlatformTools.getEnvVariable("TYPEORM_PASSWORD"),
database: PlatformTools.getEnvVariable("TYPEORM_DATABASE"),
sid: PlatformTools.getEnvVariable("TYPEORM_SID"),
extra: PlatformTools.getEnvVariable("TYPEORM_DRIVER_EXTRA") ? JSON.parse(PlatformTools.getEnvVariable("TYPEORM_DRIVER_EXTRA")) : undefined,
autoSchemaSync: OrmUtils.toBoolean(PlatformTools.getEnvVariable("TYPEORM_AUTO_SCHEMA_SYNC")),
entities: PlatformTools.getEnvVariable("TYPEORM_ENTITIES") ? PlatformTools.getEnvVariable("TYPEORM_ENTITIES").split(",") : [],
subscribers: PlatformTools.getEnvVariable("TYPEORM_SUBSCRIBERS") ? PlatformTools.getEnvVariable("TYPEORM_SUBSCRIBERS").split(",") : [],

View File

@ -1,198 +1,20 @@
import {DriverOptions} from "../driver/DriverOptions";
import {EntitySchema} from "../entity-schema/EntitySchema";
import {LoggerOptions} from "../logger/LoggerOptions";
import {NamingStrategyInterface} from "../naming-strategy/NamingStrategyInterface";
import {DriverType} from "../driver/DriverType";
import {LoggerFactory} from "../logger/LoggerFactory";
import {DriverFactory} from "../driver/DriverFactory";
import {EntityManagerFactory} from "../entity-manager/EntityManagerFactory";
import {MysqlConnectionOptions} from "../driver/mysql/MysqlConnectionOptions";
import {PostgresConnectionOptions} from "../driver/postgres/PostgresConnectionOptions";
import {SqliteConnectionOptions} from "../driver/sqlite/SqliteConnectionOptions";
import {SqlServerConnectionOptions} from "../driver/sqlserver/SqlServerConnectionOptions";
import {OracleConnectionOptions} from "../driver/oracle/OracleConnectionOptions";
import {WebSqlConnectionOptions} from "../driver/websql/WebSqlConnectionOptions";
import {MongoConnectionOptions} from "../driver/mongodb/MongoConnectionOptions";
/**
* ConnectionOptions is an interface with settings and options for specific connection.
* Options contain database and other connection-related settings.
* Consumer must provide connection options for each of your connections.
*/
export interface ConnectionOptions {
/**
* Connection name. If connection name is not given then it will be called "default".
* Different connections must have different names.
*/
readonly name?: string;
/**
* Database options of this connection.
*
* @deprecated Define options right in the connection options section.
*/
readonly driver?: DriverOptions;
/**
* Database type. This value is required.
*/
readonly type?: DriverType;
/**
* Connection url to where perform connection to.
*/
readonly url?: string;
/**
* Database host.
*/
readonly host?: string;
/**
* Database host port.
*/
readonly port?: number;
/**
* Database username.
*/
readonly username?: string;
/**
* Database password.
*/
readonly password?: string;
/**
* Database name to connect to.
*/
readonly database?: string;
/**
* Schema name. By default is "public" (used only in Postgres databases).
*/
readonly schemaName?: string;
/**
* Connection SID (used for Oracle databases).
*/
readonly sid?: string;
/**
* Storage type or path to the storage (used for SQLite databases).
*/
readonly storage?: string;
/**
* Indicates if connection pooling should be used or not.
* Be default it is enabled if its supported by a platform.
* Set to false to disable it.
*
* @todo: rename to disablePool? What about mongodb pool?
*/
readonly usePool?: boolean;
/**
* Extra connection options to be passed to the underlying driver.
*/
readonly extra?: any;
/**
* Prefix to use on all tables (collections) of this connection in the database.
*
* @todo: rename to entityPrefix
*/
readonly tablesPrefix?: string;
/**
* Naming strategy to be used to name tables and columns in the database.
*/
readonly namingStrategy?: NamingStrategyInterface;
/**
* 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?: Function[]|string[];
/**
* 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?: Function[]|string[];
/**
* Entity schemas to be loaded for this connection.
* Accepts both entity schema classes and directories where from entity schemas need to be loaded.
* Directories support glob patterns.
*/
readonly entitySchemas?: EntitySchema[]|string[];
/**
* Migrations to be loaded for this connection.
* Accepts both migration classes and directories where from migrations need to be loaded.
* Directories support glob patterns.
*/
readonly migrations?: Function[]|string[];
/**
* Logging options.
*/
readonly logging?: LoggerOptions;
/**
* Drops the schema each time connection is being established.
* Be careful with this option and don't use this in production - otherwise you'll loose all production data.
* This option is useful during debug and development.
*/
readonly dropSchemaOnConnection?: boolean;
/**
* 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 loose 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.
*
* todo: rename it simply to synchronize: boolean ?
*/
readonly autoSchemaSync?: boolean;
/**
* Indicates if migrations should be auto run on every application launch.
* Alternative to it, you can use CLI and run migration:create command.
*
* todo: rename it simply to runMigrations: boolean ?
*/
readonly autoMigrationsRun?: boolean;
/**
* Environment in which connection will run.
* Current environment is determined from the environment NODE_ENV variable's value.
* For example, if NODE_ENV is "test" and this property is set to "test",
* then this connection will be created. On any other NODE_ENV value it will be skipped.
* This option is specific to the configuration in the ormconfig.json file.
*/
readonly environment?: string;
/**
* CLI settings.
*/
readonly cli?: {
/**
* Directory where entities should be created by default.
*/
readonly entitiesDir?: string;
/**
* Directory where migrations should be created by default.
*/
readonly migrationsDir?: string;
/**
* Directory where subscribers should be created by default.
*/
readonly subscribersDir?: string;
};
}
export type ConnectionOptions = MysqlConnectionOptions|
PostgresConnectionOptions|
SqliteConnectionOptions|
SqlServerConnectionOptions|
OracleConnectionOptions|
WebSqlConnectionOptions|
MongoConnectionOptions;

View File

@ -0,0 +1,4 @@
/**
* Database type.
*/
export type DatabaseType = "mysql"|"postgres"|"mariadb"|"sqlite"|"oracle"|"mssql"|"websql"|"mongodb";

View File

@ -18,7 +18,8 @@ export class DriverFactory {
* Creates a new driver depend on a given connection's driver type.
*/
create(connection: Connection): Driver {
switch (connection.options.type) {
const type = connection.options.type;
switch (type) {
case "mysql":
return new MysqlDriver(connection);
case "postgres":
@ -36,7 +37,7 @@ export class DriverFactory {
case "mongodb":
return new MongoDriver(connection);
default:
throw new MissingDriverError(connection.options.type!);
throw new MissingDriverError(type);
}
}

View File

@ -1,4 +1,4 @@
import {DriverType} from "./DriverType";
import {DatabaseType} from "./DatabaseType";
/**
* Connectivity options used to connect to the database, and other database-driver-specific options.
@ -10,7 +10,7 @@ export interface DriverOptions {
/**
* Database type. This value is required.
*/
type?: DriverType;
type?: DatabaseType;
/**
* Connection url to where perform connection to.

View File

@ -1,4 +0,0 @@
/**
* Driver type.
*/
export type DriverType = "mysql"|"postgres"|"mariadb"|"sqlite"|"oracle"|"mssql"|"websql"|"mongodb";

View File

@ -0,0 +1,33 @@
import {BaseConnectionOptions} from "../../connection/BaseConnectionOptions";
/**
* MongoDB specific connection options.
*/
export interface MongoConnectionOptions extends BaseConnectionOptions {
/**
* Database type.
*/
readonly type: "mongodb";
/**
* Connection url where perform connection to.
*/
readonly url?: string;
/**
* Database host.
*/
readonly host?: string;
/**
* Database host port.
*/
readonly port?: number;
/**
* Database name to connect to.
*/
readonly database?: string;
}

View File

@ -11,6 +11,7 @@ import {DriverOptionNotSetError} from "../error/DriverOptionNotSetError";
import {PlatformTools} from "../../platform/PlatformTools";
import {EntityMetadata} from "../../metadata/EntityMetadata";
import {Connection} from "../../connection/Connection";
import {MongoConnectionOptions} from "./MongoConnectionOptions";
/**
* Organizes communication with MongoDB.
@ -40,12 +41,15 @@ export class MongoDriver implements Driver {
* Connection to mongodb database provided by native driver.
*/
protected pool: any;
protected options: MongoConnectionOptions;
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(protected connection: Connection) {
this.options = connection.options as MongoConnectionOptions;
// validate options to make sure everything is correct and driver will be able to establish connection
this.validateOptions(connection.options);
@ -63,7 +67,7 @@ export class MongoDriver implements Driver {
*/
connect(): Promise<void> {
return new Promise<void>((ok, fail) => {
this.mongodb.MongoClient.connect(this.buildConnectionUrl(), this.connection.options!.extra, (err: any, database: any) => {
this.mongodb.MongoClient.connect(this.buildConnectionUrl(), this.options.extra, (err: any, database: any) => {
if (err) return fail(err);
this.pool = database;
@ -241,10 +245,10 @@ export class MongoDriver implements Driver {
* Builds connection url that is passed to underlying driver to perform connection to the mongodb database.
*/
protected buildConnectionUrl(): string {
if (this.connection.options.url)
return this.connection.options.url;
if (this.options.url)
return this.options.url;
return `mongodb://${this.connection.options.host || "127.0.0.1"}:${this.connection.options.port || "27017"}/${this.connection.options.database}`;
return `mongodb://${this.options.host || "127.0.0.1"}:${this.options.port || "27017"}/${this.options.database}`;
}
}

View File

@ -672,13 +672,6 @@ export class MongoQueryRunner implements QueryRunner {
// Protected Methods
// -------------------------------------------------------------------------
/**
* Database name shortcut.
*/
protected get dbName(): string {
return this.connection.options.database as string;
}
/**
* Gets collection from the database with a given name.
*/

View File

@ -0,0 +1,43 @@
import {BaseConnectionOptions} from "../../connection/BaseConnectionOptions";
/**
* MySQL specific connection options.
*/
export interface MysqlConnectionOptions extends BaseConnectionOptions {
/**
* Database type.
*/
readonly type: "mysql"|"mariadb";
/**
* Connection url where perform connection to.
*/
readonly url?: string;
/**
* Database host.
*/
readonly host?: string;
/**
* Database host port.
*/
readonly port?: number;
/**
* Database username.
*/
readonly username?: string;
/**
* Database password.
*/
readonly password?: string;
/**
* Database name to connect to.
*/
readonly database?: string;
}

View File

@ -17,6 +17,7 @@ import {NamingStrategyInterface} from "../../naming-strategy/NamingStrategyInter
import {LazyRelationsWrapper} from "../../lazy-loading/LazyRelationsWrapper";
import {Connection} from "../../connection/Connection";
import {SchemaBuilder} from "../../schema-builder/SchemaBuilder";
import {MysqlConnectionOptions} from "./MysqlConnectionOptions";
/**
* Organizes communication with MySQL DBMS.
@ -46,21 +47,24 @@ export class MysqlDriver implements Driver {
* Pool of database connections.
*/
protected databaseConnectionPool: DatabaseConnection[] = [];
protected options: MysqlConnectionOptions;
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(protected connection: Connection) {
this.options = connection.options as MysqlConnectionOptions;
Object.assign(connection.options, DriverUtils.buildDriverOptions(connection.options)); // todo: do it better way
// validate options to make sure everything is set
if (!(connection.options.host || (connection.options.extra && connection.options.extra.socketPath)))
if (!(this.options.host || (this.options.extra && this.options.extra.socketPath)))
throw new DriverOptionNotSetError("socketPath and host");
if (!connection.options.username)
if (!this.options.username)
throw new DriverOptionNotSetError("username");
if (!connection.options.database)
if (!this.options.database)
throw new DriverOptionNotSetError("database");
// load mysql package
@ -80,30 +84,17 @@ export class MysqlDriver implements Driver {
// build connection options for the driver
const options = Object.assign({}, {
host: this.connection.options.host,
user: this.connection.options.username,
password: this.connection.options.password,
database: this.connection.options.database,
port: this.connection.options.port
}, this.connection.options.extra || {});
host: this.options.host,
user: this.options.username,
password: this.options.password,
database: this.options.database,
port: this.options.port
}, this.options.extra || {});
// pooling is enabled either when its set explicitly to true,
// either when its not defined at all (e.g. enabled by default)
if (this.connection.options.usePool === undefined || this.connection.options.usePool === true) {
this.pool = this.mysql.createPool(options);
return Promise.resolve();
} else {
return new Promise<void>((ok, fail) => {
const connection = this.mysql.createConnection(options);
this.databaseConnection = {
id: 1,
connection: connection,
isTransactionActive: false
};
this.databaseConnection.connection.connect((err: any) => err ? fail(err) : ok());
});
}
this.pool = this.mysql.createPool(options);
return Promise.resolve();
}
/**

View File

@ -15,6 +15,7 @@ import {IndexSchema} from "../../schema-builder/schema/IndexSchema";
import {QueryRunnerAlreadyReleasedError} from "../../query-runner/error/QueryRunnerAlreadyReleasedError";
import {ColumnType} from "../../metadata/types/ColumnTypes";
import {Connection} from "../../connection/Connection";
import {MysqlConnectionOptions} from "./MysqlConnectionOptions";
/**
* Runs queries on a single mysql database connection.
@ -776,7 +777,7 @@ export class MysqlQueryRunner implements QueryRunner {
* Database name shortcut.
*/
protected get dbName(): string {
return this.connection.options.database as string;
return (this.connection.options as MysqlConnectionOptions).database as string;
}
/**

View File

@ -0,0 +1,53 @@
import {BaseConnectionOptions} from "../../connection/BaseConnectionOptions";
/**
* Oracle-specific connection options.
*/
export interface OracleConnectionOptions extends BaseConnectionOptions {
/**
* Database type.
*/
readonly type: "oracle";
/**
* Connection url where perform connection to.
*/
readonly url?: string;
/**
* Database host.
*/
readonly host?: string;
/**
* Database host port.
*/
readonly port?: number;
/**
* Database username.
*/
readonly username?: string;
/**
* Database password.
*/
readonly password?: string;
/**
* Database name to connect to.
*/
readonly database?: string;
/**
* Connection SID.
*/
readonly sid?: string;
/**
* Schema name. By default is "public".
*/
readonly schemaName?: string;
}

View File

@ -17,6 +17,7 @@ import {NamingStrategyInterface} from "../../naming-strategy/NamingStrategyInter
import {LazyRelationsWrapper} from "../../lazy-loading/LazyRelationsWrapper";
import {Connection} from "../../connection/Connection";
import {SchemaBuilder} from "../../schema-builder/SchemaBuilder";
import {OracleConnectionOptions} from "./OracleConnectionOptions";
/**
* Organizes communication with Oracle DBMS.
@ -49,6 +50,8 @@ export class OracleDriver implements Driver {
*/
protected databaseConnectionPool: DatabaseConnection[] = [];
protected options: OracleConnectionOptions;
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
@ -57,12 +60,14 @@ export class OracleDriver implements Driver {
// Object.assign(connection.options, DriverUtils.buildDriverOptions(connection.options)); // todo: do it better way
this.options = connection.options as OracleConnectionOptions;
// validate options to make sure everything is set
if (!connection.options.host)
if (!this.options.host)
throw new DriverOptionNotSetError("host");
if (!connection.options.username)
if (!this.options.username)
throw new DriverOptionNotSetError("username");
if (!connection.options.sid)
if (!this.options.sid)
throw new DriverOptionNotSetError("sid");
// load oracle package
@ -83,39 +88,22 @@ export class OracleDriver implements Driver {
// build connection options for the driver
const options = Object.assign({}, {
user: this.connection.options.username,
password: this.connection.options.password,
connectString: this.connection.options.host + ":" + this.connection.options.port + "/" + this.connection.options.sid,
}, this.connection.options.extra || {});
user: this.options.username,
password: this.options.password,
connectString: this.options.host + ":" + this.options.port + "/" + this.options.sid,
}, this.options.extra || {});
// pooling is enabled either when its set explicitly to true,
// either when its not defined at all (e.g. enabled by default)
if (this.connection.options.usePool === undefined || this.connection.options.usePool === true) {
return new Promise<void>((ok, fail) => {
this.oracle.createPool(options, (err: any, pool: any) => {
if (err)
return fail(err);
return new Promise<void>((ok, fail) => {
this.oracle.createPool(options, (err: any, pool: any) => {
if (err)
return fail(err);
this.pool = pool;
ok();
});
this.pool = pool;
ok();
});
} else {
return new Promise<void>((ok, fail) => {
this.oracle.getConnection(options, (err: any, connection: any) => {
if (err)
return fail(err);
this.databaseConnection = {
id: 1,
connection: connection,
isTransactionActive: false
};
this.databaseConnection.connection.connect((err: any) => err ? fail(err) : ok());
});
});
}
});
}
/**

View File

@ -14,6 +14,7 @@ import {QueryRunnerAlreadyReleasedError} from "../../query-runner/error/QueryRun
import {ColumnType} from "../../metadata/types/ColumnTypes";
import {Connection} from "../../connection/Connection";
import {OracleDriver} from "./OracleDriver";
import {OracleConnectionOptions} from "./OracleConnectionOptions";
/**
* Runs queries on a single mysql database connection.
@ -847,7 +848,7 @@ AND cons.constraint_name = cols.constraint_name AND cons.owner = cols.owner ORDE
* Database name shortcut.
*/
protected get dbName(): string {
return this.connection.options.schemaName as string;
return (this.connection.options as OracleConnectionOptions).schemaName as string;
}
/**

View File

@ -0,0 +1,48 @@
import {BaseConnectionOptions} from "../../connection/BaseConnectionOptions";
/**
* Postgres-specific connection options.
*/
export interface PostgresConnectionOptions extends BaseConnectionOptions {
/**
* Database type.
*/
readonly type: "postgres";
/**
* Connection url where perform connection to.
*/
readonly url?: string;
/**
* Database host.
*/
readonly host?: string;
/**
* Database host port.
*/
readonly port?: number;
/**
* Database username.
*/
readonly username?: string;
/**
* Database password.
*/
readonly password?: string;
/**
* Database name to connect to.
*/
readonly database?: string;
/**
* Schema name. By default is "public".
*/
readonly schemaName?: string;
}

View File

@ -17,6 +17,7 @@ import {NamingStrategyInterface} from "../../naming-strategy/NamingStrategyInter
import {LazyRelationsWrapper} from "../../lazy-loading/LazyRelationsWrapper";
import {Connection} from "../../connection/Connection";
import {SchemaBuilder} from "../../schema-builder/SchemaBuilder";
import {PostgresConnectionOptions} from "./PostgresConnectionOptions";
// todo(tests):
// check connection with url
@ -63,21 +64,25 @@ export class PostgresDriver implements Driver {
*/
public schemaName?: string;
protected options: PostgresConnectionOptions;
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(protected connection: Connection) {
Object.assign(connection.options, DriverUtils.buildDriverOptions(connection.options)); // todo: do it better way
this.schemaName = connection.options.schemaName || "public";
this.options = connection.options as PostgresConnectionOptions;
Object.assign(this.options, DriverUtils.buildDriverOptions(connection.options)); // todo: do it better way
this.schemaName = this.options.schemaName || "public";
// validate options to make sure everything is set
if (!connection.options.host)
if (!this.options.host)
throw new DriverOptionNotSetError("host");
if (!connection.options.username)
if (!this.options.username)
throw new DriverOptionNotSetError("username");
if (!connection.options.database)
if (!this.options.database)
throw new DriverOptionNotSetError("database");
// load postgres package
@ -97,43 +102,17 @@ export class PostgresDriver implements Driver {
// build connection options for the driver
const options = Object.assign({}, {
host: this.connection.options.host,
user: this.connection.options.username,
password: this.connection.options.password,
database: this.connection.options.database,
port: this.connection.options.port
}, this.connection.options.extra || {});
host: this.options.host,
user: this.options.username,
password: this.options.password,
database: this.options.database,
port: this.options.port
}, this.options.extra || {});
// pooling is enabled either when its set explicitly to true,
// either when its not defined at all (e.g. enabled by default)
if (this.connection.options.usePool === undefined || this.connection.options.usePool === true) {
this.pool = new this.postgres.Pool(options);
return Promise.resolve();
} else {
return new Promise<void>((ok, fail) => {
this.databaseConnection = {
id: 1,
connection: new this.postgres.Client(options),
isTransactionActive: false
};
this.databaseConnection.connection.connect((err: any) => {
if (err) {
fail(err);
} else {
this.databaseConnection!.connection.query(`SET search_path TO '${this.schemaName}', 'public';`, (err: any, result: any) => {
if (err) {
this.connection.logger.logFailedQuery(`SET search_path TO '${this.schemaName}', 'public';`);
this.connection.logger.logQueryError(err);
fail(err);
} else {
ok();
}
});
}
});
});
}
this.pool = new this.postgres.Pool(options);
return Promise.resolve();
}
/**

View File

@ -15,6 +15,7 @@ import {PrimaryKeySchema} from "../../schema-builder/schema/PrimaryKeySchema";
import {QueryRunnerAlreadyReleasedError} from "../../query-runner/error/QueryRunnerAlreadyReleasedError";
import {ColumnType} from "../../metadata/types/ColumnTypes";
import {Connection} from "../../connection/Connection";
import {PostgresConnectionOptions} from "./PostgresConnectionOptions";
/**
* Runs queries on a single postgres database connection.
@ -39,7 +40,7 @@ export class PostgresQueryRunner implements QueryRunner {
constructor(protected connection: Connection,
protected databaseConnection: DatabaseConnection) {
this.schemaName = connection.options.schemaName || "public";
this.schemaName = (connection.options as PostgresConnectionOptions).schemaName || "public";
}
// -------------------------------------------------------------------------
@ -848,7 +849,7 @@ where constraint_type = 'PRIMARY KEY' AND c.table_schema = '${this.schemaName}'
* Database name shortcut.
*/
protected get dbName(): string {
return this.connection.options.database as string;
return (this.connection.options as PostgresConnectionOptions).database as string;
}
/**

View File

@ -0,0 +1,18 @@
import {BaseConnectionOptions} from "../../connection/BaseConnectionOptions";
/**
* Sqlite-specific connection options.
*/
export interface SqliteConnectionOptions extends BaseConnectionOptions {
/**
* Database type.
*/
readonly type: "sqlite";
/**
* Storage type or path to the storage.
*/
readonly database: string;
}

View File

@ -16,6 +16,7 @@ import {NamingStrategyInterface} from "../../naming-strategy/NamingStrategyInter
import {LazyRelationsWrapper} from "../../lazy-loading/LazyRelationsWrapper";
import {Connection} from "../../connection/Connection";
import {SchemaBuilder} from "../../schema-builder/SchemaBuilder";
import {SqliteConnectionOptions} from "./SqliteConnectionOptions";
/**
* Organizes communication with sqlite DBMS.
@ -36,14 +37,18 @@ export class SqliteDriver implements Driver {
*/
protected databaseConnection: DatabaseConnection|undefined;
protected options: SqliteConnectionOptions;
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(protected connection: Connection) {
this.options = connection.options as SqliteConnectionOptions;
// validate options to make sure everything is set
if (!connection.options.storage)
if (!this.options.database)
throw new DriverOptionNotSetError("storage");
// load sqlite package
@ -59,7 +64,7 @@ export class SqliteDriver implements Driver {
*/
connect(): Promise<void> {
return new Promise<void>((ok, fail) => {
const connection = new this.sqlite.Database(this.connection.options.storage, (err: any) => {
const connection = new this.sqlite.Database(this.options.database, (err: any) => {
if (err)
return fail(err);

View File

@ -0,0 +1,43 @@
import {BaseConnectionOptions} from "../../connection/BaseConnectionOptions";
/**
* Microsoft Sql Server specific connection options.
*/
export interface SqlServerConnectionOptions extends BaseConnectionOptions {
/**
* Database type.
*/
readonly type: "mssql";
/**
* Connection url where perform connection to.
*/
readonly url?: string;
/**
* Database host.
*/
readonly host?: string;
/**
* Database host port.
*/
readonly port?: number;
/**
* Database username.
*/
readonly username?: string;
/**
* Database password.
*/
readonly password?: string;
/**
* Database name to connect to.
*/
readonly database?: string;
}

View File

@ -17,6 +17,7 @@ import {NamingStrategyInterface} from "../../naming-strategy/NamingStrategyInter
import {LazyRelationsWrapper} from "../../lazy-loading/LazyRelationsWrapper";
import {Connection} from "../../connection/Connection";
import {SchemaBuilder} from "../../schema-builder/SchemaBuilder";
import {SqlServerConnectionOptions} from "./SqlServerConnectionOptions";
/**
* Organizes communication with SQL Server DBMS.
@ -51,25 +52,23 @@ export class SqlServerDriver implements Driver {
*/
protected databaseConnectionPool: DatabaseConnection[] = [];
/**
* Logger used to log queries and errors.
*/
protected logger: Logger;
protected options: SqlServerConnectionOptions;
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(protected connection: Connection) {
this.options = connection.options as SqlServerConnectionOptions;
Object.assign(connection.options, DriverUtils.buildDriverOptions(connection.options)); // todo: do it better way
// validate options to make sure everything is set
if (!connection.options.host)
if (!this.options.host)
throw new DriverOptionNotSetError("host");
if (!connection.options.username)
if (!this.options.username)
throw new DriverOptionNotSetError("username");
if (!connection.options.database)
if (!this.options.database)
throw new DriverOptionNotSetError("database");
// load mssql package
@ -89,12 +88,12 @@ export class SqlServerDriver implements Driver {
// build connection options for the driver
const options = Object.assign({}, {
server: this.connection.options.host,
user: this.connection.options.username,
password: this.connection.options.password,
database: this.connection.options.database,
port: this.connection.options.port
}, this.connection.options.extra || {});
server: this.options.host,
user: this.options.username,
password: this.options.password,
database: this.options.database,
port: this.options.port
}, this.options.extra || {});
// set default useUTC option if it hasn't been set
if (!options.options) options.options = { useUTC: false };
@ -106,13 +105,6 @@ export class SqlServerDriver implements Driver {
const connection = new this.mssql.Connection(options).connect((err: any) => {
if (err) return fail(err);
this.connectionPool = connection;
if (this.connection.options.usePool === false) {
this.databaseConnection = {
id: 1,
connection: new this.mssql.Request(connection),
isTransactionActive: false
};
}
ok();
});
});

View File

@ -16,6 +16,8 @@ import {QueryRunnerAlreadyReleasedError} from "../../query-runner/error/QueryRun
import {ColumnType} from "../../metadata/types/ColumnTypes";
import {Connection} from "../../connection/Connection";
import {MysqlDriver} from "../mysql/MysqlDriver";
import {SqliteConnectionOptions} from "../sqlite/SqliteConnectionOptions";
import {SqlServerConnectionOptions} from "./SqlServerConnectionOptions";
/**
* Runs queries on a single mysql database connection.
@ -881,7 +883,7 @@ WHERE columnUsages.TABLE_CATALOG = '${this.dbName}' AND tableConstraints.TABLE_C
* Database name shortcut.
*/
protected get dbName(): string {
return this.connection.options.database as string;
return (this.connection.options as SqlServerConnectionOptions).database as string;
}
/**

View File

@ -0,0 +1,18 @@
import {BaseConnectionOptions} from "../../connection/BaseConnectionOptions";
/**
* Websql-specific connection options.
*/
export interface WebSqlConnectionOptions extends BaseConnectionOptions {
/**
* Database type.
*/
readonly type: "websql";
/**
* Storage type or path to the storage.
*/
readonly database: string;
}

View File

@ -13,6 +13,7 @@ import {DataTransformationUtils} from "../../util/DataTransformationUtils";
import {WebsqlQueryRunner} from "./WebsqlQueryRunner";
import {Connection} from "../../connection/Connection";
import {SchemaBuilder} from "../../schema-builder/SchemaBuilder";
import {WebSqlConnectionOptions} from "./WebSqlConnectionOptions";
/**
* Declare a global function that is only available in browsers that support WebSQL.
@ -32,6 +33,8 @@ export class WebsqlDriver implements Driver {
* Connection to database.
*/
protected databaseConnection: DatabaseConnection|undefined;
protected options: WebSqlConnectionOptions;
// -------------------------------------------------------------------------
// Constructor
@ -39,6 +42,7 @@ export class WebsqlDriver implements Driver {
constructor(protected connection: Connection) {
this.options = connection.options as WebSqlConnectionOptions;
Object.assign(connection.options, DriverUtils.buildDriverOptions(connection.options)); // todo: do it better way
// validate options to make sure everything is set
@ -46,7 +50,7 @@ export class WebsqlDriver implements Driver {
// throw new DriverOptionNotSetError("host");
// if (!this.options.username)
// throw new DriverOptionNotSetError("username");
if (!connection.options.database)
if (!this.options.database)
throw new DriverOptionNotSetError("database");
// todo: what about extra options: version, description, size
}
@ -64,8 +68,8 @@ export class WebsqlDriver implements Driver {
// build connection options for the driver
const options = Object.assign({}, {
database: this.connection.options.database,
}, this.connection.options.extra || {});
database: this.options.database,
}, this.options.extra || {});
return new Promise<void>((ok, fail) => {
const connection = openDatabase(

View File

@ -92,12 +92,10 @@ describe("Connection", () => {
it("should throw DriverOptionNotSetError when extra.socketPath and host is missing", function() {
expect(() => {
connection = getConnectionManager().create(<ConnectionOptions>{
driver: {
"type": "mysql",
"username": "test",
"password": "test",
"database": "test",
},
type: "mysql",
username: "test",
password: "test",
database: "test",
entities: [],
entitySchemas: [],
dropSchemaOnConnection: false,

View File

@ -2,6 +2,7 @@ import "reflect-metadata";
import {PostgresDriver} from "../../../src/driver/postgres/PostgresDriver";
import {expect} from "chai";
import {Connection} from "../../../src/connection/Connection";
import {PostgresConnectionOptions} from "../../../src/driver/postgres/PostgresConnectionOptions";
describe("github issues > #114 Can not be parsed correctly the URL of pg.", () => {
@ -15,11 +16,12 @@ describe("github issues > #114 Can not be parsed correctly the URL of pg.", () =
});
it("should not fail in url parser", () => {
expect(connection.options.username).to.be.eq("test");
expect(connection.options.password).to.be.eq("test");
expect(connection.options.host).to.be.eq("localhost");
expect(connection.options.port).to.be.eq(5432);
expect(connection.options.database).to.be.eq("test");
const options = connection.options as PostgresConnectionOptions;
expect(options.username).to.be.eq("test");
expect(options.password).to.be.eq("test");
expect(options.host).to.be.eq("localhost");
expect(options.port).to.be.eq(5432);
expect(options.database).to.be.eq("test");
});
});

View File

@ -2,12 +2,12 @@ import {ConnectionOptions} from "../../src/connection/ConnectionOptions";
import {createConnection, createConnections} from "../../src/index";
import {Connection} from "../../src/connection/Connection";
import {EntitySchema} from "../../src/entity-schema/EntitySchema";
import {DriverType} from "../../src/driver/DriverType";
import {DatabaseType} from "../../src/driver/DatabaseType";
/**
* Interface in which data is stored in ormconfig.json of the project.
*/
export interface TestingConnectionOptions extends ConnectionOptions {
export type TestingConnectionOptions = ConnectionOptions & {
/**
* Indicates if this connection should be skipped.
@ -19,7 +19,7 @@ export interface TestingConnectionOptions extends ConnectionOptions {
*/
disabledIfNotEnabledImplicitly?: boolean;
}
};
/**
* Options used to create a connection for testing purposes.
@ -35,7 +35,7 @@ export interface TestingOptions {
/**
* List of enabled drivers for the given test suite.
*/
enabledDrivers?: DriverType[];
enabledDrivers?: DatabaseType[];
/**
* Entities needs to be included in the connection for the given test suite.
@ -73,7 +73,7 @@ export interface TestingOptions {
* Creates a testing connection options for the given driver type based on the configuration in the ormconfig.json
* and given options that can override some of its configuration for the test-specific use case.
*/
export function setupSingleTestingConnection(driverType: DriverType, options: TestingOptions) {
export function setupSingleTestingConnection(driverType: DatabaseType, options: TestingOptions): ConnectionOptions {
const testingConnections = setupTestingConnections({
name: options.name ? options.name : undefined,
@ -116,7 +116,7 @@ export function getTypeOrmConfig(): TestingConnectionOptions[] {
* Creates a testing connections options based on the configuration in the ormconfig.json
* and given options that can override some of its configuration for the test-specific use case.
*/
export function setupTestingConnections(options?: TestingOptions) {
export function setupTestingConnections(options?: TestingOptions): ConnectionOptions[] {
const ormConfigConnectionOptionsArray = getTypeOrmConfig();
if (!ormConfigConnectionOptionsArray.length)
@ -128,7 +128,7 @@ export function setupTestingConnections(options?: TestingOptions) {
return false;
if (options && options.enabledDrivers && options.enabledDrivers.length)
return options.enabledDrivers.indexOf(connectionOptions.driver!.type!) !== -1; // ! is temporary
return options.enabledDrivers.indexOf(connectionOptions.type!) !== -1; // ! is temporary
if (connectionOptions.disabledIfNotEnabledImplicitly === true)
return false;
@ -136,22 +136,15 @@ export function setupTestingConnections(options?: TestingOptions) {
return true;
})
.map(connectionOptions => {
const newConnectionOptions = Object.assign({}, connectionOptions as ConnectionOptions, {
return Object.assign({}, connectionOptions as ConnectionOptions, {
name: options && options.name ? options.name : connectionOptions.name,
entities: options && options.entities ? options.entities : [],
subscribers: options && options.subscribers ? options.subscribers : [],
entitySchemas: options && options.entitySchemas ? options.entitySchemas : [],
autoSchemaSync: options && options.entities ? options.schemaCreate : false,
dropSchemaOnConnection: options && options.entities ? options.dropSchemaOnConnection : false,
schemaName: options && options.schemaName ? options.schemaName : undefined,
});
if (options && options.schemaName && newConnectionOptions.driver) {
// todo: we use any because driver.schemaName is readonly. Need to find better solution here
(newConnectionOptions.driver as any).schemaName = options.schemaName;
}
return newConnectionOptions;
});
}