You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The mysql2 driver supports named placeholders. These can currently be used via typeorm as follows:
constdataSource=newDataSource({
...
extra: {namedPlaceholders: true},});constresult=awaitdataSource.manager.query("SELECT :msg",{msg: "hello world"}asany);// note usage of 'as any' to work around limited parameters typingconsole.log(result);// output: [ { 'hello world': 'hello world' } ]
Named placeholders are much nicer/easier/clearer to work with compared to the base supported ?/array syntax. It is also aligned with the way parameters are passed to typeorm when working with createQueryBuilder (docs).
The problem is that the query function has the following limited typing:
This could cause confusion as to which parameter format to use but this has already been stated to be driver-dependant. So perhaps confusion could be avoided through improved documentation around the query function.
Considered Alternatives
Working around the type issue using as any is possible but not clean/desirable.
Another workaround is to override the typing for the query function in a given project using declare module "typeorm"... approach, but again this is not clean/desirable.
FYI I'm currently working around this by inserting the following in my dataSource.ts file:
declare module "typeorm"{interfaceEntityManager{/** * As per normal EntityManager.query but with object literal parameters. * Workaround to https://github.com/typeorm/typeorm/issues/8828. */query(query: string,parameters?: ObjectLiteral): Promise<any>;}}
Feature Description
The Problem
The mysql2 driver supports named placeholders. These can currently be used via
typeorm
as follows:Named placeholders are much nicer/easier/clearer to work with compared to the base supported ?/array syntax. It is also aligned with the way parameters are passed to
typeorm
when working withcreateQueryBuilder
(docs).The problem is that the
query
function has the following limited typing:It is therefore not possible to pass an object literal to
parameters
without some workaround like usingas any
.The Solution
Could the typing for
parameters
of thequery
function be extended to support object literals? E.g.Note:
ObjectLiteral
is already used in a similar context here:typeorm/src/driver/Driver.ts
Line 137 in c5dfc11
It is defined here:
typeorm/src/common/ObjectLiteral.ts
Line 4 in c5dfc11
This could cause confusion as to which parameter format to use but this has already been stated to be driver-dependant. So perhaps confusion could be avoided through improved documentation around the
query
function.Considered Alternatives
Working around the type issue using
as any
is possible but not clean/desirable.Another workaround is to override the typing for the
query
function in a given project usingdeclare module "typeorm"...
approach, but again this is not clean/desirable.Additional Context
#556
Relevant Database Driver(s)
aurora-mysql
aurora-postgres
better-sqlite3
cockroachdb
cordova
expo
mongodb
mysql
nativescript
oracle
postgres
react-native
sap
sqlite
sqlite-abstract
sqljs
sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
The text was updated successfully, but these errors were encountered: