@oino-ts/db 0.16.2 → 0.17.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.
@@ -1,138 +1,69 @@
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 { OINODbApi, OINODbApiParams, OINODbParams, OINOContentType, OINODb, OINODbConstructor, OINODbApiRequestParams, OINODbSqlFilter, OINODbConfig, OINODbSqlOrder, OINODbSqlLimit, OINODbSqlParams, OINODbSqlAggregate, OINODbSqlSelect, OINOLog } from "./index.js"
8
-
9
- /**
10
- * Static factory class for easily creating things based on data
11
- *
12
- */
13
- export class OINODbFactory {
14
- private static _dbRegistry:Record<string, OINODbConstructor> = {}
15
-
16
- /**
17
- * Register a supported database class. Used to enable those that are installed in the factory
18
- * instead of forcing everyone to install all database libraries.
19
- *
20
- * @param dbName name of the database implementation class
21
- * @param dbTypeClass constructor for creating a database of that type
22
- */
23
- static registerDb(dbName:string, dbTypeClass: OINODbConstructor):void {
24
- this._dbRegistry[dbName] = dbTypeClass
25
- }
26
-
27
- /**
28
- * Create database from parameters from the registered classes.
29
- *
30
- * @param params database connection parameters
31
- * @param connect if true, connects to the database
32
- * @param validate if true, validates the database connection
33
- */
34
- static async createDb(params:OINODbParams, connect:boolean = true, validate:boolean = true):Promise<OINODb> {
35
- let result:OINODb
36
- let db_type = this._dbRegistry[params.type]
37
- if (db_type) {
38
- result = new db_type(params)
39
- } else {
40
- throw new Error("Unsupported database type: " + params.type)
41
- }
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
- }
54
- return result
55
- }
56
-
57
-
58
- /**
59
- * Create API from parameters and calls initDatamodel on the datamodel.
60
- *
61
- * @param db databased used in API
62
- * @param params parameters of the API
63
- */
64
- static async createApi(db: OINODb, params: OINODbApiParams):Promise<OINODbApi> {
65
- let result:OINODbApi = new OINODbApi(db, params)
66
- await db.initializeApiDatamodel(result)
67
- return result
68
- }
69
-
70
- /**
71
- * Creates a key-value-collection from Javascript URL parameters.
72
- *
73
- * @param request HTTP Request
74
- */
75
- static createParamsFromRequest(request:Request):OINODbApiRequestParams {
76
- const url:URL = new URL(request.url)
77
- let sql_params:OINODbSqlParams = { }
78
- const filter = url.searchParams.get(OINODbConfig.OINODB_SQL_FILTER_PARAM)
79
- if (filter) {
80
- sql_params.filter = OINODbSqlFilter.parse(filter)
81
- }
82
- const order = url.searchParams.get(OINODbConfig.OINODB_SQL_ORDER_PARAM)
83
- if (order) {
84
- sql_params.order = OINODbSqlOrder.parse(order)
85
- }
86
- const limit = url.searchParams.get(OINODbConfig.OINODB_SQL_LIMIT_PARAM)
87
- if (limit) {
88
- sql_params.limit = OINODbSqlLimit.parse(limit)
89
- }
90
- const aggregate = url.searchParams.get(OINODbConfig.OINODB_SQL_AGGREGATE_PARAM)
91
- if (aggregate) {
92
- sql_params.aggregate = OINODbSqlAggregate.parse(aggregate)
93
- }
94
- const select = url.searchParams.get(OINODbConfig.OINODB_SQL_SELECT_PARAM)
95
- if (select) {
96
- sql_params.select = OINODbSqlSelect.parse(select)
97
- }
98
-
99
- let result:OINODbApiRequestParams = { sqlParams: sql_params }
100
-
101
- const request_type = url.searchParams.get(OINODbConfig.OINODB_REQUEST_TYPE) || request.headers.get("content-type") // content-type header can be overridden by query parameter
102
- if (request_type == OINOContentType.csv) {
103
- result.requestType = OINOContentType.csv
104
-
105
- } else if (request_type == OINOContentType.urlencode) {
106
- result.requestType = OINOContentType.urlencode
107
-
108
- } else if (request_type?.startsWith(OINOContentType.formdata)) {
109
- result.requestType = OINOContentType.formdata
110
- result.multipartBoundary = request_type.split('boundary=')[1] || ""
111
-
112
- } else {
113
- result.requestType = OINOContentType.json
114
- }
115
- const response_type = url.searchParams.get(OINODbConfig.OINODB_RESPONSE_TYPE) || request.headers.get("accept") // accept header can be overridden by query parameter
116
- const accept_types = response_type?.split(', ') || []
117
- for (let i=0; i<accept_types.length; i++) {
118
- if (Object.values(OINOContentType).includes(accept_types[i] as OINOContentType)) {
119
- result.responseType = accept_types[i] as OINOContentType
120
- break
121
- }
122
- }
123
- if (result.responseType === undefined) {
124
- result.responseType = OINOContentType.json
125
- }
126
- const last_modified = request.headers.get("if-modified-since")
127
- if (last_modified) {
128
- result.lastModified = new Date(last_modified).getTime()
129
- }
130
- const etags = request.headers.get("if-none-match")?.split(',').map(e => e.trim())
131
- if (etags) {
132
- result.etags = etags
133
- }
134
-
135
- OINOLog.debug("@oino-ts/db", "OINODbFactory", "createParamsFromRequest", "Result", {params:result})
136
- return result
137
- }
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 { OINODbApi, OINODbApiParams, OINODbParams, OINODb, OINODbConstructor } from "./index.js"
8
+
9
+ /**
10
+ * Static factory class for easily creating things based on data
11
+ *
12
+ */
13
+ export class OINODbFactory {
14
+ private static _dbRegistry:Record<string, OINODbConstructor> = {}
15
+
16
+ /**
17
+ * Register a supported database class. Used to enable those that are installed in the factory
18
+ * instead of forcing everyone to install all database libraries.
19
+ *
20
+ * @param dbName name of the database implementation class
21
+ * @param dbTypeClass constructor for creating a database of that type
22
+ */
23
+ static registerDb(dbName:string, dbTypeClass: OINODbConstructor):void {
24
+ this._dbRegistry[dbName] = dbTypeClass
25
+ }
26
+
27
+ /**
28
+ * Create database from parameters from the registered classes.
29
+ *
30
+ * @param params database connection parameters
31
+ * @param connect if true, connects to the database
32
+ * @param validate if true, validates the database connection
33
+ */
34
+ static async createDb(params:OINODbParams, connect:boolean = true, validate:boolean = true):Promise<OINODb> {
35
+ let result:OINODb
36
+ let db_type = this._dbRegistry[params.type]
37
+ if (db_type) {
38
+ result = new db_type(params)
39
+ } else {
40
+ throw new Error("Unsupported database type: " + params.type)
41
+ }
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.statusText)
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.statusText)
52
+ }
53
+ }
54
+ return result
55
+ }
56
+
57
+
58
+ /**
59
+ * Create API from parameters and calls initDatamodel on the datamodel.
60
+ *
61
+ * @param db databased used in API
62
+ * @param params parameters of the API
63
+ */
64
+ static async createApi(db: OINODb, params: OINODbApiParams):Promise<OINODbApi> {
65
+ let result:OINODbApi = new OINODbApi(db, params)
66
+ await db.initializeApiDatamodel(result)
67
+ return result
68
+ }
138
69
  }