@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.
- package/dist/cjs/OINODbApi.js +131 -59
- package/dist/cjs/OINODbConfig.js +0 -4
- package/dist/cjs/OINODbFactory.js +2 -67
- package/dist/cjs/OINODbParser.js +30 -27
- package/dist/cjs/OINODbSwagger.js +4 -4
- package/dist/cjs/index.js +2 -1
- package/dist/esm/OINODbApi.js +131 -60
- package/dist/esm/OINODbConfig.js +0 -4
- package/dist/esm/OINODbFactory.js +3 -68
- package/dist/esm/OINODbParser.js +30 -27
- package/dist/esm/OINODbSwagger.js +4 -4
- package/dist/esm/index.js +1 -1
- package/dist/types/OINODb.d.ts +2 -2
- package/dist/types/OINODbApi.d.ts +46 -16
- package/dist/types/OINODbConfig.d.ts +0 -4
- package/dist/types/OINODbFactory.d.ts +1 -7
- package/dist/types/OINODbParser.d.ts +11 -10
- package/dist/types/index.d.ts +1 -16
- package/package.json +37 -37
- package/src/OINODb.ts +321 -321
- package/src/OINODbApi.test.ts +492 -499
- package/src/OINODbApi.ts +602 -517
- package/src/OINODbConfig.ts +98 -104
- package/src/OINODbDataField.ts +403 -403
- package/src/OINODbDataModel.ts +291 -291
- package/src/OINODbFactory.ts +68 -137
- package/src/OINODbModelSet.ts +351 -351
- package/src/OINODbParser.ts +447 -443
- package/src/OINODbSqlParams.ts +592 -592
- package/src/OINODbSwagger.ts +208 -208
- package/src/index.ts +120 -136
package/src/OINODbFactory.ts
CHANGED
|
@@ -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,
|
|
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.
|
|
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.
|
|
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
|
}
|