MCPcopy Create free account
hub / github.com/Effect-TS/effect / makeCompiler

Function makeCompiler

packages/sql/pg/src/PgClient.ts:594–634  ·  view source on GitHub ↗
(
  transform?: (_: string) => string,
  transformJson = true
)

Source from the content-addressed store, hash-verified

592 * @since 4.0.0
593 */
594export 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
636const escape = Statement.defaultEscape("\"")
637

Callers 1

PgClient.tsFile · 0.70

Calls 2

escapeFunction · 0.50
transformFunction · 0.50

Tested by

no test coverage detected