Skip to content

Commit

Permalink
Support for single file migration to help with tests
Browse files Browse the repository at this point in the history
In tests it's useful to just include one migration file for testing for error / exceptions.
If the path is a folder, it will load all files in the folder, if it's a single file, it will load just that file
  • Loading branch information
mderazon committed Jan 2, 2025
1 parent 421d6a8 commit c97a2eb
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Request, Response, NextFunction } from "express";
import { promises as fs } from "fs";
import path from "path";
import path, { basename } from "path";
import { readdir, stat } from "fs/promises";

import { pathToRegexp } from "path-to-regexp";
import util from "util";
const debuglog = util.debuglog("request-migrations");
Expand All @@ -27,7 +28,7 @@ export const requestMigrationMiddleware = async (
}[] = [];

const loadMigrations = async () => {
const files = await fs.readdir(migrationsDir);
const files = await getFiles(migrationsDir);

for (const file of files) {
if (file.endsWith(".migration.ts")) {
Expand Down Expand Up @@ -149,3 +150,13 @@ export const requestMigrationMiddleware = async (
}
};
};

async function getFiles(path: string): Promise<string[]> {
const stats = await stat(path);

if (stats.isDirectory()) {
return readdir(path);
} else {
return [basename(path)];
}
}

0 comments on commit c97a2eb

Please sign in to comment.