@oino-ts/db 0.6.0 → 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,10 +38,17 @@ class OINODbFactory {
38
38
  else {
39
39
  throw new Error("Unsupported database type: " + params.type);
40
40
  }
41
- await result.connect();
42
- const validate_res = await result.validate();
43
- if (validate_res.success == false) {
44
- throw new Error("Database connection validation failed: " + validate_res.statusMessage);
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
+ }
45
52
  }
46
53
  return result;
47
54
  }
@@ -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,10 +35,17 @@ export class OINODbFactory {
35
35
  else {
36
36
  throw new Error("Unsupported database type: " + params.type);
37
37
  }
38
- await result.connect();
39
- const validate_res = await result.validate();
40
- if (validate_res.success == false) {
41
- throw new Error("Database connection validation failed: " + validate_res.statusMessage);
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
+ }
42
49
  }
43
50
  return result;
44
51
  }
@@ -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,7 @@ export declare abstract class OINODb {
17
19
  * Connect to database.
18
20
  *
19
21
  */
20
- abstract connect(): Promise<boolean>;
22
+ abstract connect(): Promise<OINOResult>;
21
23
  /**
22
24
  * Validate connection to database is working.
23
25
  *
@@ -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.6.0",
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.6.0"
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.6.0",
27
+ "@oino-ts/types": "0.6.1",
28
28
  "typedoc": "^0.25.13"
29
29
  },
30
30
  "files": [
package/src/OINODb.ts CHANGED
@@ -18,20 +18,23 @@ 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>
35
38
 
36
39
  /**
37
40
  * Validate connection to database is working.
@@ -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,14 +39,22 @@ export class OINODbFactory {
39
39
  } else {
40
40
  throw new Error("Unsupported database type: " + params.type)
41
41
  }
42
- await result.connect()
43
- const validate_res = await result.validate()
44
- if (validate_res.success == false) {
45
- throw new Error("Database connection validation failed: " + validate_res.statusMessage)
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
+ }
46
53
  }
47
54
  return result
48
55
  }
49
56
 
57
+
50
58
  /**
51
59
  * Create API from parameters and calls initDatamodel on the datamodel.
52
60
  *