Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: sort by filename when using seed pattern #546

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,27 @@ This is a library to

**Table of Contents**

- [Installation](#installation)
- [Documentation](#documentation)
- [Usage](#usage)
- [Typeorm Extension 🚀](#typeorm-extension-)
- [Installation](#installation)
- [Documentation](#documentation)
- [Usage](#usage)
- [CLI](#cli)
- [Options](#options)
- [Database](#database)
- [Create](#create)
- [Drop](#drop)
- [Instances](#instances)
- [Single](#single)
- [Multiple](#multiple)
- [Seeding](#seeding)
- [Configuration](#configuration)
- [Entity](#entity)
- [Factory](#factory)
- [Seed](#seed)
- [Execute](#execute)
- [Query](#query)
- [Contributing](#contributing)
- [License](#license)
- [Contributing](#contributing)
- [License](#license)

## Installation

Expand Down Expand Up @@ -287,6 +298,8 @@ The following values are assumed by default:
- factory path: `src/database/factories/**/*{.ts,.js}`
- seed path: `src/database/seeds/**/*{.ts,.js}`

Note: When seeder paths are configured as **glob patterns**, the paths are resolved and sorted in alphabetical order using filenames. This helps to ensure that the seeders are executed in the correct order.

`data-source.ts`

```typescript
Expand Down
15 changes: 13 additions & 2 deletions src/seeder/utils/file-path.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* istanbul ignore next */
import path from 'path';
import type { LocatorInfo } from 'locter';
import { locateMany } from 'locter';
import path from 'path';

export async function resolveFilePatterns(
filesPattern: string[],
Expand All @@ -12,7 +13,7 @@ export async function resolveFilePatterns(
...(root ? { path: root } : {}),
ignore: ['**/*.d.ts'],
},
).then((files) => files.map((el) => path.join(el.path, el.name + el.extension)));
).then(buildFilePathname);
}

export function resolveFilePaths(
Expand All @@ -25,3 +26,13 @@ export function resolveFilePaths(
path.resolve(root || process.cwd(), filePath)
));
}

/**
* Exported only for testing purposes
*/
export function buildFilePathname(files: LocatorInfo[]) {
return (
// sorting by name so that we can define the order of execution using file names
files.sort((a, b) => (a.name > b.name ? 1 : -1)).map((el) => path.join(el.path, el.name + el.extension))
);
}
31 changes: 31 additions & 0 deletions test/unit/seeder/utils/file-path.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { buildFilePathname } from '../../../../src/seeder/utils/file-path';

describe('src/seeder/utils/file-path.ts', function () {
describe('buildFilePathname', function () {
it('should build file path name', function () {
const files = [
{
path: '/path/to/dir',
name: '2_file',
extension: '.ts',
},
{
path: '/path/to/dir',
name: '1_file',
extension: '.ts',
},
{
path: '/path/to/dir',
name: '0_file',
extension: '.ts',
},
];
const result = buildFilePathname(files);
expect(result).toEqual([
'/path/to/dir/0_file.ts',
'/path/to/dir/1_file.ts',
'/path/to/dir/2_file.ts',
]);
});
});
});