raw
Callable
Parameters
sql: AnyString | RawQueryFragment | QueryBuilder<any, any> | Raw<any> | QueryBuilder<T, never, never, never> | EntityKey<T> | EntityKey<T>[] | (alias) => string
optionalparams: readonly unknown[] | Dictionary<unknown>
Returns NoInfer<R>
Creates raw SQL query fragment that can be assigned to a property or part of a filter. This fragment is represented by
RawQueryFragmentclass 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.The
rawhelper supports several signatures, you can pass in a callback that receives the current property alias:You can also use the
sqltagged template function, which works the same, but supports only the simple string signature:When using inside filters, you might have to use a callback signature to create new raw instance for every filter usage.
The
rawhelper can be used within indexes and uniques to write database-agnostic SQL expressions. In that case, you can use'??'to tag your database identifiers (table name, column names, index name, ...) inside your expression, and pass those identifiers as a second parameter to therawhelper. Internally, those will automatically be quoted according to the database in use:You can also use the
quotetag function to write database-agnostic SQL expressions. The end-result is the same as using therawfunction regarding database identifiers quoting, only to have a more elegant expression syntax: