@oino-ts/db 0.5.2 → 0.6.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.
@@ -16,13 +16,15 @@ class OINODb {
16
16
  _params;
17
17
  /** Name of the database */
18
18
  name;
19
+ isConnected = false;
20
+ isValidated = false;
19
21
  /**
20
22
  * Constructor for `OINODb`.
21
23
  * @param params database parameters
22
24
  */
23
25
  constructor(params) {
24
- this._params = params;
25
- this.name = params.database;
26
+ this._params = { ...params }; // make a shallow copy of params so that changes to them do not affect the original object
27
+ this.name = this._params.database;
26
28
  }
27
29
  /**
28
30
  * Print SQL select statement with DB specific formatting.
@@ -29,7 +29,7 @@ class OINODbFactory {
29
29
  *
30
30
  * @param params database connection parameters
31
31
  */
32
- static async createDb(params) {
32
+ static async createDb(params, connect = true, validate = true) {
33
33
  let result;
34
34
  let db_type = this._dbRegistry[params.type];
35
35
  if (db_type) {
@@ -38,7 +38,18 @@ class OINODbFactory {
38
38
  else {
39
39
  throw new Error("Unsupported database type: " + params.type);
40
40
  }
41
- await result.connect();
41
+ if (connect) {
42
+ const connect_res = await result.connect();
43
+ if (connect_res.success == false) {
44
+ throw new Error("Database connection failed: " + connect_res.statusMessage);
45
+ }
46
+ }
47
+ if (validate) {
48
+ const validate_res = await result.validate();
49
+ if (validate_res.success == false) {
50
+ throw new Error("Database validation failed: " + validate_res.statusMessage);
51
+ }
52
+ }
42
53
  return result;
43
54
  }
44
55
  /**
@@ -412,7 +412,7 @@ class OINODbSqlAggregate {
412
412
  result += f.printSqlColumnName() + ",";
413
413
  }
414
414
  }
415
- index_js_1.OINOLog.debug("OINODbSqlAggregate.toSql", { result: result });
415
+ // OINOLog.debug("OINODbSqlAggregate.toSql", {result:result})
416
416
  return result.substring(0, result.length - 1);
417
417
  }
418
418
  /**
@@ -13,13 +13,15 @@ export class OINODb {
13
13
  _params;
14
14
  /** Name of the database */
15
15
  name;
16
+ isConnected = false;
17
+ isValidated = false;
16
18
  /**
17
19
  * Constructor for `OINODb`.
18
20
  * @param params database parameters
19
21
  */
20
22
  constructor(params) {
21
- this._params = params;
22
- this.name = params.database;
23
+ this._params = { ...params }; // make a shallow copy of params so that changes to them do not affect the original object
24
+ this.name = this._params.database;
23
25
  }
24
26
  /**
25
27
  * Print SQL select statement with DB specific formatting.
@@ -26,7 +26,7 @@ export class OINODbFactory {
26
26
  *
27
27
  * @param params database connection parameters
28
28
  */
29
- static async createDb(params) {
29
+ static async createDb(params, connect = true, validate = true) {
30
30
  let result;
31
31
  let db_type = this._dbRegistry[params.type];
32
32
  if (db_type) {
@@ -35,7 +35,18 @@ export class OINODbFactory {
35
35
  else {
36
36
  throw new Error("Unsupported database type: " + params.type);
37
37
  }
38
- await result.connect();
38
+ if (connect) {
39
+ const connect_res = await result.connect();
40
+ if (connect_res.success == false) {
41
+ throw new Error("Database connection failed: " + connect_res.statusMessage);
42
+ }
43
+ }
44
+ if (validate) {
45
+ const validate_res = await result.validate();
46
+ if (validate_res.success == false) {
47
+ throw new Error("Database validation failed: " + validate_res.statusMessage);
48
+ }
49
+ }
39
50
  return result;
40
51
  }
41
52
  /**
@@ -406,7 +406,7 @@ export class OINODbSqlAggregate {
406
406
  result += f.printSqlColumnName() + ",";
407
407
  }
408
408
  }
409
- OINOLog.debug("OINODbSqlAggregate.toSql", { result: result });
409
+ // OINOLog.debug("OINODbSqlAggregate.toSql", {result:result})
410
410
  return result.substring(0, result.length - 1);
411
411
  }
412
412
  /**
@@ -1,4 +1,4 @@
1
- import { OINODbParams, OINODbApi, OINODataCell, OINODataRow } from "./index.js";
1
+ import { OINODbParams, OINODbApi, OINODataCell, OINODataRow, OINOResult } from "./index.js";
2
2
  /**
3
3
  * Base class for database abstraction, implementing methods for connecting, making queries and parsing/formatting data
4
4
  * between SQL and serialization formats.
@@ -8,6 +8,8 @@ export declare abstract class OINODb {
8
8
  protected _params: OINODbParams;
9
9
  /** Name of the database */
10
10
  readonly name: string;
11
+ protected isConnected: boolean;
12
+ protected isValidated: boolean;
11
13
  /**
12
14
  * Constructor for `OINODb`.
13
15
  * @param params database parameters
@@ -17,7 +19,12 @@ export declare abstract class OINODb {
17
19
  * Connect to database.
18
20
  *
19
21
  */
20
- abstract connect(): Promise<boolean>;
22
+ abstract connect(): Promise<OINOResult>;
23
+ /**
24
+ * Validate connection to database is working.
25
+ *
26
+ */
27
+ abstract validate(): Promise<OINOResult>;
21
28
  /**
22
29
  * Print a table name using database specific SQL escaping.
23
30
  *
@@ -18,7 +18,7 @@ export declare class OINODbFactory {
18
18
  *
19
19
  * @param params database connection parameters
20
20
  */
21
- static createDb(params: OINODbParams): Promise<OINODb>;
21
+ static createDb(params: OINODbParams, connect?: boolean, validate?: boolean): Promise<OINODb>;
22
22
  /**
23
23
  * Create API from parameters and calls initDatamodel on the datamodel.
24
24
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/db",
3
- "version": "0.5.2",
3
+ "version": "0.6.1",
4
4
  "description": "OINO TS library package for publishing an SQL database tables as a REST API.",
5
5
  "author": "Matias Kiviniemi (pragmatta)",
6
6
  "license": "MPL-2.0",
@@ -19,12 +19,12 @@
19
19
  "module": "./dist/esm/index.js",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "dependencies": {
22
- "@oino-ts/common": "0.5.2"
22
+ "@oino-ts/common": "0.6.1"
23
23
  },
24
24
  "devDependencies": {
25
25
  "@types/node": "^20.14.10",
26
26
  "@types/bun": "^1.1.14",
27
- "@oino-ts/types": "0.5.2",
27
+ "@oino-ts/types": "0.6.1",
28
28
  "typedoc": "^0.25.13"
29
29
  },
30
30
  "files": [
package/src/OINODb.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
6
 
7
- import { OINODbParams, OINODbApi, OINODataCell, OINO_ERROR_PREFIX, OINODataRow, OINODB_EMPTY_ROW } from "./index.js"
7
+ import { OINODbParams, OINODbApi, OINODataCell, OINO_ERROR_PREFIX, OINODataRow, OINODB_EMPTY_ROW, OINOResult } from "./index.js"
8
8
 
9
9
  /**
10
10
  * Base class for database abstraction, implementing methods for connecting, making queries and parsing/formatting data
@@ -18,20 +18,29 @@ export abstract class OINODb {
18
18
  /** Name of the database */
19
19
  readonly name:string
20
20
 
21
+ protected isConnected:boolean = false
22
+ protected isValidated:boolean = false
23
+
21
24
  /**
22
25
  * Constructor for `OINODb`.
23
26
  * @param params database parameters
24
27
  */
25
28
  constructor(params:OINODbParams) {
26
- this._params = params
27
- this.name = params.database
29
+ this._params = { ...params } // make a shallow copy of params so that changes to them do not affect the original object
30
+ this.name = this._params.database
28
31
  }
29
32
 
30
33
  /**
31
34
  * Connect to database.
32
35
  *
33
36
  */
34
- abstract connect(): Promise<boolean>
37
+ abstract connect(): Promise<OINOResult>
38
+
39
+ /**
40
+ * Validate connection to database is working.
41
+ *
42
+ */
43
+ abstract validate(): Promise<OINOResult>
35
44
 
36
45
  /**
37
46
  * Print a table name using database specific SQL escaping.
@@ -12,7 +12,7 @@ import { OINODbBunSqlite } from "@oino-ts/db-bunsqlite"
12
12
  import { OINODbPostgresql } from "@oino-ts/db-postgresql"
13
13
  import { OINODbMariadb } from "@oino-ts/db-mariadb"
14
14
  import { OINODbMsSql } from "@oino-ts/db-mssql"
15
- import { OINODbSqlAggregate } from "./OINODbSqlParams.js";
15
+ import { OINODbSqlAggregate, OINODbSqlSelect } from "./OINODbSqlParams.js";
16
16
 
17
17
  const OINODB_POSTGRESQL_TOKEN = process.env.OINODB_POSTGRESQL_TOKEN || console.error("OINODB_POSTGRESQL_TOKEN not set") || ""
18
18
  const OINODB_MARIADB_TOKEN = process.env.OINODB_MARIADB_TOKEN || console.error("OINODB_MARIADB_TOKEN not set") || ""
@@ -67,7 +67,7 @@ const api_tests:OINOTestParams[] = [
67
67
  name: "API 4",
68
68
  apiParams: { apiName: "OrderDetails", tableName: "OrderDetails" },
69
69
  requestParams: {
70
- sqlParams: { aggregate: OINODbSqlAggregate.parse("count(ProductID),avg(UnitPrice),sum(Quantity),max(Discount)") }
70
+ sqlParams: { aggregate: OINODbSqlAggregate.parse("count(OrderID),count(ProductID),avg(UnitPrice),sum(Quantity)"), select: OINODbSqlSelect.parse("OrderID,ProductID,UnitPrice,Quantity,Discount"), order: OINODbSqlOrder.parse("Discount asc") }
71
71
  },
72
72
  postRow: [10249,77,12.34,56,0],
73
73
  putRow: [10249,77,23.45,67,0]
@@ -140,9 +140,38 @@ function createApiTemplate(api:OINODbApi):OINODbHtmlTemplate {
140
140
 
141
141
  export async function OINOTestApi(dbParams:OINODbParams, testParams: OINOTestParams) {
142
142
  // OINOLog.info("OINOTestApi", {dbParams:dbParams, apiDataset:apiDataset})
143
+
144
+ let target_name:string = ""
145
+ if (testParams.name) {
146
+ target_name = "[" + testParams.name + "]"
147
+ }
148
+ const target_db:string = "[" + dbParams.type + "]"
149
+ let target_table:string = "[" + testParams.apiParams.tableName + "]"
150
+ let target_group:string = "[CONNECTION]"
151
+
152
+ if (dbParams.type != "OINODbBunSqlite") { // no passwords in BunSqlite, it will never fail
153
+ const wrong_pwd_params:OINODbParams = Object.assign({}, dbParams)
154
+ wrong_pwd_params.password = "WRONG_PASSWORD"
155
+ const wrong_pwd_db:OINODb = await OINODbFactory.createDb( wrong_pwd_params, false, false )
156
+ test(target_name + target_db + target_table + target_group + " connection error", async () => {
157
+ expect(wrong_pwd_db).toBeDefined()
158
+
159
+ const connect_res = await wrong_pwd_db.connect()
160
+ expect(connect_res.success).toBe(false)
161
+ expect(connect_res.statusMessage).toMatchSnapshot("CONNECTION ERROR")
162
+ })
163
+ }
164
+
165
+ // const db:OINODb = await OINODbFactory.createDb( dbParams )
143
166
  const db:OINODb = await OINODbFactory.createDb( dbParams )
167
+ test(target_name + target_db + target_table + target_group + " connection success", async () => {
168
+ expect(db).toBeDefined()
169
+ expect(db.isConnected).toBe(true)
170
+ expect(db.isValidated).toBe(true)
171
+ })
172
+
144
173
  const api:OINODbApi = await OINODbFactory.createApi(db, testParams.apiParams)
145
-
174
+
146
175
  const post_dataset:OINODbMemoryDataSet = new OINODbMemoryDataSet([testParams.postRow])
147
176
  const post_modelset:OINODbModelSet = new OINODbModelSet(api.datamodel, post_dataset)
148
177
 
@@ -160,18 +189,11 @@ export async function OINOTestApi(dbParams:OINODbParams, testParams: OINOTestPar
160
189
  request_params_with_filters.sqlParams = testParams.requestParams.sqlParams
161
190
  // OINOLog.debug("OINOTestApi", {request_params:request_params, request_params_with_filters:request_params_with_filters})
162
191
 
163
- let target_name:string = ""
164
- if (testParams.name) {
165
- target_name = "[" + testParams.name + "]"
166
- }
167
- const target_db:string = "[" + dbParams.type + "]"
168
- let target_table:string = "[" + testParams.apiParams.tableName + "]"
169
- let target_group:string = "[SCHEMA]"
170
-
171
192
  // test("dummy", () => {
172
193
  // expect({foo:"h\\i"}).toMatchSnapshot()
173
194
  // })
174
195
 
196
+ target_group = "[SCHEMA]"
175
197
  test(target_name + target_db + target_table + target_group + " public properties", async () => {
176
198
  expect(api.datamodel.printFieldPublicPropertiesJson()).toMatchSnapshot("SCHEMA")
177
199
  })
@@ -31,7 +31,7 @@ export class OINODbFactory {
31
31
  *
32
32
  * @param params database connection parameters
33
33
  */
34
- static async createDb(params:OINODbParams):Promise<OINODb> {
34
+ static async createDb(params:OINODbParams, connect:boolean = true, validate:boolean = true):Promise<OINODb> {
35
35
  let result:OINODb
36
36
  let db_type = this._dbRegistry[params.type]
37
37
  if (db_type) {
@@ -39,10 +39,22 @@ export class OINODbFactory {
39
39
  } else {
40
40
  throw new Error("Unsupported database type: " + params.type)
41
41
  }
42
- await result.connect()
42
+ if (connect) {
43
+ const connect_res = await result.connect()
44
+ if (connect_res.success == false) {
45
+ throw new Error("Database connection failed: " + connect_res.statusMessage)
46
+ }
47
+ }
48
+ if (validate) {
49
+ const validate_res = await result.validate()
50
+ if (validate_res.success == false) {
51
+ throw new Error("Database validation failed: " + validate_res.statusMessage)
52
+ }
53
+ }
43
54
  return result
44
55
  }
45
56
 
57
+
46
58
  /**
47
59
  * Create API from parameters and calls initDatamodel on the datamodel.
48
60
  *
@@ -411,7 +411,7 @@ export class OINODbSqlAggregate {
411
411
  result += f.printSqlColumnName() + ","
412
412
  }
413
413
  }
414
- OINOLog.debug("OINODbSqlAggregate.toSql", {result:result})
414
+ // OINOLog.debug("OINODbSqlAggregate.toSql", {result:result})
415
415
  return result.substring(0, result.length-1)
416
416
  }
417
417