go-jet / jet

Type safe SQL builder with code generation and automatic query result data mapping
Apache License 2.0
2.23k stars 110 forks source link

Control a destination of generator/postgres.GenerateDSN #258

Closed kazhuravlev closed 11 months ago

kazhuravlev commented 11 months ago

Problem We would like to control the destination directory of postgres.GenerateDSN, but inside this function we have a hardcoded value - the database name and schema path.

Desired solution I see several options:

For our case, we would like to have the second option, hiding the complexity of the generator implementation.

go-jet commented 11 months ago

Hi @kazhuravlev. It is possible to change destination directories using Generator Customization, check these tests for more details: TestGeneratorTemplate_Schema_ChangePath, TestGeneratorTemplate_Model_ChangePath and TestGeneratorTemplate_SQLBuilder_ChangePath.

kazhuravlev commented 11 months ago

Of course I can manage the base directory. But I can't manage the rest of the directories (after the specified one).

When I set the resulting directory like ./output, I get a result like ./output/<DBNAME>/<SCHEMA_NAME>. But in my case there are multiple databases with different schemas inside each. So I don't want to have additional directories after ./output . Only ./output/[model|table] (instead of ./output/some__database_name/some__schema_name/[model|table]).

Initial problem - I have local databases (dev dbs) and multiple databases in production. Each database is a separate tenant (client) with the same schema but different data. After generating, I get a little strange code, which contains the name of the database.

Main argument: The database name is only a configuration option and must be controlled by the jet user. Ideally, the schema name is the same.

go-jet commented 11 months ago

To remove database name, you can use relative path "../".

err := postgres.Generate( // or GenerateDSN
    "./output",
    dbConnection,
    template.Default(postgres2.Dialect).
        UseSchema(func(schemaMetaData metadata.Schema) template.Schema {
            return template.DefaultSchema(schemaMetaData).UsePath("../")
        }),
)

Now generator will generate to ./output/[model|table|enum] folders.

kazhuravlev commented 11 months ago

Thanks, solved!