@oino-ts/db-mariadb 0.0.15 → 0.0.16

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/README.md CHANGED
@@ -10,15 +10,15 @@
10
10
  # GETTING STARTED
11
11
 
12
12
  ### Setup
13
- Install the `@oino-ts/core` npm package and necessary database packages and import them in your code.
13
+ Install the `@oino-ts/db` npm package and necessary database packages and import them in your code.
14
14
  ```
15
- bun install @oino-ts/core
16
- bun install @oino-ts/bunsqlite
15
+ bun install @oino-ts/db
16
+ bun install @oino-ts/db-bunsqlite
17
17
  ```
18
18
 
19
19
  ```
20
- import { OINODb, OINOApi, OINOFactory } from "@oino-ts/core";
21
- import { OINODbBunSqlite } from "@oino-ts/bunsqlite"
20
+ import { OINODb, OINOApi, OINOFactory } from "@oino-ts/db";
21
+ import { OINODbBunSqlite } from "@oino-ts/db-bunsqlite"
22
22
  ```
23
23
 
24
24
  ### Register database and logger
@@ -51,7 +51,7 @@
51
51
  ### Write results back to HTTP Response
52
52
  The results for a GET request will contain [`OINOModelSet`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbModelSet.html) data that can be written out as JSON or CSV as needed. For other requests result is just success or error with messages.
53
53
  ```
54
- return new Response(result.modelset.writeString(OINOContentType.json))
54
+ return new Response(result.data.writeString(OINOContentType.json))
55
55
  ```
56
56
 
57
57
 
@@ -129,12 +129,13 @@
129
129
  - Bun Sqlite through Bun native implementation
130
130
  - Postgresql through [pg](https://www.npmjs.com/package/pg)-package
131
131
  - Mariadb / Mysql-support through [mariadb](https://www.npmjs.com/package/mariadb)-package
132
+ - Sql Server through [mssql](https://www.npmjs.com/package/mssql)-package
132
133
 
133
134
  ## Complex Keys
134
135
  To support tables with multipart primary keys OINO generates a composite key `_OINOID_` that is included in the result and can be used as the REST ID. For example in the example above table `OrderDetails` has two primary keys `OrderID` and `ProductID` making the `_OINOID_` of form `11077:99`.
135
136
 
136
137
  ## Power Of SQL
137
- Since OINO controls the SQL, WHERE-conditions can be defined with [`OINOSqlFilter`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlFilter.html) and order with [`OINOSqlOrder`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlOrder.html) that are passed as HTTP request parameters. No more API development where you make unique API endpoints for each filter that fetch all data with original API and filter in backend code. Every API can be filtered when and as needed without unnessecary data tranfer and utilizing SQL indexing when available.
138
+ Since OINO is just generating SQL, WHERE-conditions can be defined with [`OINOSqlFilter`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlFilter.html) and order with [`OINOSqlOrder`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlOrder.html) that are passed as HTTP request parameters. No more API development where you make unique API endpoints for each filter that fetch all data with original API and filter in backend code. Every API can be filtered when and as needed without unnessecary data tranfer and utilizing SQL indexing when available.
138
139
 
139
140
  ## Swagger Support
140
141
  Swagger is great as long as the definitions are updated and with OINO you can automatically get a Swagger definition including a data model schema.
@@ -158,7 +159,7 @@
158
159
  # STATUS
159
160
  OINO is currently a hobby project which should and should considered in alpha status. That also means compatibility breaking changes can be made without prior notice when architectual issues are discovered.
160
161
 
161
- ## Beta
162
+ ## Beta
162
163
  For a beta status following milestones are planned:
163
164
 
164
165
  ### Realistic app
@@ -205,14 +206,16 @@
205
206
  # LINKS
206
207
  - [Github repository](https://github.com/pragmatta/oino-ts)
207
208
  - [NPM repository](https://www.npmjs.com/org/oino-ts)
208
-
209
+
209
210
 
210
211
  # ACKNOWLEDGEMENTS
211
212
 
212
213
  ## Libraries
213
214
  OINO uses the following open source libraries and npm packages and I would like to thank everyone for their contributions:
214
- - Postgresql support by [node-postgres package](https://www.npmjs.com/package/pg)
215
- - Mariadb / Mysql-support by [mariadb package](https://www.npmjs.com/package/mariadb)
215
+ - Postgresql [node-postgres package](https://www.npmjs.com/package/pg)
216
+ - Mariadb / Mysql [mariadb package](https://www.npmjs.com/package/mariadb)
217
+ - Sql Server [mssql package](https://www.npmjs.com/package/mssql)
218
+ - Custom base encoding [base-x package](https://www.npmjs.com/package/base-x)
216
219
 
217
220
  ## Bun
218
221
  OINO has been developed using the Bun runtime, not because of the speed improvements but for the first class Typescript support and integrated developper experience. Kudos on the bun team for making Typescript work more exiting again.
@@ -38,14 +38,25 @@ class OINOMariadbData extends db_1.OINODbDataSet {
38
38
  }
39
39
  _currentRow;
40
40
  _eof;
41
+ /**
42
+ * Is data set empty.
43
+ *
44
+ */
41
45
  isEmpty() {
42
46
  return (this._rows.length == 0);
43
47
  }
44
- // EOF means "there is no more content", i.e. either dataset is empty or we have moved beyond last line
48
+ /**
49
+ * Is there no more content, i.e. either dataset is empty or we have moved beyond last line
50
+ *
51
+ */
45
52
  isEof() {
46
53
  return (this._eof);
47
54
  }
48
- next() {
55
+ /**
56
+ * Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
57
+ *
58
+ */
59
+ async next() {
49
60
  // OINOLog.debug("OINODbDataSet.next", {currentRow:this._currentRow, length:this.sqlResult.data.length})
50
61
  if (this._currentRow < this._rows.length - 1) {
51
62
  this._currentRow = this._currentRow + 1;
@@ -53,8 +64,12 @@ class OINOMariadbData extends db_1.OINODbDataSet {
53
64
  else {
54
65
  this._eof = true;
55
66
  }
56
- return !this._eof;
67
+ return Promise.resolve(!this._eof);
57
68
  }
69
+ /**
70
+ * Gets current row of data.
71
+ *
72
+ */
58
73
  getRow() {
59
74
  if ((this._currentRow >= 0) && (this._currentRow < this._rows.length)) {
60
75
  return this._rows[this._currentRow];
@@ -71,7 +86,6 @@ class OINOMariadbData extends db_1.OINODbDataSet {
71
86
  class OINODbMariadb extends db_1.OINODb {
72
87
  static _fieldLengthRegex = /([^\(\)]+)(\s?\((\d+)\s?\,?\s?(\d*)?\))?/i;
73
88
  static _exceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i;
74
- static _tableSchemaSql = `SHOW COLUMNS from `;
75
89
  _pool;
76
90
  /**
77
91
  * Constructor of `OINODbMariadb`
@@ -256,7 +270,7 @@ class OINODbMariadb extends db_1.OINODb {
256
270
  }
257
271
  catch (err) {
258
272
  // ... error checks
259
- throw new Error(db_1.OINO_ERROR_PREFIX + ": Error connecting to Postgresql server: " + err);
273
+ throw new Error(db_1.OINO_ERROR_PREFIX + ": Error connecting to OINODbMariadb server: " + err);
260
274
  }
261
275
  }
262
276
  /**
@@ -299,6 +313,10 @@ class OINODbMariadb extends db_1.OINODb {
299
313
  db_1.OINOBenchmark.end("OINODb", "sqlExec");
300
314
  return result;
301
315
  }
316
+ _getSchemaSql(dbName, tableName) {
317
+ const sql = `SHOW COLUMNS from ${dbName}.${tableName};`;
318
+ return sql;
319
+ }
302
320
  /**
303
321
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
304
322
  * the model.
@@ -307,7 +325,7 @@ class OINODbMariadb extends db_1.OINODb {
307
325
  *
308
326
  */
309
327
  async initializeApiDatamodel(api) {
310
- const res = await this.sqlSelect(OINODbMariadb._tableSchemaSql + this._params.database + "." + api.params.tableName + ";");
328
+ const res = await this.sqlSelect(this._getSchemaSql(this._params.database, api.params.tableName));
311
329
  while (!res.isEof()) {
312
330
  const row = res.getRow();
313
331
  // OINOLog.debug("OINODbMariadb.initializeApiDatamodel", { description:row })
@@ -361,7 +379,7 @@ class OINODbMariadb extends db_1.OINODb {
361
379
  api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, 0));
362
380
  }
363
381
  }
364
- res.next();
382
+ await res.next();
365
383
  }
366
384
  db_1.OINOLog.debug("OINODbMariadb.initializeDatasetModel:\n" + api.datamodel.printDebug("\n"));
367
385
  return Promise.resolve();
@@ -35,14 +35,25 @@ class OINOMariadbData extends OINODbDataSet {
35
35
  }
36
36
  _currentRow;
37
37
  _eof;
38
+ /**
39
+ * Is data set empty.
40
+ *
41
+ */
38
42
  isEmpty() {
39
43
  return (this._rows.length == 0);
40
44
  }
41
- // EOF means "there is no more content", i.e. either dataset is empty or we have moved beyond last line
45
+ /**
46
+ * Is there no more content, i.e. either dataset is empty or we have moved beyond last line
47
+ *
48
+ */
42
49
  isEof() {
43
50
  return (this._eof);
44
51
  }
45
- next() {
52
+ /**
53
+ * Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
54
+ *
55
+ */
56
+ async next() {
46
57
  // OINOLog.debug("OINODbDataSet.next", {currentRow:this._currentRow, length:this.sqlResult.data.length})
47
58
  if (this._currentRow < this._rows.length - 1) {
48
59
  this._currentRow = this._currentRow + 1;
@@ -50,8 +61,12 @@ class OINOMariadbData extends OINODbDataSet {
50
61
  else {
51
62
  this._eof = true;
52
63
  }
53
- return !this._eof;
64
+ return Promise.resolve(!this._eof);
54
65
  }
66
+ /**
67
+ * Gets current row of data.
68
+ *
69
+ */
55
70
  getRow() {
56
71
  if ((this._currentRow >= 0) && (this._currentRow < this._rows.length)) {
57
72
  return this._rows[this._currentRow];
@@ -68,7 +83,6 @@ class OINOMariadbData extends OINODbDataSet {
68
83
  export class OINODbMariadb extends OINODb {
69
84
  static _fieldLengthRegex = /([^\(\)]+)(\s?\((\d+)\s?\,?\s?(\d*)?\))?/i;
70
85
  static _exceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i;
71
- static _tableSchemaSql = `SHOW COLUMNS from `;
72
86
  _pool;
73
87
  /**
74
88
  * Constructor of `OINODbMariadb`
@@ -253,7 +267,7 @@ export class OINODbMariadb extends OINODb {
253
267
  }
254
268
  catch (err) {
255
269
  // ... error checks
256
- throw new Error(OINO_ERROR_PREFIX + ": Error connecting to Postgresql server: " + err);
270
+ throw new Error(OINO_ERROR_PREFIX + ": Error connecting to OINODbMariadb server: " + err);
257
271
  }
258
272
  }
259
273
  /**
@@ -296,6 +310,10 @@ export class OINODbMariadb extends OINODb {
296
310
  OINOBenchmark.end("OINODb", "sqlExec");
297
311
  return result;
298
312
  }
313
+ _getSchemaSql(dbName, tableName) {
314
+ const sql = `SHOW COLUMNS from ${dbName}.${tableName};`;
315
+ return sql;
316
+ }
299
317
  /**
300
318
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
301
319
  * the model.
@@ -304,7 +322,7 @@ export class OINODbMariadb extends OINODb {
304
322
  *
305
323
  */
306
324
  async initializeApiDatamodel(api) {
307
- const res = await this.sqlSelect(OINODbMariadb._tableSchemaSql + this._params.database + "." + api.params.tableName + ";");
325
+ const res = await this.sqlSelect(this._getSchemaSql(this._params.database, api.params.tableName));
308
326
  while (!res.isEof()) {
309
327
  const row = res.getRow();
310
328
  // OINOLog.debug("OINODbMariadb.initializeApiDatamodel", { description:row })
@@ -358,7 +376,7 @@ export class OINODbMariadb extends OINODb {
358
376
  api.datamodel.addField(new OINOStringDataField(this, field_name, sql_type, field_params, 0));
359
377
  }
360
378
  }
361
- res.next();
379
+ await res.next();
362
380
  }
363
381
  OINOLog.debug("OINODbMariadb.initializeDatasetModel:\n" + api.datamodel.printDebug("\n"));
364
382
  return Promise.resolve();
@@ -6,7 +6,6 @@ import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINODataCell } from "@o
6
6
  export declare class OINODbMariadb extends OINODb {
7
7
  private static _fieldLengthRegex;
8
8
  private static _exceptionMessageRegex;
9
- private static _tableSchemaSql;
10
9
  private _pool;
11
10
  /**
12
11
  * Constructor of `OINODbMariadb`
@@ -67,6 +66,7 @@ export declare class OINODbMariadb extends OINODb {
67
66
  *
68
67
  */
69
68
  sqlExec(sql: string): Promise<OINODbDataSet>;
69
+ private _getSchemaSql;
70
70
  /**
71
71
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
72
72
  * the model.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/db-mariadb",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "description": "OINO TS package for using Mariadb databases.",
5
5
  "author": "Matias Kiviniemi (pragmatta)",
6
6
  "license": "MPL-2.0",
@@ -21,7 +21,7 @@
21
21
  "module": "./dist/esm/index.js",
22
22
  "types": "./dist/types/index.d.ts",
23
23
  "dependencies": {
24
- "@oino-ts/db": "^0.0.15",
24
+ "@oino-ts/db": "^0.0.16",
25
25
  "mariadb": "3.2.3"
26
26
  },
27
27
  "devDependencies": {
@@ -41,25 +41,40 @@ class OINOMariadbData extends OINODbDataSet {
41
41
  private _currentRow: number
42
42
  private _eof: boolean
43
43
 
44
+ /**
45
+ * Is data set empty.
46
+ *
47
+ */
44
48
  isEmpty():boolean {
45
49
  return (this._rows.length == 0)
46
50
  }
47
51
 
48
- // EOF means "there is no more content", i.e. either dataset is empty or we have moved beyond last line
52
+ /**
53
+ * Is there no more content, i.e. either dataset is empty or we have moved beyond last line
54
+ *
55
+ */
49
56
  isEof():boolean {
50
57
  return (this._eof)
51
58
  }
52
59
 
53
- next():boolean {
60
+ /**
61
+ * Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
62
+ *
63
+ */
64
+ async next():Promise<boolean> {
54
65
  // OINOLog.debug("OINODbDataSet.next", {currentRow:this._currentRow, length:this.sqlResult.data.length})
55
66
  if (this._currentRow < this._rows.length-1) {
56
67
  this._currentRow = this._currentRow + 1
57
68
  } else {
58
69
  this._eof = true
59
70
  }
60
- return !this._eof
71
+ return Promise.resolve(!this._eof)
61
72
  }
62
73
 
74
+ /**
75
+ * Gets current row of data.
76
+ *
77
+ */
63
78
  getRow(): OINODataRow {
64
79
  if ((this._currentRow >=0) && (this._currentRow < this._rows.length)) {
65
80
  return this._rows[this._currentRow]
@@ -77,7 +92,6 @@ export class OINODbMariadb extends OINODb {
77
92
 
78
93
  private static _fieldLengthRegex = /([^\(\)]+)(\s?\((\d+)\s?\,?\s?(\d*)?\))?/i
79
94
  private static _exceptionMessageRegex = /\(([^\)]*)\) (.*)\nsql\:(.*)?/i
80
- private static _tableSchemaSql:string = `SHOW COLUMNS from `
81
95
 
82
96
  private _pool:mariadb.Pool
83
97
 
@@ -269,7 +283,7 @@ export class OINODbMariadb extends OINODb {
269
283
  return Promise.resolve(true)
270
284
  } catch (err) {
271
285
  // ... error checks
272
- throw new Error(OINO_ERROR_PREFIX + ": Error connecting to Postgresql server: " + err)
286
+ throw new Error(OINO_ERROR_PREFIX + ": Error connecting to OINODbMariadb server: " + err)
273
287
  }
274
288
  }
275
289
 
@@ -315,6 +329,11 @@ export class OINODbMariadb extends OINODb {
315
329
  return result
316
330
  }
317
331
 
332
+ private _getSchemaSql(dbName:string, tableName:string):string {
333
+ const sql = `SHOW COLUMNS from ${dbName}.${tableName};`
334
+ return sql
335
+ }
336
+
318
337
  /**
319
338
  * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
320
339
  * the model.
@@ -324,7 +343,7 @@ export class OINODbMariadb extends OINODb {
324
343
  */
325
344
  async initializeApiDatamodel(api:OINODbApi): Promise<void> {
326
345
 
327
- const res:OINODbDataSet = await this.sqlSelect(OINODbMariadb._tableSchemaSql + this._params.database + "." + api.params.tableName + ";")
346
+ const res:OINODbDataSet = await this.sqlSelect(this._getSchemaSql(this._params.database, api.params.tableName))
328
347
  while (!res.isEof()) {
329
348
  const row:OINODataRow = res.getRow()
330
349
  // OINOLog.debug("OINODbMariadb.initializeApiDatamodel", { description:row })
@@ -375,7 +394,7 @@ export class OINODbMariadb extends OINODb {
375
394
  api.datamodel.addField(new OINOStringDataField(this, field_name, sql_type, field_params, 0))
376
395
  }
377
396
  }
378
- res.next()
397
+ await res.next()
379
398
  }
380
399
  OINOLog.debug("OINODbMariadb.initializeDatasetModel:\n" + api.datamodel.printDebug("\n"))
381
400
  return Promise.resolve()