Seeking a Type-Safe Ruby on Rails in TypeScript, I Started Developing an ORM
Tags: typescript, rails, orm, opensource
Published: 2024-06-11 16:49:48

I am developing an ORM library for TypeScript called Accel Record. Recently, I published an article introducing this library, titled [Introduction to "Accel Record": A TypeScript ORM Using the Active Record Pattern](https://dev.to/koyopro/introduction-to-accel-record-a-typescript-orm-using-the-active-record-pattern-2oeh).

Next, I would like to explain why I decided to develop a new ORM for TypeScript.

## Initial Motivation

As someone who uses Ruby on Rails for server-side development and TypeScript for front-end development, I often find myself thinking the following:

- **I want a framework for TypeScript with development efficiency comparable to Ruby on Rails.**

This desire stems from wanting to use TypeScript for server-side development as well. The reasons are mainly as follows:
- I want to develop the server-side with type safety.
- I want to develop both the front-end and back-end in the same language to reduce context switching and learning costs.

This thought process seems common, and in fact, there are cases where TypeScript is adopted for server-side development for these reasons.

## What Kind of Framework Do I Want?

So, what does a framework with development efficiency comparable to Ruby on Rails look like?

The requirements may vary from person to person, but in my case, I thought as follows:

- It should be able to implement functionalities for common use cases in web application development with minimal code.

More specifically, I am looking for the following elements:

- Based on developing MPA with SSR (not SPA)
- Able to implement CRUD functions for RDB with minimal code
- A framework that covers the MVC domain on the server-side

If such a framework existed, I believe server-side development with TypeScript could be as efficient as, if not more efficient than, using Ruby on Rails.

## Existing Frameworks Were Not Satisfactory

I investigated whether a framework meeting the above requirements already existed.
The conclusion I reached was that there might not be a TypeScript framework that offers functionalities for server-side processes as efficiently and with as little code as Rails.
In general terms, existing TypeScript frameworks seem to focus more on routing and enhancing front-end/back-end integration. While they are rich in features related to View and Controller (in MVC terms), the Model part seems weaker compared to Rails.

## Do We Need an ORM Like Active Record?

In Rails, the ORM handling the Model role is Active Record.
Of course, TypeScript also has ORMs, but Rails’ Active Record is more than just an ORM. It provides various functionalities related to the corresponding domain model, not just operations on DB records.

Rails is characterized by its ability to implement common functionalities with minimal code. This is possible because the model classes provided by Active Record have many features. They are tightly integrated with other parts like routing and controllers, resulting in high development efficiency.

From this perspective, I thought that to achieve the goal, TypeScript needs a highly functional ORM similar to Rails’ Active Record.

## What Elements Should the ORM Have?

To provide an efficient development experience in TypeScript similar to Rails, I thought the ORM should have the following elements:

### 1. Active Record Pattern

It should adopt the Active Record pattern. This is because I want one class to handle not only operations on DB records but also features related to the corresponding domain model.
For instance, with an ORM adopting the Table Gateway pattern, the retrieved records are plain objects, making it difficult to attach methods related to the model.
Additionally, an Active Record pattern ORM would make it easier to integrate with features like routing and View as done in Rails.

### 2. Type Safety

Leveraging the advantages of TypeScript, the operations and query interface of the ORM should be type-safe.
The initial motivation was to develop the server-side with type safety, so sufficient type support is expected.

## No Existing ORM Met the Requirements

Given the above considerations, I investigated existing ORMs but could not find one that was type-safe and adopted the Active Record pattern.

For example, the increasingly popular [Prisma](https://www.prisma.io) has high type safety but adopts the Table Gateway pattern.
The closest fit was [TypeORM](https://typeorm.io), which uses the Active Record pattern, but its type support is weaker compared to recent ORMs, and its release frequency has been low recently.

## I Decided to Try Developing an ORM

Based on the above, I decided to start developing a type-safe ORM in TypeScript that adopts the Active Record pattern.
I wanted to try developing it to see if it was feasible and identify any potential challenges.

The subsequent development process will be detailed in another article, but ultimately, I published the new ORM as [Accel Record](https://www.npmjs.com/package/accel-record).

## Conclusion

In this article, I outlined why I decided to develop a new ORM for TypeScript.

The initial motivation was the desire for a framework in TypeScript with development efficiency comparable to Ruby on Rails. As I continued researching existing libraries, I realized the need for a type-safe ORM in TypeScript adopting the Active Record pattern.

Thus, I started development, and eventually released the library. To see what kind of ORM it has become, please check out [Introduction to "Accel Record": A TypeScript ORM Using the Active Record Pattern](https://dev.to/koyopro/introduction-to-accel-record-a-typescript-orm-using-the-active-record-pattern-2oeh) and the [README](https://github.com/koyopro/accella/blob/main/packages/accel-record/README.md).