@oino-ts/db 0.0.11

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 (48) hide show
  1. package/README.md +222 -0
  2. package/dist/cjs/OINODb.js +27 -0
  3. package/dist/cjs/OINODbApi.js +270 -0
  4. package/dist/cjs/OINODbConfig.js +86 -0
  5. package/dist/cjs/OINODbDataField.js +354 -0
  6. package/dist/cjs/OINODbDataModel.js +279 -0
  7. package/dist/cjs/OINODbDataSet.js +139 -0
  8. package/dist/cjs/OINODbFactory.js +563 -0
  9. package/dist/cjs/OINODbModelSet.js +267 -0
  10. package/dist/cjs/OINODbParams.js +280 -0
  11. package/dist/cjs/OINODbRequestParams.js +280 -0
  12. package/dist/cjs/OINODbSwagger.js +201 -0
  13. package/dist/cjs/index.js +51 -0
  14. package/dist/esm/OINODb.js +23 -0
  15. package/dist/esm/OINODbApi.js +265 -0
  16. package/dist/esm/OINODbConfig.js +82 -0
  17. package/dist/esm/OINODbDataField.js +345 -0
  18. package/dist/esm/OINODbDataModel.js +275 -0
  19. package/dist/esm/OINODbDataSet.js +134 -0
  20. package/dist/esm/OINODbFactory.js +559 -0
  21. package/dist/esm/OINODbModelSet.js +263 -0
  22. package/dist/esm/OINODbRequestParams.js +274 -0
  23. package/dist/esm/OINODbSwagger.js +197 -0
  24. package/dist/esm/index.js +17 -0
  25. package/dist/types/OINODb.d.ts +75 -0
  26. package/dist/types/OINODbApi.d.ts +57 -0
  27. package/dist/types/OINODbConfig.d.ts +52 -0
  28. package/dist/types/OINODbDataField.d.ts +202 -0
  29. package/dist/types/OINODbDataModel.d.ts +108 -0
  30. package/dist/types/OINODbDataSet.d.ts +95 -0
  31. package/dist/types/OINODbFactory.d.ts +99 -0
  32. package/dist/types/OINODbModelSet.d.ts +50 -0
  33. package/dist/types/OINODbRequestParams.d.ts +130 -0
  34. package/dist/types/OINODbSwagger.d.ts +25 -0
  35. package/dist/types/index.d.ts +103 -0
  36. package/package.json +35 -0
  37. package/src/OINODb.ts +98 -0
  38. package/src/OINODbApi.test.ts +243 -0
  39. package/src/OINODbApi.ts +270 -0
  40. package/src/OINODbConfig.ts +92 -0
  41. package/src/OINODbDataField.ts +372 -0
  42. package/src/OINODbDataModel.ts +290 -0
  43. package/src/OINODbDataSet.ts +170 -0
  44. package/src/OINODbFactory.ts +570 -0
  45. package/src/OINODbModelSet.ts +286 -0
  46. package/src/OINODbRequestParams.ts +281 -0
  47. package/src/OINODbSwagger.ts +209 -0
  48. package/src/index.ts +116 -0
@@ -0,0 +1,92 @@
1
+ /** Set the name of the OINO ID field (default \_OINOID\_) */
2
+
3
+ export class OINODbConfig {
4
+ /** Name of the synthetic OINO ID field */
5
+ static OINODB_ID_FIELD:string = "_OINOID_"
6
+ /** Private key separator of the synthetic OINO ID field */
7
+ static OINODB_ID_SEPARATOR:string = "_"
8
+ private static OINODB_ID_SEPARATOR_ESCAPED:string = "%"
9
+
10
+ /** Name of the OINODbSqlFilter-parameter in request */
11
+ static OINODB_SQL_FILTER_PARAM:string = "oinosqlfilter"
12
+
13
+ /** Name of the OINODbSqlOrder-parameter in request */
14
+ static OINODB_SQL_ORDER_PARAM:string = "oinosqlorder"
15
+
16
+ /** Name of the OINODbSqlLimit-parameter in request */
17
+ static OINODB_SQL_LIMIT_PARAM:string = "oinosqllimit"
18
+
19
+ /**
20
+ * Set the name of the OINO ID field
21
+ * @param idField name of the OINO ID field
22
+ */
23
+ static setOinoIdField(idField: string) {
24
+ if (idField) {
25
+ OINODbConfig.OINODB_ID_FIELD = idField;
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Set the separator character of the OINO ID field
31
+ * @param idSeparator character to use as separator of id parts
32
+ */
33
+ static setOinoIdSeparator(idSeparator: string) {
34
+ if (idSeparator && (idSeparator.length == 1)) {
35
+ OINODbConfig.OINODB_ID_SEPARATOR = idSeparator;
36
+ OINODbConfig.OINODB_ID_SEPARATOR_ESCAPED = '%' + idSeparator.charCodeAt(0).toString(16);
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Print OINO ID for primary key values.
42
+ *
43
+ * @param primaryKeys an array of primary key values.
44
+ *
45
+ */
46
+ static printOINOId(primaryKeys:string[]):string {
47
+ let result:string = ""
48
+ for (let i=0; i< primaryKeys.length; i++) {
49
+ if (i > 0) {
50
+ result += OINODbConfig.OINODB_ID_SEPARATOR
51
+ }
52
+ result += encodeURIComponent(primaryKeys[i] as string).replaceAll(OINODbConfig.OINODB_ID_SEPARATOR, OINODbConfig.OINODB_ID_SEPARATOR_ESCAPED)
53
+ }
54
+ return result
55
+ }
56
+
57
+ /**
58
+ * Set the name of the OINODbSqlFilter-param field
59
+ *
60
+ * @param sqlFilterParam name of the http parameter with `OINODbSqlFilter` definition
61
+ *
62
+ */
63
+ static setOinoSqlFilterParam(sqlFilterParam: string) {
64
+ if (sqlFilterParam) {
65
+ OINODbConfig.OINODB_SQL_FILTER_PARAM = sqlFilterParam;
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Set the name of the OINODbSqlOrder-param field
71
+ *
72
+ * @param sqlOrderParam name of the http parameter with `OINODbSqlOrder` definition
73
+ *
74
+ */
75
+ static setOinoSqlOrderParam(sqlOrderParam: string) {
76
+ if (sqlOrderParam) {
77
+ OINODbConfig.OINODB_SQL_ORDER_PARAM = sqlOrderParam;
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Set the name of the OINODbSqlLimit-param field
83
+ *
84
+ * @param sqlLimitParam name of the http parameter with `OINODbSqlLimit` definition
85
+ *
86
+ */
87
+ static setOinoSqlLimitParam(sqlLimitParam: string) {
88
+ if (sqlLimitParam) {
89
+ OINODbConfig.OINODB_SQL_LIMIT_PARAM = sqlLimitParam;
90
+ }
91
+ }
92
+ }
@@ -0,0 +1,372 @@
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 { OINODbDataFieldParams, OINODataCell, OINODb } from "./index.js";
8
+
9
+ /**
10
+ * Base class for a column of data responsible for appropriatelly serializing/deserializing the data.
11
+ *
12
+ */
13
+ export class OINODbDataField {
14
+
15
+ /** OINODB reference*/
16
+ readonly db:OINODb;
17
+
18
+ /** Name of the field */
19
+ readonly name: string;
20
+
21
+ /** Internal type of field*/
22
+ readonly type: string;
23
+
24
+ /** SQL type of the field */
25
+ readonly sqlType: string;
26
+
27
+ /** Maximum length of the field (or 0) */
28
+ readonly maxLength: number;
29
+
30
+ /** Parameters for the field */
31
+ readonly fieldParams: OINODbDataFieldParams;
32
+
33
+ /**
34
+ * Constructor for a data field
35
+ *
36
+ * @param db OINODb reference
37
+ * @param name name of the field
38
+ * @param type internal type of the field
39
+ * @param sqlType column type in database
40
+ * @param fieldParams parameters of the field
41
+ * @param maxLength maximum length of the field (or 0)
42
+ *
43
+ */
44
+ constructor(db:OINODb, name: string, type:string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength:number = 0) {
45
+ this.db = db
46
+ this.name = name
47
+ this.type = type
48
+ this.maxLength = maxLength
49
+ this.sqlType = sqlType
50
+ this.fieldParams = fieldParams
51
+ // OINOLog.debug("OINODbDataField.constructor", {this:this})
52
+ }
53
+
54
+ /**
55
+ * Pring debug information for the field
56
+ *
57
+ * @param length length of the debug output (or 0 for as long as needed)
58
+ *
59
+ */
60
+ printColumnDebug(length:number = 0): string {
61
+ let params: string = "";
62
+ if (this.fieldParams.isPrimaryKey) {
63
+ params += "PK ";
64
+ }
65
+ if (this.fieldParams.isAutoInc) {
66
+ params += "AUTOINC ";
67
+ }
68
+ if (this.fieldParams.isNotNull) {
69
+ params += "NOTNUL ";
70
+ }
71
+ if (params != "") {
72
+ params = "{" + params.trim() + "}";
73
+ }
74
+ if (this.maxLength > 0) {
75
+ params = this.sqlType + "(" + this.maxLength + ")" + params
76
+ } else {
77
+ params = this.sqlType + params
78
+ }
79
+ const name_length:number = length - 2 - 1 - params.length
80
+ let result:string = this.name
81
+ if (length > 0) {
82
+ if (result.length > name_length) {
83
+ result = result.substring(0, name_length-2)+".."
84
+ }
85
+ result = (result + ":" + params).padEnd(length-2, " ")
86
+ } else {
87
+ result = this.type + ":" + result + ":" + params
88
+ }
89
+ return "[" + result + "]";
90
+ }
91
+
92
+ /**
93
+ * Serialize cell value in the given content format.
94
+ *
95
+ * @param cellVal cell value
96
+ *
97
+ */
98
+ serializeCell(cellVal: OINODataCell):string|null|undefined {
99
+ cellVal = this.db.parseSqlValueAsCell(cellVal, this.sqlType)
100
+ if ((cellVal === null) || (cellVal === undefined)) {
101
+ return cellVal // let content type encoder worry what to do with the value (so not force it to string)
102
+ } else {
103
+ return cellVal.toString()
104
+ }
105
+ }
106
+
107
+ /**
108
+ * Parce cell value from string using field type specific formatting rules.
109
+ *
110
+ * @param value string value
111
+ *
112
+ */
113
+ deserializeCell(value: string|null|undefined): OINODataCell {
114
+ return value
115
+ }
116
+
117
+ /**
118
+ * Print data cell (from deserialization) as SQL-string.
119
+ *
120
+ * @param cellVal cell value
121
+ *
122
+ */
123
+ printCellAsSqlValue(cellVal: OINODataCell):string {
124
+ return this.db.printCellAsSqlValue(cellVal, this.sqlType);
125
+ }
126
+
127
+ /**
128
+ * Print name of column as SQL.
129
+ *
130
+ */
131
+ printSqlColumnName():string {
132
+ return this.db.printSqlColumnname(this.name)
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Specialised class for a string column.
138
+ *
139
+ */
140
+ export class OINOStringDataField extends OINODbDataField {
141
+
142
+ /**
143
+ * Constructor for a string data field
144
+ *
145
+ * @param db OINODb reference
146
+ * @param name name of the field
147
+ * @param sqlType column type in database
148
+ * @param fieldParams parameters of the field
149
+ * @param maxLength maximum length of the field (or 0)
150
+ *
151
+ */
152
+ constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength: number) {
153
+ super(db, name, "string", sqlType, fieldParams, maxLength)
154
+ }
155
+
156
+ }
157
+
158
+ /**
159
+ * Specialised class for a boolean column.
160
+ *
161
+ */
162
+ export class OINOBooleanDataField extends OINODbDataField {
163
+
164
+ /**
165
+ * Constructor for a boolean data field
166
+ *
167
+ * @param db OINODb reference
168
+ * @param name name of the field
169
+ * @param sqlType column type in database
170
+ * @param fieldParams parameters of the field
171
+ *
172
+ */
173
+ constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams) {
174
+ super(db, name, "boolean", sqlType, fieldParams)
175
+ }
176
+ /**
177
+ * Serialize cell value in the given content format.
178
+ *
179
+ * @param cellVal cell value
180
+ *
181
+ */
182
+ serializeCell(cellVal: OINODataCell):string|null|undefined {
183
+ const parsed_value:string = (this.db.parseSqlValueAsCell(cellVal, this.sqlType) || "").toString()
184
+ let result:string
185
+ // console.log("OINOBooleanDataField.serializeCell: parsed_value=" + parsed_value)
186
+ if ((parsed_value == "") || (parsed_value.toLowerCase() == "false") || (parsed_value.match(/^0+$/))) {
187
+ result = "false"
188
+ } else {
189
+ result = "true"
190
+ }
191
+ return result
192
+ }
193
+
194
+ /**
195
+ * Parce cell value from string using field type specific formatting rules.
196
+ *
197
+ * @param value string value
198
+ *
199
+ */
200
+ deserializeCell(value: string|null|undefined): OINODataCell {
201
+ if (value == null || value == "" || value.toString().toLowerCase() == "false" || value == "0") { // TODO: testaa poistaa .toString()
202
+ return false
203
+ } else {
204
+ return true
205
+ }
206
+ }
207
+ }
208
+
209
+ /**
210
+ * Specialised class for a number column.
211
+ *
212
+ */
213
+ export class OINONumberDataField extends OINODbDataField {
214
+
215
+ /**
216
+ * Constructor for a string data field
217
+ *
218
+ * @param db OINODb reference
219
+ * @param name name of the field
220
+ * @param sqlType column type in database
221
+ * @param fieldParams parameters of the field
222
+ *
223
+ */
224
+ constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams) {
225
+ super(db, name, "number", sqlType, fieldParams)
226
+ }
227
+
228
+ /**
229
+ * Serialize cell value in the given content format.
230
+ *
231
+ * @param cellVal cell value
232
+ *
233
+ */
234
+ serializeCell(cellVal: OINODataCell):string|null|undefined {
235
+ let result:string|null
236
+ if ((cellVal === null) || (cellVal === undefined) || (cellVal === "")) {
237
+ result = null
238
+ } else {
239
+ result = cellVal.toString()
240
+ }
241
+ return result
242
+ }
243
+
244
+ /**
245
+ * Parce cell value from string using field type specific formatting rules.
246
+ *
247
+ * @param value string value
248
+ *
249
+ */
250
+ deserializeCell(value: string|null|undefined): OINODataCell {
251
+ if (!value) {
252
+ return 0
253
+ } else {
254
+ return Number.parseFloat(value)
255
+ }
256
+ }
257
+ }
258
+
259
+ /**
260
+ * Specialised class for a blob column.
261
+ *
262
+ */
263
+ export class OINOBlobDataField extends OINODbDataField {
264
+
265
+ /**
266
+ * Constructor for a blob data field
267
+ *
268
+ * @param db OINODb reference
269
+ * @param name name of the field
270
+ * @param sqlType column type in database
271
+ * @param fieldParams parameters of the field
272
+ * @param maxLength maximum length of the field (or 0)
273
+ *
274
+ */
275
+ constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength:number) {
276
+ super(db, name, "blob", sqlType, fieldParams, maxLength)
277
+ }
278
+
279
+ /**
280
+ * Serialize cell value in the given content format.
281
+ *
282
+ * @param cellVal cell value
283
+ *
284
+ */
285
+ serializeCell(cellVal: OINODataCell):string|null|undefined {
286
+ // OINOLog.debug("OINOBlobDataField.serializeCell", {cellVal:cellVal})
287
+ if ((cellVal === null) || (cellVal === undefined)) {
288
+ return cellVal
289
+
290
+ } else if (cellVal instanceof Uint8Array) {
291
+ return Buffer.from(cellVal).toString('base64')
292
+
293
+ } else {
294
+ return cellVal.toString()
295
+ }
296
+ }
297
+
298
+ /**
299
+ * Parce cell value from string using field type specific formatting rules.
300
+ *
301
+ * @param value string value
302
+ *
303
+ */
304
+ deserializeCell(value: string|null|undefined): OINODataCell {
305
+ if (value == null) {
306
+ return new Buffer(0)
307
+
308
+ } else {
309
+ return Buffer.from(value, 'base64') // Blob-field data is base64 encoded and converted internally to UInt8Array / Buffer
310
+ }
311
+ }
312
+
313
+ }
314
+
315
+ /**
316
+ * Specialised class for a datetime column.
317
+ *
318
+ */
319
+ export class OINODatetimeDataField extends OINODbDataField {
320
+
321
+ /**
322
+ * Constructor for a string data field
323
+ *
324
+ * @param db OINODb reference
325
+ * @param name name of the field
326
+ * @param sqlType column type in database
327
+ * @param fieldParams parameters of the field
328
+ *
329
+ */
330
+ constructor(db:OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams) {
331
+ super(db, name, "datetime", sqlType, fieldParams)
332
+ }
333
+
334
+ /**
335
+ * Serialize cell value in the given content format.
336
+ *
337
+ * @param cellVal cell value
338
+ *
339
+ */
340
+ serializeCell(cellVal: OINODataCell): string|null|undefined {
341
+ // OINOLog.debug("OINODatetimeDataField.serializeCell", {cellVal:cellVal, type:typeof(cellVal)})
342
+ if (typeof(cellVal) == "string") {
343
+ cellVal = this.db.parseSqlValueAsCell(cellVal, this.sqlType)
344
+ // OINOLog.debug("OINODatetimeDataField.serializeCell parsed", {cellVal:cellVal, type:typeof(cellVal)})
345
+ }
346
+ if ((cellVal === null) || (cellVal === undefined)) {
347
+ return cellVal
348
+
349
+ } else if (cellVal instanceof Date) {
350
+ return cellVal.toISOString()
351
+
352
+ } else {
353
+ return cellVal.toString()
354
+ }
355
+ }
356
+
357
+ /**
358
+ * Parce cell value from string using field type specific formatting rules.
359
+ *
360
+ * @param value string value
361
+ *
362
+ */
363
+ deserializeCell(value: string|null|undefined): OINODataCell {
364
+ // OINOLog.debug("OINODatetimeDataField.deserializeCell", {strVal:strVal})
365
+ if ((value === null) || (value === undefined)) {
366
+ return value
367
+ } else {
368
+ return new Date(value)
369
+ }
370
+ }
371
+
372
+ }