Skip to content

Commit f976a35

Browse files
committed
build
1 parent 4e28de9 commit f976a35

16 files changed

+778
-207
lines changed

cjs/src/bytes.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ const b = Object.assign(reset, messages, {
4747
return b
4848
},
4949
raw(x) {
50-
buffer = Buffer.concat([buffer.slice(0, b.i), x])
50+
buffer = Buffer.concat([buffer.subarray(0, b.i), x])
5151
b.i = buffer.length
5252
return b
5353
},
5454
end(at = 1) {
5555
buffer.writeUInt32BE(b.i - at, at)
56-
const out = buffer.slice(0, b.i)
56+
const out = buffer.subarray(0, b.i)
5757
b.i = 0
5858
buffer = Buffer.allocUnsafe(size)
5959
return out

cjs/src/connection.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -309,12 +309,12 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
309309
}
310310

311311
try {
312-
handle(incoming.slice(0, length + 1))
312+
handle(incoming.subarray(0, length + 1))
313313
} catch (e) {
314314
query && (query.cursorFn || query.describeFirst) && write(Sync)
315315
errored(e)
316316
}
317-
incoming = incoming.slice(length + 1)
317+
incoming = incoming.subarray(length + 1)
318318
remaining = 0
319319
incomings = null
320320
}
@@ -354,7 +354,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
354354
statementCount = 1
355355
lifeTimer.start()
356356
socket.on('data', data)
357-
keep_alive && socket.setKeepAlive(true, 1000 * keep_alive)
357+
keep_alive && socket.setKeepAlive && socket.setKeepAlive(true, 1000 * keep_alive)
358358
const s = StartupMessage()
359359
write(s)
360360
} catch (err) {
@@ -483,7 +483,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
483483
value = length === -1
484484
? null
485485
: query.isRaw === true
486-
? x.slice(index, index += length)
486+
? x.subarray(index, index += length)
487487
: column.parser === undefined
488488
? x.toString('utf8', index, index += length)
489489
: column.parser.array === true
@@ -493,8 +493,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
493493
query.isRaw
494494
? (row[i] = query.isRaw === true
495495
? value
496-
: transform.value.from ? transform.value.from(value) : value)
497-
: (row[column.name] = transform.value.from ? transform.value.from(value) : value)
496+
: transform.value.from ? transform.value.from(value, column) : value)
497+
: (row[column.name] = transform.value.from ? transform.value.from(value, column) : value)
498498
}
499499

500500
query.forEachFn
@@ -656,7 +656,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
656656

657657
async function AuthenticationMD5Password(x) {
658658
write(
659-
b().p().str('md5' + md5(Buffer.concat([Buffer.from(md5((await Pass()) + user)), x.slice(9)]))).z(1).end()
659+
b().p().str('md5' + md5(Buffer.concat([Buffer.from(md5((await Pass()) + user)), x.subarray(9)]))).z(1).end()
660660
)
661661
}
662662

@@ -855,11 +855,11 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
855855
}
856856

857857
function CopyData(x) {
858-
stream.push(x.slice(5)) || socket.pause()
858+
stream && (stream.push(x.subarray(5)) || socket.pause())
859859
}
860860

861861
function CopyDone() {
862-
stream.push(null)
862+
stream && stream.push(null)
863863
stream = null
864864
}
865865

cjs/src/index.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ const {
88
Identifier,
99
Builder,
1010
toPascal,
11+
pascal,
1112
toCamel,
13+
camel,
1214
toKebab,
15+
kebab,
1316
fromPascal,
1417
fromCamel,
1518
fromKebab
@@ -25,8 +28,11 @@ const largeObject = require('./large.js')
2528
Object.assign(Postgres, {
2629
PostgresError,
2730
toPascal,
31+
pascal,
2832
toCamel,
33+
camel,
2934
toKebab,
35+
kebab,
3036
fromPascal,
3137
fromCamel,
3238
fromKebab,
@@ -162,25 +168,25 @@ function Postgres(a, b) {
162168

163169
const channels = listen.channels || (listen.channels = {})
164170
, exists = name in channels
165-
, channel = exists ? channels[name] : (channels[name] = { listeners: [listener] })
166171

167172
if (exists) {
168-
channel.listeners.push(listener)
173+
channels[name].listeners.push(listener)
169174
listener.onlisten && listener.onlisten()
170-
return Promise.resolve({ ...channel.result, unlisten })
175+
return Promise.resolve({ ...channels[name].result, unlisten })
171176
}
172177

173-
channel.result = await sql`listen ${ sql(name) }`
178+
const result = await sql`listen ${ sql(name) }`
179+
channels[name] = { result, listeners: [listener] }
174180
listener.onlisten && listener.onlisten()
175-
channel.result.unlisten = unlisten
181+
result.unlisten = unlisten
176182

177-
return channel.result
183+
return result
178184

179185
async function unlisten() {
180186
if (name in channels === false)
181187
return
182188

183-
channel.listeners = channel.listeners.filter(x => x !== listener)
189+
channels[name].listeners = channels[name].listeners.filter(x => x !== listener)
184190
if (channels[name].listeners.length)
185191
return
186192

cjs/src/query.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,20 @@ const Query = module.exports.Query = class Query extends Promise {
5454
return this.canceller && (this.canceller(this), this.canceller = null)
5555
}
5656

57-
async readable() {
57+
simple() {
5858
this.options.simple = true
5959
this.options.prepare = false
60+
return this
61+
}
62+
63+
async readable() {
64+
this.simple()
6065
this.streaming = true
6166
return this
6267
}
6368

6469
async writable() {
65-
this.options.simple = true
66-
this.options.prepare = false
70+
this.simple()
6771
this.streaming = true
6872
return this
6973
}
@@ -108,7 +112,8 @@ const Query = module.exports.Query = class Query extends Promise {
108112
}
109113

110114
describe() {
111-
this.onlyDescribe = true
115+
this.options.simple = false
116+
this.onlyDescribe = this.options.prepare = true
112117
return this
113118
}
114119

cjs/src/subscribe.js

+32-23
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = Subscribe;function Subscribe(postgres, options) {
1111

1212
const sql = subscribe.sql = postgres({
1313
...options,
14+
transform: { column: {}, value: {}, row: {} },
1415
max: 1,
1516
fetch_types: false,
1617
idle_timeout: null,
@@ -103,7 +104,7 @@ module.exports = Subscribe;function Subscribe(postgres, options) {
103104

104105
function data(x) {
105106
if (x[0] === 0x77)
106-
parse(x.slice(25), state, sql.options.parsers, handle)
107+
parse(x.subarray(25), state, sql.options.parsers, handle, options.transform)
107108
else if (x[0] === 0x6b && x[17])
108109
pong()
109110
}
@@ -136,15 +137,15 @@ function Time(x) {
136137
return new Date(Date.UTC(2000, 0, 1) + Number(x / BigInt(1000)))
137138
}
138139

139-
function parse(x, state, parsers, handle) {
140+
function parse(x, state, parsers, handle, transform) {
140141
const char = (acc, [k, v]) => (acc[k.charCodeAt(0)] = v, acc)
141142

142143
Object.entries({
143144
R: x => { // Relation
144145
let i = 1
145146
const r = state[x.readUInt32BE(i)] = {
146-
schema: String(x.slice(i += 4, i = x.indexOf(0, i))) || 'pg_catalog',
147-
table: String(x.slice(i + 1, i = x.indexOf(0, i + 1))),
147+
schema: x.toString('utf8', i += 4, i = x.indexOf(0, i)) || 'pg_catalog',
148+
table: x.toString('utf8', i + 1, i = x.indexOf(0, i + 1)),
148149
columns: Array(x.readUInt16BE(i += 2)),
149150
keys: []
150151
}
@@ -156,7 +157,9 @@ function parse(x, state, parsers, handle) {
156157
while (i < x.length) {
157158
column = r.columns[columnIndex++] = {
158159
key: x[i++],
159-
name: String(x.slice(i, i = x.indexOf(0, i))),
160+
name: transform.column.from
161+
? transform.column.from(x.toString('utf8', i, i = x.indexOf(0, i)))
162+
: x.toString('utf8', i, i = x.indexOf(0, i)),
160163
type: x.readUInt32BE(i += 1),
161164
parser: parsers[x.readUInt32BE(i)],
162165
atttypmod: x.readUInt32BE(i += 4)
@@ -170,13 +173,12 @@ function parse(x, state, parsers, handle) {
170173
O: () => { /* noop */ }, // Origin
171174
B: x => { // Begin
172175
state.date = Time(x.readBigInt64BE(9))
173-
state.lsn = x.slice(1, 9)
176+
state.lsn = x.subarray(1, 9)
174177
},
175178
I: x => { // Insert
176179
let i = 1
177180
const relation = state[x.readUInt32BE(i)]
178-
const row = {}
179-
tuples(x, row, relation.columns, i += 7)
181+
const { row } = tuples(x, relation.columns, i += 7, transform)
180182

181183
handle(row, {
182184
command: 'insert',
@@ -188,13 +190,10 @@ function parse(x, state, parsers, handle) {
188190
const relation = state[x.readUInt32BE(i)]
189191
i += 4
190192
const key = x[i] === 75
191-
const row = key || x[i] === 79
192-
? {}
193+
handle(key || x[i] === 79
194+
? tuples(x, key ? relation.keys : relation.columns, i += 3, transform).row
193195
: null
194-
195-
tuples(x, row, key ? relation.keys : relation.columns, i += 3)
196-
197-
handle(row, {
196+
, {
198197
command: 'delete',
199198
relation,
200199
key
@@ -205,35 +204,36 @@ function parse(x, state, parsers, handle) {
205204
const relation = state[x.readUInt32BE(i)]
206205
i += 4
207206
const key = x[i] === 75
208-
const old = key || x[i] === 79
209-
? {}
207+
const xs = key || x[i] === 79
208+
? tuples(x, key ? relation.keys : relation.columns, i += 3, transform)
210209
: null
211210

212-
old && (i = tuples(x, old, key ? relation.keys : relation.columns, i += 3))
211+
xs && (i = xs.i)
213212

214-
const row = {}
215-
tuples(x, row, relation.columns, i + 3)
213+
const { row } = tuples(x, relation.columns, i + 3, transform)
216214

217215
handle(row, {
218216
command: 'update',
219217
relation,
220218
key,
221-
old
219+
old: xs && xs.row
222220
})
223221
},
224222
T: () => { /* noop */ }, // Truncate,
225223
C: () => { /* noop */ } // Commit
226224
}).reduce(char, {})[x[0]](x)
227225
}
228226

229-
function tuples(x, row, columns, xi) {
227+
function tuples(x, columns, xi, transform) {
230228
let type
231229
, column
230+
, value
232231

232+
const row = transform.raw ? new Array(columns.length) : {}
233233
for (let i = 0; i < columns.length; i++) {
234234
type = x[xi++]
235235
column = columns[i]
236-
row[column.name] = type === 110 // n
236+
value = type === 110 // n
237237
? null
238238
: type === 117 // u
239239
? undefined
@@ -242,9 +242,18 @@ function tuples(x, row, columns, xi) {
242242
: column.parser.array === true
243243
? column.parser(x.toString('utf8', xi + 5, xi += 4 + x.readUInt32BE(xi)))
244244
: column.parser(x.toString('utf8', xi + 4, xi += 4 + x.readUInt32BE(xi)))
245+
246+
transform.raw
247+
? (row[i] = transform.raw === true
248+
? value
249+
: transform.value.from ? transform.value.from(value, column) : value)
250+
: (row[column.name] = transform.value.from
251+
? transform.value.from(value, column)
252+
: value
253+
)
245254
}
246255

247-
return xi
256+
return { i: xi, row: transform.row.from ? transform.row.from(row) : row }
248257
}
249258

250259
function parseEvent(x) {

cjs/src/types.js

+35-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ function select(first, rest, parameters, types, options) {
153153

154154
const builders = Object.entries({
155155
values,
156-
in: values,
156+
in: (...xs) => {
157+
const x = values(...xs)
158+
return x === '()' ? '(null)' : x
159+
},
157160
select,
158161
as: select,
159162
returning: select,
@@ -323,3 +326,34 @@ const toKebab = module.exports.toKebab = x => x.replace(/_/g, '-')
323326
const fromCamel = module.exports.fromCamel = x => x.replace(/([A-Z])/g, '_$1').toLowerCase()
324327
const fromPascal = module.exports.fromPascal = x => (x.slice(0, 1) + x.slice(1).replace(/([A-Z])/g, '_$1')).toLowerCase()
325328
const fromKebab = module.exports.fromKebab = x => x.replace(/-/g, '_')
329+
330+
function createJsonTransform(fn) {
331+
return function jsonTransform(x, column) {
332+
return column.type === 114 || column.type === 3802
333+
? Array.isArray(x)
334+
? x.map(jsonTransform)
335+
: Object.entries(x).reduce((acc, [k, v]) => Object.assign(acc, { [fn(k)]: v }), {})
336+
: x
337+
}
338+
}
339+
340+
toCamel.column = { from: toCamel }
341+
toCamel.value = { from: createJsonTransform(toCamel) }
342+
fromCamel.column = { to: fromCamel }
343+
344+
const camel = module.exports.camel = { ...toCamel }
345+
camel.column.to = fromCamel;
346+
347+
toPascal.column = { from: toPascal }
348+
toPascal.value = { from: createJsonTransform(toPascal) }
349+
fromPascal.column = { to: fromPascal }
350+
351+
const pascal = module.exports.pascal = { ...toPascal }
352+
pascal.column.to = fromPascal
353+
354+
toKebab.column = { from: toKebab }
355+
toKebab.value = { from: createJsonTransform(toKebab) }
356+
fromKebab.column = { to: fromKebab }
357+
358+
const kebab = module.exports.kebab = { ...toKebab }
359+
kebab.column.to = fromKebab

0 commit comments

Comments
 (0)