MCPcopy Create free account
hub / github.com/porsager/postgres / Sql

Function Sql

src/index.js:86–145  ·  view source on GitHub ↗
(handler)

Source from the content-addressed store, hash-verified

84 return sql
85
86 function Sql(handler) {
87 handler.debug = options.debug
88
89 Object.entries(options.types).reduce((acc, [name, type]) => {
90 acc[name] = (x) => new Parameter(x, type.to)
91 return acc
92 }, typed)
93
94 Object.assign(sql, {
95 types: typed,
96 typed,
97 unsafe,
98 notify,
99 array,
100 json,
101 file
102 })
103
104 return sql
105
106 function typed(value, type) {
107 return new Parameter(value, type)
108 }
109
110 function sql(strings, ...args) {
111 const query = strings && Array.isArray(strings.raw)
112 ? new Query(strings, args, handler, cancel)
113 : typeof strings === 'string' && !args.length
114 ? new Identifier(options.transform.column.to ? options.transform.column.to(strings) : strings)
115 : new Builder(strings, args)
116 return query
117 }
118
119 function unsafe(string, args = [], options = {}) {
120 arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])
121 const query = new Query([string], args, handler, cancel, {
122 prepare: false,
123 ...options,
124 simple: 'simple' in options ? options.simple : args.length === 0
125 })
126 return query
127 }
128
129 function file(path, args = [], options = {}) {
130 arguments.length === 2 && !Array.isArray(args) && (options = args, args = [])
131 const query = new Query([], args, (query) => {
132 fs.readFile(path, 'utf8', (err, string) => {
133 if (err)
134 return query.reject(err)
135
136 query.strings = [string]
137 handler(query)
138 })
139 }, cancel, {
140 ...options,
141 simple: 'simple' in options ? options.simple : args.length === 0
142 })
143 return query

Callers 3

PostgresFunction · 0.70
reserveFunction · 0.70
scopeFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected