( transform?: (_: string) => string, transformJson = true )
| 835 | * @since 4.0.0 |
| 836 | */ |
| 837 | export const makeCompiler = ( |
| 838 | transform?: (_: string) => string, |
| 839 | transformJson = true |
| 840 | ): Statement.Compiler => { |
| 841 | const transformValue = transformJson && transform |
| 842 | ? Statement.defaultTransforms(transform).value |
| 843 | : undefined |
| 844 | |
| 845 | return Statement.makeCompiler<PgCustom>({ |
| 846 | dialect: "pg", |
| 847 | placeholder(_) { |
| 848 | return `$${_}` |
| 849 | }, |
| 850 | onIdentifier: transform ? |
| 851 | function(value, withoutTransform) { |
| 852 | return withoutTransform ? escape(value) : escape(transform(value)) |
| 853 | } : |
| 854 | escape, |
| 855 | onRecordUpdate(placeholders, valueAlias, valueColumns, values, returning) { |
| 856 | return [ |
| 857 | `(values ${placeholders}) AS ${valueAlias}${valueColumns}${returning ? ` RETURNING ${returning[0]}` : ""}`, |
| 858 | returning ? |
| 859 | values.flat().concat(returning[1]) : |
| 860 | values.flat() |
| 861 | ] |
| 862 | }, |
| 863 | onCustom(type, placeholder, withoutTransform) { |
| 864 | switch (type.kind) { |
| 865 | case "PgJson": { |
| 866 | return [ |
| 867 | placeholder(undefined), |
| 868 | [ |
| 869 | withoutTransform || transformValue === undefined |
| 870 | ? type.paramA |
| 871 | : transformValue(type.paramA) |
| 872 | ] |
| 873 | ] |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | }) |
| 878 | } |
| 879 | |
| 880 | const escape = Statement.defaultEscape("\"") |
| 881 |
no test coverage detected