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

feat(core): require explicitly marked raw queries via raw() helper #4197

Merged
merged 1 commit into from Apr 7, 2023

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Apr 6, 2023

Improves the new raw() helper that now replaces the removed expr() helper. Also adds new sql helper that allows to work with raw as with tagged template function.

The raw() helper is now required to be used for any raw SQL, both with EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part of a filter. This fragment is represented by RawQueryFragment class instance that can be serialized to a string, so it can be used both as an object value and key. When serialized, the fragment key gets cached and only such cached key will be recognized by the ORM. This adds a runtime safety to the raw query fragments.

raw() helper is required since v6 to use a raw fragment in your query, both through EntityManager and QueryBuilder.

// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });

The raw helper supports several signatures, you can pass in a callback that receives the current property alias:

await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });

You can also use the sql tagged template function, which works the same, but supports only the simple string signature:

await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });

When using inside filters, you might have to use a callback signature to create new raw instance for every filter usage.

@Filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })

@codecov
Copy link

codecov bot commented Apr 6, 2023

Codecov Report

Patch coverage: 95.73% and project coverage change: -0.06 ⚠️

Comparison is base (a812746) 99.52% compared to head (d52416e) 99.46%.

❗ Current head d52416e differs from pull request most recent head 35f4857. Consider uploading reports for the commit 35f4857 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##               v6    #4197      +/-   ##
==========================================
- Coverage   99.52%   99.46%   -0.06%     
==========================================
  Files         218      219       +1     
  Lines       14258    14311      +53     
  Branches     3260     3269       +9     
==========================================
+ Hits        14190    14235      +45     
- Misses         67       74       +7     
- Partials        1        2       +1     
Impacted Files Coverage Δ
packages/core/src/utils/QueryHelper.ts 100.00% <ø> (ø)
packages/knex/src/AbstractSqlPlatform.ts 96.49% <33.33%> (-3.51%) ⬇️
packages/core/src/utils/RawQueryFragment.ts 92.85% <92.85%> (ø)
packages/knex/src/query/QueryBuilderHelper.ts 99.27% <97.50%> (-0.49%) ⬇️
packages/core/src/metadata/MetadataDiscovery.ts 99.29% <100.00%> (+<0.01%) ⬆️
packages/core/src/platforms/Platform.ts 99.55% <100.00%> (ø)
packages/core/src/utils/Utils.ts 99.60% <100.00%> (ø)
packages/core/src/utils/index.ts 100.00% <100.00%> (ø)
packages/knex/src/AbstractSqlDriver.ts 100.00% <100.00%> (ø)
packages/knex/src/MonkeyPatchable.ts 100.00% <100.00%> (ø)
... and 7 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@B4nan B4nan force-pushed the force-raw branch 2 times, most recently from 3244f2f to d52416e Compare April 7, 2023 16:58
@B4nan B4nan marked this pull request as ready for review April 7, 2023 18:06
@B4nan B4nan merged commit 71d0ff2 into v6 Apr 7, 2023
6 checks passed
@B4nan B4nan deleted the force-raw branch April 7, 2023 18:07
B4nan added a commit that referenced this pull request Apr 7, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request Apr 10, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request Apr 12, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request Apr 26, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
jsprw pushed a commit to jsprw/mikro-orm-full-text-operators that referenced this pull request May 7, 2023
…ikro-orm#4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request May 14, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request May 14, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request May 24, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request May 26, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request Jun 11, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request Sep 10, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request Sep 20, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request Sep 24, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request Sep 30, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
@B4nan B4nan mentioned this pull request Sep 30, 2023
22 tasks
B4nan added a commit that referenced this pull request Oct 2, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request Oct 17, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request Oct 21, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request Oct 25, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request Nov 2, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
B4nan added a commit that referenced this pull request Nov 5, 2023
…4197)

Improves the new `raw()` helper that now replaces the removed `expr()`
helper. Also adds new `sql` helper that allows to work with `raw` as
with tagged template function.

The `raw()` helper is now required to be used for any raw SQL, both with
EM and QB, to protect from SQL injection.

This raw SQL query fragment that can be assigned to a property or part
of a filter. This fragment is represented by `RawQueryFragment` class
instance that can be serialized to a string, so it can be used both as
an object value and key. When serialized, the fragment key gets cached
and only such cached key will be recognized by the ORM. This adds a
runtime safety to the raw query fragments.

> **`raw()` helper is required since v6 to use a raw fragment in your
query, both through EntityManager and QueryBuilder.**

```ts
// as a value
await em.find(User, { time: raw('now()') });

// as a key
await em.find(User, { [raw('lower(name)')]: name.toLowerCase() });

// value can be empty array
await em.find(User, { [raw('(select 1 = 1)')]: [] });
```

The `raw` helper supports several signatures, you can pass in a callback
that receives the current property alias:

```ts
await em.find(User, { [raw(alias => `lower(${alias}.name)`)]: name.toLowerCase() });
```

You can also use the `sql` tagged template function, which works the
same, but supports only the simple string signature:

```ts
await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
```

When using inside filters, you might have to use a callback signature to
create new raw instance for every filter usage.

```ts
@filter({ name: 'long', cond: () => ({ [raw('length(perex)')]: { $gt: 10000 } }) })
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant