Skip to content

Commit

Permalink
feat: async data-source exports
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Oct 21, 2023
1 parent 524dc8d commit a2cdb9d
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 12 deletions.
20 changes: 16 additions & 4 deletions src/data-source/find/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import {
CodeTransformation,
adjustFilePath,
isCodeTransformation,
isPromise, readTSConfig,
safeReplaceWindowsSeparator,
} from '../../utils';
import type { DataSourceFindOptions } from './type';
import type { TSConfig } from '../../utils/tsconfig';
import { readTSConfig } from '../../utils/tsconfig';
import type { TSConfig } from '../../utils';

export async function findDataSource(
context?: DataSourceFindOptions,
Expand Down Expand Up @@ -101,13 +101,21 @@ export async function findDataSource(
);

if (info) {
const fileExports = await load(info);
let fileExports = await load(info);

if (isPromise(fileExports)) {
fileExports = await fileExports;
}

if (InstanceChecker.isDataSource(fileExports)) {
return fileExports;
}

const defaultExport = getModuleExport(fileExports);
if (isPromise(defaultExport.value)) {
defaultExport.value = await defaultExport.value;
}

if (
defaultExport &&
InstanceChecker.isDataSource(defaultExport.value)
Expand All @@ -118,7 +126,11 @@ export async function findDataSource(
if (isObject(fileExports)) {
const keys = Object.keys(fileExports);
for (let j = 0; j < keys.length; j++) {
const value = fileExports[keys[j]];
let value = fileExports[keys[j]];

if (isPromise(value)) {
value = await value;
}

if (InstanceChecker.isDataSource(value)) {
return value;
Expand Down
5 changes: 2 additions & 3 deletions src/seeder/factory/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import type { FakerOptions, LocaleDefinition } from '@faker-js/faker';
import { Faker } from '@faker-js/faker';
import type { SaveOptions } from 'typeorm';
import { isObject, load } from 'locter';
import { SeederFactoryContext } from './type';
import { hasOwnProperty } from '../../utils';
import type { SeederFactoryContext } from './type';
import { hasOwnProperty, isPromise } from '../../utils';
import { useDataSource } from '../../data-source';
import { isPromise } from '../utils';

export class SeederFactory<O extends Record<string, any>, Meta = unknown> {
public readonly context: SeederFactoryContext<O, Meta>;
Expand Down
1 change: 0 additions & 1 deletion src/seeder/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './file-path';
export * from './prepare';
export * from './promise';
export * from './template';
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './entity';
export * from './file-path';
export * from './file-system';
export * from './has-property';
export * from './promise';
export * from './separator';
export * from './slash';
export * from './tsconfig';
7 changes: 4 additions & 3 deletions src/seeder/utils/promise.ts → src/utils/promise.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export function isPromise(p: unknown) : p is Promise<unknown> {
return typeof p === 'object' &&
p !== null &&
import { isObject } from 'locter';

export function isPromise(p: unknown): p is Promise<unknown> {
return isObject(p) &&
(
p instanceof Promise ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down
19 changes: 19 additions & 0 deletions test/data/typeorm/data-source-async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { DataSource } from 'typeorm';
import { dataSource } from './data-source';

let instance : DataSource | undefined;
let instancePromise : Promise<DataSource> | undefined;

export async function useDataSource() {
if (typeof instance !== 'undefined') {
return instance;
}

if (typeof instancePromise === 'undefined') {
instancePromise = dataSource.initialize();
}

instance = await instancePromise;

return instance;
}
12 changes: 11 additions & 1 deletion test/unit/data-source/find.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'path';
import { findDataSource } from '../../../src';

describe('src/data-source/utils/find.ts', () => {
it('should find data-source', async () => {
it('should find and load data-source', async () => {
let dataSource = await findDataSource({
directory: path.join(__dirname, '..', '..', 'data', 'typeorm'),
});
Expand All @@ -22,6 +22,16 @@ describe('src/data-source/utils/find.ts', () => {
expect(InstanceChecker.isDataSource(dataSource));
});

it('should find and load async data-source', async () => {
const dataSource = await findDataSource({
directory: path.join(__dirname, '..', '..', 'data', 'typeorm'),
fileName: 'data-source-async',
});

expect(dataSource).toBeDefined();
expect(InstanceChecker.isDataSource(dataSource));
});

it('should find data-source with windows separator', async () => {
const dataSource = await findDataSource({
directory: 'test\\data\\typeorm',
Expand Down

0 comments on commit a2cdb9d

Please sign in to comment.