( transform?: (_: string) => string, transformJson = true )
| 388 | * @since 4.0.0 |
| 389 | */ |
| 390 | export const makeCompiler = ( |
| 391 | transform?: (_: string) => string, |
| 392 | transformJson = true |
| 393 | ): Statement.Compiler => { |
| 394 | const transformValue = transformJson && transform |
| 395 | ? Statement.defaultTransforms(transform).value |
| 396 | : undefined |
| 397 | |
| 398 | return Statement.makeCompiler<PgCustom>({ |
| 399 | dialect: "pg", |
| 400 | placeholder(_) { |
| 401 | return `$${_}` |
| 402 | }, |
| 403 | onIdentifier: transform ? |
| 404 | function(value, withoutTransform) { |
| 405 | return withoutTransform ? escape(value) : escape(transform(value)) |
| 406 | } : |
| 407 | escape, |
| 408 | onRecordUpdate(placeholders, valueAlias, valueColumns, values, returning) { |
| 409 | return [ |
| 410 | `(values ${placeholders}) AS ${valueAlias}${valueColumns}${returning ? ` RETURNING ${returning[0]}` : ""}`, |
| 411 | returning ? |
| 412 | values.flat().concat(returning[1]) : |
| 413 | values.flat() |
| 414 | ] |
| 415 | }, |
| 416 | onCustom(type, placeholder, withoutTransform) { |
| 417 | switch (type.kind) { |
| 418 | case "PgJson": { |
| 419 | return [ |
| 420 | placeholder(undefined), |
| 421 | [ |
| 422 | withoutTransform || transformValue === undefined |
| 423 | ? type.paramA |
| 424 | : transformValue(type.paramA) |
| 425 | ] |
| 426 | ] |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | }) |
| 431 | } |
| 432 | |
| 433 | const escape = Statement.defaultEscape("\"") |
| 434 | const escapeLiteral = (value: string) => `'${value.replace(/'/g, "''")}'` |
no test coverage detected
searching dependent graphs…