'Does TypeORM supports raw SQL queries for input and output?

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



Solution 1:[1]

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`);

Solution 2:[2]

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

Solution 3:[3]

try out this (April, 2022, typeorm ^0.3.6)

import { DataSource } from "typeorm";

(async () => {
    const AppDatasource = new DataSource({
        type: "mysql",
        host: "localhost",
        port: 3306,
        username: "root",
        password: "root",
        database: "your-database",
        synchronize: false,
        logging: false,
        entities: ['src/entity/**/*.ts']
    })
    const appDataSource = await AppDataSource.initialize();
    const queryRunner = await appDataSource.createQueryRunner();
    var result = await queryRunner.manager.query(
        `SELECT * FROM your-table LIMIT 100`
    );
    await console.log(result)
})()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 topmoon
Solution 2 PidoMN
Solution 3 user1960422