如何在打字稿上获取文件夹
我有这样的结构:
src
红外线[文件夹]
config [文件夹]
-knexfile.ts [文件]
您好,我目前有以下文件:knexfile,该文件位于我的src / config / knexfile.ts文件夹中 而我想进入src / infra / knex / migrations目录
我的knexfile.ts:
import knex from 'knex';
import path from 'path';
import dotenv from 'dotenv';
dotenv.config();
interface KnexConfig {
[key: string]: object;
}
const extension =
process.env.NODE_ENV === 'production' ||
process.env.NODE_CONFIG_ENV === 'staging'
? 'js'
: 'ts';
const database: KnexConfig = {
development: {
client: 'postgresql',
connection: {
port: process.env.DB_PORT,
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
},
pool: {
min: 2,
max: 10,
},
migrations: {
tableName: 'knex_migrations',
directory: path.resolve(__dirname, 'src', 'infra', 'knex', 'migrations'),
loadExtensions: [extension],
extension: extension,
},
seeds: {
loadExtensions: ['.ts'],
},
timezone: 'UTC',
useNullAsDefault: true,
onUpdateTrigger: (table: string) => `
CREATE TRIGGER ${table}_updated_at
BEFORE UPDATE ON ${table}
FOR EACH ROW
EXECUTE PROCEDURE on_update_timestamp();
`,
},
};
const knexConfig = database['development'] as knex.Config;
module.exports = knexConfig;
基本上我的迁移文件位于dirotiro src / infra / migrations
但是我的文件在src / config / knexfile.ts中
我有这个:
目录:path.resolve(__ dirname,'src','infra','knex', “移民”)
但是这不起作用,因为我的文件在以下文件夹中:src / config在基本文件夹中:./
答案 0 :(得分:0)
path
将在您尝试解析时理解相对路径:
directory: path.resolve(__dirname, '../infra/knex/migrations')