Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
heblol opened this issue Oct 3, 2023 · 2 comments
Open

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

heblol opened this issue Oct 3, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@heblol
Copy link

heblol commented Oct 3, 2023

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
Copy link

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

@ArthurQR98
Copy link

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);
}
})();

@spinlud spinlud added the bug Something isn't working label Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants