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

UUID fixes #2

Draft
wants to merge 11 commits into
base: cratedb
Choose a base branch
from
Draft

Conversation

hlcianfagna
Copy link

@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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants