neopg 0.0.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/index.js +6 -0
- package/lib/ModelChain.js +364 -0
- package/lib/ModelDef.js +204 -0
- package/lib/NeoPG.js +82 -0
- package/lib/SchemaSync.js +506 -0
- package/lib/TransactionScope.js +29 -0
- package/lib/dataTypes.js +60 -0
- package/lib/forbidColumns.js +29 -0
- package/lib/makeId.js +202 -0
- package/lib/makeTimestamp.js +28 -0
- package/lib/randstring.js +23 -0
- package/package.json +28 -0
- package/postgres/bytes.js +78 -0
- package/postgres/connection.js +1042 -0
- package/postgres/errors.js +53 -0
- package/postgres/index.js +566 -0
- package/postgres/large.js +70 -0
- package/postgres/query.js +173 -0
- package/postgres/queue.js +31 -0
- package/postgres/result.js +16 -0
- package/postgres/subscribe.js +277 -0
- package/postgres/types.js +367 -0
- package/test/test-db.js +44 -0
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
const { Query } = require('./query.js')
|
|
2
|
+
const { Errors } = require('./errors.js')
|
|
3
|
+
|
|
4
|
+
const types = module.exports.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
|
+
const Identifier = module.exports.Identifier = class Identifier extends NotTagged {
|
|
45
|
+
constructor(value) {
|
|
46
|
+
super()
|
|
47
|
+
this.value = escapeIdentifier(value)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const Parameter = module.exports.Parameter = 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
|
+
const Builder = module.exports.Builder = 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
|
+
module.exports.handleValue = handleValue;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
|
+
module.exports.stringify = stringify;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
|
+
const serializers = module.exports.serializers = defaultHandlers.serializers
|
|
183
|
+
const parsers = module.exports.parsers = defaultHandlers.parsers
|
|
184
|
+
|
|
185
|
+
const END = module.exports.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
|
+
const mergeUserTypes = module.exports.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
|
+
const escapeIdentifier = module.exports.escapeIdentifier = function escape(str) {
|
|
217
|
+
return '"' + str.replace(/"/g, '""').replace(/\./g, '"."') + '"'
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const inferType = module.exports.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
|
+
const arraySerializer = module.exports.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
|
+
const arrayParser = module.exports.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
|
+
const toCamel = module.exports.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
|
+
const toPascal = module.exports.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
|
+
const toKebab = module.exports.toKebab = x => x.replace(/_/g, '-')
|
|
333
|
+
|
|
334
|
+
const fromCamel = module.exports.fromCamel = x => x.replace(/([A-Z])/g, '_$1').toLowerCase()
|
|
335
|
+
const fromPascal = module.exports.fromPascal = x => (x.slice(0, 1) + x.slice(1).replace(/([A-Z])/g, '_$1')).toLowerCase()
|
|
336
|
+
const fromKebab = module.exports.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
|
+
const camel = module.exports.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
|
+
const pascal = module.exports.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
|
+
const kebab = module.exports.kebab = { ...toKebab }
|
|
367
|
+
kebab.column.to = fromKebab
|
package/test/test-db.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// index.js
|
|
2
|
+
const NeoPG = require('../lib/NeoPG.js')
|
|
3
|
+
const ModelChain = NeoPG.ModelChain
|
|
4
|
+
|
|
5
|
+
// 1. 初始化
|
|
6
|
+
const db = new NeoPG({
|
|
7
|
+
host: '127.0.0.1',
|
|
8
|
+
database: 'eoms',
|
|
9
|
+
schema: 'ai' // 全局默认 Schema
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
// 2. 定义模型
|
|
13
|
+
const UserSchema = {
|
|
14
|
+
modelName: 'User',
|
|
15
|
+
tableName: 'sys_users',
|
|
16
|
+
column: {
|
|
17
|
+
id: { type: 'varchar', primaryKey: true },
|
|
18
|
+
name: { type: 'varchar', notNull: true },
|
|
19
|
+
age: { type: 'int', default: 18 }
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// 3. 注册
|
|
24
|
+
db.add(UserSchema);
|
|
25
|
+
|
|
26
|
+
;(async () => {
|
|
27
|
+
// 插入
|
|
28
|
+
await db.model('User').insert({ name: 'Neo' });
|
|
29
|
+
|
|
30
|
+
// 动态切换 Schema
|
|
31
|
+
await db.model('User', 'tenant_a').select();
|
|
32
|
+
|
|
33
|
+
// 链式切换
|
|
34
|
+
await db.model('User').schema('tenant_b').where({ age: 20 }).select();
|
|
35
|
+
|
|
36
|
+
// 事务
|
|
37
|
+
await db.transaction(async tx => {
|
|
38
|
+
await tx.model('User').update({ age: 99 });
|
|
39
|
+
// 嵌套
|
|
40
|
+
await tx.transaction(async subTx => {
|
|
41
|
+
await subTx.table('logs').insert({ msg: 'log' });
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
})();
|