Skip to content

Commit

Permalink
fix: minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Jul 25, 2023
1 parent 369608d commit 8773e02
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 55 deletions.
44 changes: 9 additions & 35 deletions docs/guide/datasource-api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,26 +263,6 @@ declare async function buildDataSourceOptions(
**References**
- [DataSourceOptionsBuildContext](#datasourceoptionsbuildcontext)

## `extendDataSourceOptions`

```typescript
declare async function extendDataSourceOptions(
options: DataSourceOptions,
tsConfigDirectory?: string,
) : Promise<DataSourceOptions>;
```

**Parameters**

| Name | Type | Description |
|:--------------------|:----------------------------|:-------------------------------------------------------------------|
| `options` | `DataSourceOptions` | Alias for receiving the typeorm options object. Default: `default` |
| `tsConfigDirectory` | `string` or `undefined` | Directory path of tsconfig.json. Default: `process.cwd()` |

**Returns**

`Promise`<`DataSourceOptions`>

## `DataSourceFindOptions`

```typescript
Expand All @@ -296,34 +276,28 @@ export type DataSourceFindOptions = {

```typescript
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
tsconfig?: string | TSConfig,
/**
* This option indicates if file paths should be preserved,
* and treated as if the just-in-time compilation environment is detected.
*/
preserveFilePaths?: boolean
};
```
4 changes: 2 additions & 2 deletions src/cli/commands/database/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { dropDatabase } from '../../../database';
import {
adjustFilePath,
parseFilePath,
readTSConfig,
resolveFilePath,
} from '../../../utils';
import type { TSConfig } from '../../../utils/tsconfig';
import { readTSConfig } from '../../../utils/tsconfig';
import type { TSConfig } from '../../../utils';

export interface DatabaseDropArguments extends Arguments {
preserveFilePaths: boolean,
Expand Down
2 changes: 1 addition & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { hideBin } from 'yargs/helpers';
import {
DatabaseCreateCommand,
DatabaseDropCommand,
SeedCreateCommand,
SeedRunCommand,
} from './commands';
import { SeedCreateCommand } from './commands/seed/create';

yargs(hideBin(process.argv))
.scriptName('typeorm-extension')
Expand Down
4 changes: 2 additions & 2 deletions src/database/driver/cockroachdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OptionsError } from '../../errors';
import type { DatabaseCreateContext, DatabaseDropContext } from '../type';
import { createSimplePostgresConnection } from './postgres';
import { buildDriverOptions, createDriver } from './utils';
import { buildDatabaseCreateContext, buildDatabaseDropContext, setupDatabaseSchema } from '../utils';
import { buildDatabaseCreateContext, buildDatabaseDropContext, synchronizeDatabaseSchema } from '../utils';

export async function executeSimpleCockroachDBQuery(connection: any, query: string, endConnection = true) {
return new Promise(((resolve, reject) => {
Expand Down Expand Up @@ -45,7 +45,7 @@ export async function createCockroachDBDatabase(
const result = await executeSimpleCockroachDBQuery(connection, query);

if (context.synchronize) {
await setupDatabaseSchema(context.options);
await synchronizeDatabaseSchema(context.options);
}

return result;
Expand Down
4 changes: 2 additions & 2 deletions src/database/driver/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OptionsError } from '../../errors';
import type { DatabaseCreateContext, DatabaseDropContext } from '../type';
import type { DriverOptions } from './type';
import { buildDriverOptions, createDriver } from './utils';
import { buildDatabaseCreateContext, buildDatabaseDropContext, setupDatabaseSchema } from '../utils';
import { buildDatabaseCreateContext, buildDatabaseDropContext, synchronizeDatabaseSchema } from '../utils';

export async function createSimpleMongoDBConnection(
driver: MongoDriver,
Expand Down Expand Up @@ -45,7 +45,7 @@ export async function createMongoDBDatabase(
await client.close();

if (context.synchronize) {
await setupDatabaseSchema(context.options);
await synchronizeDatabaseSchema(context.options);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/database/driver/mssql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OptionsError } from '../../errors';
import type { DatabaseCreateContext, DatabaseDropContext } from '../type';
import type { DriverOptions } from './type';
import { buildDriverOptions, createDriver } from './utils';
import { buildDatabaseCreateContext, buildDatabaseDropContext, setupDatabaseSchema } from '../utils';
import { buildDatabaseCreateContext, buildDatabaseDropContext, synchronizeDatabaseSchema } from '../utils';

export async function createSimpleMsSQLConnection(
driver: SqlServerDriver,
Expand Down Expand Up @@ -49,7 +49,7 @@ export async function createMsSQLDatabase(
const result = await connection.query(query);

if (context.synchronize) {
await setupDatabaseSchema(context.options);
await synchronizeDatabaseSchema(context.options);
}

return result;
Expand Down
4 changes: 2 additions & 2 deletions src/database/driver/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OptionsError } from '../../errors';
import type { DatabaseCreateContext, DatabaseDropContext } from '../type';
import type { DriverOptions } from './type';
import { buildDriverOptions, createDriver } from './utils';
import { buildDatabaseCreateContext, buildDatabaseDropContext, setupDatabaseSchema } from '../utils';
import { buildDatabaseCreateContext, buildDatabaseDropContext, synchronizeDatabaseSchema } from '../utils';

export async function createSimpleMySQLConnection(
driver: MysqlDriver,
Expand Down Expand Up @@ -79,7 +79,7 @@ export async function createMySQLDatabase(
const result = await executeSimpleMysqlQuery(connection, query);

if (context.synchronize) {
await setupDatabaseSchema(context.options);
await synchronizeDatabaseSchema(context.options);
}

return result;
Expand Down
4 changes: 2 additions & 2 deletions src/database/driver/oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { OptionsError } from '../../errors';
import type { DatabaseCreateContext, DatabaseDropContext } from '../type';
import type { DriverOptions } from './type';
import { buildDriverOptions, createDriver } from './utils';
import { buildDatabaseCreateContext, setupDatabaseSchema } from '../utils';
import { buildDatabaseCreateContext, synchronizeDatabaseSchema } from '../utils';

export function createSimpleOracleConnection(
driver: OracleDriver,
Expand Down Expand Up @@ -63,7 +63,7 @@ export async function createOracleDatabase(
const result = await connection.execute(query);

if (context.synchronize) {
await setupDatabaseSchema(context.options);
await synchronizeDatabaseSchema(context.options);
}

return result;
Expand Down
4 changes: 2 additions & 2 deletions src/database/driver/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { DatabaseBaseContext, DatabaseCreateContext, DatabaseDropContext }
import { hasOwnProperty } from '../../utils';
import type { DriverOptions } from './type';
import { buildDriverOptions, createDriver } from './utils';
import { buildDatabaseCreateContext, buildDatabaseDropContext, setupDatabaseSchema } from '../utils';
import { buildDatabaseCreateContext, buildDatabaseDropContext, synchronizeDatabaseSchema } from '../utils';

export async function createSimplePostgresConnection(
driver: PostgresDriver | CockroachDriver,
Expand Down Expand Up @@ -94,7 +94,7 @@ export async function createPostgresDatabase(
const result = await executeSimplePostgresQuery(connection, query);

if (context.synchronize) {
await setupDatabaseSchema(context.options);
await synchronizeDatabaseSchema(context.options);
}

return result;
Expand Down
4 changes: 2 additions & 2 deletions src/database/driver/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'node:fs';
import { OptionsError } from '../../errors';
import type { DatabaseCreateContext, DatabaseDropContext } from '../type';
import { buildDriverOptions } from './utils';
import { buildDatabaseCreateContext, buildDatabaseDropContext, setupDatabaseSchema } from '../utils';
import { buildDatabaseCreateContext, buildDatabaseDropContext, synchronizeDatabaseSchema } from '../utils';

export async function createSQLiteDatabase(
context?: DatabaseCreateContext,
Expand All @@ -28,7 +28,7 @@ export async function createSQLiteDatabase(
await fs.promises.access(directoryPath, fs.constants.W_OK);

if (context.synchronize) {
await setupDatabaseSchema(context.options);
await synchronizeDatabaseSchema(context.options);
}
}

Expand Down
14 changes: 11 additions & 3 deletions src/database/utils/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { DataSourceOptions } from 'typeorm';
import type { DataSourceOptions, Migration } from 'typeorm';
import { DataSource, InstanceChecker } from 'typeorm';

export async function setupDatabaseSchema(input: DataSource | DataSourceOptions) {
export async function synchronizeDatabaseSchema(
input: DataSource | DataSourceOptions,
) : Promise<Migration[]> {
let dataSource: DataSource;
let options: DataSourceOptions;

Expand All @@ -24,13 +26,19 @@ export async function setupDatabaseSchema(input: DataSource | DataSourceOptions)
Object.keys(input.migrations).length;
}

let migrations : Migration[] = [];

if (migrationsCount > 0) {
await dataSource.runMigrations({ transaction: options.migrationsTransactionMode });
migrations = await dataSource.runMigrations({
transaction: options.migrationsTransactionMode,
});
} else {
await dataSource.synchronize(false);
}

if (!InstanceChecker.isDataSource(input)) {
await dataSource.destroy();
}

return migrations;
}

0 comments on commit 8773e02

Please sign in to comment.