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.
@@ -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, NULL} = options
43
+ let {table, columns, lang} = options
34
44
 
35
45
  super ({writableObjectMode: true})
36
46
 
37
- this.NULL = typeof NULL === 'string' ? NULL : ''
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
- switch (v) {
109
- case Number.POSITIVE_INFINITY:
110
- case Number.NEGATIVE_INFINITY:
111
- throw Error (`${column.name}: infinite values not supported`)
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
- s += CH_QQ
129
- s += QQ_ESC.escape (String (v))
130
- s += CH_QQ
138
+ switch (v) {
131
139
 
132
- }
133
- else if (typeDef instanceof DbTypeArithmetic) {
140
+ case true:
141
+ s += this.TRUE
142
+ continue
134
143
 
135
- const n = Number (v)
144
+ case false:
145
+ s += this.FALSE
146
+ continue
136
147
 
137
- if (typeDef instanceof DbTypeArithmeticInt) {
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
- if (!Number.isInteger (Number (v))) throw Error (`${column.name} must be integer, found ${value}`)
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
- s += v
158
+ }
142
159
 
143
- }
144
- else if (typeDef instanceof DbTypeArithmeticFixed) {
160
+ if (typeDef instanceof DbTypeCharacter) {
145
161
 
146
- if (!Number.isFinite (n)) throw Error (`${column.name} must be a finite number, found ${value}`)
162
+ s += CH_QQ
163
+ s += QQ_ESC.escape (String (v))
164
+ s += CH_QQ
147
165
 
148
- s += n.toFixed (column.scale)
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
- if (Number.isNaN (n)) throw Error (`${column.name} must be a number, found ${value}`)
171
+ if (typeDef instanceof DbTypeArithmeticInt && !Number.isInteger (Number (v))) throw Error (`${column.name} must be integer, found ${value}`)
154
172
 
155
- s += v
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
@@ -4,12 +4,10 @@ class DbPool extends ResourcePool {
4
4
 
5
5
  constructor (o) {
6
6
 
7
- super ()
7
+ super (o)
8
8
 
9
9
  this.shared.add ('model')
10
10
  this.shared.add ('lang')
11
-
12
- this.logger = o.logger
13
11
 
14
12
  }
15
13
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doix-db",
3
- "version": "1.0.77",
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.57"
43
+ "doix": "^1.0.61"
44
44
  },
45
45
  "devDependencies": {
46
46
  "jest": "^29.6.1"