Brief Intro to TypeORM
Published: 2021-06-06 17:03:46

###What Even is an ORM?
For those who might not know, let’s start with the definition of Object Relational Mapping (ORM). After a quick google search, you’ll see that ORM is

>_a programming technique for converting data between incompatible type systems using object-oriented programming languages_ 

So, what exactly does this mean? It means that by using an ORM framework you can query your database using the object-oriented paradigm that you prefer, rather than directly querying the database.

###TypeORM
TypeORM is a popular ORM framework that aims to support the latest JavaScript features and provides developers with additional features that help with the development of applications that use databases.

###How it Works
You might be asking yourself, so how does TypeORM work? Well, TypeORM will map the entity objects in our applications to our databases tables and rows. This then enables us to interact with our database through the objects in our application.

Now that we know how TypeORM works, let’s explain it a bit more, but this time using an example.

Here we have a table in our database called products, and it looks like the following code. We have the product id, name, description, and the date posted. 

```
CREATE TABLE products (
	id SERIAL PRIMARY KEY,
	name VARCHAR(225) NOT NULL,
	description VARCHAR(225) NOT NULL,
	date_posted DATE NOT NULL
);
```

Now let’s say we want to query our table to select the product where the name is 'Product Name'. Below is how we would do this query in plain SQL. This would return the entire row from our database.

```
SELECT * FROM products WHERE name = 'Product Name';
```

Now let’s do the same thing but this time utilizing TypeORM. This will return an object. We can now see that TypeORM enables us to select a row from our database and returns it to us as an object.

```
const product = productsRepository.findOne({ name: 'Product Name' });

{
	id: 1,
	name: 'Product Name',
	description: 'Product description here',
	date_posted: '2021-01-01'
}
```

Although the second method, using TypeORM, looks different than the plain SQL it essentially does the same thing. TypeORM will transform our query into an equivalent plain SQL query, similar to our first example. 

###TypeORMs Patterns
ORM patterns are patterns that define how developers can access the data from their database and use it for their applications. Two common patterns are Active Record and Data Mapper patterns, and both are supported by TypeORM.

The Active Record pattern uses an object to wrap the database. We are then able to add methods, which enable us to access and modify the data directly. When using this pattern we define all of our query methods inside of the model which then enables us to modify our objects using the model methods.

The Data Mapper pattern separates the objects and database so that they are unaware of each other's existence. When using this pattern we define all of our query methods in separate classes, called repositories. We are then able to modify our objects using these repositories. 

So what’s the difference between the two? To put it into more straightforward terms...
* Active Records pattern allows us to access our database within our models
* Data Mapper pattern allows us to access our database within our repositories

###Why Use TypeORM or Any ORM?
It’s simple enough to directly query our database, so why bother using TypeORM or any other ORM framework? One of the main reasons that developers choose to use an ORM framework is so they can continue writing their code in the language they already know, instead of using plain SQL. Overall, using an ORM framework enables developers to create their applications with a bit more ease, while also making their applications significantly easier to maintain.