doix-db 1.0.77 → 1.0.79
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/lib/DbCsvPrinter.js +47 -31
- package/lib/DbPool.js +1 -3
- package/package.json +2 -2
package/lib/DbCsvPrinter.js
CHANGED
|
@@ -2,6 +2,7 @@ const DbColumn = require ('./model/DbColumn.js')
|
|
|
2
2
|
const DbTypeArithmetic = require ('./model/types/DbTypeArithmetic.js')
|
|
3
3
|
const DbTypeArithmeticInt = require ('./model/types/DbTypeArithmeticInt.js')
|
|
4
4
|
const DbTypeArithmeticFixed = require ('./model/types/DbTypeArithmeticFixed.js')
|
|
5
|
+
const DbTypeArithmeticFloat = require ('./model/types/DbTypeArithmeticFloat.js')
|
|
5
6
|
const DbTypeCharacter = require ('./model/types/DbTypeCharacter.js')
|
|
6
7
|
const DbTypeTemporal = require ('./model/types/DbTypeTemporal.js')
|
|
7
8
|
const DbTypeDate = require ('./model/types/DbTypeDate.js')
|
|
@@ -26,15 +27,24 @@ const FMT_ISO_TIMESTAMP = new Intl.DateTimeFormat ('sv', {
|
|
|
26
27
|
fractionalSecondDigits: 3
|
|
27
28
|
})
|
|
28
29
|
|
|
30
|
+
const DEFAULT = {
|
|
31
|
+
NULL : '',
|
|
32
|
+
NaN : 'NaN',
|
|
33
|
+
TRUE : '1',
|
|
34
|
+
FALSE : '0',
|
|
35
|
+
POSITIVE_INFINITY : 'inf',
|
|
36
|
+
NEGATIVE_INFINITY : '-inf',
|
|
37
|
+
}
|
|
38
|
+
|
|
29
39
|
class DbCsvPrinter extends Transform {
|
|
30
40
|
|
|
31
41
|
constructor (options = {}) {
|
|
32
42
|
|
|
33
|
-
let {table, columns, lang
|
|
43
|
+
let {table, columns, lang} = options
|
|
34
44
|
|
|
35
45
|
super ({writableObjectMode: true})
|
|
36
46
|
|
|
37
|
-
|
|
47
|
+
for (const k in DEFAULT) this [k] = options [k] ?? DEFAULT [k]
|
|
38
48
|
|
|
39
49
|
if (!columns) {
|
|
40
50
|
if (!table) throw Error (`Either table or columns must be defined`)
|
|
@@ -105,56 +115,62 @@ class DbCsvPrinter extends Transform {
|
|
|
105
115
|
|
|
106
116
|
const value = r [name], v = value == null ? column.default : value
|
|
107
117
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
118
|
+
if (Number.isNaN (v)) { // `Number.NaN` doesn't work as a `switch` `case`
|
|
119
|
+
|
|
120
|
+
if (!(typeDef instanceof DbTypeArithmeticFloat)) throw Error (`NaN values are only supported for floating point numeric types, but ${column.name} is defined as ${column.type}`)
|
|
121
|
+
|
|
122
|
+
s += this.NaN
|
|
123
|
+
|
|
124
|
+
continue
|
|
125
|
+
|
|
112
126
|
}
|
|
113
127
|
|
|
114
|
-
if (v == null) {
|
|
128
|
+
if (v == null) { // here, `undefined` fits too
|
|
115
129
|
|
|
116
130
|
if (!column.nullable) throw Error (`${name} doesn't allow null values`)
|
|
117
131
|
|
|
118
132
|
s += this.NULL
|
|
119
133
|
|
|
120
|
-
|
|
121
|
-
else if (typeof v === 'boolean') {
|
|
122
|
-
|
|
123
|
-
s += v ? '1' : '0'
|
|
134
|
+
continue
|
|
124
135
|
|
|
125
136
|
}
|
|
126
|
-
else if (typeDef instanceof DbTypeCharacter) {
|
|
127
137
|
|
|
128
|
-
|
|
129
|
-
s += QQ_ESC.escape (String (v))
|
|
130
|
-
s += CH_QQ
|
|
138
|
+
switch (v) {
|
|
131
139
|
|
|
132
|
-
|
|
133
|
-
|
|
140
|
+
case true:
|
|
141
|
+
s += this.TRUE
|
|
142
|
+
continue
|
|
134
143
|
|
|
135
|
-
|
|
144
|
+
case false:
|
|
145
|
+
s += this.FALSE
|
|
146
|
+
continue
|
|
136
147
|
|
|
137
|
-
|
|
148
|
+
case Number.POSITIVE_INFINITY:
|
|
149
|
+
if (!(typeDef instanceof DbTypeArithmeticFloat)) throw Error (`Infinity values are only supported for floating point numeric types, but ${column.name} is defined as ${column.type}`)
|
|
150
|
+
s += this.POSITIVE_INFINITY
|
|
151
|
+
continue
|
|
138
152
|
|
|
139
|
-
|
|
153
|
+
case Number.NEGATIVE_INFINITY:
|
|
154
|
+
if (!(typeDef instanceof DbTypeArithmeticFloat)) throw Error (`-Infinity values are only supported for floating point numeric types, but ${column.name} is defined as ${column.type}`)
|
|
155
|
+
s += this.NEGATIVE_INFINITY
|
|
156
|
+
continue
|
|
140
157
|
|
|
141
|
-
|
|
158
|
+
}
|
|
142
159
|
|
|
143
|
-
|
|
144
|
-
else if (typeDef instanceof DbTypeArithmeticFixed) {
|
|
160
|
+
if (typeDef instanceof DbTypeCharacter) {
|
|
145
161
|
|
|
146
|
-
|
|
162
|
+
s += CH_QQ
|
|
163
|
+
s += QQ_ESC.escape (String (v))
|
|
164
|
+
s += CH_QQ
|
|
147
165
|
|
|
148
|
-
|
|
166
|
+
}
|
|
167
|
+
else if (typeDef instanceof DbTypeArithmetic) {
|
|
149
168
|
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
169
|
+
const n = Number (v); if (Number.isNaN (n)) throw Error (`${column.name} must be a number, found a ${typeof value} '${value}'`)
|
|
152
170
|
|
|
153
|
-
|
|
171
|
+
if (typeDef instanceof DbTypeArithmeticInt && !Number.isInteger (Number (v))) throw Error (`${column.name} must be integer, found ${value}`)
|
|
154
172
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
173
|
+
s += typeDef instanceof DbTypeArithmeticFixed ? n.toFixed (column.scale) : n
|
|
158
174
|
|
|
159
175
|
}
|
|
160
176
|
else if (typeDef instanceof DbTypeTemporal) {
|
package/lib/DbPool.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "doix-db",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.79",
|
|
4
4
|
"description": "Shared database related code for doix",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
},
|
|
41
41
|
"homepage": "https://github.com/do-/node-doix-db#readme",
|
|
42
42
|
"peerDependencies": {
|
|
43
|
-
"doix": "^1.0.
|
|
43
|
+
"doix": "^1.0.61"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"jest": "^29.6.1"
|