@oino-ts/db 0.0.11

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 (48) hide show
  1. package/README.md +222 -0
  2. package/dist/cjs/OINODb.js +27 -0
  3. package/dist/cjs/OINODbApi.js +270 -0
  4. package/dist/cjs/OINODbConfig.js +86 -0
  5. package/dist/cjs/OINODbDataField.js +354 -0
  6. package/dist/cjs/OINODbDataModel.js +279 -0
  7. package/dist/cjs/OINODbDataSet.js +139 -0
  8. package/dist/cjs/OINODbFactory.js +563 -0
  9. package/dist/cjs/OINODbModelSet.js +267 -0
  10. package/dist/cjs/OINODbParams.js +280 -0
  11. package/dist/cjs/OINODbRequestParams.js +280 -0
  12. package/dist/cjs/OINODbSwagger.js +201 -0
  13. package/dist/cjs/index.js +51 -0
  14. package/dist/esm/OINODb.js +23 -0
  15. package/dist/esm/OINODbApi.js +265 -0
  16. package/dist/esm/OINODbConfig.js +82 -0
  17. package/dist/esm/OINODbDataField.js +345 -0
  18. package/dist/esm/OINODbDataModel.js +275 -0
  19. package/dist/esm/OINODbDataSet.js +134 -0
  20. package/dist/esm/OINODbFactory.js +559 -0
  21. package/dist/esm/OINODbModelSet.js +263 -0
  22. package/dist/esm/OINODbRequestParams.js +274 -0
  23. package/dist/esm/OINODbSwagger.js +197 -0
  24. package/dist/esm/index.js +17 -0
  25. package/dist/types/OINODb.d.ts +75 -0
  26. package/dist/types/OINODbApi.d.ts +57 -0
  27. package/dist/types/OINODbConfig.d.ts +52 -0
  28. package/dist/types/OINODbDataField.d.ts +202 -0
  29. package/dist/types/OINODbDataModel.d.ts +108 -0
  30. package/dist/types/OINODbDataSet.d.ts +95 -0
  31. package/dist/types/OINODbFactory.d.ts +99 -0
  32. package/dist/types/OINODbModelSet.d.ts +50 -0
  33. package/dist/types/OINODbRequestParams.d.ts +130 -0
  34. package/dist/types/OINODbSwagger.d.ts +25 -0
  35. package/dist/types/index.d.ts +103 -0
  36. package/package.json +35 -0
  37. package/src/OINODb.ts +98 -0
  38. package/src/OINODbApi.test.ts +243 -0
  39. package/src/OINODbApi.ts +270 -0
  40. package/src/OINODbConfig.ts +92 -0
  41. package/src/OINODbDataField.ts +372 -0
  42. package/src/OINODbDataModel.ts +290 -0
  43. package/src/OINODbDataSet.ts +170 -0
  44. package/src/OINODbFactory.ts +570 -0
  45. package/src/OINODbModelSet.ts +286 -0
  46. package/src/OINODbRequestParams.ts +281 -0
  47. package/src/OINODbSwagger.ts +209 -0
  48. package/src/index.ts +116 -0
@@ -0,0 +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
+ }
209
+ }
package/src/index.ts ADDED
@@ -0,0 +1,116 @@
1
+ import { OINOContentType } from "@oino-ts/types"
2
+ export { OINOContentType }
3
+
4
+ export { OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX, OINOStr, OINOBenchmark, OINOLog, OINOLogLevel, OINOConsoleLog, OINOResult } from "@oino-ts/types"
5
+
6
+ import { OINODb } from "./OINODb.js"
7
+ import { OINODbDataField } from "./OINODbDataField.js"
8
+ import { OINODbSqlFilter, OINODbSqlLimit, OINODbSqlOrder } from "./OINODbRequestParams.js"
9
+
10
+ export { OINODbApiResult, OINODbApi } from "./OINODbApi.js"
11
+ export { OINODbDataModel } from "./OINODbDataModel.js"
12
+ export { OINODbModelSet } from "./OINODbModelSet.js"
13
+ export { OINODbDataField, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINOBlobDataField, OINODatetimeDataField } from "./OINODbDataField.js"
14
+ export { OINODb } from "./OINODb.js"
15
+ export { OINODbDataSet, OINODbMemoryDataSet } from "./OINODbDataSet.js"
16
+ export { OINODbSqlFilter, OINODbSqlOrder, OINODbSqlComparison, OINODbSqlLimit, OINODbSqlBooleanOperation } from "./OINODbRequestParams.js"
17
+ export { OINODbConfig } from "./OINODbConfig.js"
18
+ export { OINODbFactory } from "./OINODbFactory.js"
19
+ export { OINODbSwagger } from "./OINODbSwagger.js"
20
+
21
+ /** API parameters */
22
+ export type OINODbApiParams = {
23
+ /** Name of the database table */
24
+ tableName: string
25
+ /** Reject values that exceed field max length (behaviour on such is platform dependent) */
26
+ failOnOversizedValues?: Boolean
27
+ /** Reject PUT-requests that contain values for autoinc-type fields */
28
+ failOnUpdateOnAutoinc?: boolean
29
+ /** Reject POST-requests without primary key value (can work if DB-side ) */
30
+ failOnInsertWithoutKey?: boolean
31
+ /** Treat date type fields as just strings and use the native formatting instead of the ISO 8601 format */
32
+ useDatesAsString?: Boolean
33
+ /** Exclude all fields with this prefix from the API */
34
+ excludeFieldPrefix?:string
35
+ /** Exclude given fields from the API */
36
+ excludeFields?:string[],
37
+ /** Enable hashids for numeric primarykeys by adding a 32 char key */
38
+ hashidKey?:string,
39
+ /** Set (minimum) length (12-32 chars) of the hashids */
40
+ hashidLength?:number,
41
+ /** Make hashids static per row/table */
42
+ hashidRandomIds?: boolean
43
+ }
44
+
45
+ /**
46
+ * Database class (constructor) type
47
+ * @param dbParams database parameters
48
+ */
49
+ export type OINODbConstructor = new (dbParams:OINODbParams) => OINODb
50
+
51
+ /** Database parameters */
52
+ export type OINODbParams = {
53
+ /** Name of the database class (e.g. OINODbPostgresql) */
54
+ type: string
55
+ /** Connection URL, either file://-path or an IP-address or an HTTP-url */
56
+ url: string
57
+ /** Name of the database */
58
+ database: string
59
+ /** TCP port of the database */
60
+ port?: number
61
+ /** Username used to authenticate */
62
+ user?: string
63
+ /** Password used to authenticate */
64
+ password?: string
65
+ }
66
+
67
+ /** Field parameters in database */
68
+ export type OINODbDataFieldParams = {
69
+ /** Is the field a primary key */
70
+ isPrimaryKey: Boolean
71
+ /** Is the field an auto inc type */
72
+ isAutoInc: Boolean
73
+ /** Is the field allowed to have null values */
74
+ isNotNull: Boolean
75
+ }
76
+
77
+ /**
78
+ * Callback to filter data fields
79
+ * @param field fields to filter
80
+ */
81
+ export type OINODbDataFieldFilter = (field:OINODbDataField) => Boolean
82
+
83
+ /** Request options */
84
+ export type OINODbSqlParams = {
85
+ /** Additional SQL select where-conditions */
86
+ filter?:OINODbSqlFilter,
87
+ /** SQL result ordering conditions */
88
+ order?:OINODbSqlOrder
89
+ /** SQL result limit condition */
90
+ limit?:OINODbSqlLimit
91
+ }
92
+
93
+ /** Request options */
94
+ export type OINORequestParams = {
95
+ /** Content type of the request body */
96
+ requestType?:OINOContentType
97
+ /** Content type of the response body */
98
+ responseType?:OINOContentType
99
+ /** Multipart boundary token */
100
+ multipartBoundary?:string
101
+ /** SQL parameters */
102
+ sqlParams:OINODbSqlParams
103
+ }
104
+
105
+ /** A single column value of a data row */
106
+ export type OINODataCell = string | bigint | number | boolean | Date | Uint8Array | Buffer | null | undefined
107
+ /** A single data row */
108
+ export type OINODataRow = Array<OINODataCell>
109
+ /** Empty row instance */
110
+ export const OINODB_EMPTY_ROW:OINODataRow = []
111
+ /** Empty row array instance */
112
+ export const OINODB_EMPTY_ROWS:OINODataRow[] = [OINODB_EMPTY_ROW]
113
+
114
+ /** Key-value collection */
115
+ export type OINOValues = Record<string, string>
116
+