如何在 TypeScript 项目中配置 Knex.ts?
How to configure Knex.ts in TypeScript project?
我正在尝试在 TypeScript 中配置 Knexfile。我用 knex init -x ts
:
创建了 knexfile.ts
const defaults = {
client: 'postgresql',
connection: {
host: DB_HOST,
user: DB_USER,
password: DB_PASSWORD,
database: DB_DATABASE
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
};
const knexConfig = {
local: {
client: 'sqlite3',
connection: {
filename: './dev.sqlite3'
}
},
development: {
...defaults,
debug: true,
useNullAsDefault: true
},
production: {
...defaults
}
};
export default knexConfig;
然后我创建 knex.ts
文件来建立连接:
import Knex, { Config } from 'knex';
import knexConfig from '../utils/knexfile';
import { NODE_ENV } from '../utils/config';
// Set environment from `.env`
const knex = Knex(knexConfig[NODE_ENV]);
export default knex;
但我在 (knexConfig[NODE_ENV])
处收到错误消息:
(alias) const NODE_ENV: string
import NODE_ENV
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ local: { client: string; connection: { filename: string; }; }; development: { debug: boolean; useNullAsDefault: boolean; client: string; connection: { host: string; user: string; password: string; database: string; }; pool: { ...; }; migrations: { ...; }; }; production: { ...; }; }'.
No index signature with a parameter of type 'string' was found on type '{ local: { client: string; connection: { filename: string; }; }; development: { debug: boolean; useNullAsDefault: boolean; client: string; connection: { host: string; user: string; password: string; database: string; }; pool: { ...; }; migrations: { ...; }; }; production: { ...; }; }'.ts(7053)
============================================= ===========
我做错了什么?
请帮忙。
我相信您可以通过设置来抑制这些错误:
"suppressImplicitAnyIndexErrors": true,
在你的tsconfig.json
或者您可以通过某种方式为 knexConfig
对象创建索引签名:
interface KnexConfig {
[key: string]: object;
};
const knexConfig: KnexConfig = {
local: {
client: 'sqlite3',
connection: {
filename: './dev.sqlite3'
}
},
development: {
...defaults,
debug: true,
useNullAsDefault: true
},
production: {
...defaults
}
};
有关更多可能性,请参阅此问题的可能重复项:
未来,在 ^1.0.4 版本中,knex 将导出 Knex 界面中的所有类型,因此
import { Knex } from "knex";
并获取配置自动完成
module.exports = {
client: "mysql",
connection: {
filename: path.resolve(__dirname, "src", "database", "connection.ts"),
},
migrations: {
directory: path.resolve(__dirname, "src", "database", "migrations"),
},
} as Knex.Config;
我正在尝试在 TypeScript 中配置 Knexfile。我用 knex init -x ts
:
knexfile.ts
const defaults = {
client: 'postgresql',
connection: {
host: DB_HOST,
user: DB_USER,
password: DB_PASSWORD,
database: DB_DATABASE
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
};
const knexConfig = {
local: {
client: 'sqlite3',
connection: {
filename: './dev.sqlite3'
}
},
development: {
...defaults,
debug: true,
useNullAsDefault: true
},
production: {
...defaults
}
};
export default knexConfig;
然后我创建 knex.ts
文件来建立连接:
import Knex, { Config } from 'knex';
import knexConfig from '../utils/knexfile';
import { NODE_ENV } from '../utils/config';
// Set environment from `.env`
const knex = Knex(knexConfig[NODE_ENV]);
export default knex;
但我在 (knexConfig[NODE_ENV])
处收到错误消息:
(alias) const NODE_ENV: string
import NODE_ENV
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ local: { client: string; connection: { filename: string; }; }; development: { debug: boolean; useNullAsDefault: boolean; client: string; connection: { host: string; user: string; password: string; database: string; }; pool: { ...; }; migrations: { ...; }; }; production: { ...; }; }'.
No index signature with a parameter of type 'string' was found on type '{ local: { client: string; connection: { filename: string; }; }; development: { debug: boolean; useNullAsDefault: boolean; client: string; connection: { host: string; user: string; password: string; database: string; }; pool: { ...; }; migrations: { ...; }; }; production: { ...; }; }'.ts(7053)
============================================= ===========
我做错了什么? 请帮忙。
我相信您可以通过设置来抑制这些错误:
"suppressImplicitAnyIndexErrors": true,
在你的tsconfig.json
或者您可以通过某种方式为 knexConfig
对象创建索引签名:
interface KnexConfig {
[key: string]: object;
};
const knexConfig: KnexConfig = {
local: {
client: 'sqlite3',
connection: {
filename: './dev.sqlite3'
}
},
development: {
...defaults,
debug: true,
useNullAsDefault: true
},
production: {
...defaults
}
};
有关更多可能性,请参阅此问题的可能重复项:
未来,在 ^1.0.4 版本中,knex 将导出 Knex 界面中的所有类型,因此
import { Knex } from "knex";
并获取配置自动完成
module.exports = {
client: "mysql",
connection: {
filename: path.resolve(__dirname, "src", "database", "connection.ts"),
},
migrations: {
directory: path.resolve(__dirname, "src", "database", "migrations"),
},
} as Knex.Config;