( transform?: (_: string) => string, transformJson = true )
| 592 | * @since 4.0.0 |
| 593 | */ |
| 594 | export const makeCompiler = ( |
| 595 | transform?: (_: string) => string, |
| 596 | transformJson = true |
| 597 | ): Statement.Compiler => { |
| 598 | const transformValue = transformJson && transform |
| 599 | ? Statement.defaultTransforms(transform).value |
| 600 | : undefined |
| 601 | |
| 602 | return Statement.makeCompiler<PgCustom>({ |
| 603 | dialect: "pg", |
| 604 | placeholder(_) { |
| 605 | return `$${_}` |
| 606 | }, |
| 607 | onIdentifier: transform ? |
| 608 | function(value, withoutTransform) { |
| 609 | return withoutTransform ? escape(value) : escape(transform(value)) |
| 610 | } : |
| 611 | escape, |
| 612 | onRecordUpdate(placeholders, valueAlias, valueColumns, values, returning) { |
| 613 | return [ |
| 614 | `(values ${placeholders}) AS ${valueAlias}${valueColumns}${returning ? ` RETURNING ${returning[0]}` : ""}`, |
| 615 | returning ? |
| 616 | values.flat().concat(returning[1]) : |
| 617 | values.flat() |
| 618 | ] |
| 619 | }, |
| 620 | onCustom(type, placeholder, withoutTransform) { |
| 621 | switch (type.kind) { |
| 622 | case "PgJson": { |
| 623 | const value = withoutTransform || transformValue === undefined |
| 624 | ? type.paramA |
| 625 | : transformValue(type.paramA) |
| 626 | return [ |
| 627 | placeholder(undefined), |
| 628 | [PgTypes.jsonb(value)] |
| 629 | ] |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | }) |
| 634 | } |
| 635 | |
| 636 | const escape = Statement.defaultEscape("\"") |
| 637 |
no test coverage detected