@oino-ts/db 0.1.2 → 0.2.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.
- package/dist/types/OINODb.d.ts +180 -0
- package/dist/types/OINODbApi.d.ts +82 -0
- package/dist/types/OINODbConfig.d.ts +52 -0
- package/dist/types/OINODbDataField.d.ts +202 -0
- package/dist/types/OINODbDataModel.d.ts +108 -0
- package/dist/types/OINODbDataSet.d.ts +95 -0
- package/dist/types/OINODbFactory.d.ts +35 -0
- package/dist/types/OINODbModelSet.d.ts +51 -0
- package/dist/types/OINODbParser.d.ts +53 -0
- package/dist/types/OINODbRequestParams.d.ts +146 -0
- package/dist/types/OINODbSqlParams.d.ts +147 -0
- package/dist/types/OINODbSwagger.d.ts +25 -0
- package/dist/types/index.d.ts +111 -0
- package/package.json +3 -3
- package/src/OINODbApi.test.ts +2 -2
- package/src/OINODbSqlParams.ts +3 -2
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { OINODbParams, OINODbApi, OINODataCell, OINODataRow } from "./index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Base class for database abstraction, implementing methods for connecting, making queries and parsing/formatting data
|
|
4
|
+
* between SQL and serialization formats.
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
export declare abstract class OINODb {
|
|
8
|
+
protected _params: OINODbParams;
|
|
9
|
+
/** Name of the database */
|
|
10
|
+
readonly name: string;
|
|
11
|
+
/**
|
|
12
|
+
* Constructor for `OINODb`.
|
|
13
|
+
* @param params database parameters
|
|
14
|
+
*/
|
|
15
|
+
constructor(params: OINODbParams);
|
|
16
|
+
/**
|
|
17
|
+
* Connect to database.
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
abstract connect(): Promise<boolean>;
|
|
21
|
+
/**
|
|
22
|
+
* Print a table name using database specific SQL escaping.
|
|
23
|
+
*
|
|
24
|
+
* @param sqlTable name of the table
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
27
|
+
abstract printSqlTablename(sqlTable: string): string;
|
|
28
|
+
/**
|
|
29
|
+
* Print a column name with correct SQL escaping.
|
|
30
|
+
*
|
|
31
|
+
* @param sqlColumn name of the column
|
|
32
|
+
*
|
|
33
|
+
*/
|
|
34
|
+
abstract printSqlColumnname(sqlColumn: string): string;
|
|
35
|
+
/**
|
|
36
|
+
* Print a single data value from serialization using the context of the native data
|
|
37
|
+
* type with the correct SQL escaping.
|
|
38
|
+
*
|
|
39
|
+
* @param cellValue data from sql results
|
|
40
|
+
* @param sqlType native type name for table column
|
|
41
|
+
*
|
|
42
|
+
*/
|
|
43
|
+
abstract printCellAsSqlValue(cellValue: OINODataCell, sqlType: string): string;
|
|
44
|
+
/**
|
|
45
|
+
* Parse a single SQL result value for serialization using the context of the native data
|
|
46
|
+
* type.
|
|
47
|
+
*
|
|
48
|
+
* @param sqlValue data from serialization
|
|
49
|
+
* @param sqlType native type name for table column
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
abstract parseSqlValueAsCell(sqlValue: OINODataCell, sqlType: string): OINODataCell;
|
|
53
|
+
/**
|
|
54
|
+
* Execute a select operation.
|
|
55
|
+
*
|
|
56
|
+
* @param sql SQL statement.
|
|
57
|
+
*
|
|
58
|
+
*/
|
|
59
|
+
abstract sqlSelect(sql: string): Promise<OINODbDataSet>;
|
|
60
|
+
/**
|
|
61
|
+
* Execute other sql operations.
|
|
62
|
+
*
|
|
63
|
+
* @param sql SQL statement.
|
|
64
|
+
*
|
|
65
|
+
*/
|
|
66
|
+
abstract sqlExec(sql: string): Promise<OINODbDataSet>;
|
|
67
|
+
/**
|
|
68
|
+
* Initialize a data model by getting the SQL schema and populating OINODbDataFields of
|
|
69
|
+
* the model.
|
|
70
|
+
*
|
|
71
|
+
* @param api api which data model to initialize.
|
|
72
|
+
*
|
|
73
|
+
*/
|
|
74
|
+
abstract initializeApiDatamodel(api: OINODbApi): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Print SQL select statement with DB specific formatting.
|
|
77
|
+
*
|
|
78
|
+
* @param tableName - The name of the table to select from.
|
|
79
|
+
* @param columnNames - The columns to be selected.
|
|
80
|
+
* @param whereCondition - The WHERE clause to filter the results.
|
|
81
|
+
* @param orderCondition - The ORDER BY clause to sort the results.
|
|
82
|
+
* @param limitCondition - The LIMIT clause to limit the number of results.
|
|
83
|
+
*
|
|
84
|
+
*/
|
|
85
|
+
printSqlSelect(tableName: string, columnNames: string, whereCondition: string, orderCondition: string, limitCondition: string): string;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Base class for SQL results that can be asynchronously iterated (but
|
|
89
|
+
* not necessarity rewinded). Idea is to handle database specific mechanisms
|
|
90
|
+
* for returning and formatting conventions in the database specific
|
|
91
|
+
* implementation. Data might be in memory or streamed in chunks and
|
|
92
|
+
* `OINODbDataSet` will serve it out consistently.
|
|
93
|
+
*
|
|
94
|
+
*/
|
|
95
|
+
export declare abstract class OINODbDataSet {
|
|
96
|
+
private _data;
|
|
97
|
+
/** Error messages */
|
|
98
|
+
readonly messages: string[];
|
|
99
|
+
/**
|
|
100
|
+
* Constructor for `OINODbDataSet`.
|
|
101
|
+
*
|
|
102
|
+
* @param data internal database specific data type (constructor will throw if invalid)
|
|
103
|
+
* @param messages error messages from SQL-query
|
|
104
|
+
*
|
|
105
|
+
*/
|
|
106
|
+
constructor(data: unknown, messages?: string[]);
|
|
107
|
+
/**
|
|
108
|
+
* Is data set empty.
|
|
109
|
+
*
|
|
110
|
+
*/
|
|
111
|
+
abstract isEmpty(): boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Is there no more content, i.e. either dataset is empty or we have moved beyond last line
|
|
114
|
+
*
|
|
115
|
+
*/
|
|
116
|
+
abstract isEof(): boolean;
|
|
117
|
+
/**
|
|
118
|
+
* Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
|
|
119
|
+
*
|
|
120
|
+
*/
|
|
121
|
+
abstract next(): Promise<boolean>;
|
|
122
|
+
/**
|
|
123
|
+
* Gets current row of data.
|
|
124
|
+
*
|
|
125
|
+
*/
|
|
126
|
+
abstract getRow(): OINODataRow;
|
|
127
|
+
/**
|
|
128
|
+
* Checks if the messages contain errors.
|
|
129
|
+
*
|
|
130
|
+
*/
|
|
131
|
+
hasErrors(): boolean;
|
|
132
|
+
/**
|
|
133
|
+
* Checks if the messages contain errors.
|
|
134
|
+
*
|
|
135
|
+
*/
|
|
136
|
+
getFirstError(): string;
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Generic in memory implementation of a data set where data is an array of rows. Used
|
|
140
|
+
* by BunSqlite and automated testing. Can be rewinded.
|
|
141
|
+
*
|
|
142
|
+
*/
|
|
143
|
+
export declare class OINODbMemoryDataSet extends OINODbDataSet {
|
|
144
|
+
private _rows;
|
|
145
|
+
private _currentRow;
|
|
146
|
+
private _eof;
|
|
147
|
+
/**
|
|
148
|
+
* Constructor of `OINODbMemoryDataSet`.
|
|
149
|
+
*
|
|
150
|
+
* @param data data as OINODataRow[] (constructor will throw if invalid)
|
|
151
|
+
* @param errors error messages from SQL-query
|
|
152
|
+
*
|
|
153
|
+
*/
|
|
154
|
+
constructor(data: unknown, errors?: string[]);
|
|
155
|
+
/**
|
|
156
|
+
* Is data set empty.
|
|
157
|
+
*
|
|
158
|
+
*/
|
|
159
|
+
isEmpty(): boolean;
|
|
160
|
+
/**
|
|
161
|
+
* Is there no more content, i.e. either dataset is empty or we have moved beyond last line
|
|
162
|
+
*
|
|
163
|
+
*/
|
|
164
|
+
isEof(): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
|
|
167
|
+
*
|
|
168
|
+
*/
|
|
169
|
+
next(): Promise<boolean>;
|
|
170
|
+
/**
|
|
171
|
+
* Gets current row of data.
|
|
172
|
+
*
|
|
173
|
+
*/
|
|
174
|
+
getRow(): OINODataRow;
|
|
175
|
+
/**
|
|
176
|
+
* Rewinds data set to the first row, returns !isEof().
|
|
177
|
+
*
|
|
178
|
+
*/
|
|
179
|
+
first(): boolean;
|
|
180
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { OINODbApiParams, OINODb, OINODbDataModel, OINODataRow, OINODbModelSet, OINODbApiRequestParams, OINOHttpResult, OINOHtmlTemplate } from "./index.js";
|
|
2
|
+
import { OINOResult } from "@oino-ts/common";
|
|
3
|
+
import { OINOHashid } from "@oino-ts/hashid";
|
|
4
|
+
/**
|
|
5
|
+
* OINO API request result object with returned data and/or http status code/message and
|
|
6
|
+
* error / warning messages.
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
export declare class OINODbApiResult extends OINOResult {
|
|
10
|
+
/** DbApi request params */
|
|
11
|
+
params: OINODbApiRequestParams;
|
|
12
|
+
/** Returned data if any */
|
|
13
|
+
data?: OINODbModelSet;
|
|
14
|
+
/**
|
|
15
|
+
* Constructor of OINODbApiResult.
|
|
16
|
+
*
|
|
17
|
+
* @param params DbApi request parameters
|
|
18
|
+
* @param data result data
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
constructor(params: OINODbApiRequestParams, data?: OINODbModelSet);
|
|
22
|
+
/**
|
|
23
|
+
* Creates a HTTP Response from API results.
|
|
24
|
+
*
|
|
25
|
+
* @param headers Headers to include in the response
|
|
26
|
+
*
|
|
27
|
+
*/
|
|
28
|
+
getResponse(headers?: Record<string, string>): Promise<Response>;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Specialized HTML template that can render ´OINODbApiResult´.
|
|
32
|
+
*
|
|
33
|
+
*/
|
|
34
|
+
export declare class OINODbHtmlTemplate extends OINOHtmlTemplate {
|
|
35
|
+
/**
|
|
36
|
+
* Creates HTML Response from API modelset.
|
|
37
|
+
*
|
|
38
|
+
* @param modelset OINO API dataset
|
|
39
|
+
* @param overrideValues values to override in the data
|
|
40
|
+
*
|
|
41
|
+
*/
|
|
42
|
+
renderFromDbData(modelset: OINODbModelSet, overrideValues?: any): Promise<OINOHttpResult>;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* API class with method to process HTTP REST requests.
|
|
46
|
+
*
|
|
47
|
+
*/
|
|
48
|
+
export declare class OINODbApi {
|
|
49
|
+
/** API database reference */
|
|
50
|
+
readonly db: OINODb;
|
|
51
|
+
/** API datamodel */
|
|
52
|
+
readonly datamodel: OINODbDataModel;
|
|
53
|
+
/** API parameters */
|
|
54
|
+
readonly params: OINODbApiParams;
|
|
55
|
+
/** API hashid */
|
|
56
|
+
readonly hashid: OINOHashid | null;
|
|
57
|
+
/**
|
|
58
|
+
* Constructor of API object.
|
|
59
|
+
* NOTE! OINODb.initDatamodel must be called if created manually instead of the factory.
|
|
60
|
+
*
|
|
61
|
+
* @param db database for the API
|
|
62
|
+
* @param params parameters for the API
|
|
63
|
+
*
|
|
64
|
+
*/
|
|
65
|
+
constructor(db: OINODb, params: OINODbApiParams);
|
|
66
|
+
private _validateRowValues;
|
|
67
|
+
private _doGet;
|
|
68
|
+
private _doPost;
|
|
69
|
+
private _doPut;
|
|
70
|
+
private _doDelete;
|
|
71
|
+
/**
|
|
72
|
+
* Method for handlind a HTTP REST request with GET, POST, PUT, DELETE corresponding to
|
|
73
|
+
* SQL select, insert, update and delete.
|
|
74
|
+
*
|
|
75
|
+
* @param method HTTP verb (uppercase)
|
|
76
|
+
* @param id URL id of the REST request
|
|
77
|
+
* @param body HTTP body data as either serialized string or unserialized JS object / OINODataRow-array
|
|
78
|
+
* @param params HTTP URL parameters as key-value-pairs
|
|
79
|
+
*
|
|
80
|
+
*/
|
|
81
|
+
doRequest(method: string, id: string, body: string | OINODataRow[] | Buffer | any, params?: OINODbApiRequestParams): Promise<OINODbApiResult>;
|
|
82
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** Set the name of the OINO ID field (default \_OINOID\_) */
|
|
2
|
+
export declare class OINODbConfig {
|
|
3
|
+
/** Name of the synthetic OINO ID field */
|
|
4
|
+
static OINODB_ID_FIELD: string;
|
|
5
|
+
/** Private key separator of the synthetic OINO ID field */
|
|
6
|
+
static OINODB_ID_SEPARATOR: string;
|
|
7
|
+
private static OINODB_ID_SEPARATOR_ESCAPED;
|
|
8
|
+
/** Name of the OINODbSqlFilter-parameter in request */
|
|
9
|
+
static OINODB_SQL_FILTER_PARAM: string;
|
|
10
|
+
/** Name of the OINODbSqlOrder-parameter in request */
|
|
11
|
+
static OINODB_SQL_ORDER_PARAM: string;
|
|
12
|
+
/** Name of the OINODbSqlLimit-parameter in request */
|
|
13
|
+
static OINODB_SQL_LIMIT_PARAM: string;
|
|
14
|
+
/**
|
|
15
|
+
* Set the name of the OINO ID field
|
|
16
|
+
* @param idField name of the OINO ID field
|
|
17
|
+
*/
|
|
18
|
+
static setOinoIdField(idField: string): void;
|
|
19
|
+
/**
|
|
20
|
+
* Set the separator character of the OINO ID field
|
|
21
|
+
* @param idSeparator character to use as separator of id parts
|
|
22
|
+
*/
|
|
23
|
+
static setOinoIdSeparator(idSeparator: string): void;
|
|
24
|
+
/**
|
|
25
|
+
* Print OINO ID for primary key values.
|
|
26
|
+
*
|
|
27
|
+
* @param primaryKeys an array of primary key values.
|
|
28
|
+
*
|
|
29
|
+
*/
|
|
30
|
+
static printOINOId(primaryKeys: string[]): string;
|
|
31
|
+
/**
|
|
32
|
+
* Set the name of the OINODbSqlFilter-param field
|
|
33
|
+
*
|
|
34
|
+
* @param sqlFilterParam name of the http parameter with `OINODbSqlFilter` definition
|
|
35
|
+
*
|
|
36
|
+
*/
|
|
37
|
+
static setOinoSqlFilterParam(sqlFilterParam: string): void;
|
|
38
|
+
/**
|
|
39
|
+
* Set the name of the OINODbSqlOrder-param field
|
|
40
|
+
*
|
|
41
|
+
* @param sqlOrderParam name of the http parameter with `OINODbSqlOrder` definition
|
|
42
|
+
*
|
|
43
|
+
*/
|
|
44
|
+
static setOinoSqlOrderParam(sqlOrderParam: string): void;
|
|
45
|
+
/**
|
|
46
|
+
* Set the name of the OINODbSqlLimit-param field
|
|
47
|
+
*
|
|
48
|
+
* @param sqlLimitParam name of the http parameter with `OINODbSqlLimit` definition
|
|
49
|
+
*
|
|
50
|
+
*/
|
|
51
|
+
static setOinoSqlLimitParam(sqlLimitParam: string): void;
|
|
52
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { OINODbDataFieldParams, OINODataCell, OINODb } from "./index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Base class for a column of data responsible for appropriatelly serializing/deserializing the data.
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export declare class OINODbDataField {
|
|
7
|
+
/** OINODB reference*/
|
|
8
|
+
readonly db: OINODb;
|
|
9
|
+
/** Name of the field */
|
|
10
|
+
readonly name: string;
|
|
11
|
+
/** Internal type of field*/
|
|
12
|
+
readonly type: string;
|
|
13
|
+
/** SQL type of the field */
|
|
14
|
+
readonly sqlType: string;
|
|
15
|
+
/** Maximum length of the field (or 0) */
|
|
16
|
+
readonly maxLength: number;
|
|
17
|
+
/** Parameters for the field */
|
|
18
|
+
readonly fieldParams: OINODbDataFieldParams;
|
|
19
|
+
/**
|
|
20
|
+
* Constructor for a data field
|
|
21
|
+
*
|
|
22
|
+
* @param db OINODb reference
|
|
23
|
+
* @param name name of the field
|
|
24
|
+
* @param type internal type of the field
|
|
25
|
+
* @param sqlType column type in database
|
|
26
|
+
* @param fieldParams parameters of the field
|
|
27
|
+
* @param maxLength maximum length of the field (or 0)
|
|
28
|
+
*
|
|
29
|
+
*/
|
|
30
|
+
constructor(db: OINODb, name: string, type: string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength?: number);
|
|
31
|
+
/**
|
|
32
|
+
* Pring debug information for the field
|
|
33
|
+
*
|
|
34
|
+
* @param length length of the debug output (or 0 for as long as needed)
|
|
35
|
+
*
|
|
36
|
+
*/
|
|
37
|
+
printColumnDebug(length?: number): string;
|
|
38
|
+
/**
|
|
39
|
+
* Serialize cell value in the given content format.
|
|
40
|
+
*
|
|
41
|
+
* @param cellVal cell value
|
|
42
|
+
*
|
|
43
|
+
*/
|
|
44
|
+
serializeCell(cellVal: OINODataCell): string | null | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
47
|
+
*
|
|
48
|
+
* @param value string value
|
|
49
|
+
*
|
|
50
|
+
*/
|
|
51
|
+
deserializeCell(value: string | null | undefined): OINODataCell;
|
|
52
|
+
/**
|
|
53
|
+
* Print data cell (from deserialization) as SQL-string.
|
|
54
|
+
*
|
|
55
|
+
* @param cellVal cell value
|
|
56
|
+
*
|
|
57
|
+
*/
|
|
58
|
+
printCellAsSqlValue(cellVal: OINODataCell): string;
|
|
59
|
+
/**
|
|
60
|
+
* Print name of column as SQL.
|
|
61
|
+
*
|
|
62
|
+
*/
|
|
63
|
+
printSqlColumnName(): string;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Specialised class for a string column.
|
|
67
|
+
*
|
|
68
|
+
*/
|
|
69
|
+
export declare class OINOStringDataField extends OINODbDataField {
|
|
70
|
+
/**
|
|
71
|
+
* Constructor for a string data field
|
|
72
|
+
*
|
|
73
|
+
* @param db OINODb reference
|
|
74
|
+
* @param name name of the field
|
|
75
|
+
* @param sqlType column type in database
|
|
76
|
+
* @param fieldParams parameters of the field
|
|
77
|
+
* @param maxLength maximum length of the field (or 0)
|
|
78
|
+
*
|
|
79
|
+
*/
|
|
80
|
+
constructor(db: OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength: number);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Specialised class for a boolean column.
|
|
84
|
+
*
|
|
85
|
+
*/
|
|
86
|
+
export declare class OINOBooleanDataField extends OINODbDataField {
|
|
87
|
+
/**
|
|
88
|
+
* Constructor for a boolean data field
|
|
89
|
+
*
|
|
90
|
+
* @param db OINODb reference
|
|
91
|
+
* @param name name of the field
|
|
92
|
+
* @param sqlType column type in database
|
|
93
|
+
* @param fieldParams parameters of the field
|
|
94
|
+
*
|
|
95
|
+
*/
|
|
96
|
+
constructor(db: OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams);
|
|
97
|
+
/**
|
|
98
|
+
* Serialize cell value in the given content format.
|
|
99
|
+
*
|
|
100
|
+
* @param cellVal cell value
|
|
101
|
+
*
|
|
102
|
+
*/
|
|
103
|
+
serializeCell(cellVal: OINODataCell): string | null | undefined;
|
|
104
|
+
/**
|
|
105
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
106
|
+
*
|
|
107
|
+
* @param value string value
|
|
108
|
+
*
|
|
109
|
+
*/
|
|
110
|
+
deserializeCell(value: string | null | undefined): OINODataCell;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Specialised class for a number column.
|
|
114
|
+
*
|
|
115
|
+
*/
|
|
116
|
+
export declare class OINONumberDataField extends OINODbDataField {
|
|
117
|
+
/**
|
|
118
|
+
* Constructor for a string data field
|
|
119
|
+
*
|
|
120
|
+
* @param db OINODb reference
|
|
121
|
+
* @param name name of the field
|
|
122
|
+
* @param sqlType column type in database
|
|
123
|
+
* @param fieldParams parameters of the field
|
|
124
|
+
*
|
|
125
|
+
*/
|
|
126
|
+
constructor(db: OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams);
|
|
127
|
+
/**
|
|
128
|
+
* Serialize cell value in the given content format.
|
|
129
|
+
*
|
|
130
|
+
* @param cellVal cell value
|
|
131
|
+
*
|
|
132
|
+
*/
|
|
133
|
+
serializeCell(cellVal: OINODataCell): string | null | undefined;
|
|
134
|
+
/**
|
|
135
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
136
|
+
*
|
|
137
|
+
* @param value string value
|
|
138
|
+
*
|
|
139
|
+
*/
|
|
140
|
+
deserializeCell(value: string | null | undefined): OINODataCell;
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Specialised class for a blob column.
|
|
144
|
+
*
|
|
145
|
+
*/
|
|
146
|
+
export declare class OINOBlobDataField extends OINODbDataField {
|
|
147
|
+
/**
|
|
148
|
+
* Constructor for a blob data field
|
|
149
|
+
*
|
|
150
|
+
* @param db OINODb reference
|
|
151
|
+
* @param name name of the field
|
|
152
|
+
* @param sqlType column type in database
|
|
153
|
+
* @param fieldParams parameters of the field
|
|
154
|
+
* @param maxLength maximum length of the field (or 0)
|
|
155
|
+
*
|
|
156
|
+
*/
|
|
157
|
+
constructor(db: OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams, maxLength: number);
|
|
158
|
+
/**
|
|
159
|
+
* Serialize cell value in the given content format.
|
|
160
|
+
*
|
|
161
|
+
* @param cellVal cell value
|
|
162
|
+
*
|
|
163
|
+
*/
|
|
164
|
+
serializeCell(cellVal: OINODataCell): string | null | undefined;
|
|
165
|
+
/**
|
|
166
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
167
|
+
*
|
|
168
|
+
* @param value string value
|
|
169
|
+
*
|
|
170
|
+
*/
|
|
171
|
+
deserializeCell(value: string | null | undefined): OINODataCell;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Specialised class for a datetime column.
|
|
175
|
+
*
|
|
176
|
+
*/
|
|
177
|
+
export declare class OINODatetimeDataField extends OINODbDataField {
|
|
178
|
+
/**
|
|
179
|
+
* Constructor for a string data field
|
|
180
|
+
*
|
|
181
|
+
* @param db OINODb reference
|
|
182
|
+
* @param name name of the field
|
|
183
|
+
* @param sqlType column type in database
|
|
184
|
+
* @param fieldParams parameters of the field
|
|
185
|
+
*
|
|
186
|
+
*/
|
|
187
|
+
constructor(db: OINODb, name: string, sqlType: string, fieldParams: OINODbDataFieldParams);
|
|
188
|
+
/**
|
|
189
|
+
* Serialize cell value in the given content format.
|
|
190
|
+
*
|
|
191
|
+
* @param cellVal cell value
|
|
192
|
+
*
|
|
193
|
+
*/
|
|
194
|
+
serializeCell(cellVal: OINODataCell): string | null | undefined;
|
|
195
|
+
/**
|
|
196
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
197
|
+
*
|
|
198
|
+
* @param value string value
|
|
199
|
+
*
|
|
200
|
+
*/
|
|
201
|
+
deserializeCell(value: string | null | undefined): OINODataCell;
|
|
202
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { OINODbDataField, OINODbApi, OINODataRow, OINODbDataFieldFilter, OINODbSqlParams } from "./index.js";
|
|
2
|
+
/**
|
|
3
|
+
* OINO Datamodel object for representing one database table and it's columns.
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export declare class OINODbDataModel {
|
|
7
|
+
private _columnLookup;
|
|
8
|
+
/** Database refererence of the table */
|
|
9
|
+
readonly api: OINODbApi;
|
|
10
|
+
/** Field refererences of the API */
|
|
11
|
+
readonly fields: OINODbDataField[];
|
|
12
|
+
/**
|
|
13
|
+
* Constructor of the data model.
|
|
14
|
+
* NOTE! OINODbDataModel.initialize must be called after constructor to populate fields.
|
|
15
|
+
*
|
|
16
|
+
* @param api api of the data model
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
constructor(api: OINODbApi);
|
|
20
|
+
/**
|
|
21
|
+
* Initialize datamodel from SQL schema.
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
initialize(): Promise<void>;
|
|
25
|
+
private _printSqlColumnNames;
|
|
26
|
+
private _printSqlInsertColumnsAndValues;
|
|
27
|
+
private _printSqlUpdateValues;
|
|
28
|
+
private _printSqlPrimaryKeyCondition;
|
|
29
|
+
/**
|
|
30
|
+
* Add a field to the datamodel.
|
|
31
|
+
*
|
|
32
|
+
* @param field dataset field
|
|
33
|
+
*
|
|
34
|
+
*/
|
|
35
|
+
addField(field: OINODbDataField): void;
|
|
36
|
+
/**
|
|
37
|
+
* Find a field of a given name if any.
|
|
38
|
+
*
|
|
39
|
+
* @param name name of the field to find
|
|
40
|
+
*
|
|
41
|
+
*/
|
|
42
|
+
findFieldByName(name: string): OINODbDataField | null;
|
|
43
|
+
/**
|
|
44
|
+
* Find index of a field of a given name if any.
|
|
45
|
+
*
|
|
46
|
+
* @param name name of the field to find
|
|
47
|
+
*
|
|
48
|
+
*/
|
|
49
|
+
findFieldIndexByName(name: string): number;
|
|
50
|
+
/**
|
|
51
|
+
* Find all fields based of given filter callback criteria (e.g. fields of certain data type, primary keys etc.)
|
|
52
|
+
*
|
|
53
|
+
* @param filter callback called for each field to include or not
|
|
54
|
+
*
|
|
55
|
+
*/
|
|
56
|
+
filterFields(filter: OINODbDataFieldFilter): OINODbDataField[];
|
|
57
|
+
/**
|
|
58
|
+
* Return the primary key values of one row in order of the data model
|
|
59
|
+
*
|
|
60
|
+
* @param row data row
|
|
61
|
+
* @param hashidValues apply hashid when applicable
|
|
62
|
+
*
|
|
63
|
+
*/
|
|
64
|
+
getRowPrimarykeyValues(row: OINODataRow, hashidValues?: boolean): string[];
|
|
65
|
+
/**
|
|
66
|
+
* Print debug information about the fields.
|
|
67
|
+
*
|
|
68
|
+
* @param separator string to separate field prints
|
|
69
|
+
*
|
|
70
|
+
*/
|
|
71
|
+
printDebug(separator?: string): string;
|
|
72
|
+
/**
|
|
73
|
+
* Print all public properties (db, table name, fields) of the datamodel. Used
|
|
74
|
+
* in automated testing validate schema has stayed the same.
|
|
75
|
+
*
|
|
76
|
+
*/
|
|
77
|
+
printFieldPublicPropertiesJson(): string;
|
|
78
|
+
/**
|
|
79
|
+
* Print SQL select statement using optional id and filter.
|
|
80
|
+
*
|
|
81
|
+
* @param id OINO ID (i.e. combined primary key values)
|
|
82
|
+
* @param params OINO reqest params
|
|
83
|
+
*
|
|
84
|
+
*/
|
|
85
|
+
printSqlSelect(id: string, params: OINODbSqlParams): string;
|
|
86
|
+
/**
|
|
87
|
+
* Print SQL insert statement from one data row.
|
|
88
|
+
*
|
|
89
|
+
* @param row one row of data in the data model
|
|
90
|
+
*
|
|
91
|
+
*/
|
|
92
|
+
printSqlInsert(row: OINODataRow): string;
|
|
93
|
+
/**
|
|
94
|
+
* Print SQL insert statement from one data row.
|
|
95
|
+
*
|
|
96
|
+
* @param id OINO ID (i.e. combined primary key values)
|
|
97
|
+
* @param row one row of data in the data model
|
|
98
|
+
*
|
|
99
|
+
*/
|
|
100
|
+
printSqlUpdate(id: string, row: OINODataRow): string;
|
|
101
|
+
/**
|
|
102
|
+
* Print SQL delete statement for id.
|
|
103
|
+
*
|
|
104
|
+
* @param id OINO ID (i.e. combined primary key values)
|
|
105
|
+
*
|
|
106
|
+
*/
|
|
107
|
+
printSqlDelete(id: string): string;
|
|
108
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { OINODataRow } from "./index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Base class for SQL results that can be asynchronously iterated (but
|
|
4
|
+
* not necessarity rewinded). Idea is to handle database specific mechanisms
|
|
5
|
+
* for returning and formatting conventions in the database specific
|
|
6
|
+
* implementation. Data might be in memory or streamed in chunks and
|
|
7
|
+
* `OINODbDataSet` will serve it out consistently.
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
export declare abstract class OINODbDataSet {
|
|
11
|
+
private _data;
|
|
12
|
+
/** Error messages */
|
|
13
|
+
readonly messages: string[];
|
|
14
|
+
/**
|
|
15
|
+
* Constructor for `OINODbDataSet`.
|
|
16
|
+
*
|
|
17
|
+
* @param data internal database specific data type (constructor will throw if invalid)
|
|
18
|
+
* @param messages error messages from SQL-query
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
constructor(data: unknown, messages?: string[]);
|
|
22
|
+
/**
|
|
23
|
+
* Is data set empty.
|
|
24
|
+
*
|
|
25
|
+
*/
|
|
26
|
+
abstract isEmpty(): boolean;
|
|
27
|
+
/**
|
|
28
|
+
* Is there no more content, i.e. either dataset is empty or we have moved beyond last line
|
|
29
|
+
*
|
|
30
|
+
*/
|
|
31
|
+
abstract isEof(): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
|
|
34
|
+
*
|
|
35
|
+
*/
|
|
36
|
+
abstract next(): Promise<boolean>;
|
|
37
|
+
/**
|
|
38
|
+
* Gets current row of data.
|
|
39
|
+
*
|
|
40
|
+
*/
|
|
41
|
+
abstract getRow(): OINODataRow;
|
|
42
|
+
/**
|
|
43
|
+
* Checks if the messages contain errors.
|
|
44
|
+
*
|
|
45
|
+
*/
|
|
46
|
+
hasErrors(): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Checks if the messages contain errors.
|
|
49
|
+
*
|
|
50
|
+
*/
|
|
51
|
+
getFirstError(): string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Generic in memory implementation of a data set where data is an array of rows. Used
|
|
55
|
+
* by BunSqlite and automated testing. Can be rewinded.
|
|
56
|
+
*
|
|
57
|
+
*/
|
|
58
|
+
export declare class OINODbMemoryDataSet extends OINODbDataSet {
|
|
59
|
+
private _rows;
|
|
60
|
+
private _currentRow;
|
|
61
|
+
private _eof;
|
|
62
|
+
/**
|
|
63
|
+
* Constructor of `OINODbMemoryDataSet`.
|
|
64
|
+
*
|
|
65
|
+
* @param data data as OINODataRow[] (constructor will throw if invalid)
|
|
66
|
+
* @param errors error messages from SQL-query
|
|
67
|
+
*
|
|
68
|
+
*/
|
|
69
|
+
constructor(data: unknown, errors?: string[]);
|
|
70
|
+
/**
|
|
71
|
+
* Is data set empty.
|
|
72
|
+
*
|
|
73
|
+
*/
|
|
74
|
+
isEmpty(): boolean;
|
|
75
|
+
/**
|
|
76
|
+
* Is there no more content, i.e. either dataset is empty or we have moved beyond last line
|
|
77
|
+
*
|
|
78
|
+
*/
|
|
79
|
+
isEof(): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
|
|
82
|
+
*
|
|
83
|
+
*/
|
|
84
|
+
next(): Promise<boolean>;
|
|
85
|
+
/**
|
|
86
|
+
* Gets current row of data.
|
|
87
|
+
*
|
|
88
|
+
*/
|
|
89
|
+
getRow(): OINODataRow;
|
|
90
|
+
/**
|
|
91
|
+
* Rewinds data set to the first row, returns !isEof().
|
|
92
|
+
*
|
|
93
|
+
*/
|
|
94
|
+
first(): boolean;
|
|
95
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { OINODbApi, OINODbApiParams, OINODbParams, OINODb, OINODbConstructor, OINODbApiRequestParams } from "./index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Static factory class for easily creating things based on data
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export declare class OINODbFactory {
|
|
7
|
+
private static _dbRegistry;
|
|
8
|
+
/**
|
|
9
|
+
* Register a supported database class. Used to enable those that are installed in the factory
|
|
10
|
+
* instead of forcing everyone to install all database libraries.
|
|
11
|
+
*
|
|
12
|
+
* @param dbName name of the database implementation class
|
|
13
|
+
* @param dbTypeClass constructor for creating a database of that type
|
|
14
|
+
*/
|
|
15
|
+
static registerDb(dbName: string, dbTypeClass: OINODbConstructor): void;
|
|
16
|
+
/**
|
|
17
|
+
* Create database from parameters from the registered classes.
|
|
18
|
+
*
|
|
19
|
+
* @param params database connection parameters
|
|
20
|
+
*/
|
|
21
|
+
static createDb(params: OINODbParams): Promise<OINODb>;
|
|
22
|
+
/**
|
|
23
|
+
* Create API from parameters and calls initDatamodel on the datamodel.
|
|
24
|
+
*
|
|
25
|
+
* @param db databased used in API
|
|
26
|
+
* @param params parameters of the API
|
|
27
|
+
*/
|
|
28
|
+
static createApi(db: OINODb, params: OINODbApiParams): Promise<OINODbApi>;
|
|
29
|
+
/**
|
|
30
|
+
* Creates a key-value-collection from Javascript URL parameters.
|
|
31
|
+
*
|
|
32
|
+
* @param request HTTP Request
|
|
33
|
+
*/
|
|
34
|
+
static createParamsFromRequest(request: Request): OINODbApiRequestParams;
|
|
35
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { OINODbDataSet, OINODbDataModel, OINOContentType, OINODataCell } from "./index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Class for dataset based on a data model that can be serialized to
|
|
4
|
+
* a supported format:
|
|
5
|
+
* - JSON (application/json)
|
|
6
|
+
* - CSV (text/csv)
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
export declare class OINODbModelSet {
|
|
10
|
+
/** Reference to datamodel */
|
|
11
|
+
readonly datamodel: OINODbDataModel;
|
|
12
|
+
/** Reference to data set */
|
|
13
|
+
readonly dataset: OINODbDataSet;
|
|
14
|
+
/** Collection of errors */
|
|
15
|
+
errors: string[];
|
|
16
|
+
/**
|
|
17
|
+
* Constructor for `OINODbModelSet`.
|
|
18
|
+
*
|
|
19
|
+
* @param datamodel data model
|
|
20
|
+
* @param dataset data set
|
|
21
|
+
*/
|
|
22
|
+
constructor(datamodel: OINODbDataModel, dataset: OINODbDataSet);
|
|
23
|
+
private _encodeAndHashFieldValue;
|
|
24
|
+
private _writeRowJson;
|
|
25
|
+
private _writeStringJson;
|
|
26
|
+
private _writeHeaderCsv;
|
|
27
|
+
private _writeRowCsv;
|
|
28
|
+
private _writeStringCsv;
|
|
29
|
+
private _writeRowFormdataParameterBlock;
|
|
30
|
+
private _writeRowFormdataFileBlock;
|
|
31
|
+
private _writeRowFormdata;
|
|
32
|
+
private _writeStringFormdata;
|
|
33
|
+
private _writeRowUrlencode;
|
|
34
|
+
private _writeStringUrlencode;
|
|
35
|
+
/**
|
|
36
|
+
* Serialize model set in the given format.
|
|
37
|
+
*
|
|
38
|
+
* @param [contentType=OINOContentType.json] serialization content type
|
|
39
|
+
*
|
|
40
|
+
*/
|
|
41
|
+
writeString(contentType?: OINOContentType): Promise<string>;
|
|
42
|
+
/**
|
|
43
|
+
* Get value of given field in the current row. Undefined if no rows,
|
|
44
|
+
* field not found or value does not exist.
|
|
45
|
+
*
|
|
46
|
+
* @param fieldName name of the field
|
|
47
|
+
* @param serialize serialize the value
|
|
48
|
+
*
|
|
49
|
+
*/
|
|
50
|
+
getValueByFieldName(fieldName: string, serialize?: boolean): OINODataCell;
|
|
51
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { OINODbDataModel, OINODataRow, OINODbApiRequestParams } from "./index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Static factory class for easily creating things based on data
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export declare class OINODbParser {
|
|
7
|
+
/**
|
|
8
|
+
* Create data rows from request body based on the datamodel.
|
|
9
|
+
*
|
|
10
|
+
* @param datamodel datamodel of the api
|
|
11
|
+
* @param data data as a string or Buffer or object
|
|
12
|
+
* @param requestParams parameters
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
static createRows(datamodel: OINODbDataModel, data: string | Buffer | object, requestParams: OINODbApiRequestParams): OINODataRow[];
|
|
16
|
+
/**
|
|
17
|
+
* Create data rows from request body based on the datamodel.
|
|
18
|
+
*
|
|
19
|
+
* @param datamodel datamodel of the api
|
|
20
|
+
* @param data data as a string
|
|
21
|
+
* @param requestParams parameters
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
static createRowsFromText(datamodel: OINODbDataModel, data: string, requestParams: OINODbApiRequestParams): OINODataRow[];
|
|
25
|
+
/**
|
|
26
|
+
* Create data rows from request body based on the datamodel.
|
|
27
|
+
*
|
|
28
|
+
* @param datamodel datamodel of the api
|
|
29
|
+
* @param data data as an Buffer
|
|
30
|
+
* @param requestParams parameters
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
static createRowsFromBlob(datamodel: OINODbDataModel, data: Buffer, requestParams: OINODbApiRequestParams): OINODataRow[];
|
|
34
|
+
/**
|
|
35
|
+
* Create one data row from javascript object based on the datamodel.
|
|
36
|
+
* NOTE! Data assumed to be unserialized i.e. of the native type (string, number, boolean, Buffer)
|
|
37
|
+
*
|
|
38
|
+
* @param datamodel datamodel of the api
|
|
39
|
+
* @param data data as javascript object
|
|
40
|
+
*
|
|
41
|
+
*/
|
|
42
|
+
static createRowFromObject(datamodel: OINODbDataModel, data: any): OINODataRow;
|
|
43
|
+
private static _findCsvLineEnd;
|
|
44
|
+
private static _parseCsvLine;
|
|
45
|
+
private static _createRowFromCsv;
|
|
46
|
+
private static _createRowFromJsonObj;
|
|
47
|
+
private static _createRowFromJson;
|
|
48
|
+
private static _findMultipartBoundary;
|
|
49
|
+
private static _parseMultipartLine;
|
|
50
|
+
private static _multipartHeaderRegex;
|
|
51
|
+
private static _createRowFromFormdata;
|
|
52
|
+
private static _createRowFromUrlencoded;
|
|
53
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { OINODbDataModel } from "./index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Supported logical conjunctions in filter predicates.
|
|
4
|
+
* @enum
|
|
5
|
+
*/
|
|
6
|
+
export declare enum OINODbSqlBooleanOperation {
|
|
7
|
+
and = "and",
|
|
8
|
+
or = "or",
|
|
9
|
+
not = "not"
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Supported logical conjunctions in filter predicates.
|
|
13
|
+
* @enum
|
|
14
|
+
*/
|
|
15
|
+
export declare enum OINODbSqlComparison {
|
|
16
|
+
lt = "lt",
|
|
17
|
+
le = "le",
|
|
18
|
+
eq = "eq",
|
|
19
|
+
ge = "ge",
|
|
20
|
+
gt = "gt",
|
|
21
|
+
like = "like"
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Class for recursively parsing of filters and printing them as SQL conditions.
|
|
25
|
+
* Supports three types of statements
|
|
26
|
+
* - comparison: (field)-lt|le|eq|ge|gt|like(value)
|
|
27
|
+
* - negation: -not(filter)
|
|
28
|
+
* - conjunction/disjunction: (filter)-and|or(filter)
|
|
29
|
+
* Supported conditions are comparisons (<, <=, =, >=, >) and substring match (LIKE).
|
|
30
|
+
*
|
|
31
|
+
*/
|
|
32
|
+
export declare class OINODbSqlFilter {
|
|
33
|
+
private static _booleanOperationRegex;
|
|
34
|
+
private static _negationRegex;
|
|
35
|
+
private static _filterComparisonRegex;
|
|
36
|
+
private _leftSide;
|
|
37
|
+
private _rightSide;
|
|
38
|
+
private _operator;
|
|
39
|
+
/**
|
|
40
|
+
* Constructor of `OINODbSqlFilter`
|
|
41
|
+
* @param leftSide left side of the filter, either another filter or a column name
|
|
42
|
+
* @param operation operation of the filter, either `OINODbSqlComparison` or `OINODbSqlBooleanOperation`
|
|
43
|
+
* @param rightSide right side of the filter, either another filter or a value
|
|
44
|
+
*/
|
|
45
|
+
constructor(leftSide: OINODbSqlFilter | string, operation: OINODbSqlComparison | OINODbSqlBooleanOperation | null, rightSide: OINODbSqlFilter | string);
|
|
46
|
+
/**
|
|
47
|
+
* Constructor for `OINOFilter` as parser of http parameter.
|
|
48
|
+
*
|
|
49
|
+
* @param filterString string representation of filter from HTTP-request
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
static parse(filterString: string): OINODbSqlFilter;
|
|
53
|
+
/**
|
|
54
|
+
* Construct a new `OINOFilter` as combination of (boolean and/or) of two filters.
|
|
55
|
+
*
|
|
56
|
+
* @param leftSide left side to combine
|
|
57
|
+
* @param operation boolean operation to use in combination
|
|
58
|
+
* @param rightSide right side to combine
|
|
59
|
+
*
|
|
60
|
+
*/
|
|
61
|
+
static combine(leftSide: OINODbSqlFilter | undefined, operation: OINODbSqlBooleanOperation, rightSide: OINODbSqlFilter | undefined): OINODbSqlFilter | undefined;
|
|
62
|
+
private _operatorToSql;
|
|
63
|
+
/**
|
|
64
|
+
* Does filter contain any valid conditions.
|
|
65
|
+
*
|
|
66
|
+
*/
|
|
67
|
+
isEmpty(): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Print filter as SQL condition based on the datamodel of the API.
|
|
70
|
+
*
|
|
71
|
+
* @param dataModel data model (and database) to use for formatting of values
|
|
72
|
+
*
|
|
73
|
+
*/
|
|
74
|
+
toSql(dataModel: OINODbDataModel): string;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Class for ordering select results on a number of columns.
|
|
78
|
+
*
|
|
79
|
+
*/
|
|
80
|
+
export declare class OINODbSqlOrder {
|
|
81
|
+
private static _orderColumnRegex;
|
|
82
|
+
private _columns;
|
|
83
|
+
private _descending;
|
|
84
|
+
/**
|
|
85
|
+
* Constructor for `OINODbSqlOrder`.
|
|
86
|
+
*
|
|
87
|
+
* @param column_or_array single or array of columns to order on
|
|
88
|
+
* @param descending_or_array single or array of booleans if ordes is descending
|
|
89
|
+
*
|
|
90
|
+
*/
|
|
91
|
+
constructor(column_or_array: string[] | string, descending_or_array: boolean[] | boolean);
|
|
92
|
+
/**
|
|
93
|
+
* Constructor for `OINODbSqlOrder` as parser of http parameter.
|
|
94
|
+
*
|
|
95
|
+
* @param orderString string representation of ordering from HTTP-request
|
|
96
|
+
*
|
|
97
|
+
*/
|
|
98
|
+
static parse(orderString: string): OINODbSqlOrder;
|
|
99
|
+
/**
|
|
100
|
+
* Does filter contain any valid conditions.
|
|
101
|
+
*
|
|
102
|
+
*/
|
|
103
|
+
isEmpty(): boolean;
|
|
104
|
+
/**
|
|
105
|
+
* Print order as SQL condition based on the datamodel of the API.
|
|
106
|
+
*
|
|
107
|
+
* @param dataModel data model (and database) to use for formatting of values
|
|
108
|
+
*
|
|
109
|
+
*/
|
|
110
|
+
toSql(dataModel: OINODbDataModel): string;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Class for limiting the number of results.
|
|
114
|
+
*
|
|
115
|
+
*/
|
|
116
|
+
export declare class OINODbSqlLimit {
|
|
117
|
+
private static _limitRegex;
|
|
118
|
+
private _limit;
|
|
119
|
+
private _page;
|
|
120
|
+
/**
|
|
121
|
+
* Constructor for `OINODbSqlLimit`.
|
|
122
|
+
*
|
|
123
|
+
* @param limit maximum number of items to return
|
|
124
|
+
*
|
|
125
|
+
*/
|
|
126
|
+
constructor(limit: number, page?: number);
|
|
127
|
+
/**
|
|
128
|
+
* Constructor for `OINODbSqlLimit` as parser of http parameter.
|
|
129
|
+
*
|
|
130
|
+
* @param limitString string representation of limit from HTTP-request
|
|
131
|
+
*
|
|
132
|
+
*/
|
|
133
|
+
static parse(limitString: string): OINODbSqlLimit;
|
|
134
|
+
/**
|
|
135
|
+
* Does filter contain any valid conditions.
|
|
136
|
+
*
|
|
137
|
+
*/
|
|
138
|
+
isEmpty(): boolean;
|
|
139
|
+
/**
|
|
140
|
+
* Print order as SQL condition based on the datamodel of the API.
|
|
141
|
+
*
|
|
142
|
+
* @param dataModel data model (and database) to use for formatting of values
|
|
143
|
+
*
|
|
144
|
+
*/
|
|
145
|
+
toSql(dataModel: OINODbDataModel): string;
|
|
146
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { OINODbDataModel } from "./index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Supported logical conjunctions in filter predicates.
|
|
4
|
+
* @enum
|
|
5
|
+
*/
|
|
6
|
+
export declare enum OINODbSqlBooleanOperation {
|
|
7
|
+
and = "and",
|
|
8
|
+
or = "or",
|
|
9
|
+
not = "not"
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Supported logical conjunctions in filter predicates.
|
|
13
|
+
* @enum
|
|
14
|
+
*/
|
|
15
|
+
export declare enum OINODbSqlComparison {
|
|
16
|
+
lt = "lt",
|
|
17
|
+
le = "le",
|
|
18
|
+
eq = "eq",
|
|
19
|
+
ge = "ge",
|
|
20
|
+
gt = "gt",
|
|
21
|
+
like = "like"
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Class for recursively parsing of filters and printing them as SQL conditions.
|
|
25
|
+
* Supports three types of statements
|
|
26
|
+
* - comparison: (field)-lt|le|eq|ge|gt|like(value)
|
|
27
|
+
* - negation: -not(filter)
|
|
28
|
+
* - conjunction/disjunction: (filter)-and|or(filter)
|
|
29
|
+
* Supported conditions are comparisons (<, <=, =, >=, >) and substring match (LIKE).
|
|
30
|
+
*
|
|
31
|
+
*/
|
|
32
|
+
export declare class OINODbSqlFilter {
|
|
33
|
+
private static _booleanOperationRegex;
|
|
34
|
+
private static _negationRegex;
|
|
35
|
+
private static _filterComparisonRegex;
|
|
36
|
+
private _leftSide;
|
|
37
|
+
private _rightSide;
|
|
38
|
+
private _operator;
|
|
39
|
+
/**
|
|
40
|
+
* Constructor of `OINODbSqlFilter`
|
|
41
|
+
* @param leftSide left side of the filter, either another filter or a column name
|
|
42
|
+
* @param operation operation of the filter, either `OINODbSqlComparison` or `OINODbSqlBooleanOperation`
|
|
43
|
+
* @param rightSide right side of the filter, either another filter or a value
|
|
44
|
+
*/
|
|
45
|
+
constructor(leftSide: OINODbSqlFilter | string, operation: OINODbSqlComparison | OINODbSqlBooleanOperation | null, rightSide: OINODbSqlFilter | string);
|
|
46
|
+
/**
|
|
47
|
+
* Constructor for `OINOFilter` as parser of http parameter.
|
|
48
|
+
*
|
|
49
|
+
* @param filterString string representation of filter from HTTP-request
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
static parse(filterString: string): OINODbSqlFilter;
|
|
53
|
+
/**
|
|
54
|
+
* Construct a new `OINOFilter` as combination of (boolean and/or) of two filters.
|
|
55
|
+
*
|
|
56
|
+
* @param leftSide left side to combine
|
|
57
|
+
* @param operation boolean operation to use in combination
|
|
58
|
+
* @param rightSide right side to combine
|
|
59
|
+
*
|
|
60
|
+
*/
|
|
61
|
+
static combine(leftSide: OINODbSqlFilter | undefined, operation: OINODbSqlBooleanOperation, rightSide: OINODbSqlFilter | undefined): OINODbSqlFilter | undefined;
|
|
62
|
+
private _operatorToSql;
|
|
63
|
+
/**
|
|
64
|
+
* Does filter contain any valid conditions.
|
|
65
|
+
*
|
|
66
|
+
*/
|
|
67
|
+
isEmpty(): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Print filter as SQL condition based on the datamodel of the API.
|
|
70
|
+
*
|
|
71
|
+
* @param dataModel data model (and database) to use for formatting of values
|
|
72
|
+
*
|
|
73
|
+
*/
|
|
74
|
+
toSql(dataModel: OINODbDataModel): string;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Class for ordering select results on a number of columns.
|
|
78
|
+
*
|
|
79
|
+
*/
|
|
80
|
+
export declare class OINODbSqlOrder {
|
|
81
|
+
private static _orderColumnRegex;
|
|
82
|
+
private _columns;
|
|
83
|
+
private _descending;
|
|
84
|
+
/**
|
|
85
|
+
* Constructor for `OINODbSqlOrder`.
|
|
86
|
+
*
|
|
87
|
+
* @param column_or_array single or array of columns to order on
|
|
88
|
+
* @param descending_or_array single or array of booleans if ordes is descending
|
|
89
|
+
*
|
|
90
|
+
*/
|
|
91
|
+
constructor(column_or_array: string[] | string, descending_or_array: boolean[] | boolean);
|
|
92
|
+
/**
|
|
93
|
+
* Constructor for `OINODbSqlOrder` as parser of http parameter.
|
|
94
|
+
*
|
|
95
|
+
* @param orderString string representation of ordering from HTTP-request
|
|
96
|
+
*
|
|
97
|
+
*/
|
|
98
|
+
static parse(orderString: string): OINODbSqlOrder;
|
|
99
|
+
/**
|
|
100
|
+
* Does filter contain any valid conditions.
|
|
101
|
+
*
|
|
102
|
+
*/
|
|
103
|
+
isEmpty(): boolean;
|
|
104
|
+
/**
|
|
105
|
+
* Print order as SQL condition based on the datamodel of the API.
|
|
106
|
+
*
|
|
107
|
+
* @param dataModel data model (and database) to use for formatting of values
|
|
108
|
+
*
|
|
109
|
+
*/
|
|
110
|
+
toSql(dataModel: OINODbDataModel): string;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Class for limiting the number of results.
|
|
114
|
+
*
|
|
115
|
+
*/
|
|
116
|
+
export declare class OINODbSqlLimit {
|
|
117
|
+
private static _limitRegex;
|
|
118
|
+
private _limit;
|
|
119
|
+
private _page;
|
|
120
|
+
/**
|
|
121
|
+
* Constructor for `OINODbSqlLimit`.
|
|
122
|
+
*
|
|
123
|
+
* @param limit maximum number of items to return
|
|
124
|
+
* @param page page number to return starting from 1
|
|
125
|
+
*
|
|
126
|
+
*/
|
|
127
|
+
constructor(limit: number, page?: number);
|
|
128
|
+
/**
|
|
129
|
+
* Constructor for `OINODbSqlLimit` as parser of http parameter.
|
|
130
|
+
*
|
|
131
|
+
* @param limitString string representation of limit from HTTP-request
|
|
132
|
+
*
|
|
133
|
+
*/
|
|
134
|
+
static parse(limitString: string): OINODbSqlLimit;
|
|
135
|
+
/**
|
|
136
|
+
* Does filter contain any valid conditions.
|
|
137
|
+
*
|
|
138
|
+
*/
|
|
139
|
+
isEmpty(): boolean;
|
|
140
|
+
/**
|
|
141
|
+
* Print order as SQL condition based on the datamodel of the API.
|
|
142
|
+
*
|
|
143
|
+
* @param dataModel data model (and database) to use for formatting of values
|
|
144
|
+
*
|
|
145
|
+
*/
|
|
146
|
+
toSql(dataModel: OINODbDataModel): string;
|
|
147
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { OINODbApi } from "./index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Static class for Swagger utilities
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export declare class OINODbSwagger {
|
|
7
|
+
private static _getSchemaApiMethodParamsQueryId;
|
|
8
|
+
private static _getSchemaApiMethodParamsBody;
|
|
9
|
+
private static _getSchemaApiMethodDescription;
|
|
10
|
+
private static _getSchemaApiMethodOperationId;
|
|
11
|
+
private static _getSchemaOinoResponse;
|
|
12
|
+
private static _getSchemaFieldType;
|
|
13
|
+
private static _getSwaggerApiType;
|
|
14
|
+
private static _getSchemaType;
|
|
15
|
+
private static _getSchemaApiMethodParams;
|
|
16
|
+
private static _getSchemaApiMethod;
|
|
17
|
+
private static _getSwaggerApiPath;
|
|
18
|
+
/**
|
|
19
|
+
* Returns swagger.json as object of the given API's.
|
|
20
|
+
*
|
|
21
|
+
* @param apis array of API's use for Swagger definition
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
static getApiDefinition(apis: OINODbApi[]): any;
|
|
25
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { OINOContentType } from "@oino-ts/common";
|
|
2
|
+
export { OINOContentType };
|
|
3
|
+
export { OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX, OINOStr, OINOBenchmark, OINOLog, OINOLogLevel, OINOConsoleLog, OINOResult, OINOHttpResult, OINOHtmlTemplate } from "@oino-ts/common";
|
|
4
|
+
import { OINODb } from "./OINODb.js";
|
|
5
|
+
import { OINODbDataField } from "./OINODbDataField.js";
|
|
6
|
+
import { OINODbSqlFilter, OINODbSqlLimit, OINODbSqlOrder } from "./OINODbSqlParams.js";
|
|
7
|
+
export { OINODbApiResult, OINODbHtmlTemplate, OINODbApi } from "./OINODbApi.js";
|
|
8
|
+
export { OINODbDataModel } from "./OINODbDataModel.js";
|
|
9
|
+
export { OINODbModelSet } from "./OINODbModelSet.js";
|
|
10
|
+
export { OINODbDataField, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINOBlobDataField, OINODatetimeDataField } from "./OINODbDataField.js";
|
|
11
|
+
export { OINODbDataSet, OINODbMemoryDataSet, OINODb } from "./OINODb.js";
|
|
12
|
+
export { OINODbSqlFilter, OINODbSqlOrder, OINODbSqlComparison, OINODbSqlLimit, OINODbSqlBooleanOperation } from "./OINODbSqlParams.js";
|
|
13
|
+
export { OINODbConfig } from "./OINODbConfig.js";
|
|
14
|
+
export { OINODbFactory } from "./OINODbFactory.js";
|
|
15
|
+
export { OINODbSwagger } from "./OINODbSwagger.js";
|
|
16
|
+
export { OINODbParser } from "./OINODbParser.js";
|
|
17
|
+
/** API parameters */
|
|
18
|
+
export type OINODbApiParams = {
|
|
19
|
+
/** Name of the database table */
|
|
20
|
+
tableName: string;
|
|
21
|
+
/** Reject values that exceed field max length (behaviour on such is platform dependent) */
|
|
22
|
+
failOnOversizedValues?: Boolean;
|
|
23
|
+
/** Reject PUT-requests that contain values for autoinc-type fields */
|
|
24
|
+
failOnUpdateOnAutoinc?: boolean;
|
|
25
|
+
/** Reject POST-requests without primary key value (can work if DB-side ) */
|
|
26
|
+
failOnInsertWithoutKey?: boolean;
|
|
27
|
+
/** Treat date type fields as just strings and use the native formatting instead of the ISO 8601 format */
|
|
28
|
+
useDatesAsString?: Boolean;
|
|
29
|
+
/** Exclude all fields with this prefix from the API */
|
|
30
|
+
excludeFieldPrefix?: string;
|
|
31
|
+
/** Exclude given fields from the API */
|
|
32
|
+
excludeFields?: string[];
|
|
33
|
+
/** Enable hashids for numeric primarykeys by adding a 32 char key */
|
|
34
|
+
hashidKey?: string;
|
|
35
|
+
/** Set (minimum) length (12-32 chars) of the hashids */
|
|
36
|
+
hashidLength?: number;
|
|
37
|
+
/** Make hashids static per row/table */
|
|
38
|
+
hashidStaticIds?: boolean;
|
|
39
|
+
/** Name of field that has the modified field */
|
|
40
|
+
cacheModifiedField?: string;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Database class (constructor) type
|
|
44
|
+
* @param dbParams database parameters
|
|
45
|
+
*/
|
|
46
|
+
export type OINODbConstructor = new (dbParams: OINODbParams) => OINODb;
|
|
47
|
+
/** Database parameters */
|
|
48
|
+
export type OINODbParams = {
|
|
49
|
+
/** Name of the database class (e.g. OINODbPostgresql) */
|
|
50
|
+
type: string;
|
|
51
|
+
/** Connection URL, either file://-path or an IP-address or an HTTP-url */
|
|
52
|
+
url: string;
|
|
53
|
+
/** Name of the database */
|
|
54
|
+
database: string;
|
|
55
|
+
/** TCP port of the database */
|
|
56
|
+
port?: number;
|
|
57
|
+
/** Username used to authenticate */
|
|
58
|
+
user?: string;
|
|
59
|
+
/** Password used to authenticate */
|
|
60
|
+
password?: string;
|
|
61
|
+
};
|
|
62
|
+
/** Field parameters in database */
|
|
63
|
+
export type OINODbDataFieldParams = {
|
|
64
|
+
/** Is the field a primary key */
|
|
65
|
+
isPrimaryKey: Boolean;
|
|
66
|
+
/** Is the field a primary key */
|
|
67
|
+
isForeignKey: Boolean;
|
|
68
|
+
/** Is the field an auto inc type */
|
|
69
|
+
isAutoInc: Boolean;
|
|
70
|
+
/** Is the field allowed to have null values */
|
|
71
|
+
isNotNull: Boolean;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Callback to filter data fields
|
|
75
|
+
* @param field fields to filter
|
|
76
|
+
*/
|
|
77
|
+
export type OINODbDataFieldFilter = (field: OINODbDataField) => Boolean;
|
|
78
|
+
/** Request options */
|
|
79
|
+
export type OINODbSqlParams = {
|
|
80
|
+
/** Additional SQL select where-conditions */
|
|
81
|
+
filter?: OINODbSqlFilter;
|
|
82
|
+
/** SQL result ordering conditions */
|
|
83
|
+
order?: OINODbSqlOrder;
|
|
84
|
+
/** SQL result limit condition */
|
|
85
|
+
limit?: OINODbSqlLimit;
|
|
86
|
+
};
|
|
87
|
+
/** Request options */
|
|
88
|
+
export type OINODbApiRequestParams = {
|
|
89
|
+
/** Content type of the request body */
|
|
90
|
+
requestType?: OINOContentType;
|
|
91
|
+
/** Content type of the response body */
|
|
92
|
+
responseType?: OINOContentType;
|
|
93
|
+
/** Multipart boundary token */
|
|
94
|
+
multipartBoundary?: string;
|
|
95
|
+
/** Request last-modified value */
|
|
96
|
+
lastModified?: number;
|
|
97
|
+
/** Request etag values */
|
|
98
|
+
etags?: string[];
|
|
99
|
+
/** SQL parameters */
|
|
100
|
+
sqlParams?: OINODbSqlParams;
|
|
101
|
+
};
|
|
102
|
+
/** A single column value of a data row */
|
|
103
|
+
export type OINODataCell = string | bigint | number | boolean | Date | Uint8Array | Buffer | null | undefined;
|
|
104
|
+
/** A single data row */
|
|
105
|
+
export type OINODataRow = Array<OINODataCell>;
|
|
106
|
+
/** Empty row instance */
|
|
107
|
+
export declare const OINODB_EMPTY_ROW: OINODataRow;
|
|
108
|
+
/** Empty row array instance */
|
|
109
|
+
export declare const OINODB_EMPTY_ROWS: OINODataRow[];
|
|
110
|
+
/** Key-value collection */
|
|
111
|
+
export type OINOValues = Record<string, string>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oino-ts/db",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "OINO TS library package for publishing an SQL database tables as a REST API.",
|
|
5
5
|
"author": "Matias Kiviniemi (pragmatta)",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -19,12 +19,12 @@
|
|
|
19
19
|
"module": "./dist/esm/index.js",
|
|
20
20
|
"types": "./dist/types/index.d.ts",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@oino-ts/common": "0.
|
|
22
|
+
"@oino-ts/common": "0.2.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@types/node": "^20.14.10",
|
|
26
26
|
"@types/bun": "^1.1.14",
|
|
27
|
-
"@oino-ts/types": "0.
|
|
27
|
+
"@oino-ts/types": "0.2.0",
|
|
28
28
|
"typedoc": "^0.25.13"
|
|
29
29
|
},
|
|
30
30
|
"files": [
|
package/src/OINODbApi.test.ts
CHANGED
|
@@ -39,7 +39,7 @@ const api_tests:OINOTestParams[] = [
|
|
|
39
39
|
name: "API",
|
|
40
40
|
apiParams: { tableName: "Orders" },
|
|
41
41
|
requestParams: {
|
|
42
|
-
sqlParams: { filter: OINODbSqlFilter.parse("(ShipPostalCode)-like(0502%)"), order: OINODbSqlOrder.parse("ShipPostalCode
|
|
42
|
+
sqlParams: { filter: OINODbSqlFilter.parse("(ShipPostalCode)-like(0502%)"), order: OINODbSqlOrder.parse("ShipPostalCode-,Freight+"), limit: OINODbSqlLimit.parse("5 page 2") }
|
|
43
43
|
},
|
|
44
44
|
postRow: [30000,"CACTU",1,new Date("2024-04-05"),new Date("2024-04-06"),new Date("2024-04-07"),2,"184.75","a'b\"c%d_e\tf\rg\nh\\i","Garden House Crowther Way","Cowes","British Isles","PO31 7PJ","UK"],
|
|
45
45
|
putRow: [30000,"CACTU",1,new Date("2023-04-05"),new Date("2023-04-06"),new Date("2023-04-07"),2,"847.51","k'l\"m%n_o\tp\rq\nr\\s","59 rue de l'Abbaye","Cowes2","Western Europe","PO31 8PJ","UK"]
|
|
@@ -48,7 +48,7 @@ const api_tests:OINOTestParams[] = [
|
|
|
48
48
|
name: "API",
|
|
49
49
|
apiParams: { tableName: "Products", failOnOversizedValues: true },
|
|
50
50
|
requestParams: {
|
|
51
|
-
sqlParams: { filter: OINODbSqlFilter.parse("(UnitsInStock)-le(5)"), order: OINODbSqlOrder.parse("UnitsInStock
|
|
51
|
+
sqlParams: { filter: OINODbSqlFilter.parse("(UnitsInStock)-le(5)"), order: OINODbSqlOrder.parse("UnitsInStock,UnitPrice"), limit: OINODbSqlLimit.parse("7") }
|
|
52
52
|
},
|
|
53
53
|
postRow: [99, "Umeshu", 1, 1, "500 ml", 12.99, 2, 0, 20, 0],
|
|
54
54
|
putRow: [99, "Umeshu", 1, 1, undefined, 24.99, 3, 0, 20, 0]
|
package/src/OINODbSqlParams.ts
CHANGED
|
@@ -181,7 +181,7 @@ export class OINODbSqlFilter {
|
|
|
181
181
|
*
|
|
182
182
|
*/
|
|
183
183
|
export class OINODbSqlOrder {
|
|
184
|
-
private static _orderColumnRegex = /^\s*(\w+)\s?(ASC|DESC)?\s*?$/i
|
|
184
|
+
private static _orderColumnRegex = /^\s*(\w+)\s?(ASC|DESC|\+|\-)?\s*?$/i
|
|
185
185
|
|
|
186
186
|
private _columns: string[]
|
|
187
187
|
private _descending: boolean[]
|
|
@@ -223,7 +223,8 @@ export class OINODbSqlOrder {
|
|
|
223
223
|
let match = OINODbSqlOrder._orderColumnRegex.exec(column_strings[i])
|
|
224
224
|
if (match != null) {
|
|
225
225
|
columns.push(match[1])
|
|
226
|
-
|
|
226
|
+
const dir:string = (match[2] || "ASC").toUpperCase()
|
|
227
|
+
directions.push((dir == "DESC") || (dir == "-"))
|
|
227
228
|
}
|
|
228
229
|
}
|
|
229
230
|
return new OINODbSqlOrder(columns, directions)
|