Skip to content

Commit

Permalink
feat: remove legacy data-source options building
Browse files Browse the repository at this point in the history
BREAKING CHANGE: ormconfig no longer supported
  • Loading branch information
tada5hi committed May 29, 2023
1 parent c5d1d09 commit 6cb4d77
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 154 deletions.
16 changes: 0 additions & 16 deletions src/cli/commands/database/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { CodeTransformation, setCodeTransformation } from '../../../utils';
export interface DatabaseCreateArguments extends Arguments {
codeTransformation: string,
root: string;
connection: 'default' | string;
config: 'ormconfig' | string;
dataSource: 'data-source' | string;
synchronize: string;
initialDatabase?: unknown;
Expand All @@ -31,18 +29,6 @@ export class DatabaseCreateCommand implements CommandModule {
default: process.cwd(),
describe: 'Path to the data-source / config file.',
})
.option('connection', {
alias: 'c',
default: 'default',
describe: 'Name of the connection on which run a query.',
deprecated: true,
})
.option('config', {
alias: 'f',
default: 'ormconfig',
describe: 'Name of the file with the data-source configuration.',
deprecated: true,
})
.option('dataSource', {
alias: 'd',
default: 'data-source',
Expand All @@ -67,8 +53,6 @@ export class DatabaseCreateCommand implements CommandModule {
}

const dataSourceOptions = await buildDataSourceOptions({
name: args.connection,
configName: args.config,
directory: args.root,
dataSourceName: args.dataSource,
});
Expand Down
16 changes: 0 additions & 16 deletions src/cli/commands/database/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { CodeTransformation, setCodeTransformation } from '../../../utils';
export interface DatabaseDropArguments extends Arguments {
codeTransformation: string,
root: string;
connection: 'default' | string;
config: 'ormconfig' | string;
dataSource: 'data-source' | string;
}

Expand All @@ -29,18 +27,6 @@ export class DatabaseDropCommand implements CommandModule {
default: process.cwd(),
describe: 'Path to the data-source / config file.',
})
.option('connection', {
alias: 'c',
default: 'default',
describe: 'Name of the connection on which run a query.',
deprecated: true,
})
.option('config', {
alias: 'f',
default: 'ormconfig',
describe: 'Name of the file with the data-source configuration.',
deprecated: true,
})
.option('dataSource', {
alias: 'd',
default: 'data-source',
Expand All @@ -59,8 +45,6 @@ export class DatabaseDropCommand implements CommandModule {
}

const dataSourceOptions = await buildDataSourceOptions({
name: args.connection,
configName: args.config,
directory: args.root,
dataSourceName: args.dataSource,
});
Expand Down
16 changes: 0 additions & 16 deletions src/cli/commands/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import { CodeTransformation, setCodeTransformation } from '../../utils';
export interface DatabaseSeedArguments extends Arguments {
codeTransformation: string,
root: string;
connection: 'default' | string;
config: 'ormconfig' | string;
dataSource: 'data-source' | string;
seed: undefined | string,
}
Expand All @@ -29,18 +27,6 @@ export class SeedCommand implements CommandModule {
default: process.cwd(),
describe: 'Path to the data-source / config file.',
})
.option('connection', {
alias: 'c',
default: 'default',
describe: 'Name of the connection on which run a query.',
deprecated: true,
})
.option('config', {
alias: 'f',
default: 'ormconfig',
describe: 'Name of the file with the data-source configuration.',
deprecated: true,
})
.option('dataSource', {
alias: 'd',
default: 'data-source',
Expand All @@ -60,8 +46,6 @@ export class SeedCommand implements CommandModule {
}

const dataSourceOptions = await buildDataSourceOptions({
name: args.connection,
configName: args.config,
directory: args.root,
dataSourceName: args.dataSource,
});
Expand Down
40 changes: 6 additions & 34 deletions src/data-source/options/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DataSourceOptions } from 'typeorm';
import { ConnectionOptionsReader } from 'typeorm';
import { useEnv } from '../../env';
import { OptionsError } from '../../errors';
import type { SeederOptions } from '../../seeder';
import { findDataSource } from '../find';
import type { DataSourceOptionsBuildContext } from './type';
Expand Down Expand Up @@ -53,28 +53,6 @@ export async function extendDataSourceOptions(
return options;
}

/**
* Build DataSourceOptions from configuration.
*
* @deprecated
* @param context
*/
export async function buildLegacyDataSourceOptions(
context: DataSourceOptionsBuildContext,
) : Promise<DataSourceOptions> {
const directory : string = context.directory || process.cwd();
const tsconfigDirectory : string = context.tsconfigDirectory || process.cwd();

const connectionOptionsReader = new ConnectionOptionsReader({
root: directory,
configName: context.configName,
});

const dataSourceOptions = await connectionOptionsReader.get(context.name || 'default');

return extendDataSourceOptions(dataSourceOptions, tsconfigDirectory);
}

/**
* Build DataSourceOptions from DataSource or from configuration.
*
Expand All @@ -99,19 +77,13 @@ export async function buildDataSourceOptions(
tsconfigDirectory,
);

if (context.experimental) {
return mergeDataSourceOptionsWithEnv(options);
}

return options;
return mergeDataSourceOptionsWithEnv(options);
}

if (context.experimental) {
const options = readDataSourceOptionsFromEnv();
if (options) {
return extendDataSourceOptions(options);
}
const options = readDataSourceOptionsFromEnv();
if (options) {
return extendDataSourceOptions(options);
}

return buildLegacyDataSourceOptions(context);
throw OptionsError.notFound();
}
25 changes: 4 additions & 21 deletions src/data-source/options/type.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
export type DataSourceOptionsBuildContext = {
/**
* Database connection name
* Default: default
*
* @deprecated
*/
name?: string,
/**
* Configuration file name without extension
* Default: ormconfig
*
* @deprecated
*/
configName?: string,
/**
* Data source file name without extension
*
* Default: data-source
*/
dataSourceName?: string,
/**
* Directory where to find dataSource + config
*
* Default: process.cwd()
*/
directory?: string,
/**
* Directory path to the tsconfig.json file
*
* Default: process.cwd()
*/
tsconfigDirectory?: string,

/**
* Use experimental features,
* like merging env and file data-source options.
*/
experimental?: boolean
tsconfigDirectory?: string
};
4 changes: 4 additions & 0 deletions src/errors/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export class OptionsError extends TypeormExtensionError {
return new OptionsError('The database options could not be determined.');
}

static notFound() {
return new OptionsError('The database options could not be located/loaded.');
}

static databaseNotDefined() {
return new OptionsError('The database name to connect to is not defined.');
}
Expand Down
17 changes: 0 additions & 17 deletions test/unit/connection/index.spec.ts

This file was deleted.

5 changes: 2 additions & 3 deletions test/unit/data-source/options/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
mergeDataSourceOptionsWithEnv,
readDataSourceOptionsFromEnv
} from "../../../../src";
import {EnvironmentVariableName, resetEnv, } from "../../../../src/env";
import {EnvironmentVariableName, resetEnv, } from "../../../../src";
import {User} from "../../../data/entity/user";

describe('src/data-source/options/env', function () {
Expand Down Expand Up @@ -64,8 +64,7 @@ describe('src/data-source/options/env', function () {
};

const options = await buildDataSourceOptions({
directory: 'test/data/typeorm',
experimental: true
directory: 'test/data/typeorm'
});

expect(options).toBeDefined();
Expand Down
34 changes: 3 additions & 31 deletions test/unit/database/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,20 @@
import path from "path";
import path from "node:path";
import {
buildLegacyDataSourceOptions,
buildDataSourceOptions,
} from "../../../src";
import {buildDriverOptions} from "../../../src";
import {getCharsetFromDataSourceOptions} from "../../../src";
import {checkTestDatabase, destroyTestDatabase} from "../../data/typeorm/utils";

describe('src/database/module.ts', () => {
const rootPath : string = path.resolve(process.cwd(), 'test/data/typeorm');

it('should build simple connection options', async () => {
const options = await buildLegacyDataSourceOptions({
const options = await buildDataSourceOptions({
directory: rootPath,
configName: 'ormconfig.json'
});

expect(options).toBeDefined();

const driverOptions = buildDriverOptions(options);
expect(driverOptions).toBeDefined();
expect(driverOptions.host).toEqual('localhost');
expect(driverOptions.user).toEqual('root');
expect(driverOptions.password).toEqual('admin');
expect(driverOptions.database).toEqual('test');
expect(driverOptions.extra).toBeDefined();
if(driverOptions.extra) {
expect(driverOptions.extra.socketPath).toEqual('/var/mysqld/mysqld.sock');
}
expect(driverOptions.port).toEqual(3306);
expect(driverOptions.charset).toEqual('UTF8_GENERAL_CI');
});

it('should extend database operation options', async () => {
const connectionOptions = await buildLegacyDataSourceOptions({
directory: rootPath,
configName: 'ormconfig.json'
});

expect(connectionOptions).toBeDefined();

const charset = getCharsetFromDataSourceOptions(connectionOptions);
expect(charset).toEqual('UTF8_GENERAL_CI');
})

it('should check database', async () => {
let check = await checkTestDatabase();

Expand Down
File renamed without changes.

0 comments on commit 6cb4d77

Please sign in to comment.