Have you ever faced problems using a socket connection to the database (Aurora) overflow? How did you address the problem? You may solve this problem by connecting through Data API via HTTP endpoint requests. 

But that is not the end of your trouble. You still have to find the answer to one big question—how do you integrate TypeORM with Aurora Data API? So, let's figure it out in this article to get a heads up with your next TypeORM project. But discussing the integration process in detail, first, we need to have a clear idea of TypeORM. 

In this article:

  1. What Is TypeORM?
  2. How to Set Aurora to Use Data API
  3. Save Time with typeorm-aurora-data-api-driver
  4. Additional TypeORM Configuration Options for Data API Usage
  5. Need Help With Your TypeORM Project?
  6. Concluding Note

What Is TypeORM?

What Is TypeORM?TypeORM is the most updated framework in the node.js environment for Object Relationship Mapping (ORM). ORM is a tool to facilitate your database operations, as you can map database tables with ORM. Therefore, familiarity with ORM will help us understand TypeORM better. 

There are three components of ORM—Object, Relational, and Mapping. The primary domain or model is called Object, while the relationship of tables with your system databases is referred to as the Relation part of ORM. The Mapping part indicates how you connect or map the tables on the database model. 

So, let’s see what TypeORM is. Running in node.js and created with TypeScript, TypeORM introduced optional typing to advance JavaScript further. In addition, TypeScript has made TypeORM highly compatible with the Nest framework. 

TypeORM natively supports ES5, ES6, ES7, and ES8 revisions of JavaScript. The major supported platforms include NodeJS, Cordova, Browser, PhoneGap, React Native, Expo, Electron, and NativeScript. It is easy to operate TypeORM with CLI commands, and you can create unique indices for any of your TableColumn objects.

There are quite a few great benefits that TypeORM offers, such as:

  • Quality and loose coupling. With support for both Data Mapper and Active Record patterns, you can use TypeORM to build loosely coupled and high-quality applications with ease. Loose coupling means the application components will function independently. 
  • Scalability. With support for database-specific column names and types, TypeORM lets you write highly scalable applications from scratch. You can begin with writing a small and simple application and then build upon it as your app’s usability grows over time. 
  • Integration. TypeORM has multiple setup connection options, making the integration process with other applications, modules, and platforms less complicated. SQLite, Microsoft SQL Server, SAP HANA, MySQL, and Oracle are the most commonly used supported modules. 
  • Compatibility. TypeORM easily adapts to the architecture of different sizes. The QueryBuilder is simple, flexible, powerful, and customizable as needed. The code you produce will give an optimal performance while also offering outstanding maintainability. 

Now that we have a good familiarity with TypeORM, let’s dive into the process of setting up Aurora for using the Data API. Then, keep reading to find out how to do it.  

How to Set Aurora to Use Data API

The primary purpose of the Amazon Aurora relational database is to offer you excellent performance and availability of large enterprise-level databases at a lower cost and simplicity similar to open source databases. You will need to configure Aurora for Data API usage. Follow these steps to do this:

  • Creating an Aurora serverless cluster. As a starter, the first task is to create an Aurora serverless cluster. Here, you may be wondering the reason for using a serverless architecture. The best advantage for using a serverless architecture is cost reduction because you won’t have to maintain an expensive server. Instead, you would pay only for the storage and capacity you use. At the same time, serverless architecture is easy to deploy within a short time. 

Now, you can navigate to the RDS dashboard located on the AWS console to create an Aurora serverless. But here is a word of caution: as serverless is a developing technology, AWS has not extended it to all regions. You will need to choose one of the AWS regions compatible with Data API. 

  • Switching from regular Aurora to Aurora Serverless. If you already have a regular Aurora cluster, you should migrate to a serverless Aurora cluster. 
  • Enabling the Data API. Next, in the connectivity section, you will have to enable Data API under the web service option. For everything else, you can opt for the default values.

There are several advantages of opting for Data API with Aurora Serverless, such as:

  • The Data API configuration of Aurora Serverless will let you use the web-services interface when you work with your Aurora Serverless DB cluster. 
  • You won’t have to guarantee a fully stable connection with the DB cluster while using Data API. Instead, you can enjoy the convenience of compatibility with AWS SDKs through a secured HTTP endpoint. 
  • The Data API configuration will let you run SQL statements utilizing the HTTP endpoint. At the same time, it will reduce your workload, as you won’t have to manage connections.  

Now you are all set to create your new TypeORM project. At this point, you can already use a vanilla Data API client. But if you want to save time or you don't want to change your existing project with TypeORM, you can use the adapter library for this. Let’s check that out now.  

Save Time with typeorm-aurora-data-api-driver

Instead of precisely following all the steps discussed, you can simply use the typeorm-aurora-data-api-driver, a project to form a connection between Aurora Data API and your TypeORM project. You can integrate TypeORM with the Aurora Data API by managing the connection configuration. The feature is convenient for working in serverless environments. 

The typeorm-aurora-data-api-driver project has the following compatibility options: 

  • Compatible with Postgres and MySQL
  • Compatible with casting so that you can set enums, UUID, date with appropriate structure, and column name for time. 

Take a look below to learn how to use the typeorm-aurora-data-api-driver project:

  • Step 1. First, you will activate the Data API on your database.  
  • Step 2. Perform the driver installation with one of the following commands:

Option 1: 

yarn add typeorm-aurora-data-api-driver

Option 2: 

npm i --save typeorm-aurora-data-api-driver
  • Step 3. Configure your connection either with MySQL or Postgres. 

Now you are ready to use the newly created connection similar to any other connections to the database in TypeORM. There are also some other useful settings in typeorm-aurora-data-api-driver, which we will explore now.

Additional TypeORM Configuration Options for Data API Usage

Natively, the typeorm-aurora-data-api-driver uses Data API client settings. However, if you want to explore additional configurations, use the two properties “formatOptions” and “serviceConfigOptions.” There are two configuration options—Automatic Casting and Parameter Casting. 

Additional TypeORM Configuration Options for Data API UsageHere’s how to do it: 

Automatic Casting

The project driver will initially attempt to cast the entity fields on insert and utilize the type casting from the Data API clients and entity metadata to update the SQL queries. The process makes it possible for you to use enum columns and UUID. 

However, you can also suspend the process by changing the formatOptions value. To suspend, set the value as false for 

formatOptions.castParameters.

There is another method for you to cast the UUID automatically. Based on RegEx, you can configure 

enableUuidHack: true” as “formatOptions.”

Parameter Casting

The parameter casting lets you set a definite value for SQL query parameters. You can do it by passing an object with properties defined as value and cast. 

Take a look at the following example to learn how to cast it manually: 

const dbPost = await postRepository.findOne({
  title: {
    value: 'f01bdc12-ed72-4260-86aa-b7123f08cab9',
    cast: 'uuid',
  },
})

Need Help With Your TypeORM Project?

You know quite a bit about implementing Aurora Data API with TypeORM by now. But is this enough? If you feel you could use some professional advice, do not hesitate to ask for it. 

The Geniusee team will help you make the most of your data. The highly skilled data science team will help you scale your business, generate revenue, improve efficiency, and lower your project implementation and operational costs. The data science team at Geniusee comprehensively works on all major technologies and platforms, including Python, R/Shiny, Alteryx, Tableau, TensorFlow, Sparkle, Hadoop, and MySQL. 

Send a note to Geniusee to get information on how TypeORM integration with Aurora Data API can reduce the effort and cost of your product. 

Concluding Note

If you have trouble with socket connection overflow, you can set up Data API on your Aurora instance and easily use it with TypeORM via typeorm-aurora-data-api-driver. This way, you can spare a lot of time, using habitual ORM instead of a vanilla Data API client.