Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does TypeORM supports raw SQL queries for input and output?

Tags:

orm

typeorm

I would like to know if there is a feature of TypeORM that supports raw sql queries for Insert Update Delete Select etc..

like image 433
Roel Avatar asked Jun 12 '17 07:06

Roel


People also ask

Should I use raw SQL or ORM?

ORM is good only for developers and maintenance because most developers aren't very good at SQL, but if you're actually talking about performance, SQL completely trumps it.

What are raw queries in SQL?

Raw SQL, sometimes also called native SQL, is the most basic, most low-level form of database interaction. You tell the database what to do in the language of the database. Most developers should know basics of SQL. This means how to CREATE tables and views, how to SELECT and JOIN data, how to UPDATE and DELETE data.

What databases does TypeORM support?

TypeORM is a TypeScript ORM (object-relational mapper) library that makes it easy to link your TypeScript application up to a relational database database. TypeORM supports MySQL, SQlite, Postgres, MS SQL Server, and a host of other traditional options.


2 Answers

According to this issue comment, TypeORM enables you to use any queries to your heart's content. using entityManager.query() Here is the documentation.

UPDATE

Link above is outdated, try this instead entity-manager-api.

const rawData = await manager.query(`SELECT * FROM USERS`); 
like image 119
Roel Avatar answered Oct 19 '22 23:10

Roel


2020 UPDATE, entityManager.query() basing the entityManager off the EntityManager class, was not working for me so had to do this:

  import { getManager } from 'typeorm';    const entityManager = getManager();   const someQuery = await entityManager.query(`   SELECT      fw."X",     fw."Y",     ew.*   FROM "table1" as fw   JOIN "table2" as ew     ON fw."X" = $1 AND ew.id = fw."Y";   `, [param1]); 

https://orkhan.gitbook.io/typeorm/docs/working-with-entity-manager

like image 27
Ruben Martinez Avatar answered Oct 20 '22 00:10

Ruben Martinez