Skip to content

Commit

Permalink
fix: adjust data source cleanup behaviour to jsdoc description
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Aug 11, 2024
1 parent 39dcc92 commit e27f42e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
49 changes: 19 additions & 30 deletions src/database/check.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { DataSourceOptions } from 'typeorm';
import { DataSource, MigrationExecutor } from 'typeorm';
import {
hasDataSource,
setDataSource,
unsetDataSource,
useDataSource,
useDataSourceOptions,
} from '../data-source';
Expand All @@ -15,39 +14,37 @@ import type { DatabaseCheckContext, DatabaseCheckResult } from './type';
* @param context
*/
export async function checkDatabase(context: DatabaseCheckContext = {}) : Promise<DatabaseCheckResult> {
context.dataSourceCleanup = context.dataSourceCleanup ?? true;

const result : DatabaseCheckResult = {
exists: true,
schema: false,
migrationsPending: [],
};

let { dataSource } = context;
let dataSource : DataSource;
let dataSourceCleanup : boolean;

if (
typeof dataSource === 'undefined' &&
typeof context.dataSource === 'undefined' &&
typeof context.options === 'undefined' &&
hasDataSource(context.alias)
) {
// todo: data-source might get initialized here
dataSource = await useDataSource(context.alias);
}

const dataSourceExisted = !!dataSource;

if (typeof dataSource === 'undefined') {
dataSource = await useDataSource(context.alias);
dataSourceCleanup = false;
} else {
let dataSourceOptions : DataSourceOptions;
if (context.options) {
dataSource = new DataSource({
...context.options,
synchronize: false,
});
dataSourceOptions = context.options;
} else {
const options = await useDataSourceOptions(context.alias);
dataSource = new DataSource({
...options,
synchronize: false,
});
dataSourceOptions = await useDataSourceOptions(context.alias);
}

dataSource = new DataSource({
...dataSourceOptions,
synchronize: true,
});
dataSourceCleanup = context.dataSourceCleanup ?? true;
}

try {
Expand Down Expand Up @@ -103,16 +100,8 @@ export async function checkDatabase(context: DatabaseCheckContext = {}) : Promis

await queryRunner.release();

if (!dataSourceExisted) {
if (context.dataSourceCleanup) {
await dataSource.destroy();

if (!context.dataSource) {
unsetDataSource(context.alias);
}
} else {
setDataSource(dataSource, context.alias);
}
if (dataSourceCleanup) {
await dataSource.destroy();
}

return result;
Expand Down
11 changes: 9 additions & 2 deletions src/database/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ export type DatabaseBaseContext = {
initialDatabase?: string,
};

export type DatabaseCheckContext = Omit<DatabaseBaseContext, 'initialDatabase'> & {
export type DatabaseCheckContext = {
/**
* Options for finding the typeorm DataSource.
*
* Default: undefined
*/
options?: DataSourceOptions,

/**
* Use alias to access already registered DataSource / DataSourceOptions.
*
Expand All @@ -32,7 +39,7 @@ export type DatabaseCheckContext = Omit<DatabaseBaseContext, 'initialDatabase'>

/**
* Indicates whether to destroy the data-source
* afterwards or not.
* afterward or not.
* If a datasource previously existed, this option will be ignored.
*
* default: true
Expand Down

0 comments on commit e27f42e

Please sign in to comment.