@oino-ts/db 0.0.18 → 0.1.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.
package/dist/cjs/index.js CHANGED
@@ -1,21 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OINODB_EMPTY_ROWS = exports.OINODB_EMPTY_ROW = exports.OINODbSwagger = exports.OINODbFactory = exports.OINODbConfig = exports.OINODbSqlBooleanOperation = exports.OINODbSqlLimit = exports.OINODbSqlComparison = exports.OINODbSqlOrder = exports.OINODbSqlFilter = exports.OINODbMemoryDataSet = exports.OINODbDataSet = exports.OINODb = exports.OINODatetimeDataField = exports.OINOBlobDataField = exports.OINOStringDataField = exports.OINONumberDataField = exports.OINOBooleanDataField = exports.OINODbDataField = exports.OINODbModelSet = exports.OINODbDataModel = exports.OINODbApi = exports.OINODbHtmlTemplate = exports.OINODbApiResult = exports.OINOHtmlTemplate = exports.OINOHttpResult = exports.OINOResult = exports.OINOConsoleLog = exports.OINOLogLevel = exports.OINOLog = exports.OINOBenchmark = exports.OINOStr = exports.OINO_DEBUG_PREFIX = exports.OINO_INFO_PREFIX = exports.OINO_WARNING_PREFIX = exports.OINO_ERROR_PREFIX = exports.OINOContentType = void 0;
4
- const types_1 = require("@oino-ts/types");
5
- Object.defineProperty(exports, "OINOContentType", { enumerable: true, get: function () { return types_1.OINOContentType; } });
6
- var types_2 = require("@oino-ts/types");
7
- Object.defineProperty(exports, "OINO_ERROR_PREFIX", { enumerable: true, get: function () { return types_2.OINO_ERROR_PREFIX; } });
8
- Object.defineProperty(exports, "OINO_WARNING_PREFIX", { enumerable: true, get: function () { return types_2.OINO_WARNING_PREFIX; } });
9
- Object.defineProperty(exports, "OINO_INFO_PREFIX", { enumerable: true, get: function () { return types_2.OINO_INFO_PREFIX; } });
10
- Object.defineProperty(exports, "OINO_DEBUG_PREFIX", { enumerable: true, get: function () { return types_2.OINO_DEBUG_PREFIX; } });
11
- Object.defineProperty(exports, "OINOStr", { enumerable: true, get: function () { return types_2.OINOStr; } });
12
- Object.defineProperty(exports, "OINOBenchmark", { enumerable: true, get: function () { return types_2.OINOBenchmark; } });
13
- Object.defineProperty(exports, "OINOLog", { enumerable: true, get: function () { return types_2.OINOLog; } });
14
- Object.defineProperty(exports, "OINOLogLevel", { enumerable: true, get: function () { return types_2.OINOLogLevel; } });
15
- Object.defineProperty(exports, "OINOConsoleLog", { enumerable: true, get: function () { return types_2.OINOConsoleLog; } });
16
- Object.defineProperty(exports, "OINOResult", { enumerable: true, get: function () { return types_2.OINOResult; } });
17
- Object.defineProperty(exports, "OINOHttpResult", { enumerable: true, get: function () { return types_2.OINOHttpResult; } });
18
- Object.defineProperty(exports, "OINOHtmlTemplate", { enumerable: true, get: function () { return types_2.OINOHtmlTemplate; } });
3
+ exports.OINODB_EMPTY_ROWS = exports.OINODB_EMPTY_ROW = exports.OINODbParser = exports.OINODbSwagger = exports.OINODbFactory = exports.OINODbConfig = exports.OINODbSqlBooleanOperation = exports.OINODbSqlLimit = exports.OINODbSqlComparison = exports.OINODbSqlOrder = exports.OINODbSqlFilter = exports.OINODb = exports.OINODbMemoryDataSet = exports.OINODbDataSet = exports.OINODatetimeDataField = exports.OINOBlobDataField = exports.OINOStringDataField = exports.OINONumberDataField = exports.OINOBooleanDataField = exports.OINODbDataField = exports.OINODbModelSet = exports.OINODbDataModel = exports.OINODbApi = exports.OINODbHtmlTemplate = exports.OINODbApiResult = exports.OINOHtmlTemplate = exports.OINOHttpResult = exports.OINOResult = exports.OINOConsoleLog = exports.OINOLogLevel = exports.OINOLog = exports.OINOBenchmark = exports.OINOStr = exports.OINO_DEBUG_PREFIX = exports.OINO_INFO_PREFIX = exports.OINO_WARNING_PREFIX = exports.OINO_ERROR_PREFIX = exports.OINOContentType = void 0;
4
+ const common_1 = require("@oino-ts/common");
5
+ Object.defineProperty(exports, "OINOContentType", { enumerable: true, get: function () { return common_1.OINOContentType; } });
6
+ var common_2 = require("@oino-ts/common");
7
+ Object.defineProperty(exports, "OINO_ERROR_PREFIX", { enumerable: true, get: function () { return common_2.OINO_ERROR_PREFIX; } });
8
+ Object.defineProperty(exports, "OINO_WARNING_PREFIX", { enumerable: true, get: function () { return common_2.OINO_WARNING_PREFIX; } });
9
+ Object.defineProperty(exports, "OINO_INFO_PREFIX", { enumerable: true, get: function () { return common_2.OINO_INFO_PREFIX; } });
10
+ Object.defineProperty(exports, "OINO_DEBUG_PREFIX", { enumerable: true, get: function () { return common_2.OINO_DEBUG_PREFIX; } });
11
+ Object.defineProperty(exports, "OINOStr", { enumerable: true, get: function () { return common_2.OINOStr; } });
12
+ Object.defineProperty(exports, "OINOBenchmark", { enumerable: true, get: function () { return common_2.OINOBenchmark; } });
13
+ Object.defineProperty(exports, "OINOLog", { enumerable: true, get: function () { return common_2.OINOLog; } });
14
+ Object.defineProperty(exports, "OINOLogLevel", { enumerable: true, get: function () { return common_2.OINOLogLevel; } });
15
+ Object.defineProperty(exports, "OINOConsoleLog", { enumerable: true, get: function () { return common_2.OINOConsoleLog; } });
16
+ Object.defineProperty(exports, "OINOResult", { enumerable: true, get: function () { return common_2.OINOResult; } });
17
+ Object.defineProperty(exports, "OINOHttpResult", { enumerable: true, get: function () { return common_2.OINOHttpResult; } });
18
+ Object.defineProperty(exports, "OINOHtmlTemplate", { enumerable: true, get: function () { return common_2.OINOHtmlTemplate; } });
19
19
  var OINODbApi_js_1 = require("./OINODbApi.js");
20
20
  Object.defineProperty(exports, "OINODbApiResult", { enumerable: true, get: function () { return OINODbApi_js_1.OINODbApiResult; } });
21
21
  Object.defineProperty(exports, "OINODbHtmlTemplate", { enumerable: true, get: function () { return OINODbApi_js_1.OINODbHtmlTemplate; } });
@@ -32,10 +32,9 @@ Object.defineProperty(exports, "OINOStringDataField", { enumerable: true, get: f
32
32
  Object.defineProperty(exports, "OINOBlobDataField", { enumerable: true, get: function () { return OINODbDataField_js_1.OINOBlobDataField; } });
33
33
  Object.defineProperty(exports, "OINODatetimeDataField", { enumerable: true, get: function () { return OINODbDataField_js_1.OINODatetimeDataField; } });
34
34
  var OINODb_js_1 = require("./OINODb.js");
35
+ Object.defineProperty(exports, "OINODbDataSet", { enumerable: true, get: function () { return OINODb_js_1.OINODbDataSet; } });
36
+ Object.defineProperty(exports, "OINODbMemoryDataSet", { enumerable: true, get: function () { return OINODb_js_1.OINODbMemoryDataSet; } });
35
37
  Object.defineProperty(exports, "OINODb", { enumerable: true, get: function () { return OINODb_js_1.OINODb; } });
36
- var OINODbDataSet_js_1 = require("./OINODbDataSet.js");
37
- Object.defineProperty(exports, "OINODbDataSet", { enumerable: true, get: function () { return OINODbDataSet_js_1.OINODbDataSet; } });
38
- Object.defineProperty(exports, "OINODbMemoryDataSet", { enumerable: true, get: function () { return OINODbDataSet_js_1.OINODbMemoryDataSet; } });
39
38
  var OINODbSqlParams_js_1 = require("./OINODbSqlParams.js");
40
39
  Object.defineProperty(exports, "OINODbSqlFilter", { enumerable: true, get: function () { return OINODbSqlParams_js_1.OINODbSqlFilter; } });
41
40
  Object.defineProperty(exports, "OINODbSqlOrder", { enumerable: true, get: function () { return OINODbSqlParams_js_1.OINODbSqlOrder; } });
@@ -48,6 +47,8 @@ var OINODbFactory_js_1 = require("./OINODbFactory.js");
48
47
  Object.defineProperty(exports, "OINODbFactory", { enumerable: true, get: function () { return OINODbFactory_js_1.OINODbFactory; } });
49
48
  var OINODbSwagger_js_1 = require("./OINODbSwagger.js");
50
49
  Object.defineProperty(exports, "OINODbSwagger", { enumerable: true, get: function () { return OINODbSwagger_js_1.OINODbSwagger; } });
50
+ var OINODbParser_js_1 = require("./OINODbParser.js");
51
+ Object.defineProperty(exports, "OINODbParser", { enumerable: true, get: function () { return OINODbParser_js_1.OINODbParser; } });
51
52
  /** Empty row instance */
52
53
  exports.OINODB_EMPTY_ROW = [];
53
54
  /** Empty row array instance */
@@ -3,6 +3,7 @@
3
3
  * License, v. 2.0. If a copy of the MPL was not distributed with this
4
4
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
+ import { OINO_ERROR_PREFIX, OINODB_EMPTY_ROW } from "./index.js";
6
7
  /**
7
8
  * Base class for database abstraction, implementing methods for connecting, making queries and parsing/formatting data
8
9
  * between SQL and serialization formats.
@@ -47,3 +48,131 @@ export class OINODb {
47
48
  return result;
48
49
  }
49
50
  }
51
+ /**
52
+ * Base class for SQL results that can be asynchronously iterated (but
53
+ * not necessarity rewinded). Idea is to handle database specific mechanisms
54
+ * for returning and formatting conventions in the database specific
55
+ * implementation. Data might be in memory or streamed in chunks and
56
+ * `OINODbDataSet` will serve it out consistently.
57
+ *
58
+ */
59
+ export class OINODbDataSet {
60
+ _data;
61
+ /** Error messages */
62
+ messages;
63
+ /**
64
+ * Constructor for `OINODbDataSet`.
65
+ *
66
+ * @param data internal database specific data type (constructor will throw if invalid)
67
+ * @param messages error messages from SQL-query
68
+ *
69
+ */
70
+ constructor(data, messages = []) {
71
+ this._data = data;
72
+ this.messages = messages;
73
+ }
74
+ /**
75
+ * Checks if the messages contain errors.
76
+ *
77
+ */
78
+ hasErrors() {
79
+ for (let i = 0; i < this.messages.length; i++) {
80
+ if (this.messages[i].startsWith(OINO_ERROR_PREFIX)) {
81
+ return true;
82
+ }
83
+ }
84
+ return false;
85
+ }
86
+ /**
87
+ * Checks if the messages contain errors.
88
+ *
89
+ */
90
+ getFirstError() {
91
+ for (let i = 0; i < this.messages.length; i++) {
92
+ if (this.messages[i].startsWith(OINO_ERROR_PREFIX)) {
93
+ return this.messages[i];
94
+ }
95
+ }
96
+ return "";
97
+ }
98
+ }
99
+ /**
100
+ * Generic in memory implementation of a data set where data is an array of rows. Used
101
+ * by BunSqlite and automated testing. Can be rewinded.
102
+ *
103
+ */
104
+ export class OINODbMemoryDataSet extends OINODbDataSet {
105
+ _rows;
106
+ _currentRow;
107
+ _eof;
108
+ /**
109
+ * Constructor of `OINODbMemoryDataSet`.
110
+ *
111
+ * @param data data as OINODataRow[] (constructor will throw if invalid)
112
+ * @param errors error messages from SQL-query
113
+ *
114
+ */
115
+ constructor(data, errors = []) {
116
+ super(data, errors);
117
+ if ((data == null) || !(Array.isArray(data))) {
118
+ throw new Error(OINO_ERROR_PREFIX + ": Data needs to be compatible with OINORow[]!"); // TODO: maybe check all rows
119
+ }
120
+ this._rows = data;
121
+ if (this.isEmpty()) {
122
+ this._currentRow = -1;
123
+ this._eof = true;
124
+ }
125
+ else {
126
+ this._currentRow = 0;
127
+ this._eof = false;
128
+ }
129
+ }
130
+ /**
131
+ * Is data set empty.
132
+ *
133
+ */
134
+ isEmpty() {
135
+ return (this._rows.length == 0);
136
+ }
137
+ /**
138
+ * Is there no more content, i.e. either dataset is empty or we have moved beyond last line
139
+ *
140
+ */
141
+ isEof() {
142
+ return (this._eof);
143
+ }
144
+ /**
145
+ * Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
146
+ *
147
+ */
148
+ async next() {
149
+ if (this._currentRow < this._rows.length - 1) {
150
+ this._currentRow = this._currentRow + 1;
151
+ }
152
+ else {
153
+ this._eof = true;
154
+ }
155
+ return Promise.resolve(!this._eof);
156
+ }
157
+ /**
158
+ * Gets current row of data.
159
+ *
160
+ */
161
+ getRow() {
162
+ if ((this._currentRow >= 0) && (this._currentRow < this._rows.length)) {
163
+ return this._rows[this._currentRow];
164
+ }
165
+ else {
166
+ return OINODB_EMPTY_ROW;
167
+ }
168
+ }
169
+ /**
170
+ * Rewinds data set to the first row, returns !isEof().
171
+ *
172
+ */
173
+ first() {
174
+ this._currentRow = 0;
175
+ this._eof = this._rows.length == 0;
176
+ return !this._eof;
177
+ }
178
+ }
@@ -3,10 +3,9 @@
3
3
  * License, v. 2.0. If a copy of the MPL was not distributed with this
4
4
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
- import { OINODbDataModel, OINOStringDataField, OINO_ERROR_PREFIX, OINODbModelSet, OINOBenchmark, OINODbConfig, OINOHtmlTemplate, OINONumberDataField } from "./index.js";
7
- import { OINOResult } from "@oino-ts/types";
6
+ import { OINODbDataModel, OINOStringDataField, OINO_ERROR_PREFIX, OINODbModelSet, OINOBenchmark, OINODbConfig, OINOHtmlTemplate, OINONumberDataField, OINODbParser } from "./index.js";
7
+ import { OINOResult } from "@oino-ts/common";
8
8
  import { OINOHashid } from "@oino-ts/hashid";
9
- import { OINOParser } from "@oino-ts/types";
10
9
  const API_EMPTY_PARAMS = { sqlParams: {} };
11
10
  /**
12
11
  * OINO API request result object with returned data and/or http status code/message and
@@ -291,7 +290,7 @@ export class OINODbApi {
291
290
  rows = body;
292
291
  }
293
292
  else {
294
- rows = OINOParser.createRows(this.datamodel, body, params);
293
+ rows = OINODbParser.createRows(this.datamodel, body, params);
295
294
  }
296
295
  }
297
296
  catch (e) {