@oino-ts/db 0.3.2 → 0.3.4
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/OINODb.js +3 -3
- package/dist/cjs/OINODbSqlParams.js +6 -5
- package/dist/cjs/index.js +3 -1
- package/dist/esm/OINODb.js +3 -3
- package/dist/esm/OINODbSqlParams.js +6 -5
- package/dist/esm/index.js +1 -1
- package/dist/types/OINODbSqlParams.d.ts +2 -2
- package/dist/types/index.d.ts +1 -1
- package/package.json +36 -36
- package/src/OINODb.ts +291 -291
- package/src/OINODbApi.test.ts +411 -411
- package/src/OINODbApi.ts +384 -384
- package/src/OINODbConfig.ts +95 -95
- package/src/OINODbDataField.ts +382 -382
- package/src/OINODbDataModel.ts +289 -289
- package/src/OINODbFactory.ts +124 -124
- package/src/OINODbModelSet.ts +298 -298
- package/src/OINODbParser.ts +457 -457
- package/src/OINODbSqlParams.ts +438 -437
- package/src/OINODbSwagger.ts +208 -208
- package/src/index.ts +130 -130
- package/README.md +0 -190
package/src/OINODbFactory.ts
CHANGED
|
@@ -1,125 +1,125 @@
|
|
|
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 } from "./index.js"
|
|
8
|
-
import { OINODbSqlAggregate } from "./OINODbSqlParams.js"
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Static factory class for easily creating things based on data
|
|
12
|
-
*
|
|
13
|
-
*/
|
|
14
|
-
export class OINODbFactory {
|
|
15
|
-
private static _dbRegistry:Record<string, OINODbConstructor> = {}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Register a supported database class. Used to enable those that are installed in the factory
|
|
19
|
-
* instead of forcing everyone to install all database libraries.
|
|
20
|
-
*
|
|
21
|
-
* @param dbName name of the database implementation class
|
|
22
|
-
* @param dbTypeClass constructor for creating a database of that type
|
|
23
|
-
*/
|
|
24
|
-
static registerDb(dbName:string, dbTypeClass: OINODbConstructor):void {
|
|
25
|
-
// OINOLog.debug("OINODbFactory.registerDb", {dbType:dbName})
|
|
26
|
-
this._dbRegistry[dbName] = dbTypeClass
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Create database from parameters from the registered classes.
|
|
32
|
-
*
|
|
33
|
-
* @param params database connection parameters
|
|
34
|
-
*/
|
|
35
|
-
static async createDb(params:OINODbParams):Promise<OINODb> {
|
|
36
|
-
let result:OINODb
|
|
37
|
-
let db_type = this._dbRegistry[params.type]
|
|
38
|
-
if (db_type) {
|
|
39
|
-
result = new db_type(params)
|
|
40
|
-
} else {
|
|
41
|
-
throw new Error("Unsupported database type: " + params.type)
|
|
42
|
-
}
|
|
43
|
-
await result.connect()
|
|
44
|
-
return result
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Create API from parameters and calls initDatamodel on the datamodel.
|
|
49
|
-
*
|
|
50
|
-
* @param db databased used in API
|
|
51
|
-
* @param params parameters of the API
|
|
52
|
-
*/
|
|
53
|
-
static async createApi(db: OINODb, params: OINODbApiParams):Promise<OINODbApi> {
|
|
54
|
-
let result:OINODbApi = new OINODbApi(db, params)
|
|
55
|
-
await db.initializeApiDatamodel(result)
|
|
56
|
-
return result
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Creates a key-value-collection from Javascript URL parameters.
|
|
61
|
-
*
|
|
62
|
-
* @param request HTTP Request
|
|
63
|
-
*/
|
|
64
|
-
static createParamsFromRequest(request:Request):OINODbApiRequestParams {
|
|
65
|
-
const url:URL = new URL(request.url)
|
|
66
|
-
let sql_params:OINODbSqlParams = { }
|
|
67
|
-
const filter = url.searchParams.get(OINODbConfig.OINODB_SQL_FILTER_PARAM)
|
|
68
|
-
if (filter) {
|
|
69
|
-
sql_params.filter = OINODbSqlFilter.parse(filter)
|
|
70
|
-
}
|
|
71
|
-
const order = url.searchParams.get(OINODbConfig.OINODB_SQL_ORDER_PARAM)
|
|
72
|
-
if (order) {
|
|
73
|
-
sql_params.order = OINODbSqlOrder.parse(order)
|
|
74
|
-
}
|
|
75
|
-
const limit = url.searchParams.get(OINODbConfig.OINODB_SQL_LIMIT_PARAM)
|
|
76
|
-
if (limit) {
|
|
77
|
-
sql_params.limit = OINODbSqlLimit.parse(limit)
|
|
78
|
-
}
|
|
79
|
-
const aggregate = url.searchParams.get(OINODbConfig.OINODB_SQL_AGGREGATE_PARAM)
|
|
80
|
-
if (aggregate) {
|
|
81
|
-
sql_params.aggregate = OINODbSqlAggregate.parse(aggregate)
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
let result:OINODbApiRequestParams = { sqlParams: sql_params }
|
|
85
|
-
|
|
86
|
-
const content_type = request.headers.get("content-type")
|
|
87
|
-
if (content_type == OINOContentType.csv) {
|
|
88
|
-
result.requestType = OINOContentType.csv
|
|
89
|
-
|
|
90
|
-
} else if (content_type == OINOContentType.urlencode) {
|
|
91
|
-
result.requestType = OINOContentType.urlencode
|
|
92
|
-
|
|
93
|
-
} else if (content_type?.startsWith(OINOContentType.formdata)) {
|
|
94
|
-
result.requestType = OINOContentType.formdata
|
|
95
|
-
result.multipartBoundary = content_type.split('boundary=')[1] || ""
|
|
96
|
-
|
|
97
|
-
} else {
|
|
98
|
-
result.requestType = OINOContentType.json
|
|
99
|
-
}
|
|
100
|
-
const accept = request.headers.get("accept")
|
|
101
|
-
// OINOLog.debug("createParamsFromRequest: accept headers", {accept:accept})
|
|
102
|
-
const accept_types = accept?.split(', ') || []
|
|
103
|
-
for (let i=0; i<accept_types.length; i++) {
|
|
104
|
-
if (Object.values(OINOContentType).includes(accept_types[i] as OINOContentType)) {
|
|
105
|
-
result.responseType = accept_types[i] as OINOContentType
|
|
106
|
-
// OINOLog.debug("createParamsFromRequest: response type found", {respnse_type:result.responseType})
|
|
107
|
-
break
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
if (result.responseType === undefined) {
|
|
111
|
-
result.responseType = OINOContentType.json
|
|
112
|
-
}
|
|
113
|
-
const last_modified = request.headers.get("if-modified-since")
|
|
114
|
-
if (last_modified) {
|
|
115
|
-
result.lastModified = new Date(last_modified).getTime()
|
|
116
|
-
}
|
|
117
|
-
const etags = request.headers.get("if-none-match")?.split(',').map(e => e.trim())
|
|
118
|
-
if (etags) {
|
|
119
|
-
result.etags = etags
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// OINOLog.debug("createParamsFromRequest", {params:result})
|
|
123
|
-
return result
|
|
124
|
-
}
|
|
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 } from "./index.js"
|
|
8
|
+
import { OINODbSqlAggregate } from "./OINODbSqlParams.js"
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Static factory class for easily creating things based on data
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
export class OINODbFactory {
|
|
15
|
+
private static _dbRegistry:Record<string, OINODbConstructor> = {}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Register a supported database class. Used to enable those that are installed in the factory
|
|
19
|
+
* instead of forcing everyone to install all database libraries.
|
|
20
|
+
*
|
|
21
|
+
* @param dbName name of the database implementation class
|
|
22
|
+
* @param dbTypeClass constructor for creating a database of that type
|
|
23
|
+
*/
|
|
24
|
+
static registerDb(dbName:string, dbTypeClass: OINODbConstructor):void {
|
|
25
|
+
// OINOLog.debug("OINODbFactory.registerDb", {dbType:dbName})
|
|
26
|
+
this._dbRegistry[dbName] = dbTypeClass
|
|
27
|
+
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Create database from parameters from the registered classes.
|
|
32
|
+
*
|
|
33
|
+
* @param params database connection parameters
|
|
34
|
+
*/
|
|
35
|
+
static async createDb(params:OINODbParams):Promise<OINODb> {
|
|
36
|
+
let result:OINODb
|
|
37
|
+
let db_type = this._dbRegistry[params.type]
|
|
38
|
+
if (db_type) {
|
|
39
|
+
result = new db_type(params)
|
|
40
|
+
} else {
|
|
41
|
+
throw new Error("Unsupported database type: " + params.type)
|
|
42
|
+
}
|
|
43
|
+
await result.connect()
|
|
44
|
+
return result
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Create API from parameters and calls initDatamodel on the datamodel.
|
|
49
|
+
*
|
|
50
|
+
* @param db databased used in API
|
|
51
|
+
* @param params parameters of the API
|
|
52
|
+
*/
|
|
53
|
+
static async createApi(db: OINODb, params: OINODbApiParams):Promise<OINODbApi> {
|
|
54
|
+
let result:OINODbApi = new OINODbApi(db, params)
|
|
55
|
+
await db.initializeApiDatamodel(result)
|
|
56
|
+
return result
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Creates a key-value-collection from Javascript URL parameters.
|
|
61
|
+
*
|
|
62
|
+
* @param request HTTP Request
|
|
63
|
+
*/
|
|
64
|
+
static createParamsFromRequest(request:Request):OINODbApiRequestParams {
|
|
65
|
+
const url:URL = new URL(request.url)
|
|
66
|
+
let sql_params:OINODbSqlParams = { }
|
|
67
|
+
const filter = url.searchParams.get(OINODbConfig.OINODB_SQL_FILTER_PARAM)
|
|
68
|
+
if (filter) {
|
|
69
|
+
sql_params.filter = OINODbSqlFilter.parse(filter)
|
|
70
|
+
}
|
|
71
|
+
const order = url.searchParams.get(OINODbConfig.OINODB_SQL_ORDER_PARAM)
|
|
72
|
+
if (order) {
|
|
73
|
+
sql_params.order = OINODbSqlOrder.parse(order)
|
|
74
|
+
}
|
|
75
|
+
const limit = url.searchParams.get(OINODbConfig.OINODB_SQL_LIMIT_PARAM)
|
|
76
|
+
if (limit) {
|
|
77
|
+
sql_params.limit = OINODbSqlLimit.parse(limit)
|
|
78
|
+
}
|
|
79
|
+
const aggregate = url.searchParams.get(OINODbConfig.OINODB_SQL_AGGREGATE_PARAM)
|
|
80
|
+
if (aggregate) {
|
|
81
|
+
sql_params.aggregate = OINODbSqlAggregate.parse(aggregate)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let result:OINODbApiRequestParams = { sqlParams: sql_params }
|
|
85
|
+
|
|
86
|
+
const content_type = request.headers.get("content-type")
|
|
87
|
+
if (content_type == OINOContentType.csv) {
|
|
88
|
+
result.requestType = OINOContentType.csv
|
|
89
|
+
|
|
90
|
+
} else if (content_type == OINOContentType.urlencode) {
|
|
91
|
+
result.requestType = OINOContentType.urlencode
|
|
92
|
+
|
|
93
|
+
} else if (content_type?.startsWith(OINOContentType.formdata)) {
|
|
94
|
+
result.requestType = OINOContentType.formdata
|
|
95
|
+
result.multipartBoundary = content_type.split('boundary=')[1] || ""
|
|
96
|
+
|
|
97
|
+
} else {
|
|
98
|
+
result.requestType = OINOContentType.json
|
|
99
|
+
}
|
|
100
|
+
const accept = request.headers.get("accept")
|
|
101
|
+
// OINOLog.debug("createParamsFromRequest: accept headers", {accept:accept})
|
|
102
|
+
const accept_types = accept?.split(', ') || []
|
|
103
|
+
for (let i=0; i<accept_types.length; i++) {
|
|
104
|
+
if (Object.values(OINOContentType).includes(accept_types[i] as OINOContentType)) {
|
|
105
|
+
result.responseType = accept_types[i] as OINOContentType
|
|
106
|
+
// OINOLog.debug("createParamsFromRequest: response type found", {respnse_type:result.responseType})
|
|
107
|
+
break
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (result.responseType === undefined) {
|
|
111
|
+
result.responseType = OINOContentType.json
|
|
112
|
+
}
|
|
113
|
+
const last_modified = request.headers.get("if-modified-since")
|
|
114
|
+
if (last_modified) {
|
|
115
|
+
result.lastModified = new Date(last_modified).getTime()
|
|
116
|
+
}
|
|
117
|
+
const etags = request.headers.get("if-none-match")?.split(',').map(e => e.trim())
|
|
118
|
+
if (etags) {
|
|
119
|
+
result.etags = etags
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// OINOLog.debug("createParamsFromRequest", {params:result})
|
|
123
|
+
return result
|
|
124
|
+
}
|
|
125
125
|
}
|