Skip to content

Commit

Permalink
feat: lazy transpiling of seeder-/data-source-files
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Feb 14, 2023
1 parent ac02729 commit 5d092ad
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 51 deletions.
11 changes: 6 additions & 5 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,19 @@ The following commands are available in the terminal:
- `typeorm-extension db:drop` to drop the database
- `typeorm-extension seed` seed the database

Alternatively, the full command path can be set in the package.json file to run it e.g. with ts-node.

```
"scripts": {
...
"db:create": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js db:create",
"db:drop": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js db:drop",
"seed": "ts-node ./node_modules/typeorm-extension/dist/cli/index.js seed"
"db:create": "typeorm-extension db:create",
"db:drop": "typeorm-extension db:drop",
"seed": "typeorm-extension seed"
...
}
```

Ts-node is no longer needed due to the fact that TypeScript files are transpiled on the file as needed.
The same applies to files from another module system (CJS / ESM) 🧙‍.

> Read the [Seeding Configuration](#configuration) section to find out how to specify the path,
> for the seeder- & factory-location.
Expand Down
89 changes: 58 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"build": "rm -rf ./dist && tsc",
"build:watch": "npm run build -- --watch",
"commit": "npx git-cz",
"test": "cross-env NODE_ENV=test jest --config ./test/jest.config.js",
"test": "jest --config ./test/jest.config.js",
"test:coverage": "cross-env NODE_ENV=test jest --config ./test/jest.config.js --coverage",
"lint": "eslint --ext .js,.vue,.ts ./src",
"lint:fix": "npm run lint -- --fix",
Expand Down Expand Up @@ -48,7 +48,7 @@
"license": "MIT",
"dependencies": {
"@faker-js/faker": "^7.6.0",
"locter": "^0.7.0",
"locter": "^1.0.3",
"rapiq": "^0.6.3",
"reflect-metadata": "^0.1.13",
"yargs": "^17.6.2"
Expand Down
8 changes: 4 additions & 4 deletions src/data-source/find/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
loadScriptFileSync,
locateFile,
load,
locate,
removeFileNameExtension,
} from 'locter';
import path from 'path';
Expand Down Expand Up @@ -77,13 +77,13 @@ export async function findDataSource(
}

for (let i = 0; i < fileNames.length; i++) {
const info = await locateFile(`${fileNames[i]}.{cjs,js,mjs,ts}`, {
const info = await locate(`${fileNames[i]}.{cjs,js,mjs,ts}`, {
path: paths,
ignore: ['**/*.d.ts'],
});

if (info) {
const fileExports = loadScriptFileSync(info);
const fileExports = await load(info);
if (InstanceChecker.isDataSource(fileExports)) {
return fileExports;
}
Expand Down
20 changes: 13 additions & 7 deletions src/seeder/module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { DataSource, DataSourceOptions } from 'typeorm';
import {
loadScriptFileExportSync,
loadScriptFileSync,
} from 'locter';
import { load } from 'locter';
import { hasOwnProperty } from '../utils';
import { SeederConstructor, SeederOptions } from './type';
import { resolveFilePaths, resolveFilePatterns, setDefaultSeederOptions } from './utils';
import { modifyDataSourceOptionForRuntimeEnvironment, setDataSource } from '../data-source';
Expand Down Expand Up @@ -35,7 +33,7 @@ async function prepareSeeder(
factoryFiles = resolveFilePaths(factoryFiles);

for (let i = 0; i < factoryFiles.length; i++) {
loadScriptFileSync(factoryFiles[i]);
await load(factoryFiles[i]);
}
}

Expand Down Expand Up @@ -71,9 +69,13 @@ async function prepareSeeder(
seedFiles = resolveFilePaths(seedFiles);

for (let i = 0; i < seedFiles.length; i++) {
const fileExport = loadScriptFileExportSync(seedFiles[i]);
let fileExport = await load(seedFiles[i]);
if (hasOwnProperty(fileExport, 'default')) {
fileExport = fileExport.default;
}

if (fileExport) {
const item = fileExport.value as SeederConstructor;
const item = fileExport as SeederConstructor;

if (!options.seedName || options.seedName === item.name) {
items.push(item);
Expand All @@ -99,6 +101,10 @@ export async function runSeeder(
seeder: SeederConstructor,
options?: SeederOptions,
) : Promise<unknown> {
if (hasOwnProperty(seeder, 'default')) {
seeder = seeder.default as SeederConstructor;
}

options = options || {};
options.seeds = [seeder];
options.factoriesLoad = options.factoriesLoad ?? true;
Expand Down
4 changes: 2 additions & 2 deletions src/seeder/utils/file-path.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* istanbul ignore next */
import path from 'path';
import { locateFiles } from 'locter';
import { locateMany } from 'locter';

export async function resolveFilePatterns(
filesPattern: string[],
root?: string,
) : Promise<string[]> {
return locateFiles(
return locateMany(
filesPattern,
{
...(root ? { path: root } : {}),
Expand Down

0 comments on commit 5d092ad

Please sign in to comment.