Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: documentation for parameters in Repository, DataSource, EntityManager query methods #10848

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions docs/data-source-api.md
Expand Up @@ -141,6 +141,41 @@ await dataSource.transaction(async (manager) => {

```typescript
const rawData = await dataSource.query(`SELECT * FROM USERS`)

// You can also use parameters to avoid SQL injection
// The syntax differs between the drivers

// aurora-mysql, better-sqlite3, capacitor, cordova,
// expo, mariadb, mysql, nativescript, react-native,
// sap, sqlite, sqljs
const rawData = await dataSource.query(
'SELECT * FROM USERS WHERE name = ? and age = ?',
[ 'John', 24 ]
)

// aurora-postgres, cockroachdb, postgres
const rawData = await dataSource.query(
'SELECT * FROM USERS WHERE name = $1 and age = $2',
['John', 24]
)

// oracle
const rawData = await dataSource.query(
'SELECT * FROM USERS WHERE name = :1 and age = :2',
['John', 24]
)

// spanner
const rawData = await dataSource.query(
'SELECT * FROM USERS WHERE name = @param0 and age = @param1',
[ 'John', 24 ]
)

// mssql
const rawData = await dataSource.query(
'SELECT * FROM USERS WHERE name = @0 and age = @1',
[ 'John', 24 ]
)
```

- `createQueryBuilder` - Creates a query builder, which can be used to build queries.
Expand Down
35 changes: 35 additions & 0 deletions docs/entity-manager-api.md
Expand Up @@ -28,6 +28,41 @@ await manager.transaction(async (manager) => {

```typescript
const rawData = await manager.query(`SELECT * FROM USERS`)

// You can also use parameters to avoid SQL injection
// The syntax differs between the drivers

// aurora-mysql, better-sqlite3, capacitor, cordova,
// expo, mariadb, mysql, nativescript, react-native,
// sap, sqlite, sqljs
const rawData = await manager.query(
'SELECT * FROM USERS WHERE name = ? and age = ?',
[ 'John', 24 ]
)

// aurora-postgres, cockroachdb, postgres
const rawData = await manager.query(
'SELECT * FROM USERS WHERE name = $1 and age = $2',
['John', 24]
)

// oracle
const rawData = await manager.query(
'SELECT * FROM USERS WHERE name = :1 and age = :2',
['John', 24]
)

// spanner
const rawData = await manager.query(
'SELECT * FROM USERS WHERE name = @param0 and age = @param1',
[ 'John', 24 ]
)

// mssql
const rawData = await manager.query(
'SELECT * FROM USERS WHERE name = @0 and age = @1',
[ 'John', 24 ]
)
```

- `createQueryBuilder` - Creates a query builder use to build SQL queries.
Expand Down
35 changes: 35 additions & 0 deletions docs/repository-api.md
Expand Up @@ -391,6 +391,41 @@ const timber = await repository.findOneByOrFail({ firstName: "Timber" })

```typescript
const rawData = await repository.query(`SELECT * FROM USERS`)

// You can also use parameters to avoid SQL injection
// The syntax differs between the drivers

// aurora-mysql, better-sqlite3, capacitor, cordova,
// expo, mariadb, mysql, nativescript, react-native,
// sap, sqlite, sqljs
const rawData = await repository.query(
'SELECT * FROM USERS WHERE name = ? and age = ?',
[ 'John', 24 ]
)

// aurora-postgres, cockroachdb, postgres
const rawData = await repository.query(
'SELECT * FROM USERS WHERE name = $1 and age = $2',
['John', 24]
)

// oracle
const rawData = await repository.query(
'SELECT * FROM USERS WHERE name = :1 and age = :2',
['John', 24]
)

// spanner
const rawData = await repository.query(
'SELECT * FROM USERS WHERE name = @param0 and age = @param1',
[ 'John', 24 ]
)

// mssql
const rawData = await repository.query(
'SELECT * FROM USERS WHERE name = @0 and age = @1',
[ 'John', 24 ]
)
```

- `clear` - Clears all the data from the given table (truncates/drops it).
Expand Down
2 changes: 2 additions & 0 deletions src/data-source/DataSource.ts
Expand Up @@ -520,6 +520,8 @@ export class DataSource {

/**
* Executes raw SQL query and returns raw database results.
*
* @see [Official docs](https://typeorm.io/data-source-api) for examples.
*/
async query<T = any>(
query: string,
Expand Down
2 changes: 2 additions & 0 deletions src/entity-manager/EntityManager.ts
Expand Up @@ -169,6 +169,8 @@ export class EntityManager {

/**
* Executes raw SQL query and returns raw database results.
*
* @see [Official docs](https://typeorm.io/entity-manager-api) for examples.
*/
async query<T = any>(query: string, parameters?: any[]): Promise<T> {
return this.connection.query(query, parameters, this.queryRunner)
Expand Down
2 changes: 2 additions & 0 deletions src/repository/Repository.ts
Expand Up @@ -644,6 +644,8 @@ export class Repository<Entity extends ObjectLiteral> {
/**
* Executes a raw SQL query and returns a raw database results.
* Raw query execution is supported only by relational databases (MongoDB is not supported).
*
* @see [Official docs](https://typeorm.io/repository-api) for examples.
*/
query(query: string, parameters?: any[]): Promise<any> {
return this.manager.query(query, parameters)
Expand Down