@oino-ts/db 0.3.3 → 0.4.0

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.
Files changed (39) hide show
  1. package/dist/cjs/OINODb.js +3 -3
  2. package/dist/cjs/OINODbApi.js +43 -2
  3. package/dist/cjs/OINODbConfig.js +2 -0
  4. package/dist/cjs/OINODbDataField.js +23 -0
  5. package/dist/cjs/OINODbDataModel.js +12 -5
  6. package/dist/cjs/OINODbFactory.js +5 -2
  7. package/dist/cjs/OINODbModelSet.js +17 -1
  8. package/dist/cjs/OINODbSqlParams.js +76 -11
  9. package/dist/cjs/index.js +4 -1
  10. package/dist/esm/OINODb.js +3 -3
  11. package/dist/esm/OINODbApi.js +44 -3
  12. package/dist/esm/OINODbConfig.js +2 -0
  13. package/dist/esm/OINODbDataField.js +23 -0
  14. package/dist/esm/OINODbDataModel.js +13 -6
  15. package/dist/esm/OINODbFactory.js +5 -2
  16. package/dist/esm/OINODbModelSet.js +17 -1
  17. package/dist/esm/OINODbSqlParams.js +75 -11
  18. package/dist/esm/index.js +3 -1
  19. package/dist/types/OINODb.d.ts +7 -0
  20. package/dist/types/OINODbApi.d.ts +13 -0
  21. package/dist/types/OINODbConfig.d.ts +2 -0
  22. package/dist/types/OINODbDataField.d.ts +8 -0
  23. package/dist/types/OINODbModelSet.d.ts +5 -2
  24. package/dist/types/OINODbSqlParams.d.ts +44 -4
  25. package/dist/types/index.d.ts +6 -2
  26. package/package.json +36 -36
  27. package/src/OINODb.ts +299 -291
  28. package/src/OINODbApi.test.ts +427 -411
  29. package/src/OINODbApi.ts +423 -384
  30. package/src/OINODbConfig.ts +98 -95
  31. package/src/OINODbDataField.ts +406 -382
  32. package/src/OINODbDataModel.ts +295 -289
  33. package/src/OINODbFactory.ts +127 -124
  34. package/src/OINODbModelSet.ts +315 -298
  35. package/src/OINODbParser.ts +457 -457
  36. package/src/OINODbSqlParams.ts +505 -437
  37. package/src/OINODbSwagger.ts +208 -208
  38. package/src/index.ts +134 -130
  39. package/README.md +0 -190
@@ -1,209 +1,209 @@
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, OINODbDataField } from "./index.js";
8
-
9
- /**
10
- * Static class for Swagger utilities
11
- *
12
- */
13
- export class OINODbSwagger {
14
-
15
- private static _getSchemaApiMethodParamsQueryId(): any {
16
- return {
17
- "schema": {
18
- "type": "string"
19
- },
20
- "in": "path",
21
- "name": "id",
22
- "required": true
23
- }
24
- }
25
-
26
- private static _getSchemaApiMethodParamsBody(tableName:string): any {
27
- return {
28
- "schema": {
29
- "$ref": "#/components/schemas/" + tableName
30
- },
31
- "in": "body",
32
- "required": true
33
- }
34
- }
35
-
36
- private static _getSchemaApiMethodDescription(method:string, tableName:string, hasQueryIdParam:boolean): any {
37
- if (hasQueryIdParam) {
38
- return method.toUpperCase() + " " + tableName + " object"
39
- } else {
40
- return method.toUpperCase() + " " +tableName + " object array"
41
- }
42
- }
43
-
44
- private static _getSchemaApiMethodOperationId(method:string, tableName:string, hasQueryIdParam:boolean): any {
45
- if (hasQueryIdParam) {
46
- return method+tableName
47
- } else {
48
- return method+tableName+"All"
49
- }
50
- }
51
-
52
- private static _getSchemaOinoResponse(): any {
53
- return {
54
- "type": "object",
55
- "properties": {
56
- "success": {
57
- "type": "boolean"
58
- },
59
- "statusCode": {
60
- "type": "number"
61
- },
62
- "statusMessage": {
63
- "type": "string"
64
- },
65
- "messages": {
66
- "type": "array",
67
- "items": {
68
- "type": "string"
69
- }
70
- }
71
- },
72
- "required": [
73
- "success",
74
- "statusCode",
75
- "statusMessage",
76
- "messages"
77
- ]
78
- }
79
- }
80
-
81
- private static _getSchemaFieldType(field:OINODbDataField): any {
82
- let type_string:string
83
- if (field.type == "boolean") {
84
- type_string = "boolean"
85
- } else if (field.type == "integer" || field.type == "number") {
86
- type_string = "number"
87
- } else {
88
- type_string = "string"
89
- }
90
- if (!field.fieldParams.isNotNull) {
91
- return {
92
- "anyOf": [
93
- {
94
- "type": type_string
95
- },
96
- {
97
- "type": "null"
98
- }
99
- ]
100
- }
101
- } else {
102
- return { type: type_string }
103
- }
104
- }
105
-
106
- private static _getSwaggerApiType(api:OINODbApi):any {
107
- let result:any = {
108
- type: "object",
109
- properties: {},
110
- required: []
111
- }
112
- let field:OINODbDataField
113
- for (field of api.datamodel.fields) {
114
- result.properties[field.name] = this._getSchemaFieldType(field)
115
- if (field.fieldParams.isPrimaryKey) {
116
- result.required.push(field.name)
117
- }
118
- }
119
- return result
120
- }
121
-
122
- private static _getSchemaType(tableName:string, hasQueryIdParam:boolean, hasResultData:boolean) {
123
- if (hasResultData && hasQueryIdParam) {
124
- return { "$ref": "#/components/schemas/" + tableName }
125
-
126
- } else if (hasResultData) {
127
- return { type : "array", items : { "$ref": "#/components/schemas/" + tableName } }
128
-
129
- } else if (hasQueryIdParam) {
130
- return { "$ref": "#/components/schemas/OINOResponse" }
131
-
132
- } else {
133
- return { "$ref": "#/components/schemas/OINOResponse" }
134
- }
135
- }
136
-
137
- private static _getSchemaApiMethodParams(tableName:string, hasQueryIdParam:boolean, hasResultData:boolean) {
138
- if (hasResultData && hasQueryIdParam) {
139
- return [ this._getSchemaApiMethodParamsQueryId() ]
140
-
141
- } else if (hasResultData) {
142
- return []
143
-
144
- } else if (hasQueryIdParam) {
145
- return [ this._getSchemaApiMethodParamsQueryId(), this._getSchemaApiMethodParamsBody(tableName) ]
146
-
147
- } else {
148
- return [ this._getSchemaApiMethodParamsBody(tableName) ]
149
- }
150
- }
151
-
152
- private static _getSchemaApiMethod(method:string, tableName:string, hasQueryIdParam:boolean, hasBody:boolean, hasResultData:boolean) {
153
- return {
154
- responses: {
155
- 200: { description: this._getSchemaApiMethodDescription(method, tableName, hasQueryIdParam), content: { "application/json": { schema: this._getSchemaType(tableName, hasQueryIdParam, hasResultData) } } }
156
- },
157
- "operationId": this._getSchemaApiMethodOperationId(method, tableName, hasQueryIdParam),
158
- "parameters": this._getSchemaApiMethodParams(tableName, hasQueryIdParam, hasResultData)
159
- }
160
- }
161
-
162
- private static _getSwaggerApiPath(tableName:string, hasQueryIdParam:boolean):any {
163
-
164
- if (hasQueryIdParam) {
165
- return {
166
- get: this._getSchemaApiMethod("get", tableName, hasQueryIdParam, false, true),
167
- put: this._getSchemaApiMethod("put", tableName, hasQueryIdParam, true, false),
168
- delete: this._getSchemaApiMethod("delete", tableName, hasQueryIdParam, false, false)
169
- }
170
- } else {
171
- return {
172
- get: this._getSchemaApiMethod("get", tableName, hasQueryIdParam, false, true),
173
- post: this._getSchemaApiMethod("post", tableName, hasQueryIdParam, true, false)
174
- }
175
- }
176
- }
177
-
178
- /**
179
- * Returns swagger.json as object of the given API's.
180
- *
181
- * @param apis array of API's use for Swagger definition
182
- *
183
- */
184
- static getApiDefinition(apis:OINODbApi[]): any {
185
- let result:any = {
186
- "openapi": "3.1.0",
187
- "info": {
188
- "title": "",
189
- "description": "",
190
- "version": ""
191
- },
192
- "paths": {
193
-
194
- },
195
- "components": {
196
- "schemas": {
197
- OINOResponse: this._getSchemaOinoResponse()
198
- }
199
- }
200
- }
201
- for (let i=0; i<apis.length; i++) {
202
- const table_name = apis[i].params.tableName
203
- result.paths["/" + table_name] = this._getSwaggerApiPath(table_name, false)
204
- result.paths["/" + table_name + "/{id}"] = this._getSwaggerApiPath(table_name, true)
205
- result.components.schemas[table_name] = this._getSwaggerApiType(apis[i])
206
- }
207
- return result
208
- }
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, OINODbDataField } from "./index.js";
8
+
9
+ /**
10
+ * Static class for Swagger utilities
11
+ *
12
+ */
13
+ export class OINODbSwagger {
14
+
15
+ private static _getSchemaApiMethodParamsQueryId(): any {
16
+ return {
17
+ "schema": {
18
+ "type": "string"
19
+ },
20
+ "in": "path",
21
+ "name": "id",
22
+ "required": true
23
+ }
24
+ }
25
+
26
+ private static _getSchemaApiMethodParamsBody(tableName:string): any {
27
+ return {
28
+ "schema": {
29
+ "$ref": "#/components/schemas/" + tableName
30
+ },
31
+ "in": "body",
32
+ "required": true
33
+ }
34
+ }
35
+
36
+ private static _getSchemaApiMethodDescription(method:string, tableName:string, hasQueryIdParam:boolean): any {
37
+ if (hasQueryIdParam) {
38
+ return method.toUpperCase() + " " + tableName + " object"
39
+ } else {
40
+ return method.toUpperCase() + " " +tableName + " object array"
41
+ }
42
+ }
43
+
44
+ private static _getSchemaApiMethodOperationId(method:string, tableName:string, hasQueryIdParam:boolean): any {
45
+ if (hasQueryIdParam) {
46
+ return method+tableName
47
+ } else {
48
+ return method+tableName+"All"
49
+ }
50
+ }
51
+
52
+ private static _getSchemaOinoResponse(): any {
53
+ return {
54
+ "type": "object",
55
+ "properties": {
56
+ "success": {
57
+ "type": "boolean"
58
+ },
59
+ "statusCode": {
60
+ "type": "number"
61
+ },
62
+ "statusMessage": {
63
+ "type": "string"
64
+ },
65
+ "messages": {
66
+ "type": "array",
67
+ "items": {
68
+ "type": "string"
69
+ }
70
+ }
71
+ },
72
+ "required": [
73
+ "success",
74
+ "statusCode",
75
+ "statusMessage",
76
+ "messages"
77
+ ]
78
+ }
79
+ }
80
+
81
+ private static _getSchemaFieldType(field:OINODbDataField): any {
82
+ let type_string:string
83
+ if (field.type == "boolean") {
84
+ type_string = "boolean"
85
+ } else if (field.type == "integer" || field.type == "number") {
86
+ type_string = "number"
87
+ } else {
88
+ type_string = "string"
89
+ }
90
+ if (!field.fieldParams.isNotNull) {
91
+ return {
92
+ "anyOf": [
93
+ {
94
+ "type": type_string
95
+ },
96
+ {
97
+ "type": "null"
98
+ }
99
+ ]
100
+ }
101
+ } else {
102
+ return { type: type_string }
103
+ }
104
+ }
105
+
106
+ private static _getSwaggerApiType(api:OINODbApi):any {
107
+ let result:any = {
108
+ type: "object",
109
+ properties: {},
110
+ required: []
111
+ }
112
+ let field:OINODbDataField
113
+ for (field of api.datamodel.fields) {
114
+ result.properties[field.name] = this._getSchemaFieldType(field)
115
+ if (field.fieldParams.isPrimaryKey) {
116
+ result.required.push(field.name)
117
+ }
118
+ }
119
+ return result
120
+ }
121
+
122
+ private static _getSchemaType(tableName:string, hasQueryIdParam:boolean, hasResultData:boolean) {
123
+ if (hasResultData && hasQueryIdParam) {
124
+ return { "$ref": "#/components/schemas/" + tableName }
125
+
126
+ } else if (hasResultData) {
127
+ return { type : "array", items : { "$ref": "#/components/schemas/" + tableName } }
128
+
129
+ } else if (hasQueryIdParam) {
130
+ return { "$ref": "#/components/schemas/OINOResponse" }
131
+
132
+ } else {
133
+ return { "$ref": "#/components/schemas/OINOResponse" }
134
+ }
135
+ }
136
+
137
+ private static _getSchemaApiMethodParams(tableName:string, hasQueryIdParam:boolean, hasResultData:boolean) {
138
+ if (hasResultData && hasQueryIdParam) {
139
+ return [ this._getSchemaApiMethodParamsQueryId() ]
140
+
141
+ } else if (hasResultData) {
142
+ return []
143
+
144
+ } else if (hasQueryIdParam) {
145
+ return [ this._getSchemaApiMethodParamsQueryId(), this._getSchemaApiMethodParamsBody(tableName) ]
146
+
147
+ } else {
148
+ return [ this._getSchemaApiMethodParamsBody(tableName) ]
149
+ }
150
+ }
151
+
152
+ private static _getSchemaApiMethod(method:string, tableName:string, hasQueryIdParam:boolean, hasBody:boolean, hasResultData:boolean) {
153
+ return {
154
+ responses: {
155
+ 200: { description: this._getSchemaApiMethodDescription(method, tableName, hasQueryIdParam), content: { "application/json": { schema: this._getSchemaType(tableName, hasQueryIdParam, hasResultData) } } }
156
+ },
157
+ "operationId": this._getSchemaApiMethodOperationId(method, tableName, hasQueryIdParam),
158
+ "parameters": this._getSchemaApiMethodParams(tableName, hasQueryIdParam, hasResultData)
159
+ }
160
+ }
161
+
162
+ private static _getSwaggerApiPath(tableName:string, hasQueryIdParam:boolean):any {
163
+
164
+ if (hasQueryIdParam) {
165
+ return {
166
+ get: this._getSchemaApiMethod("get", tableName, hasQueryIdParam, false, true),
167
+ put: this._getSchemaApiMethod("put", tableName, hasQueryIdParam, true, false),
168
+ delete: this._getSchemaApiMethod("delete", tableName, hasQueryIdParam, false, false)
169
+ }
170
+ } else {
171
+ return {
172
+ get: this._getSchemaApiMethod("get", tableName, hasQueryIdParam, false, true),
173
+ post: this._getSchemaApiMethod("post", tableName, hasQueryIdParam, true, false)
174
+ }
175
+ }
176
+ }
177
+
178
+ /**
179
+ * Returns swagger.json as object of the given API's.
180
+ *
181
+ * @param apis array of API's use for Swagger definition
182
+ *
183
+ */
184
+ static getApiDefinition(apis:OINODbApi[]): any {
185
+ let result:any = {
186
+ "openapi": "3.1.0",
187
+ "info": {
188
+ "title": "",
189
+ "description": "",
190
+ "version": ""
191
+ },
192
+ "paths": {
193
+
194
+ },
195
+ "components": {
196
+ "schemas": {
197
+ OINOResponse: this._getSchemaOinoResponse()
198
+ }
199
+ }
200
+ }
201
+ for (let i=0; i<apis.length; i++) {
202
+ const table_name = apis[i].params.tableName
203
+ result.paths["/" + table_name] = this._getSwaggerApiPath(table_name, false)
204
+ result.paths["/" + table_name + "/{id}"] = this._getSwaggerApiPath(table_name, true)
205
+ result.components.schemas[table_name] = this._getSwaggerApiType(apis[i])
206
+ }
207
+ return result
208
+ }
209
209
  }