@depup/postgres 3.4.8-depup.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.js ADDED
@@ -0,0 +1,367 @@
1
+ import { Query } from './query.js'
2
+ import { Errors } from './errors.js'
3
+
4
+ export const types = {
5
+ string: {
6
+ to: 25,
7
+ from: null, // defaults to string
8
+ serialize: x => '' + x
9
+ },
10
+ number: {
11
+ to: 0,
12
+ from: [21, 23, 26, 700, 701],
13
+ serialize: x => '' + x,
14
+ parse: x => +x
15
+ },
16
+ json: {
17
+ to: 114,
18
+ from: [114, 3802],
19
+ serialize: x => JSON.stringify(x),
20
+ parse: x => JSON.parse(x)
21
+ },
22
+ boolean: {
23
+ to: 16,
24
+ from: 16,
25
+ serialize: x => x === true ? 't' : 'f',
26
+ parse: x => x === 't'
27
+ },
28
+ date: {
29
+ to: 1184,
30
+ from: [1082, 1114, 1184],
31
+ serialize: x => (x instanceof Date ? x : new Date(x)).toISOString(),
32
+ parse: x => new Date(x)
33
+ },
34
+ bytea: {
35
+ to: 17,
36
+ from: 17,
37
+ serialize: x => '\\x' + Buffer.from(x).toString('hex'),
38
+ parse: x => Buffer.from(x.slice(2), 'hex')
39
+ }
40
+ }
41
+
42
+ class NotTagged { then() { notTagged() } catch() { notTagged() } finally() { notTagged() }}
43
+
44
+ export class Identifier extends NotTagged {
45
+ constructor(value) {
46
+ super()
47
+ this.value = escapeIdentifier(value)
48
+ }
49
+ }
50
+
51
+ export class Parameter extends NotTagged {
52
+ constructor(value, type, array) {
53
+ super()
54
+ this.value = value
55
+ this.type = type
56
+ this.array = array
57
+ }
58
+ }
59
+
60
+ export class Builder extends NotTagged {
61
+ constructor(first, rest) {
62
+ super()
63
+ this.first = first
64
+ this.rest = rest
65
+ }
66
+
67
+ build(before, parameters, types, options) {
68
+ const keyword = builders.map(([x, fn]) => ({ fn, i: before.search(x) })).sort((a, b) => a.i - b.i).pop()
69
+ return keyword.i === -1
70
+ ? escapeIdentifiers(this.first, options)
71
+ : keyword.fn(this.first, this.rest, parameters, types, options)
72
+ }
73
+ }
74
+
75
+ export function handleValue(x, parameters, types, options) {
76
+ let value = x instanceof Parameter ? x.value : x
77
+ if (value === undefined) {
78
+ x instanceof Parameter
79
+ ? x.value = options.transform.undefined
80
+ : value = x = options.transform.undefined
81
+
82
+ if (value === undefined)
83
+ throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')
84
+ }
85
+
86
+ return '$' + (types.push(
87
+ x instanceof Parameter
88
+ ? (parameters.push(x.value), x.array
89
+ ? x.array[x.type || inferType(x.value)] || x.type || firstIsString(x.value)
90
+ : x.type
91
+ )
92
+ : (parameters.push(x), inferType(x))
93
+ ))
94
+ }
95
+
96
+ const defaultHandlers = typeHandlers(types)
97
+
98
+ export function stringify(q, string, value, parameters, types, options) { // eslint-disable-line
99
+ for (let i = 1; i < q.strings.length; i++) {
100
+ string += (stringifyValue(string, value, parameters, types, options)) + q.strings[i]
101
+ value = q.args[i]
102
+ }
103
+
104
+ return string
105
+ }
106
+
107
+ function stringifyValue(string, value, parameters, types, o) {
108
+ return (
109
+ value instanceof Builder ? value.build(string, parameters, types, o) :
110
+ value instanceof Query ? fragment(value, parameters, types, o) :
111
+ value instanceof Identifier ? value.value :
112
+ value && value[0] instanceof Query ? value.reduce((acc, x) => acc + ' ' + fragment(x, parameters, types, o), '') :
113
+ handleValue(value, parameters, types, o)
114
+ )
115
+ }
116
+
117
+ function fragment(q, parameters, types, options) {
118
+ q.fragment = true
119
+ return stringify(q, q.strings[0], q.args[0], parameters, types, options)
120
+ }
121
+
122
+ function valuesBuilder(first, parameters, types, columns, options) {
123
+ return first.map(row =>
124
+ '(' + columns.map(column =>
125
+ stringifyValue('values', row[column], parameters, types, options)
126
+ ).join(',') + ')'
127
+ ).join(',')
128
+ }
129
+
130
+ function values(first, rest, parameters, types, options) {
131
+ const multi = Array.isArray(first[0])
132
+ const columns = rest.length ? rest.flat() : Object.keys(multi ? first[0] : first)
133
+ return valuesBuilder(multi ? first : [first], parameters, types, columns, options)
134
+ }
135
+
136
+ function select(first, rest, parameters, types, options) {
137
+ typeof first === 'string' && (first = [first].concat(rest))
138
+ if (Array.isArray(first))
139
+ return escapeIdentifiers(first, options)
140
+
141
+ let value
142
+ const columns = rest.length ? rest.flat() : Object.keys(first)
143
+ return columns.map(x => {
144
+ value = first[x]
145
+ return (
146
+ value instanceof Query ? fragment(value, parameters, types, options) :
147
+ value instanceof Identifier ? value.value :
148
+ handleValue(value, parameters, types, options)
149
+ ) + ' as ' + escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x)
150
+ }).join(',')
151
+ }
152
+
153
+ const builders = Object.entries({
154
+ values,
155
+ in: (...xs) => {
156
+ const x = values(...xs)
157
+ return x === '()' ? '(null)' : x
158
+ },
159
+ select,
160
+ as: select,
161
+ returning: select,
162
+ '\\(': select,
163
+
164
+ update(first, rest, parameters, types, options) {
165
+ return (rest.length ? rest.flat() : Object.keys(first)).map(x =>
166
+ escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x) +
167
+ '=' + stringifyValue('values', first[x], parameters, types, options)
168
+ )
169
+ },
170
+
171
+ insert(first, rest, parameters, types, options) {
172
+ const columns = rest.length ? rest.flat() : Object.keys(Array.isArray(first) ? first[0] : first)
173
+ return '(' + escapeIdentifiers(columns, options) + ')values' +
174
+ valuesBuilder(Array.isArray(first) ? first : [first], parameters, types, columns, options)
175
+ }
176
+ }).map(([x, fn]) => ([new RegExp('((?:^|[\\s(])' + x + '(?:$|[\\s(]))(?![\\s\\S]*\\1)', 'i'), fn]))
177
+
178
+ function notTagged() {
179
+ throw Errors.generic('NOT_TAGGED_CALL', 'Query not called as a tagged template literal')
180
+ }
181
+
182
+ export const serializers = defaultHandlers.serializers
183
+ export const parsers = defaultHandlers.parsers
184
+
185
+ export const END = {}
186
+
187
+ function firstIsString(x) {
188
+ if (Array.isArray(x))
189
+ return firstIsString(x[0])
190
+ return typeof x === 'string' ? 1009 : 0
191
+ }
192
+
193
+ export const mergeUserTypes = function(types) {
194
+ const user = typeHandlers(types || {})
195
+ return {
196
+ serializers: Object.assign({}, serializers, user.serializers),
197
+ parsers: Object.assign({}, parsers, user.parsers)
198
+ }
199
+ }
200
+
201
+ function typeHandlers(types) {
202
+ return Object.keys(types).reduce((acc, k) => {
203
+ types[k].from && [].concat(types[k].from).forEach(x => acc.parsers[x] = types[k].parse)
204
+ if (types[k].serialize) {
205
+ acc.serializers[types[k].to] = types[k].serialize
206
+ types[k].from && [].concat(types[k].from).forEach(x => acc.serializers[x] = types[k].serialize)
207
+ }
208
+ return acc
209
+ }, { parsers: {}, serializers: {} })
210
+ }
211
+
212
+ function escapeIdentifiers(xs, { transform: { column } }) {
213
+ return xs.map(x => escapeIdentifier(column.to ? column.to(x) : x)).join(',')
214
+ }
215
+
216
+ export const escapeIdentifier = function escape(str) {
217
+ return '"' + str.replace(/"/g, '""').replace(/\./g, '"."') + '"'
218
+ }
219
+
220
+ export const inferType = function inferType(x) {
221
+ return (
222
+ x instanceof Parameter ? x.type :
223
+ x instanceof Date ? 1184 :
224
+ x instanceof Uint8Array ? 17 :
225
+ (x === true || x === false) ? 16 :
226
+ typeof x === 'bigint' ? 20 :
227
+ Array.isArray(x) ? inferType(x[0]) :
228
+ 0
229
+ )
230
+ }
231
+
232
+ const escapeBackslash = /\\/g
233
+ const escapeQuote = /"/g
234
+
235
+ function arrayEscape(x) {
236
+ return x
237
+ .replace(escapeBackslash, '\\\\')
238
+ .replace(escapeQuote, '\\"')
239
+ }
240
+
241
+ export const arraySerializer = function arraySerializer(xs, serializer, options, typarray) {
242
+ if (Array.isArray(xs) === false)
243
+ return xs
244
+
245
+ if (!xs.length)
246
+ return '{}'
247
+
248
+ const first = xs[0]
249
+ // Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter
250
+ const delimiter = typarray === 1020 ? ';' : ','
251
+
252
+ if (Array.isArray(first) && !first.type)
253
+ return '{' + xs.map(x => arraySerializer(x, serializer, options, typarray)).join(delimiter) + '}'
254
+
255
+ return '{' + xs.map(x => {
256
+ if (x === undefined) {
257
+ x = options.transform.undefined
258
+ if (x === undefined)
259
+ throw Errors.generic('UNDEFINED_VALUE', 'Undefined values are not allowed')
260
+ }
261
+
262
+ return x === null
263
+ ? 'null'
264
+ : '"' + arrayEscape(serializer ? serializer(x.type ? x.value : x) : '' + x) + '"'
265
+ }).join(delimiter) + '}'
266
+ }
267
+
268
+ const arrayParserState = {
269
+ i: 0,
270
+ char: null,
271
+ str: '',
272
+ quoted: false,
273
+ last: 0
274
+ }
275
+
276
+ export const arrayParser = function arrayParser(x, parser, typarray) {
277
+ arrayParserState.i = arrayParserState.last = 0
278
+ return arrayParserLoop(arrayParserState, x, parser, typarray)
279
+ }
280
+
281
+ function arrayParserLoop(s, x, parser, typarray) {
282
+ const xs = []
283
+ // Only _box (1020) has the ';' delimiter for arrays, all other types use the ',' delimiter
284
+ const delimiter = typarray === 1020 ? ';' : ','
285
+ for (; s.i < x.length; s.i++) {
286
+ s.char = x[s.i]
287
+ if (s.quoted) {
288
+ if (s.char === '\\') {
289
+ s.str += x[++s.i]
290
+ } else if (s.char === '"') {
291
+ xs.push(parser ? parser(s.str) : s.str)
292
+ s.str = ''
293
+ s.quoted = x[s.i + 1] === '"'
294
+ s.last = s.i + 2
295
+ } else {
296
+ s.str += s.char
297
+ }
298
+ } else if (s.char === '"') {
299
+ s.quoted = true
300
+ } else if (s.char === '{') {
301
+ s.last = ++s.i
302
+ xs.push(arrayParserLoop(s, x, parser, typarray))
303
+ } else if (s.char === '}') {
304
+ s.quoted = false
305
+ s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))
306
+ s.last = s.i + 1
307
+ break
308
+ } else if (s.char === delimiter && s.p !== '}' && s.p !== '"') {
309
+ xs.push(parser ? parser(x.slice(s.last, s.i)) : x.slice(s.last, s.i))
310
+ s.last = s.i + 1
311
+ }
312
+ s.p = s.char
313
+ }
314
+ s.last < s.i && xs.push(parser ? parser(x.slice(s.last, s.i + 1)) : x.slice(s.last, s.i + 1))
315
+ return xs
316
+ }
317
+
318
+ export const toCamel = x => {
319
+ let str = x[0]
320
+ for (let i = 1; i < x.length; i++)
321
+ str += x[i] === '_' ? x[++i].toUpperCase() : x[i]
322
+ return str
323
+ }
324
+
325
+ export const toPascal = x => {
326
+ let str = x[0].toUpperCase()
327
+ for (let i = 1; i < x.length; i++)
328
+ str += x[i] === '_' ? x[++i].toUpperCase() : x[i]
329
+ return str
330
+ }
331
+
332
+ export const toKebab = x => x.replace(/_/g, '-')
333
+
334
+ export const fromCamel = x => x.replace(/([A-Z])/g, '_$1').toLowerCase()
335
+ export const fromPascal = x => (x.slice(0, 1) + x.slice(1).replace(/([A-Z])/g, '_$1')).toLowerCase()
336
+ export const fromKebab = x => x.replace(/-/g, '_')
337
+
338
+ function createJsonTransform(fn) {
339
+ return function jsonTransform(x, column) {
340
+ return typeof x === 'object' && x !== null && (column.type === 114 || column.type === 3802)
341
+ ? Array.isArray(x)
342
+ ? x.map(x => jsonTransform(x, column))
343
+ : Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: jsonTransform(v, column) }), {})
344
+ : x
345
+ }
346
+ }
347
+
348
+ toCamel.column = { from: toCamel }
349
+ toCamel.value = { from: createJsonTransform(toCamel) }
350
+ fromCamel.column = { to: fromCamel }
351
+
352
+ export const camel = { ...toCamel }
353
+ camel.column.to = fromCamel
354
+
355
+ toPascal.column = { from: toPascal }
356
+ toPascal.value = { from: createJsonTransform(toPascal) }
357
+ fromPascal.column = { to: fromPascal }
358
+
359
+ export const pascal = { ...toPascal }
360
+ pascal.column.to = fromPascal
361
+
362
+ toKebab.column = { from: toKebab }
363
+ toKebab.value = { from: createJsonTransform(toKebab) }
364
+ fromKebab.column = { to: fromKebab }
365
+
366
+ export const kebab = { ...toKebab }
367
+ kebab.column.to = fromKebab