spinlud / sequelize-typescript-generator

70 stars 25 forks source link

The ModelBuilder does not take the schema name from postgres? #57

Open heblol opened 11 months ago

heblol commented 11 months ago

Hi there, I have a suggested improvement to the package.

Problem: I had some trouble using the correct schema in my project. I added the schema inside the connection object (which is a Options object from Sequelize). However, when I added the schema there, it did not generate types! It said it was empty and I actually thought the repo was broken. However, looking inside the source code, I found that I needed to add the schema to the metadata object.

Also: Adding using npx terminal command with the -s or --schema makes the generation step not look into the schema.

npx stg -D mysql -h localhost -p 3306 -d myDatabase -u myUsername -s mySchema -x myPassword --indices --dialect-options-file path/to/dialectOptions.json --case camel --out-dir models --clean

Probably the connection schema field is necessary, but for me, it was really confusing.

Solution:

  1. Add some more documentation that the schema should be added in the metadata object
  2. When the schema field is added to the connection object, automatically add it to the metadata.
import {
  IConfig,
  ModelBuilder,
  DialectPostgres,
} from "sequelize-typescript-generator";

(async () => {
  const config: IConfig = {
    connection: {
      dialect: "postgres",
      database: "eetlijst-db",
      schema: "eetschema",
      host: "localhost",
      port: 5432,
      username: "root",
      password: "PASSWORD",
    },
    metadata: {
      indices: true,
      case: "UNDERSCORE",
    },
    output: {
      clean: true,
      outDir: "database/pg/models",
    },
    strict: true,
  };

  const dialect = new DialectPostgres();

  const builder = new ModelBuilder(config, dialect);

  try {
    await builder.build();
  } catch (err) {
    console.error(err);
    process.exit(1);
  }
})();

Output printed to terminal: As you can see, it looks at the t.table_schema='public' and i expected t.table_schema='eetschema'

Fetching metadata from source
Executing (default): SELECT 1+1 AS result
Executing (default): SELECT 
                t.table_name                AS table_name,
                obj_description(pc.oid)     AS table_comment
            FROM information_schema.tables t
            JOIN pg_class pc
                ON t.table_name = pc.relname
            WHERE t.table_schema='public' AND pc.relkind = 'r';
Couldn't find any table for database eetlijst-db and provided filters
ArthurQR98 commented 9 months ago

They still haven't solved it, did you solve it any other way?

ArthurQR98 commented 9 months ago

The solution would be this way.

import { IConfig, ModelBuilder, DialectPostgres, } from "sequelize-typescript-generator";

(async () => { const config: IConfig = { connection: { dialect: "postgres", database: "eetlijst-db", host: "localhost", port: 5432, username: "root", password: "PASSWORD", }, metadata: { indices: true, case: "UNDERSCORE", schema: "eetschema", }, output: { clean: true, outDir: "database/pg/models", }, strict: true, };

const dialect = new DialectPostgres();

const builder = new ModelBuilder(config, dialect);

try { await builder.build(); } catch (err) { console.error(err); process.exit(1); } })();