@oino-ts/types 0.16.1 → 0.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,6 +18,11 @@ export declare abstract class OINOBenchmark {
18
18
  *
19
19
  */
20
20
  static setInstance(instance: OINOBenchmark): void;
21
+ /**
22
+ * Get active benchmarking instance.
23
+ *
24
+ */
25
+ static getInstance(): OINOBenchmark;
21
26
  protected abstract _reset(): void;
22
27
  /**
23
28
  * Reset benchmark data (but not what is enabled).
@@ -0,0 +1,57 @@
1
+ import { OINOContentType } from ".";
2
+ export interface OINORequestInit {
3
+ params?: Record<string, string>;
4
+ }
5
+ /**
6
+ * OINO API request result object with returned data and/or http status code/message and
7
+ * error / warning messages.
8
+ *
9
+ */
10
+ export declare class OINORequest {
11
+ /** Key-value parameters */
12
+ params?: Record<string, string>;
13
+ /**
14
+ * Constructor of OINORequest.
15
+ *
16
+ * @param init initialization values
17
+ *
18
+ */
19
+ constructor(init?: OINORequestInit);
20
+ /**
21
+ * Copy values from different result.
22
+ *
23
+ * @param request source value
24
+ */
25
+ copy(request: OINORequest): void;
26
+ }
27
+ export interface OINOHttpRequestInit extends OINORequestInit {
28
+ url?: URL;
29
+ method?: string;
30
+ headers?: Record<string, string>;
31
+ body?: string;
32
+ requestType?: OINOContentType;
33
+ responseType?: OINOContentType;
34
+ multipartBoundary?: string;
35
+ lastModified?: number;
36
+ }
37
+ /**
38
+ * Specialized result for HTTP responses.
39
+ */
40
+ export declare class OINOHttpRequest extends OINORequest {
41
+ readonly url?: URL;
42
+ readonly method: string;
43
+ readonly headers: Record<string, string>;
44
+ readonly body: string;
45
+ readonly requestType: OINOContentType;
46
+ readonly responseType: OINOContentType;
47
+ readonly multipartBoundary?: string;
48
+ readonly lastModified?: number;
49
+ readonly etags?: string[];
50
+ /**
51
+ * Constructor for a `OINOHttpRequest`
52
+ *
53
+ * @param init initialization values
54
+ *
55
+ */
56
+ constructor(init: OINOHttpRequestInit);
57
+ }
@@ -1,3 +1,9 @@
1
+ export interface OINOResultInit {
2
+ success?: boolean;
3
+ status?: number;
4
+ statusText?: string;
5
+ messages?: string[];
6
+ }
1
7
  /**
2
8
  * OINO API request result object with returned data and/or http status code/message and
3
9
  * error / warning messages.
@@ -7,16 +13,18 @@ export declare class OINOResult {
7
13
  /** Wheter request was successfully executed */
8
14
  success: boolean;
9
15
  /** HTTP status code */
10
- statusCode: number;
16
+ status: number;
11
17
  /** HTTP status message */
12
- statusMessage: string;
18
+ statusText: string;
13
19
  /** Error / warning messages */
14
20
  messages: string[];
15
21
  /**
16
22
  * Constructor of OINOResult.
17
23
  *
24
+ * @param init initialization values
25
+ *
18
26
  */
19
- constructor();
27
+ constructor(init?: OINOResultInit);
20
28
  /**
21
29
  * Copy values from different result.
22
30
  *
@@ -31,12 +39,12 @@ export declare class OINOResult {
31
39
  /**
32
40
  * Set HTTP error status using given code and message. Returns self reference for chaining.
33
41
  *
34
- * @param statusCode HTTP status code
35
- * @param statusMessage HTTP status message
42
+ * @param status HTTP status code
43
+ * @param statusText HTTP status message
36
44
  * @param operation operation where error occured
37
45
  *
38
46
  */
39
- setError(statusCode: number, statusMessage: string, operation: string): OINOResult;
47
+ setError(status: number, statusText: string, operation: string): OINOResult;
40
48
  /**
41
49
  * Add warning message. Returns self reference for chaining.
42
50
  *
@@ -77,12 +85,11 @@ export declare class OINOResult {
77
85
  *
78
86
  */
79
87
  printLog(): string;
80
- /**
81
- * Get a Response object from the result values.
82
- *
83
- * @param headers HTTP headers (overrides existing values)
84
- */
85
- getStatusResponse(headers?: Record<string, string>): Response;
88
+ }
89
+ export interface OINOHttpResultInit extends OINOResultInit {
90
+ body?: string;
91
+ expires?: number;
92
+ lastModified?: number;
86
93
  }
87
94
  /**
88
95
  * Specialized result for HTTP responses.
@@ -91,17 +98,19 @@ export declare class OINOHttpResult extends OINOResult {
91
98
  private _etag;
92
99
  /** HTTP body data */
93
100
  readonly body: string;
94
- /** HTTP cache expiration value */
101
+ /** HTTP cache expiration value
102
+ * Note: default 0 means no expiration and 'Pragma: no-cache' is set.
103
+ */
95
104
  expires: number;
96
105
  /** HTTP cache last-modified value */
97
106
  lastModified: number;
98
107
  /**
99
108
  * Constructor for a `OINOHttpResult`
100
109
  *
101
- * @param body HTTP body
110
+ * @param init initialization values
102
111
  *
103
112
  */
104
- constructor(body: string);
113
+ constructor(init?: OINOHttpResultInit);
105
114
  /**
106
115
  * Get the ETag value for the body opportunistically, i.e. don't calculate until requested and reuse value.
107
116
  *
@@ -1,6 +1,7 @@
1
1
  export { OINOBenchmark, OINOMemoryBenchmark } from "./OINOBenchmark.js";
2
2
  export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js";
3
- export { OINOResult, OINOHttpResult } from "./OINOResult.js";
3
+ export { OINOResult, OINOHttpResult, type OINOResultInit, type OINOHttpResultInit } from "./OINOResult.js";
4
+ export { OINORequest, OINOHttpRequest, type OINORequestInit, type OINOHttpRequestInit } from "./OINORequest.js";
4
5
  export { OINOStr } from "./OINOStr.js";
5
6
  export { OINOHtmlTemplate } from "./OINOHtmlTemplate.js";
6
7
  export { OINOFormatter, OINO_EMPTY_FORMATTER } from "./OINOFormatter.js";
@@ -12,6 +13,10 @@ export declare const OINO_WARNING_PREFIX = "OINO WARNING";
12
13
  export declare const OINO_INFO_PREFIX = "OINO INFO";
13
14
  /** OINO debug message prefix */
14
15
  export declare const OINO_DEBUG_PREFIX = "OINO DEBUG";
16
+ /** Name of the OINOContentType-parameter request */
17
+ export declare const OINO_REQUEST_TYPE_PARAM = "oinorequesttype";
18
+ /** Name of the OINOContentType-parameter request */
19
+ export declare const OINO_RESPONSE_TYPE_PARAM = "oinoresponsetype";
15
20
  /**
16
21
  * Supported content format mime-types
17
22
  */
@@ -0,0 +1,206 @@
1
+ import { OINODbParams, OINODbApi, OINODataCell, OINODataRow, OINOResult } 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
+ isConnected: boolean;
12
+ isValidated: boolean;
13
+ /**
14
+ * Constructor for `OINODb`.
15
+ * @param params database parameters
16
+ */
17
+ constructor(params: OINODbParams);
18
+ /**
19
+ * Connect to database.
20
+ *
21
+ */
22
+ abstract connect(): Promise<OINOResult>;
23
+ /**
24
+ * Validate connection to database is working.
25
+ *
26
+ */
27
+ abstract validate(): Promise<OINOResult>;
28
+ /**
29
+ * Print a table name using database specific SQL escaping.
30
+ *
31
+ * @param sqlTable name of the table
32
+ *
33
+ */
34
+ abstract printSqlTablename(sqlTable: string): string;
35
+ /**
36
+ * Print a column name with correct SQL escaping.
37
+ *
38
+ * @param sqlColumn name of the column
39
+ *
40
+ */
41
+ abstract printSqlColumnname(sqlColumn: string): string;
42
+ /**
43
+ * Print a single data value from serialization using the context of the native data
44
+ * type with the correct SQL escaping.
45
+ *
46
+ * @param cellValue data from sql results
47
+ * @param sqlType native type name for table column
48
+ *
49
+ */
50
+ abstract printCellAsSqlValue(cellValue: OINODataCell, sqlType: string): string;
51
+ /**
52
+ * Print a single string value as valid sql literal
53
+ *
54
+ * @param sqlString string value
55
+ *
56
+ */
57
+ abstract printSqlString(sqlString: string): string;
58
+ /**
59
+ * Parse a single SQL result value for serialization using the context of the native data
60
+ * type.
61
+ *
62
+ * @param sqlValue data from serialization
63
+ * @param sqlType native type name for table column
64
+ *
65
+ */
66
+ abstract parseSqlValueAsCell(sqlValue: OINODataCell, sqlType: string): OINODataCell;
67
+ /**
68
+ * Execute a select operation.
69
+ *
70
+ * @param sql SQL statement.
71
+ *
72
+ */
73
+ abstract sqlSelect(sql: string): Promise<OINODbDataSet>;
74
+ /**
75
+ * Execute other sql operations.
76
+ *
77
+ * @param sql SQL statement.
78
+ *
79
+ */
80
+ abstract sqlExec(sql: string): Promise<OINODbDataSet>;
81
+ /**
82
+ * Initialize a data model by getting the SQL schema and populating OINODbDataFields of
83
+ * the model.
84
+ *
85
+ * @param api api which data model to initialize.
86
+ *
87
+ */
88
+ abstract initializeApiDatamodel(api: OINODbApi): Promise<void>;
89
+ /**
90
+ * Print SQL select statement with DB specific formatting.
91
+ *
92
+ * @param tableName - The name of the table to select from.
93
+ * @param columnNames - The columns to be selected.
94
+ * @param whereCondition - The WHERE clause to filter the results.
95
+ * @param orderCondition - The ORDER BY clause to sort the results.
96
+ * @param limitCondition - The LIMIT clause to limit the number of results.
97
+ * @param groupByCondition - The GROUP BY clause to group the results.
98
+ *
99
+ */
100
+ printSqlSelect(tableName: string, columnNames: string, whereCondition: string, orderCondition: string, limitCondition: string, groupByCondition: string): string;
101
+ }
102
+ /**
103
+ * Base class for SQL results that can be asynchronously iterated (but
104
+ * not necessarity rewinded). Idea is to handle database specific mechanisms
105
+ * for returning and formatting conventions in the database specific
106
+ * implementation. Data might be in memory or streamed in chunks and
107
+ * `OINODbDataSet` will serve it out consistently.
108
+ *
109
+ */
110
+ export declare abstract class OINODbDataSet {
111
+ private _data;
112
+ /** Error messages */
113
+ readonly messages: string[];
114
+ /**
115
+ * Constructor for `OINODbDataSet`.
116
+ *
117
+ * @param data internal database specific data type (constructor will throw if invalid)
118
+ * @param messages error messages from SQL-query
119
+ *
120
+ */
121
+ constructor(data: unknown, messages?: string[]);
122
+ /**
123
+ * Is data set empty.
124
+ *
125
+ */
126
+ abstract isEmpty(): boolean;
127
+ /**
128
+ * Is there no more content, i.e. either dataset is empty or we have moved beyond last line
129
+ *
130
+ */
131
+ abstract isEof(): boolean;
132
+ /**
133
+ * Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
134
+ *
135
+ */
136
+ abstract next(): Promise<boolean>;
137
+ /**
138
+ * Gets current row of data.
139
+ *
140
+ */
141
+ abstract getRow(): OINODataRow;
142
+ /**
143
+ * Gets all rows of data.
144
+ *
145
+ * NOTE: This is left abstract instead of just using `getRow()` so that DB implementations can hopefully optimize not duplicating data *
146
+ */
147
+ abstract getAllRows(): Promise<OINODataRow[]>;
148
+ /**
149
+ * Checks if the messages contain errors.
150
+ *
151
+ */
152
+ hasErrors(): boolean;
153
+ /**
154
+ * Checks if the messages contain errors.
155
+ *
156
+ */
157
+ getFirstError(): string;
158
+ }
159
+ /**
160
+ * Generic in memory implementation of a data set where data is an array of rows. Used
161
+ * by BunSqlite and automated testing. Can be rewinded.
162
+ *
163
+ */
164
+ export declare class OINODbMemoryDataSet extends OINODbDataSet {
165
+ private _rows;
166
+ private _currentRow;
167
+ private _eof;
168
+ /**
169
+ * Constructor of `OINODbMemoryDataSet`.
170
+ *
171
+ * @param data data as OINODataRow[] (constructor will throw if invalid)
172
+ * @param errors error messages from SQL-query
173
+ *
174
+ */
175
+ constructor(data: unknown, errors?: string[]);
176
+ /**
177
+ * Is data set empty.
178
+ *
179
+ */
180
+ isEmpty(): boolean;
181
+ /**
182
+ * Is there no more content, i.e. either dataset is empty or we have moved beyond last line
183
+ *
184
+ */
185
+ isEof(): boolean;
186
+ /**
187
+ * Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
188
+ *
189
+ */
190
+ next(): Promise<boolean>;
191
+ /**
192
+ * Gets current row of data.
193
+ *
194
+ */
195
+ getRow(): OINODataRow;
196
+ /**
197
+ * Gets all rows of data.
198
+ *
199
+ */
200
+ getAllRows(): Promise<OINODataRow[]>;
201
+ /**
202
+ * Rewinds data set to the first row, returns !isEof().
203
+ *
204
+ */
205
+ first(): boolean;
206
+ }
@@ -0,0 +1,153 @@
1
+ import { OINODbApiParams, OINODb, OINODbDataModel, OINODataRow, OINODbModelSet, OINOHttpResult, OINOHtmlTemplate, OINODbSqlParams, OINODbSqlAggregate, OINODbSqlSelect, OINODbSqlFilter, OINODbSqlOrder, OINODbSqlLimit } from "./index.js";
2
+ import { OINOResult, OINOHttpRequest, OINOHttpRequestInit } from "@oino-ts/common";
3
+ import { OINOHashid } from "@oino-ts/hashid";
4
+ export interface OINODbApiRequestInit extends OINOHttpRequestInit {
5
+ rowId?: string;
6
+ data?: string | OINODataRow[] | Buffer | Uint8Array | object | null;
7
+ sqlParams?: OINODbSqlParams;
8
+ filter?: OINODbSqlFilter;
9
+ order?: OINODbSqlOrder;
10
+ limit?: OINODbSqlLimit;
11
+ aggregate?: OINODbSqlAggregate;
12
+ select?: OINODbSqlSelect;
13
+ }
14
+ export declare class OINODbApiRequest extends OINOHttpRequest {
15
+ readonly rowId: string;
16
+ readonly data: string | OINODataRow[] | Buffer | Uint8Array | object | null;
17
+ readonly sqlParams: OINODbSqlParams;
18
+ constructor(init: OINODbApiRequestInit);
19
+ }
20
+ /**
21
+ * OINO API request result object with returned data and/or http status code/message and
22
+ * error / warning messages.
23
+ *
24
+ */
25
+ export declare class OINODbApiResult extends OINOResult {
26
+ /** DbApi request params */
27
+ request: OINODbApiRequest;
28
+ /** Returned data if any */
29
+ data?: OINODbModelSet;
30
+ /**
31
+ * Constructor of OINODbApiResult.
32
+ *
33
+ * @param request DbApi request parameters
34
+ * @param data result data
35
+ *
36
+ */
37
+ constructor(request: OINODbApiRequest, data?: OINODbModelSet);
38
+ /**
39
+ * Creates a HTTP Response from API results.
40
+ *
41
+ * @param headers Headers to include in the response
42
+ *
43
+ */
44
+ writeApiResponse(headers?: Record<string, string>): Promise<Response>;
45
+ }
46
+ /**
47
+ * Specialized HTML template that can render ´OINODbApiResult´.
48
+ *
49
+ */
50
+ export declare class OINODbHtmlTemplate extends OINOHtmlTemplate {
51
+ /** Locale validation regex */
52
+ static LOCALE_REGEX: RegExp;
53
+ /** Locale formatter */
54
+ protected _locale: Intl.DateTimeFormat | null;
55
+ protected _numberDecimals: number;
56
+ /**
57
+ * Constructor of OINODbHtmlTemplate.
58
+ *
59
+ * @param template HTML template string
60
+ * @param numberDecimals Number of decimals to use for numbers, -1 for no formatting
61
+ * @param dateLocaleStr Datetime format string, either "iso" for ISO8601 or "default" for system default or valid locale string
62
+ * @param dateLocaleStyle Datetime format style, either "short/medium/long/full" or Intl.DateTimeFormat options
63
+ *
64
+ */
65
+ constructor(template: string, numberDecimals?: number, dateLocaleStr?: string, dateLocaleStyle?: string | any);
66
+ /**
67
+ * Creates HTML Response from API modelset.
68
+ *
69
+ * @param modelset OINO API dataset
70
+ * @param overrideValues values to override in the data
71
+ *
72
+ */
73
+ renderFromDbData(modelset: OINODbModelSet, overrideValues?: any): Promise<OINOHttpResult>;
74
+ }
75
+ /**
76
+ * API class with method to process HTTP REST requests.
77
+ *
78
+ */
79
+ export declare class OINODbApi {
80
+ /** Enable debug output on errors */
81
+ private _debugOnError;
82
+ /** API database reference */
83
+ readonly db: OINODb;
84
+ /** API datamodel */
85
+ readonly datamodel: OINODbDataModel;
86
+ /** API parameters */
87
+ readonly params: OINODbApiParams;
88
+ /** API hashid */
89
+ readonly hashid: OINOHashid | null;
90
+ /**
91
+ * Constructor of API object.
92
+ * NOTE! OINODb.initDatamodel must be called if created manually instead of the factory.
93
+ *
94
+ * @param db database for the API
95
+ * @param params parameters for the API
96
+ *
97
+ */
98
+ constructor(db: OINODb, params: OINODbApiParams);
99
+ private _validateRow;
100
+ private _parseData;
101
+ private _doGet;
102
+ private _doPost;
103
+ private _doPut;
104
+ private _doDelete;
105
+ /**
106
+ * Enable or disable debug output on errors.
107
+ *
108
+ * @param debugOnError true to enable debug output on errors, false to disable
109
+ */
110
+ setDebugOnError(debugOnError: boolean): void;
111
+ /**
112
+ * Method for handling a HTTP REST request with GET, POST, PUT, DELETE corresponding to
113
+ * SQL select, insert, update and delete.
114
+ *
115
+ * @param method HTTP method of the REST request
116
+ * @param rowId URL id of the REST request
117
+ * @param data HTTP body data as either serialized string or unserialized JS object or OINODataRow-array or Buffer/Uint8Array binary data
118
+ * @param sqlParams SQL parameters for the REST request
119
+ *
120
+ */
121
+ doRequest(method: string, rowId: string, data: string | OINODataRow[] | Buffer | Uint8Array | object | null, sqlParams: OINODbSqlParams): Promise<OINODbApiResult>;
122
+ /**
123
+ * Method for handling a HTTP REST request with GET, POST, PUT, DELETE corresponding to
124
+ * SQL select, insert, update and delete.
125
+ *
126
+ * @param request OINO DB API request
127
+ *
128
+ */
129
+ runRequest(request: OINODbApiRequest): Promise<OINODbApiResult>;
130
+ /**
131
+ * Method for handling a HTTP REST request with batch update using PUT or DELETE methods.
132
+ *
133
+ * @param method HTTP method of the REST request
134
+ * @param rowId URL id of the REST request
135
+ * @param data HTTP body data as either serialized string or unserialized JS object or OINODataRow-array or Buffer/Uint8Array binary data
136
+ *
137
+ */
138
+ doBatchUpdate(method: string, rowId: string, data: string | OINODataRow[] | Buffer | Uint8Array | object | null, sqlParams?: OINODbSqlParams): Promise<OINODbApiResult>;
139
+ /**
140
+ * Method for handling a HTTP REST request with batch update using PUT or DELETE methods.
141
+ *
142
+ * @param request HTTP URL parameters as key-value-pairs
143
+ *
144
+ */
145
+ runBatchUpdate(request: OINODbApiRequest): Promise<OINODbApiResult>;
146
+ /**
147
+ * Method to check if a field is included in the API params.
148
+ *
149
+ * @param fieldName name of the field
150
+ *
151
+ */
152
+ isFieldIncluded(fieldName: string): boolean;
153
+ }
@@ -0,0 +1,56 @@
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
+ /** Name of the OINODbSqlAggregate-parameter in request */
15
+ static OINODB_SQL_AGGREGATE_PARAM: string;
16
+ /** Name of the OINODbSqlSelect-parameter in request */
17
+ static OINODB_SQL_SELECT_PARAM: string;
18
+ /**
19
+ * Set the name of the OINO ID field
20
+ * @param idField name of the OINO ID field
21
+ */
22
+ static setOinoIdField(idField: string): void;
23
+ /**
24
+ * Set the separator character of the OINO ID field
25
+ * @param idSeparator character to use as separator of id parts
26
+ */
27
+ static setOinoIdSeparator(idSeparator: string): void;
28
+ /**
29
+ * Print OINO ID for primary key values.
30
+ *
31
+ * @param primaryKeys an array of primary key values.
32
+ *
33
+ */
34
+ static printOINOId(primaryKeys: string[]): string;
35
+ /**
36
+ * Set the name of the OINODbSqlFilter-param field
37
+ *
38
+ * @param sqlFilterParam name of the http parameter with `OINODbSqlFilter` definition
39
+ *
40
+ */
41
+ static setOinoSqlFilterParam(sqlFilterParam: string): void;
42
+ /**
43
+ * Set the name of the OINODbSqlOrder-param field
44
+ *
45
+ * @param sqlOrderParam name of the http parameter with `OINODbSqlOrder` definition
46
+ *
47
+ */
48
+ static setOinoSqlOrderParam(sqlOrderParam: string): void;
49
+ /**
50
+ * Set the name of the OINODbSqlLimit-param field
51
+ *
52
+ * @param sqlLimitParam name of the http parameter with `OINODbSqlLimit` definition
53
+ *
54
+ */
55
+ static setOinoSqlLimitParam(sqlLimitParam: string): void;
56
+ }