-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathknex-esm-compat.ts
64 lines (50 loc) · 1.84 KB
/
knex-esm-compat.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Knex } from "knex";
import path from "path";
import fs from "fs/promises";
import { sortBy } from "lodash";
class FileInfo {
constructor(
public directory: string,
public file: string,
) {}
public toString() {
return this.file;
}
}
async function readFiles(dirs: string[], extensions: readonly string[]): Promise<FileInfo[]> {
const files = await Promise.all(
dirs.map(async (directory: string) => {
const absoluteDir = path.resolve(process.cwd(), directory);
const files = await fs.readdir(absoluteDir);
return files.map(file => new FileInfo(directory, file));
}),
);
const matching = files.flat().filter(({ file }) => extensions.includes(path.extname(file)));
return sortBy(matching, "file");
}
export class EsmFsSeedSource implements Knex.SeedSource<FileInfo> {
extensions = [".ts", ".js"];
async getSeeds(config: Knex.SeederConfig<any>): Promise<FileInfo[]> {
const dirs: string[] = [config.directory ?? "seed"].flat();
return readFiles(dirs, this.extensions);
}
getSeed(seed: FileInfo): Promise<Knex.Seed> {
const absoluteDir = path.resolve(process.cwd(), seed.directory);
const _path = path.join(absoluteDir, seed.file);
return import(_path);
}
}
export class EsmFsMigrationSource implements Knex.MigrationSource<FileInfo> {
constructor(private migrationsPaths: string[] = ["migrations"]) {}
async getMigrations(loadExtensions: readonly string[]): Promise<FileInfo[]> {
return readFiles(this.migrationsPaths, loadExtensions);
}
getMigrationName(migration: FileInfo): string {
return migration.file;
}
async getMigration(migration: FileInfo): Promise<Knex.Migration> {
const absoluteDir = path.resolve(process.cwd(), migration.directory);
const migrationPath = path.join(absoluteDir, migration.file);
return import(migrationPath);
}
}