@mikro-orm/core
Version:
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.
131 lines (130 loc) • 6.02 kB
TypeScript
import { inspect } from 'node:util';
import type { AnyString, Dictionary, EntityKey } from '../typings';
export declare class RawQueryFragment {
#private;
readonly sql: string;
readonly params: unknown[];
static cloneRegistry?: Set<string>;
constructor(sql: string, params?: unknown[]);
as(alias: string): RawQueryFragment;
valueOf(): string;
toJSON(): string;
toString(): string;
/** @internal */
assign(): void;
clone(): RawQueryFragment;
static run<T>(cb: (...args: any[]) => Promise<T>): Promise<T>;
/**
* @internal allows testing we don't leak memory, as the raw fragments cache needs to be cleared automatically
*/
static checkCacheSize(): number;
static isKnownFragment(key: string | RawQueryFragment): boolean;
static getKnownFragment(key: string | RawQueryFragment, cleanup?: boolean): RawQueryFragment | undefined;
static remove(key: string): void;
/** @ignore */
[inspect.custom](): {
sql: string;
params: unknown[];
} | {
sql: string;
params?: undefined;
};
}
/** @internal */
export declare const ALIAS_REPLACEMENT = "[::alias::]";
/** @internal */
export declare const ALIAS_REPLACEMENT_RE = "\\[::alias::\\]";
/**
* Creates 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 } }) })
* ```
*
* The `raw` helper 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 the `raw` helper. Internally, those will automatically be quoted according to the database in use:
*
* ```ts
* // On postgres, will produce: create index "index custom_idx_on_name" on "library.author" ("country")
* // On mysql, will produce: create index `index custom_idx_on_name` on `library.author` (`country`)
* @Index({ name: 'custom_idx_on_name', expression: (table, columns) => raw(`create index ?? on ?? (??)`, ['custom_idx_on_name', table, columns.name]) })
* @Entity({ schema: 'library' })
* export class Author { ... }
* ```
*
* You can also use the `quote` tag function to write database-agnostic SQL expressions. The end-result is the same as using the `raw` function regarding database identifiers quoting, only to have a more elegant expression syntax:
*
* ```ts
* @Index({ name: 'custom_idx_on_name', expression: (table, columns) => quote`create index ${'custom_idx_on_name'} on ${table} (${columns.name})` })
* @Entity({ schema: 'library' })
* export class Author { ... }
* ```
*/
export declare function raw<T extends object = any, R = any>(sql: EntityKey<T> | EntityKey<T>[] | AnyString | ((alias: string) => string) | RawQueryFragment, params?: readonly unknown[] | Dictionary<unknown>): R;
/**
* Alternative to the `raw()` helper allowing to use it as a tagged template function for the simple cases.
*
* ```ts
* // as a value
* await em.find(User, { time: sql`now()` });
*
* // as a key
* await em.find(User, { [sql`lower(name)`]: name.toLowerCase() });
*
* // value can be empty array
* await em.find(User, { [sql`(select 1 = 1)`]: [] });
* ```
*/
export declare function sql(sql: readonly string[], ...values: unknown[]): any;
export declare namespace sql {
var ref: <T extends object>(...keys: string[]) => RawQueryFragment;
var now: (length?: number) => string;
var lower: <T extends object>(key: string | ((alias: string) => string)) => string;
var upper: <T extends object>(key: string | ((alias: string) => string)) => string;
}
export declare function createSqlFunction<T extends object, R = string>(func: string, key: string | ((alias: string) => string)): R;
/**
* Tag function providing quoting of db identifiers (table name, columns names, index names, ...).
*
* Within the template literal on which the tag function is applied, all placeholders are considered to be database identifiers, and will thus be quoted as so according to the database in use.
*
* ```ts
* // On postgres, will produce: create index "index custom_idx_on_name" on "library.author" ("name")
* // On mysql, will produce: create index `index custom_idx_on_name` on `library.author` (`name`)
* @Index({ name: 'custom_idx_on_name', expression: (table, columns) => quote`create index ${'custom_idx_on_name'} on ${table} (${columns.name})` })
* @Entity({ schema: 'library' })
* export class Author { ... }
* ```
*/
export declare function quote(expParts: readonly string[], ...values: (string | {
toString(): string;
})[]): any;