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