MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / raw

Function raw

packages/core/src/utils/RawQueryFragment.ts:219–254  ·  view source on GitHub ↗
(
  sql: EntityKey<T> | EntityKey<T>[] | AnyString | ((alias: string) => string) | RawQueryFragment,
  params?: readonly unknown[] | Dictionary<unknown>,
)

Source from the content-addressed store, hash-verified

217 * ```
218 */
219export function raw<R = RawQueryFragment & symbol, T extends object = any>(
220 sql: EntityKey<T> | EntityKey<T>[] | AnyString | ((alias: string) => string) | RawQueryFragment,
221 params?: readonly unknown[] | Dictionary<unknown>,
222): R {
223 if (isRaw(sql)) {
224 return sql as R;
225 }
226
227 if (sql instanceof Function) {
228 sql = sql(ALIAS_REPLACEMENT);
229 }
230
231 if (sql === '??' && Array.isArray(params)) {
232 return new RawQueryFragment(sql, params) as R;
233 }
234
235 if (Array.isArray(sql)) {
236 // for composite FK we return just a simple string
237 return Utils.getPrimaryKeyHash(sql) as R;
238 }
239
240 if (typeof params === 'object' && !Array.isArray(params)) {
241 const pairs = Object.entries(params);
242 const objectParams = [];
243
244 for (const [key, value] of pairs) {
245 sql = sql.replace(`:${key}:`, '??');
246 sql = sql.replace(`:${key}`, '?');
247 objectParams.push(value);
248 }
249
250 return new RawQueryFragment(sql, objectParams) as R;
251 }
252
253 return new RawQueryFragment(sql, params) as R;
254}
255
256/**
257 * Alternative to the `raw()` helper allowing to use it as a tagged template function for the simple cases.

Callers 15

getSearchJsonPropertyKeyFunction · 0.90
countByMethod · 0.90
nativeCloneTPTFunction · 0.90
buildCloneFieldsFunction · 0.90
addParamsFunction · 0.90
mapPropToFieldNamesFunction · 0.90
lockPessimisticFunction · 0.90
applyUnionWhereFunction · 0.90
buildPopulateOrderByFunction · 0.90

Calls 5

isRawFunction · 0.85
sqlFunction · 0.85
getPrimaryKeyHashMethod · 0.80
entriesMethod · 0.80
pushMethod · 0.80

Tested by 3

upMethod · 0.72
upMethod · 0.72
downMethod · 0.72