halvardssm / deno-nessie

A modular Deno library for PostgreSQL, MySQL, MariaDB and SQLite migrations
MIT License
527 stars 31 forks source link

[QUESTION] Experimental classes - Basic configuration and setup for Postgres #83

Closed gaylonalfano closed 4 years ago

gaylonalfano commented 4 years ago

Your question Hello - Alright, this is slow going for me but I'll get there eventually. I'm pretty sure it's not a bug, so I wanted to first confirm my basic configuration with you. For some reason whenever I try to perform my first migrate with a basic create_users.ts migration file, I encounter a TypeError: MigrationClass is not a constructor (see below).

My Basic Config

nessie.config.ts ``` // https://github.com/halvardssm/deno-nessie/blob/master/examples/config-postgres.ts import { ClientOptions, ClientPostgreSQL, } from "https://deno.land/x/nessie/mod.ts"; import type { ConnectionOptions } from "https://deno.land/x/postgres@v0.4.5/connection_params.ts"; import config from "./src/app/config/config.ts"; const clientConfig: ClientOptions = { migrationFolder: "./src/app/db/migrations", seedFolder: "./src/app/db/seeds", experimental: true, }; const connectionConfig: ConnectionOptions = { database: "deno_postgres_db", hostname: "localhost", port: 5432, user: "postgres", password: "postgres", }; export default { /* exposeQueryBuilder: true, */ client: new ClientPostgreSQL(clientConfig, connectionConfig), }; ```
create_users.ts ``` import { AbstractMigration, Info } from "https://deno.land/x/nessie/mod.ts"; import type { Client } from "https://deno.land/x/postgres@v0.4.5/mod.ts"; export default class ExperimentalMigration extends AbstractMigration { /** Runs on migrate */ async up({ dialect }: Info): Promise { this.client.query("CREATE TABLE table1 (id int)"); } /** Runs on rollback */ async down({ dialect }: Info): Promise { this.client.query("DROP TABLE table1"); } } ```
migrate -d output (TypeError) ``` ❯ deno-vue-postgres-starter/backend add-nessie [!?] deno run --allow-net --allow-read --allow-write https://deno.land/x/nessie/cli.ts migrate -d State: [ true, "file:///Users/gaylonalfano/Code/deno-vue-postgres-starter/backend/nessie.config.ts" ] Checking config path Amount pre: undefined Latest migrations: undefined Filtered and sorted migration files: [ "1602682202673-create_users.ts" ] TypeError: MigrationClass is not a constructor at ClientPostgreSQL._migrationHandler (AbstractClient.ts:215:25) at async ClientPostgreSQL.migrate (AbstractClient.ts:97:9) at async ClientPostgreSQL.migrate (ClientPostgreSQL.ts:73:5) at async run (https://deno.land/x/nessie@v1.1.2/cli.ts:74:11) ```

I looked inside your AbstractClient.ts file for the MigrationClass. Here is a snippet that the error log points to:

if (this.experimental) {
  const MigrationClass: new (
    props: AbstractMigrationProps<Client>,
  ) => AbstractMigration<Client> = await import(
    parsePath(
      this.migrationFolder,
      fileName,
    )
  );

  const migration = new MigrationClass({ client: this.client });

I've hit a roadblock to be honest. My code attempts to follow your basic examples on the README.md and within your examples folder (experimental only). I create the ClientPostgeSQL as client inside the nessie.config.ts. However, I'm not sure whether I need to use/import this client into the migration file or not. No rush. Just trying to get a working basic example before I try to add anything else e.g., Dex (like in your more extensive example).

halvardssm commented 4 years ago

Hey @gaylonalfano ! Thank you for reporting this bug! I found the issue with the default import, to say it like this: You did everything correctly. I am merging the changes as soon as the pipeline passes, and I will release it under version 1.1.1.

I appreciate the time you spent reporting this!