@oino-ts/common 0.21.2 → 1.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.
Files changed (69) hide show
  1. package/README.md +183 -0
  2. package/dist/cjs/OINOApi.js +322 -0
  3. package/dist/cjs/OINOConfig.js +104 -0
  4. package/dist/cjs/OINOConstants.js +42 -0
  5. package/dist/cjs/OINODataField.js +346 -0
  6. package/dist/cjs/OINODataModel.js +182 -0
  7. package/dist/cjs/OINODataSource.js +165 -0
  8. package/dist/cjs/OINOFormatter.js +6 -5
  9. package/dist/cjs/OINOHtmlTemplate.js +21 -18
  10. package/dist/cjs/OINOModelSet.js +333 -0
  11. package/dist/cjs/OINOParser.js +448 -0
  12. package/dist/cjs/OINOQueryParams.js +434 -0
  13. package/dist/cjs/OINORequest.js +21 -13
  14. package/dist/cjs/OINOResult.js +13 -12
  15. package/dist/cjs/OINOStr.js +11 -11
  16. package/dist/cjs/OINOSwagger.js +205 -0
  17. package/dist/cjs/index.js +57 -39
  18. package/dist/esm/OINOApi.js +315 -0
  19. package/dist/esm/OINOConfig.js +100 -0
  20. package/dist/esm/OINOConstants.js +39 -0
  21. package/dist/esm/OINODataField.js +337 -0
  22. package/dist/esm/OINODataModel.js +178 -0
  23. package/dist/esm/OINODataSource.js +159 -0
  24. package/dist/esm/OINOFormatter.js +2 -1
  25. package/dist/esm/OINOHtmlTemplate.js +4 -1
  26. package/dist/esm/OINOModelSet.js +329 -0
  27. package/dist/esm/OINOParser.js +444 -0
  28. package/dist/esm/OINOQueryParams.js +426 -0
  29. package/dist/esm/OINORequest.js +9 -1
  30. package/dist/esm/OINOResult.js +2 -1
  31. package/dist/esm/OINOStr.js +1 -1
  32. package/dist/esm/OINOSwagger.js +201 -0
  33. package/dist/esm/index.js +14 -32
  34. package/dist/types/OINOApi.d.ts +191 -0
  35. package/dist/types/OINOConfig.d.ts +63 -0
  36. package/dist/types/OINOConstants.d.ts +51 -0
  37. package/dist/types/OINODataField.d.ts +209 -0
  38. package/dist/types/OINODataModel.d.ts +78 -0
  39. package/dist/types/OINODataSource.d.ts +184 -0
  40. package/dist/types/OINOHtmlTemplate.d.ts +1 -1
  41. package/dist/types/OINOModelSet.d.ts +64 -0
  42. package/dist/types/OINOParser.d.ts +42 -0
  43. package/dist/types/OINOQueryParams.d.ts +270 -0
  44. package/dist/types/OINORequest.d.ts +4 -1
  45. package/dist/types/OINOResult.d.ts +1 -1
  46. package/dist/types/OINOStr.d.ts +1 -1
  47. package/dist/types/OINOSwagger.d.ts +25 -0
  48. package/dist/types/index.d.ts +14 -31
  49. package/package.json +32 -32
  50. package/src/OINOApi.ts +429 -0
  51. package/src/OINOBenchmark.ts +323 -323
  52. package/src/OINOConfig.ts +113 -0
  53. package/src/OINOConstants.ts +59 -0
  54. package/src/OINODataField.ts +371 -0
  55. package/src/OINODataModel.ts +187 -0
  56. package/src/OINODataSource.ts +280 -0
  57. package/src/OINOFormatter.ts +166 -165
  58. package/src/OINOHeaders.ts +51 -51
  59. package/src/OINOHtmlTemplate.test.ts +114 -114
  60. package/src/OINOHtmlTemplate.ts +225 -222
  61. package/src/OINOLog.ts +292 -292
  62. package/src/OINOModelSet.ts +359 -0
  63. package/src/OINOParser.ts +441 -0
  64. package/src/OINOQueryParams.ts +449 -0
  65. package/src/OINORequest.ts +204 -196
  66. package/src/OINOResult.ts +331 -330
  67. package/src/OINOStr.ts +254 -254
  68. package/src/OINOSwagger.ts +213 -0
  69. package/src/index.ts +18 -38
@@ -0,0 +1,187 @@
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 { OINODataRow } from "./OINOConstants.js"
8
+ import { OINOApi } from "./OINOApi.js"
9
+ import { OINODataField, OINONumberDataField, OINODataFieldFilter } from "./OINODataField.js"
10
+
11
+ /**
12
+ * OINO Datamodel object for representing one database table and it's columns.
13
+ *
14
+ */
15
+ export class OINODataModel {
16
+ private _fieldIndexLookup:Record<string, number>;
17
+
18
+ /** Database refererence of the table */
19
+ readonly api:OINOApi
20
+
21
+ /** Field refererences of the API */
22
+ readonly fields: OINODataField[]
23
+
24
+ /**
25
+ * Constructor of the data model.
26
+ * NOTE! OINODbDataModel.initialize must be called after constructor to populate fields.
27
+ *
28
+ * @param api api of the data model
29
+ *
30
+ */
31
+ constructor(api:OINOApi) {
32
+ this._fieldIndexLookup = {}
33
+ this.api = api
34
+ this.fields = []
35
+ }
36
+
37
+ /**
38
+ * Add a field to the datamodel.
39
+ *
40
+ * @param field dataset field
41
+ *
42
+ */
43
+ addField(field:OINODataField) {
44
+ this.fields.push(field)
45
+ this._fieldIndexLookup[field.name] = this.fields.length-1
46
+ }
47
+
48
+ /**
49
+ * Find a field of a given name if any.
50
+ *
51
+ * @param name name of the field to find
52
+ *
53
+ */
54
+ findFieldByName(name:string):OINODataField|null {
55
+ const i:number = this._fieldIndexLookup[name]
56
+ if (i >= 0) {
57
+ return this.fields[i]
58
+ } else {
59
+ return null
60
+ }
61
+ }
62
+
63
+ /**
64
+ * Find index of a field of a given name if any.
65
+ *
66
+ * @param name name of the field to find
67
+ *
68
+ */
69
+ findFieldIndexByName(name:string):number {
70
+ const i:number = this._fieldIndexLookup[name]
71
+ if (i >= 0) {
72
+ return i
73
+ } else {
74
+ return -1
75
+ }
76
+ }
77
+
78
+ /**
79
+ * Find all fields based of given filter callback criteria (e.g. fields of certain data type, primary keys etc.)
80
+ *
81
+ * @param filter callback called for each field to include or not
82
+ *
83
+ */
84
+ filterFields(filter:OINODataFieldFilter):OINODataField[] {
85
+ let result:OINODataField[] = []
86
+ for (let f of this.fields) {
87
+ if (filter(f)) {
88
+ result.push(f)
89
+ }
90
+ }
91
+ return result
92
+ }
93
+
94
+ /**
95
+ * Return the primary key values of one row in order of the data model
96
+ *
97
+ * @param row data row
98
+ * @param hashidValues apply hashid when applicable
99
+ *
100
+ */
101
+ getRowPrimarykeyValues(row: OINODataRow, hashidValues:boolean = false): string[] {
102
+ let values: string[] = [];
103
+ for (let i=0; i< this.fields.length; i++) {
104
+ const f = this.fields[i]
105
+ if (f.fieldParams.isPrimaryKey) {
106
+ const value:string = row[i]?.toString() || ""
107
+ if (hashidValues && value && (f instanceof OINONumberDataField) && this.api.hashid) {
108
+ values.push(this.api.hashid.encode(value))
109
+ } else {
110
+ values.push(value)
111
+ }
112
+ }
113
+ }
114
+ return values
115
+ }
116
+
117
+ /**
118
+ * Pring debug information for the field
119
+ *
120
+ * @param length length of the debug output (or 0 for as long as needed)
121
+ *
122
+ */
123
+ printColumnDebug(field: OINODataField, length:number = 0): string {
124
+ let params: string = "";
125
+ if (field.fieldParams.isPrimaryKey) {
126
+ params += "PK ";
127
+ }
128
+ if (field.fieldParams.isForeignKey) {
129
+ params += "FK ";
130
+ }
131
+ if (field.fieldParams.isAutoInc) {
132
+ params += "AUTOINC ";
133
+ }
134
+ if (field.fieldParams.isNotNull) {
135
+ params += "NOTNUL ";
136
+ }
137
+ if (params != "") {
138
+ params = "{" + params.trim() + "}";
139
+ }
140
+ if (field.maxLength > 0) {
141
+ params = field.nativeType + "(" + field.maxLength + ")" + params
142
+ } else {
143
+ params = field.nativeType + params
144
+ }
145
+ const name_length:number = length - 2 - 1 - params.length
146
+ let result:string = field.name
147
+ if (length > 0) {
148
+ if (result.length > name_length) {
149
+ result = result.substring(0, name_length-2)+".."
150
+ }
151
+ result = (result + ":" + params).padEnd(length-2, " ")
152
+ } else {
153
+ result = field.type + ":" + result + ":" + params
154
+ }
155
+ return "[" + result + "]";
156
+ }
157
+
158
+ /**
159
+ * Print debug information about the fields.
160
+ *
161
+ * @param separator string to separate field prints
162
+ *
163
+ */
164
+ printDebug(separator:string = ""): string {
165
+ let result: string = this.api.params.tableName + ":" + separator;
166
+ for (let f of this.fields) {
167
+ result += this.printColumnDebug(f) + separator;
168
+ }
169
+ return result;
170
+ }
171
+
172
+ /**
173
+ * Print all public properties (db, table name, fields) of the datamodel. Used
174
+ * in automated testing validate schema has stayed the same.
175
+ *
176
+ */
177
+ printFieldPublicPropertiesJson():string {
178
+ const result:string = JSON.stringify(this.fields, (key:any, value:any) => {
179
+ if (key.startsWith("_")) {
180
+ return undefined
181
+ } else {
182
+ return value
183
+ }
184
+ })
185
+ return result
186
+ }
187
+ }
@@ -0,0 +1,280 @@
1
+ import { OINODataCell, OINODataRow, OINO_EMPTY_ROW, OINO_ERROR_PREFIX } from "./OINOConstants.js"
2
+ import { OINOResult } from "./OINOResult.js"
3
+ import { OINOApi } from "./OINOApi.js"
4
+
5
+ /**
6
+ * Base class for database abstraction, implementing methods for connecting, making queries and parsing/formatting data
7
+ * between SQL and serialization formats.
8
+ *
9
+ */
10
+ export abstract class OINODataSource {
11
+
12
+ isConnected:boolean = false
13
+ isValidated:boolean = false
14
+
15
+ /**
16
+ * Connect to database.
17
+ *
18
+ */
19
+ abstract connect(): Promise<OINOResult>
20
+
21
+ /**
22
+ * Validate connection to database is working.
23
+ *
24
+ */
25
+ abstract validate(): Promise<OINOResult>
26
+
27
+ /**
28
+ * Disconnect from database.
29
+ *
30
+ */
31
+ abstract disconnect(): Promise<void>
32
+
33
+ /**
34
+ * Print a table name using database specific SQL escaping.
35
+ *
36
+ * @param sqlTable name of the table
37
+ *
38
+ */
39
+ abstract printTableName(sqlTable:string): string
40
+
41
+ /**
42
+ * Print a column name with correct SQL escaping.
43
+ *
44
+ * @param sqlColumn name of the column
45
+ *
46
+ */
47
+ abstract printColumnName(sqlColumn:string): string
48
+
49
+ /**
50
+ * Print a single data value from serialization using the context of the native data
51
+ * type with the correct SQL escaping.
52
+ *
53
+ * @param cellValue data from sql results
54
+ * @param sqlType native type name for table column
55
+ *
56
+ */
57
+ abstract printCellAsValue(cellValue:OINODataCell, sqlType: string): string
58
+
59
+ /**
60
+ * Print a single string value as valid sql literal
61
+ *
62
+ * @param sqlString string value
63
+ *
64
+ */
65
+ abstract printStringValue(sqlString:string): string
66
+
67
+ /**
68
+ * Parse a single SQL result value for serialization using the context of the native data
69
+ * type.
70
+ *
71
+ * @param sqlValue data from serialization
72
+ * @param sqlType native type name for table column
73
+ *
74
+ */
75
+ abstract parseValueAsCell(sqlValue:OINODataCell, sqlType: string): OINODataCell
76
+
77
+ /**
78
+ * Initialize a data model by getting the SQL schema and populating OINODataFields of
79
+ * the model.
80
+ *
81
+ * @param api api which data model to initialize.
82
+ *
83
+ */
84
+ abstract initializeApiDatamodel(api:OINOApi): Promise<void>
85
+
86
+ }
87
+
88
+ /**
89
+ * Base class for SQL results that can be asynchronously iterated (but
90
+ * not necessarity rewinded). Idea is to handle database specific mechanisms
91
+ * for returning and formatting conventions in the database specific
92
+ * implementation. Data might be in memory or streamed in chunks and
93
+ * `OINODataSet` will serve it out consistently.
94
+ *
95
+ */
96
+
97
+ export abstract class OINODataSet extends OINOResult {
98
+ private _data: unknown;
99
+
100
+ /** Error messages */
101
+ readonly messages: string[]
102
+
103
+ /**
104
+ * Constructor for `OINODataSet`.
105
+ *
106
+ * @param data internal database specific data type (constructor will throw if invalid)
107
+ * @param messages error messages from SQL-query
108
+ *
109
+ */
110
+ constructor(data: unknown, messages: string[] = []) {
111
+ super();
112
+ this._data = data;
113
+ this.messages = messages;
114
+ }
115
+
116
+
117
+ /**
118
+ * Is data set empty.
119
+ *
120
+ */
121
+ abstract isEmpty(): boolean;
122
+
123
+ /**
124
+ * Is there no more content, i.e. either dataset is empty or we have moved beyond last line
125
+ *
126
+ */
127
+ abstract isEof(): boolean;
128
+
129
+ /**
130
+ * Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
131
+ *
132
+ */
133
+ abstract next(): Promise<boolean>;
134
+
135
+ /**
136
+ * Gets current row of data.
137
+ *
138
+ */
139
+ abstract getRow(): OINODataRow;
140
+
141
+ /**
142
+ * Gets all rows of data.
143
+ *
144
+ * NOTE: This is left abstract instead of just using `getRow()` so that DB implementations can hopefully optimize not duplicating data *
145
+ */
146
+ abstract getAllRows(): Promise<OINODataRow[]>;
147
+
148
+ /**
149
+ * Checks if the messages contain errors.
150
+ *
151
+ */
152
+ hasErrors(): boolean {
153
+ for (let i = 0; i < this.messages.length; i++) {
154
+ if (this.messages[i].startsWith(OINO_ERROR_PREFIX)) {
155
+ return true;
156
+ }
157
+ }
158
+ return false;
159
+ }
160
+
161
+ /**
162
+ * Finds the first error message that occured
163
+ *
164
+ */
165
+ getFirstError(): string {
166
+ for (let i = this.messages.length-1; i >= 0; i--) {
167
+ if (this.messages[i].startsWith(OINO_ERROR_PREFIX)) {
168
+ return this.messages[i];
169
+ }
170
+ }
171
+ return "";
172
+ }
173
+
174
+ /**
175
+ * Finds the last error message that occured
176
+ *
177
+ */
178
+ getLastError(): string {
179
+ for (let i = 0; i < this.messages.length; i++) {
180
+ if (this.messages[i].startsWith(OINO_ERROR_PREFIX)) {
181
+ return this.messages[i];
182
+ }
183
+ }
184
+ return "";
185
+ }
186
+ }
187
+
188
+ /**
189
+ * Generic in memory implementation of a data set where data is an array of rows. Used
190
+ * by BunSqlite and automated testing. Can be rewinded.
191
+ *
192
+ */
193
+
194
+ export class OINOMemoryDataset extends OINODataSet {
195
+ private _rows: OINODataRow[];
196
+ private _currentRow: number;
197
+ private _eof: boolean;
198
+
199
+ /**
200
+ * Constructor of `OINOMemoryDataset`.
201
+ *
202
+ * @param data data as OINODataRow[] (constructor will throw if invalid)
203
+ * @param errors error messages from SQL-query
204
+ *
205
+ */
206
+ constructor(data: unknown, errors: string[] = []) {
207
+ super(data, errors);
208
+ if ((data == null) || !(Array.isArray(data))) {
209
+ throw new Error(OINO_ERROR_PREFIX + ": Data needs to be compatible with OINORow[]!"); // TODO: maybe check all rows
210
+ }
211
+ this._rows = data as OINODataRow[];
212
+ if (this.isEmpty()) {
213
+ this._currentRow = -1;
214
+ this._eof = true;
215
+ } else {
216
+ this._currentRow = 0;
217
+ this._eof = false;
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Is data set empty.
223
+ *
224
+ */
225
+ isEmpty(): boolean {
226
+ return (this._rows.length == 0);
227
+ }
228
+
229
+ /**
230
+ * Is there no more content, i.e. either dataset is empty or we have moved beyond last line
231
+ *
232
+ */
233
+ isEof(): boolean {
234
+ return (this._eof);
235
+ }
236
+
237
+ /**
238
+ * Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
239
+ *
240
+ */
241
+ async next(): Promise<boolean> {
242
+ if (this._currentRow < this._rows.length - 1) {
243
+ this._currentRow = this._currentRow + 1;
244
+ } else {
245
+ this._eof = true;
246
+ }
247
+ return Promise.resolve(!this._eof);
248
+ }
249
+
250
+ /**
251
+ * Gets current row of data.
252
+ *
253
+ */
254
+ getRow(): OINODataRow {
255
+ if ((this._currentRow >= 0) && (this._currentRow < this._rows.length)) {
256
+ return this._rows[this._currentRow];
257
+ } else {
258
+ return OINO_EMPTY_ROW;
259
+ }
260
+ }
261
+
262
+ /**
263
+ * Gets all rows of data.
264
+ *
265
+ */
266
+ async getAllRows(): Promise<OINODataRow[]> {
267
+ return this._rows // at the moment theres no result streaming, so we can just return the rows
268
+ }
269
+
270
+ /**
271
+ * Rewinds data set to the first row, returns !isEof().
272
+ *
273
+ */
274
+ first(): boolean {
275
+ this._currentRow = 0;
276
+ this._eof = this._rows.length == 0;
277
+ return !this._eof;
278
+ }
279
+ }
280
+