NodeJS 与 Objection 和 Knex - 错误:无法获取连接

NodeJS with Objection and Knex - Error: Unable to acquire a connection

提问人:andorando 提问时间:11/16/2023 最后编辑:andorando 更新时间:11/16/2023 访问量:27

问:

我正在使用 Knex、Objection、Node 和 Typescript 设置一个项目,当我尝试运行一个简单的命令时,例如 ,我不断得到:knex --knexfile ./src/db/knexfile.ts migrate:list --client pg

错误:无法获取连接

我不知道为什么会这样。这是我的knexfile:

export const config: { [key: string]: Knex.Config } = {
  development: {
    client: "pg",
    connection: {
      database: "tests",
      user: "postgres",
      password: "examplepassword"
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: "knex_migrations",
      loadExtensions: ['.js', '.ts']
    },
    acquireConnectionTimeout: 3000,
    ...knexSnakeCaseMappers,
  },

这是我用于连接的文件:

import Knex from 'knex';
import { Model } from 'objection';
import { config } from "./knexfile";

const db = Knex(config.development);
Model.knex(db);

export default db;

现在在我的主服务器文件(index.ts)中,我只需导入 db 文件。我的index.ts如下所示:

import express from "express";
import names from "./routes/Names";
import "../src/db/db";

const app = express();
  
app.use(express.json());
app.use("/api/names", names);
  
app.listen(8080, () => {
  console.log("We are online!");
});

我尝试列出或运行的迁移是:

import { Knex } from "knex";
import { TABLE_NAMES } from "../../constants/tableNames";
//import "../db"; <--- importing the connection yields the same error!

export async function up(knex: Knex): Promise<void> {
    await knex.schema.createTable(TABLE_NAMES.Names, (table) => {
        table.increments('id').primary();
        table.string('name', 255).notNullable();
        table.json('hobbies');
        table.string('email', 255).notNullable();
        table.timestamp('createdAt');
        table.timestamp('updatedAt');
    });
}


export async function down(knex: Knex): Promise<void> {
    return knex.schema.dropTableIfExists(TABLE_NAMES.Names);
}

我不知道我做错了什么?

节点 .js 打字稿 knex.js 异议.js

评论

0赞 Lior Kupers 11/16/2023
你在 localhost 上运行吗?在默认端口上?我没有看到您的连接配置中指定了这些内容

答: 暂无答案