@oino-ts/db 0.21.2 → 1.0.1

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.
Files changed (39) hide show
  1. package/dist/cjs/OINODb.js +6 -144
  2. package/dist/cjs/OINODbApi.js +50 -318
  3. package/dist/cjs/OINODbConfig.js +10 -10
  4. package/dist/cjs/OINODbConstants.js +10 -0
  5. package/dist/cjs/OINODbDataField.js +28 -70
  6. package/dist/cjs/OINODbDataModel.js +30 -144
  7. package/dist/cjs/OINODbFactory.js +2 -2
  8. package/dist/cjs/OINODbModelSet.js +23 -23
  9. package/dist/cjs/OINODbQueryParams.js +201 -0
  10. package/dist/cjs/index.js +12 -41
  11. package/dist/esm/OINODb.js +6 -142
  12. package/dist/esm/OINODbApi.js +49 -314
  13. package/dist/esm/OINODbConstants.js +7 -0
  14. package/dist/esm/OINODbDataModel.js +31 -145
  15. package/dist/esm/OINODbFactory.js +1 -1
  16. package/dist/esm/OINODbQueryParams.js +194 -0
  17. package/dist/esm/index.js +4 -14
  18. package/dist/types/OINODb.d.ts +6 -173
  19. package/dist/types/OINODbApi.d.ts +18 -104
  20. package/dist/types/OINODbConstants.d.ts +23 -0
  21. package/dist/types/OINODbDataModel.d.ts +7 -61
  22. package/dist/types/OINODbFactory.d.ts +5 -2
  23. package/dist/types/OINODbQueryParams.d.ts +72 -0
  24. package/dist/types/index.d.ts +4 -108
  25. package/package.json +37 -37
  26. package/src/OINODb.ts +99 -348
  27. package/src/OINODbApi.test.ts +507 -498
  28. package/src/OINODbApi.ts +389 -667
  29. package/src/OINODbConstants.ts +32 -0
  30. package/src/OINODbDataModel.ts +191 -307
  31. package/src/OINODbFactory.ts +73 -68
  32. package/src/OINODbQueryParams.ts +203 -0
  33. package/src/index.ts +6 -118
  34. package/src/OINODbConfig.ts +0 -98
  35. package/src/OINODbDataField.ts +0 -405
  36. package/src/OINODbModelSet.ts +0 -353
  37. package/src/OINODbParser.ts +0 -438
  38. package/src/OINODbSqlParams.ts +0 -593
  39. package/src/OINODbSwagger.ts +0 -209
@@ -1,405 +0,0 @@
1
- /*
2
- * This Source Code Form is subject to the terms of the Mozilla Public
3
- * License, v. 2.0. If a copy of the MPL was not distributed with this
4
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
- */
6
-
7
- import { Buffer } from "buffer"
8
- import { OINO_ERROR_PREFIX, OINOLog } from "@oino-ts/common"
9
- import { OINODbDataFieldParams, OINODataCell, OINODb } from "./index.js";
10
-
11
- /**
12
- * Base class for a column of data responsible for appropriatelly serializing/deserializing the data.
13
- *
14
- */
15
- export class OINODbDataField {
16
-
17
- /** OINODB reference*/
18
- readonly db:OINODb;
19
-
20
- /** Name of the field */
21
- readonly name: string;
22
-
23
- /** Internal type of field*/
24
- readonly type: string;
25
-
26
- /** SQL type of the field */
27
- readonly sqlType: string;
28
-
29
- /** Maximum length of the field (or 0) */
30
- readonly maxLength: number;
31
-
32
- /** Parameters for the field */
33
- readonly fieldParams: OINODbDataFieldParams;
34
-
35
- /**
36
- * Constructor for a data field
37
- *
38
- * @param db OINODb reference
39
- * @param name name of the field
40
- * @param type internal type of the field
41
- * @param sqlType column type in database
42
- * @param fieldParams parameters of the field
43
- * @param maxLength maximum length of the field (or 0)
44
- *
45
- */
46
- constructor(db:OINODb, name: string, type:string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength:number = 0) {
47
- this.db = db
48
- this.name = name
49
- this.type = type
50
- this.maxLength = maxLength
51
- this.sqlType = sqlType
52
- this.fieldParams = fieldParams
53
- }
54
-
55
- /**
56
- * Pring debug information for the field
57
- *
58
- * @param length length of the debug output (or 0 for as long as needed)
59
- *
60
- */
61
- printColumnDebug(length:number = 0): string {
62
- let params: string = "";
63
- if (this.fieldParams.isPrimaryKey) {
64
- params += "PK ";
65
- }
66
- if (this.fieldParams.isForeignKey) {
67
- params += "FK ";
68
- }
69
- if (this.fieldParams.isAutoInc) {
70
- params += "AUTOINC ";
71
- }
72
- if (this.fieldParams.isNotNull) {
73
- params += "NOTNUL ";
74
- }
75
- if (params != "") {
76
- params = "{" + params.trim() + "}";
77
- }
78
- if (this.maxLength > 0) {
79
- params = this.sqlType + "(" + this.maxLength + ")" + params
80
- } else {
81
- params = this.sqlType + params
82
- }
83
- const name_length:number = length - 2 - 1 - params.length
84
- let result:string = this.name
85
- if (length > 0) {
86
- if (result.length > name_length) {
87
- result = result.substring(0, name_length-2)+".."
88
- }
89
- result = (result + ":" + params).padEnd(length-2, " ")
90
- } else {
91
- result = this.type + ":" + result + ":" + params
92
- }
93
- return "[" + result + "]";
94
- }
95
-
96
- /**
97
- * Serialize cell value in the given content format.
98
- *
99
- * @param cellVal cell value
100
- *
101
- */
102
- serializeCell(cellVal: OINODataCell):string|null|undefined {
103
- cellVal = this.db.parseSqlValueAsCell(cellVal, this.sqlType)
104
- if ((cellVal === null) || (cellVal === undefined)) {
105
- return cellVal // let content type encoder worry what to do with the value (so not force it to string)
106
- } else {
107
- return cellVal.toString()
108
- }
109
- }
110
-
111
- /**
112
- * Parce cell value from string using field type specific formatting rules.
113
- *
114
- * @param value string value
115
- *
116
- */
117
- deserializeCell(value: string|null|undefined): OINODataCell {
118
- return value
119
- }
120
-
121
- /**
122
- * Print data cell (from deserialization) as SQL-string.
123
- *
124
- * @param cellVal cell value
125
- *
126
- */
127
- printCellAsSqlValue(cellVal: OINODataCell):string {
128
- return this.db.printCellAsSqlValue(cellVal, this.sqlType);
129
- }
130
-
131
- /**
132
- * Print name of column as SQL.
133
- *
134
- */
135
- printSqlColumnName():string {
136
- return this.db.printSqlColumnname(this.name)
137
- }
138
- }
139
-
140
- /**
141
- * Specialised class for a string column.
142
- *
143
- */
144
- export class OINOStringDataField extends OINODbDataField {
145
-
146
- /**
147
- * Constructor for a string data field
148
- *
149
- * @param db OINODb reference
150
- * @param name name of the field
151
- * @param sqlType column type in database
152
- * @param fieldParams parameters of the field
153
- * @param maxLength maximum length of the field (or 0)
154
- *
155
- */
156
- constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength: number) {
157
- super(db, name, "string", sqlType, fieldParams, maxLength)
158
- }
159
-
160
- }
161
-
162
- /**
163
- * Specialised class for a boolean column.
164
- *
165
- */
166
- export class OINOBooleanDataField extends OINODbDataField {
167
-
168
- /**
169
- * Constructor for a boolean data field
170
- *
171
- * @param db OINODb reference
172
- * @param name name of the field
173
- * @param sqlType column type in database
174
- * @param fieldParams parameters of the field
175
- *
176
- */
177
- constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams) {
178
- super(db, name, "boolean", sqlType, fieldParams)
179
- }
180
- /**
181
- * Serialize cell value in the given content format.
182
- *
183
- * @param cellVal cell value
184
- *
185
- */
186
- serializeCell(cellVal: OINODataCell):string|null|undefined {
187
- const parsed_value:string = (this.db.parseSqlValueAsCell(cellVal, this.sqlType) || "").toString()
188
- let result:string
189
- // console.log("OINOBooleanDataField.serializeCell: parsed_value=" + parsed_value)
190
- if ((parsed_value == "") || (parsed_value.toLowerCase() == "false") || (parsed_value.match(/^0+$/))) {
191
- result = "false"
192
- } else {
193
- result = "true"
194
- }
195
- return result
196
- }
197
-
198
- /**
199
- * Parce cell value from string using field type specific formatting rules.
200
- *
201
- * @param value string value
202
- *
203
- */
204
- deserializeCell(value: string|null|undefined): OINODataCell {
205
- if (value == null || value == "" || value.toString().toLowerCase() == "false" || value == "0") { // TODO: testaa poistaa .toString()
206
- return false
207
- } else {
208
- return true
209
- }
210
- }
211
- }
212
-
213
- /**
214
- * Specialised class for a number column.
215
- *
216
- */
217
- export class OINONumberDataField extends OINODbDataField {
218
-
219
- /**
220
- * Constructor for a string data field
221
- *
222
- * @param db OINODb reference
223
- * @param name name of the field
224
- * @param sqlType column type in database
225
- * @param fieldParams parameters of the field
226
- *
227
- */
228
- constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams) {
229
- super(db, name, "number", sqlType, fieldParams)
230
- }
231
-
232
- /**
233
- * Serialize cell value in the given content format.
234
- *
235
- * @param cellVal cell value
236
- *
237
- */
238
- serializeCell(cellVal: OINODataCell):string|null|undefined {
239
- let result:string|null
240
- if ((cellVal === null) || (cellVal === undefined) || (cellVal === "")) {
241
- result = null
242
- } else {
243
- result = cellVal.toString()
244
- }
245
- return result
246
- }
247
-
248
- /**
249
- * Parce cell value from string using field type specific formatting rules.
250
- *
251
- * @param value string value
252
- *
253
- */
254
- deserializeCell(value: string|null|undefined): OINODataCell {
255
- if (value === undefined) {
256
- return undefined
257
- } else if ((value === "") || (value === null)) {
258
- return null
259
- } else {
260
- const result:number = parseFloat(value)
261
- if (isNaN(result)) {
262
- OINOLog.error("@oino-ts/db", "OINONumberDataField", "toSql", "Invalid value!", {value:value})
263
- throw new Error(OINO_ERROR_PREFIX + ": OINONumberDataField.deserializeCell - Invalid value '" + value + "'") // incorrectly formatted data could be a security risk, abort processing
264
- }
265
- return result
266
- }
267
- }
268
- }
269
-
270
- /**
271
- * Specialised class for a blob column.
272
- *
273
- */
274
- export class OINOBlobDataField extends OINODbDataField {
275
-
276
- /**
277
- * Constructor for a blob data field
278
- *
279
- * @param db OINODb reference
280
- * @param name name of the field
281
- * @param sqlType column type in database
282
- * @param fieldParams parameters of the field
283
- * @param maxLength maximum length of the field (or 0)
284
- *
285
- */
286
- constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength:number) {
287
- super(db, name, "blob", sqlType, fieldParams, maxLength)
288
- }
289
-
290
- /**
291
- * Serialize cell value in the given content format.
292
- *
293
- * @param cellVal cell value
294
- *
295
- */
296
- serializeCell(cellVal: OINODataCell):string|null|undefined {
297
- // console.log("OINOBlobDataField.serializeCell: cellVal", cellVal, typeof(cellVal))
298
- if ((cellVal === null) || (cellVal === undefined)) {
299
- return cellVal
300
-
301
- } else if (cellVal instanceof Buffer) {
302
- return cellVal.toString('base64')
303
-
304
- } else if (cellVal instanceof Uint8Array) {
305
- return Buffer.from(cellVal).toString('base64')
306
-
307
- } else {
308
- return this.db.parseSqlValueAsCell(cellVal, this.sqlType)?.toString()
309
- }
310
- }
311
-
312
- /**
313
- * Parce cell value from string using field type specific formatting rules.
314
- *
315
- * @param value string value
316
- *
317
- */
318
- deserializeCell(value: string|null|undefined): OINODataCell {
319
- if (value == null) {
320
- return Buffer.alloc(0)
321
-
322
- } else {
323
- return Buffer.from(value, 'base64') // Blob-field data is base64 encoded and converted internally to UInt8Array / Buffer
324
- }
325
- }
326
-
327
- }
328
-
329
- /**
330
- * Specialised class for a datetime column.
331
- *
332
- */
333
- export class OINODatetimeDataField extends OINODbDataField {
334
-
335
- /**
336
- * Constructor for a string data field
337
- *
338
- * @param db OINODb reference
339
- * @param name name of the field
340
- * @param sqlType column type in database
341
- * @param fieldParams parameters of the field
342
- *
343
- */
344
- constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams) {
345
- super(db, name, "datetime", sqlType, fieldParams)
346
- }
347
-
348
- /**
349
- * Serialize cell value in the given content format.
350
- *
351
- * @param cellVal cell value
352
- *
353
- */
354
- serializeCell(cellVal: OINODataCell): string|null|undefined {
355
- if (typeof(cellVal) == "string") {
356
- cellVal = this.db.parseSqlValueAsCell(cellVal, this.sqlType)
357
- }
358
- if ((cellVal === null) || (cellVal === undefined)) {
359
- return cellVal
360
-
361
- } else if (cellVal instanceof Date) {
362
- return cellVal.toISOString()
363
-
364
- } else {
365
- return cellVal.toString()
366
- }
367
- }
368
-
369
- /**
370
- * Serialize cell value in the given content format.
371
- *
372
- * @param cellVal cell value
373
- * @param locale locale-object to format datetimes with
374
- *
375
- */
376
- serializeCellWithLocale(cellVal: OINODataCell, locale:Intl.DateTimeFormat): string|null|undefined {
377
- if (typeof(cellVal) == "string") {
378
- cellVal = this.db.parseSqlValueAsCell(cellVal, this.sqlType)
379
- }
380
- if ((cellVal === null) || (cellVal === undefined)) {
381
- return cellVal
382
-
383
- } else if (cellVal instanceof Date) {
384
- return locale.format(cellVal)
385
-
386
- } else {
387
- return cellVal.toString()
388
- }
389
- }
390
-
391
- /**
392
- * Parce cell value from string using field type specific formatting rules.
393
- *
394
- * @param value string value
395
- *
396
- */
397
- deserializeCell(value: string|null|undefined): OINODataCell {
398
- if ((value === null) || (value === undefined)) {
399
- return value
400
- } else {
401
- return new Date(value)
402
- }
403
- }
404
-
405
- }