@oino-ts/db 0.0.17 → 0.1.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 (44) hide show
  1. package/README.md +4 -7
  2. package/dist/cjs/OINODb.js +132 -1
  3. package/dist/cjs/OINODbApi.js +15 -12
  4. package/dist/cjs/OINODbDataField.js +7 -1
  5. package/dist/cjs/OINODbDataModel.js +1 -1
  6. package/dist/cjs/OINODbFactory.js +0 -356
  7. package/dist/cjs/OINODbModelSet.js +1 -1
  8. package/dist/cjs/OINODbParser.js +466 -0
  9. package/dist/cjs/OINODbRequestParams.js +24 -18
  10. package/dist/cjs/OINODbSqlParams.js +336 -0
  11. package/dist/cjs/index.js +26 -25
  12. package/dist/esm/OINODb.js +129 -0
  13. package/dist/esm/OINODbApi.js +15 -12
  14. package/dist/esm/OINODbDataField.js +7 -1
  15. package/dist/esm/OINODbDataModel.js +2 -2
  16. package/dist/esm/OINODbFactory.js +1 -357
  17. package/dist/esm/OINODbModelSet.js +1 -1
  18. package/dist/esm/OINODbParser.js +462 -0
  19. package/dist/esm/OINODbRequestParams.js +24 -18
  20. package/dist/esm/OINODbSqlParams.js +330 -0
  21. package/dist/esm/index.js +5 -5
  22. package/package.json +4 -3
  23. package/src/OINODb.ts +163 -1
  24. package/src/OINODbApi.test.ts +182 -44
  25. package/src/OINODbApi.ts +16 -14
  26. package/src/OINODbDataField.ts +7 -2
  27. package/src/OINODbDataModel.ts +1 -1
  28. package/src/OINODbFactory.ts +1 -358
  29. package/src/OINODbModelSet.ts +1 -1
  30. package/src/OINODbParser.ts +458 -0
  31. package/src/{OINODbRequestParams.ts → OINODbSqlParams.ts} +24 -16
  32. package/src/index.ts +6 -6
  33. package/dist/types/OINODb.d.ts +0 -86
  34. package/dist/types/OINODbApi.d.ts +0 -82
  35. package/dist/types/OINODbConfig.d.ts +0 -52
  36. package/dist/types/OINODbDataField.d.ts +0 -202
  37. package/dist/types/OINODbDataModel.d.ts +0 -108
  38. package/dist/types/OINODbDataSet.d.ts +0 -95
  39. package/dist/types/OINODbFactory.d.ts +0 -63
  40. package/dist/types/OINODbModelSet.d.ts +0 -51
  41. package/dist/types/OINODbRequestParams.d.ts +0 -146
  42. package/dist/types/OINODbSwagger.d.ts +0 -25
  43. package/dist/types/index.d.ts +0 -111
  44. package/src/OINODbDataSet.ts +0 -167
@@ -1,146 +0,0 @@
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
- }
@@ -1,25 +0,0 @@
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
- }
@@ -1,111 +0,0 @@
1
- import { OINOContentType } from "@oino-ts/types";
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/types";
4
- import { OINODb } from "./OINODb.js";
5
- import { OINODbDataField } from "./OINODbDataField.js";
6
- import { OINODbSqlFilter, OINODbSqlLimit, OINODbSqlOrder } from "./OINODbRequestParams.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 { OINODb } from "./OINODb.js";
12
- export { OINODbDataSet, OINODbMemoryDataSet } from "./OINODbDataSet.js";
13
- export { OINODbSqlFilter, OINODbSqlOrder, OINODbSqlComparison, OINODbSqlLimit, OINODbSqlBooleanOperation } from "./OINODbRequestParams.js";
14
- export { OINODbConfig } from "./OINODbConfig.js";
15
- export { OINODbFactory } from "./OINODbFactory.js";
16
- export { OINODbSwagger } from "./OINODbSwagger.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>;
@@ -1,167 +0,0 @@
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 { OINODataRow, OINO_ERROR_PREFIX, OINODB_EMPTY_ROW } from "./index.js";
8
-
9
- /**
10
- * Base class for SQL results that can be asynchronously iterated (but
11
- * not necessarity rewinded). Idea is to handle database specific mechanisms
12
- * for returning and formatting conventions in the database specific
13
- * implementation. Data might be in memory or streamed in chunks and
14
- * `OINODbDataSet` will serve it out consistently.
15
- *
16
- */
17
- export abstract class OINODbDataSet {
18
- private _data: unknown;
19
-
20
- /** Error messages */
21
- readonly messages: string[];
22
-
23
- /**
24
- * Constructor for `OINODbDataSet`.
25
- *
26
- * @param data internal database specific data type (constructor will throw if invalid)
27
- * @param messages error messages from SQL-query
28
- *
29
- */
30
- constructor(data: unknown, messages: string[] = []) {
31
- this._data = data;
32
- this.messages = messages;
33
- }
34
-
35
- /**
36
- * Is data set empty.
37
- *
38
- */
39
- abstract isEmpty(): boolean;
40
-
41
- /**
42
- * Is there no more content, i.e. either dataset is empty or we have moved beyond last line
43
- *
44
- */
45
- abstract isEof(): boolean;
46
-
47
- /**
48
- * Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
49
- *
50
- */
51
- abstract next(): Promise<boolean>;
52
-
53
- /**
54
- * Gets current row of data.
55
- *
56
- */
57
- abstract getRow(): OINODataRow;
58
-
59
- /**
60
- * Checks if the messages contain errors.
61
- *
62
- */
63
- hasErrors(): boolean {
64
- for (let i=0; i<this.messages.length; i++) {
65
- if (this.messages[i].startsWith(OINO_ERROR_PREFIX)) {
66
- return true
67
- }
68
- }
69
- return false
70
- }
71
- /**
72
- * Checks if the messages contain errors.
73
- *
74
- */
75
- getFirstError(): string {
76
- for (let i=0; i<this.messages.length; i++) {
77
- if (this.messages[i].startsWith(OINO_ERROR_PREFIX)) {
78
- return this.messages[i]
79
- }
80
- }
81
- return ""
82
- }
83
- }
84
-
85
- /**
86
- * Generic in memory implementation of a data set where data is an array of rows. Used
87
- * by BunSqlite and automated testing. Can be rewinded.
88
- *
89
- */
90
- export class OINODbMemoryDataSet extends OINODbDataSet {
91
- private _rows: OINODataRow[];
92
- private _currentRow: number;
93
- private _eof: boolean;
94
-
95
- /**
96
- * Constructor of `OINODbMemoryDataSet`.
97
- *
98
- * @param data data as OINODataRow[] (constructor will throw if invalid)
99
- * @param errors error messages from SQL-query
100
- *
101
- */
102
- constructor(data: unknown, errors: string[] = []) {
103
- super(data, errors);
104
- if ((data == null) || !(Array.isArray(data))) {
105
- throw new Error(OINO_ERROR_PREFIX + ": Data needs to be compatible with OINORow[]!"); // TODO: maybe check all rows
106
- }
107
- this._rows = data as OINODataRow[];
108
- if (this.isEmpty()) {
109
- this._currentRow = -1;
110
- this._eof = true;
111
- } else {
112
- this._currentRow = 0;
113
- this._eof = false;
114
- }
115
- }
116
-
117
- /**
118
- * Is data set empty.
119
- *
120
- */
121
- isEmpty(): boolean {
122
- return (this._rows.length == 0);
123
- }
124
-
125
- /**
126
- * Is there no more content, i.e. either dataset is empty or we have moved beyond last line
127
- *
128
- */
129
- isEof(): boolean {
130
- return (this._eof);
131
- }
132
-
133
- /**
134
- * Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
135
- *
136
- */
137
- async next(): Promise<boolean> {
138
- if (this._currentRow < this._rows.length - 1) {
139
- this._currentRow = this._currentRow + 1;
140
- } else {
141
- this._eof = true;
142
- }
143
- return Promise.resolve(!this._eof);
144
- }
145
-
146
- /**
147
- * Gets current row of data.
148
- *
149
- */
150
- getRow(): OINODataRow {
151
- if ((this._currentRow >= 0) && (this._currentRow < this._rows.length)) {
152
- return this._rows[this._currentRow];
153
- } else {
154
- return OINODB_EMPTY_ROW;
155
- }
156
- }
157
-
158
- /**
159
- * Rewinds data set to the first row, returns !isEof().
160
- *
161
- */
162
- first(): boolean {
163
- this._currentRow = 0;
164
- this._eof = this._rows.length == 0;
165
- return !this._eof;
166
- }
167
- }