Skip to content

Type mapper and sequence fixes#2

Open
hlcianfagna wants to merge 11 commits intocrate-workbench:cratedbfrom
hlcianfagna:cratedb
Open

Type mapper and sequence fixes#2
hlcianfagna wants to merge 11 commits intocrate-workbench:cratedbfrom
hlcianfagna:cratedb

Conversation

@hlcianfagna
Copy link
Copy Markdown

@hlcianfagna hlcianfagna commented May 3, 2024

About

The CrateDB dialect for TypeORM needs a type mapper, and special treatments for the fact that primary autogenerated keys with CrateDB are UUIDs, stored as strings, as a workaround.

Details

The file src/driver/cratedb/CrateDBPostgresQueryRunner.ts had to be copied into the tree as a whole, in order to be able to overwrite the buildCreateColumnSql method, because it is flagged as protected.

/**
* Builds a query for create column.
*/
protected buildCreateColumnSql(table: Table, column: TableColumn) {
let c = '"' + column.name + '"'
if (
column.isGenerated === true &&
column.generationStrategy !== "uuid"
) {
if (column.generationStrategy === "identity") {
// Postgres 10+ Identity generated column
const generatedIdentityOrDefault =
column.generatedIdentity || "BY DEFAULT"
c += ` ${column.type} GENERATED ${generatedIdentityOrDefault} AS IDENTITY`
} else {
// classic SERIAL primary column
if (
column.type === "integer" ||
column.type === "int" ||
column.type === "int4"
)
c += " SERIAL"
if (column.type === "smallint" || column.type === "int2")
c += " SMALLSERIAL"
if (column.type === "bigint" || column.type === "int8")
c += " BIGSERIAL"
}
}
if (column.type === "enum" || column.type === "simple-enum") {
c += " " + this.buildEnumName(table, column)
if (column.isArray) c += " array"
} else if (!column.isGenerated || column.type === "uuid") {
c += " " + this.connection.driver.createFullType(column)
}
// Postgres only supports the stored generated column type
if (column.generatedType === "STORED" && column.asExpression) {
c += ` GENERATED ALWAYS AS (${column.asExpression}) STORED`
}
if (column.charset) c += ' CHARACTER SET "' + column.charset + '"'
if (column.collation) c += ' COLLATE "' + column.collation + '"'
if (
column.isGenerated &&
column.generationStrategy === "uuid"
)
c += ` TEXT `
if (column.isNullable !== true) c += " NOT NULL"
if (column.default !== undefined && column.default !== null)
c += " DEFAULT " + column.default
if (
column.isGenerated &&
column.generationStrategy === "uuid"
)
c += ` DEFAULT ${this.driver.uuidGenerator}`
return c
}

References

It is based on basic work to unlock CrateDB with TypeORM.

@amotl
Copy link
Copy Markdown

amotl commented May 8, 2024

The file src/driver/cratedb/CrateDBPostgresQueryRunner.ts had to be copied into the tree as a whole, in order to be able to overwrite the buildCreateColumnSql method, because it is flagged as protected.

Look at this patch.

Corresponding constraints also had to be relaxed, in this case private methods of Python, in order to be able to properly derive a CrateDB dialect from the existing PostgreSQL one, without needing to duplicate large swaths of code.

@amotl
Copy link
Copy Markdown

amotl commented Feb 24, 2026

Hi again. Apologies for the late reply. After GH-3 was resolved now, we validated this patch successfully on behalf of the TypeORM example application. Thank you Hernan! 💯

@amotl amotl marked this pull request as ready for review February 24, 2026 23:20
@amotl amotl changed the title UUID fixes Type mapper and sequence / UUID fixes Feb 24, 2026
@amotl amotl changed the title Type mapper and sequence / UUID fixes Type mapper and sequence fixes Feb 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants