kysely-org / kysely-postgres-js

Kysely dialect for PostgreSQL using the Postgres.js client.
MIT License
58 stars 3 forks source link

Deno - error: Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'replace') #11

Closed bidipeppercrap closed 4 months ago

bidipeppercrap commented 5 months ago

image image

Spec

Runtime: Deno Database Provider: Neon

Reproduce:

I run into this error when I try to migrate.

Code

deno.json

{
    "imports": {
        "db/": "./src/db/",
        "kysely": "npm:kysely@^0.27.2",
        "kysely-postgres-js": "npm:kysely-postgres-js@^2.0.0",
        "postgres": "https://deno.land/x/postgresjs@v3.4.3/mod.js"
    },
    "tasks": {
        "migrate": "deno run -A ./src/db/migrator.ts"
    }
}

migrator:

import * as path from "node:path";
import { fileURLToPath } from "node:url";
import { promises as fs } from "node:fs";
import { Migrator, FileMigrationProvider } from "kysely";
import { db } from "db/database.ts";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function migrateToLatest() {
    const migrator = new Migrator({
        db,
        provider: new FileMigrationProvider({
            fs,
            path,
            migrationFolder: path.join(__dirname, "./migrations")
        })
    });

    const { error, results } = await migrator.migrateToLatest();

    results?.forEach((it) => {
        if (it.status === "Success") {
            console.log(`migration "${it.migrationName}" was executed successfully`);
        } else if (it.status === "Error") {
            console.error(`failed to execute migration "${it.migrationName}"`);
        }
    });

    if (error) {
        console.error("failed to migrate");
        console.error(error);
        Deno.exit(1);
    }

    await db.destroy();
}

migrateToLatest();

db instance:

import { Database } from "db/types/index.ts";
import postgres from "postgres";
import { Kysely } from "kysely";
import { PostgresJSDialect } from "kysely-postgres-js";
import { load } from "https://deno.land/std@0.212.0/dotenv/mod.ts";

const env = await load();

const dialect = new PostgresJSDialect({
    postgres: postgres({
        database: env["PGDATABASE"]!,
        host: env["PGHOST"]!,
        user: env["PGUSER"]!,
        password: env["PGPASSWORD"]!,
        port: 5434,
        max: 10,
    })
});

export const db = new Kysely<Database>({
    dialect
});
igalklebanov commented 4 months ago

Hey 👋

Print env object, it might have an undefined connection string. Try npm i postgres@3.4.1.

igalklebanov commented 4 months ago

Seems similar to #10. Closing this as it's related to postgres and not kysely-postgres-js.

Sleepful commented 2 months ago

very cryptic error yeah, it means the connection to the DB isn't working, in my case I had the wrong port number