Skip to content

Add example project using TypeORM#187

Draft
amotl wants to merge 7 commits intomainfrom
amo/typeorm
Draft

Add example project using TypeORM#187
amotl wants to merge 7 commits intomainfrom
amo/typeorm

Conversation

@amotl
Copy link
Copy Markdown
Member

@amotl amotl commented Dec 5, 2023

About

Verify TypeORM works well with CrateDB.

Details

The code has been scaffolded like this:

npx typeorm init --name . --database postgres --express

References

This example uses that other dialect patch needed to make TypeORM compatible with CrateDB.

/cc @proddata, @hammerhead, @hlcianfagna

Comment on lines +18 to +22
"scripts": {
"start": "ts-node src/index.ts",
"typeorm": "typeorm-ts-node-commonjs"
}
} No newline at end of file
Copy link
Copy Markdown
Member Author

@amotl amotl Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem

The web server does not start, otherwise Express server has started on port 3000 would have been displayed on stdout. It feels like the program just freezes before being able to conclude any significant work.

sink:javascript-typeorm amo$ npm start

> .@0.0.1 start
> ts-node src/index.ts

query: SELECT * FROM current_schema()
query: SELECT version();

Do you have any other idea what could go wrong here? In the CrateDB logs, there is zero indication about any error, effectively no message at all.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts

CrateDB will respond with a version string like that:

CrateDB 5.2.2 (built a33862e/2023-02-09T10:26:09Z, Linux 5.10.124-linuxkit amd64, OpenJDK 64-Bit Server VM 19.0.2+7)

Not all applications/dialects/drivers like that, because they expect it to be compatible with PostgreSQL, and run feature discovery procedures on it. Does this trip TypeORM as well?

In Python lands, in order to make psycopg and asyncpg compatible with the CrateDB SQLAlchemy dialect, this was one of the details needed to be made compatible.

-- https://github.com/daq-tools/sqlalchemy-postgresql-relaxed/blob/main/sqlalchemy_postgresql_relaxed/base.py#L7-L22
-- crate/crate-python#532

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With crate-workbench/typeorm@fcd3841918, the example program advances to a type error, which is natural.

driverError: error: Cannot find data type: serial

Copy link
Copy Markdown
Member Author

@amotl amotl Dec 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With ce929ef, which adjusts the @PrimaryGeneratedColumn to use the UUID strategy, this SQL DDL is generated.

CREATE TABLE "user" (
  "id" uuid NOT NULL DEFAULT uuid_generate_v4(),
  "firstName" character varying NOT NULL,
  "lastName" character varying NOT NULL,
  "age" integer NOT NULL,
  CONSTRAINT "PK_cace4a159ff9f2512dd42373760"
  PRIMARY KEY ("id")
)

PostgresConnectionOptions says:

/**
 * The Postgres extension to use to generate UUID columns. Defaults to uuid-ossp.
 * If pgcrypto is selected, TypeORM will use the gen_random_uuid() function from this extension.
 * If uuid-ossp is selected, TypeORM will use the uuid_generate_v4() function from this extension.
 */
readonly uuidExtension?: "pgcrypto" | "uuid-ossp"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To improve the situation, crate-workbench/typeorm#1 uses a dialect for CrateDB to provide CrateDB's gen_random_text_uuid() function as an uuidGenerator to TypeORM.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Within the SQL DDL statement above, the careful reader already spotted that the NOT NULL constraint precedes the DEFAULT clause, and wondered if that would work with CrateDB.

It turns out, it does not.

Now that the dialect does the right thing on the DEFAULT definition, ...

CREATE TABLE "user" ("id" uuid NOT NULL DEFAULT gen_random_text_uuid() ...

... it would be so sweet if CrateDB could be supportive on that end.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case the output of version() were still an issue, please note that since 5.1.1 it is possible to do the following:

cr> set search_path='public,pg_catalog';
SET OK, 0 rows affected  (0.006 sec)
cr> show search_path;
+--------------------+
| setting            |
+--------------------+
| public, pg_catalog |
+--------------------+
SHOW 1 row in set (0.007 sec)
cr> create function public.version()  RETURNS TEXT
    LANGUAGE JAVASCRIPT
    AS $$function version() {return 'test';}$$;
CREATE OK, 1 row affected  (0.057 sec)
cr> select version();
+--------+
| 'test' |
+--------+
| test   |
+--------+
SELECT 1 row in set (0.912 sec)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. It will also be interesting for https://github.com/daq-tools/sqlalchemy-postgresql-relaxed.

Copy link
Copy Markdown
Member Author

@amotl amotl Dec 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: It looks like further explorations stalled after @seut improved this detail on CrateDB the other day and @hlcianfagna added another improvement to the dialect implementation.

@amotl
Copy link
Copy Markdown
Member Author

amotl commented May 3, 2024

@hlcianfagna: Thank you very much for supporting this patch by adding further refinements.

amotl and others added 6 commits February 24, 2026 22:50
The code has been scaffolded like this:

  npx typeorm init --name . --database postgres --express

This commit adds the result unmodified.
* Check PK data type to support uuid

* Reflect that id is now an uuid and not a number
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 3, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch amo/typeorm

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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