@oino-ts/db 0.21.2 → 1.0.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.
- package/dist/cjs/OINODb.js +6 -144
- package/dist/cjs/OINODbApi.js +50 -318
- package/dist/cjs/OINODbConfig.js +10 -10
- package/dist/cjs/OINODbConstants.js +10 -0
- package/dist/cjs/OINODbDataField.js +28 -70
- package/dist/cjs/OINODbDataModel.js +30 -144
- package/dist/cjs/OINODbFactory.js +2 -2
- package/dist/cjs/OINODbModelSet.js +23 -23
- package/dist/cjs/OINODbQueryParams.js +201 -0
- package/dist/cjs/index.js +12 -41
- package/dist/esm/OINODb.js +6 -142
- package/dist/esm/OINODbApi.js +49 -314
- package/dist/esm/OINODbConstants.js +7 -0
- package/dist/esm/OINODbDataModel.js +31 -145
- package/dist/esm/OINODbFactory.js +1 -1
- package/dist/esm/OINODbQueryParams.js +194 -0
- package/dist/esm/index.js +4 -14
- package/dist/types/OINODb.d.ts +6 -173
- package/dist/types/OINODbApi.d.ts +18 -104
- package/dist/types/OINODbConstants.d.ts +23 -0
- package/dist/types/OINODbDataModel.d.ts +7 -61
- package/dist/types/OINODbFactory.d.ts +5 -2
- package/dist/types/OINODbQueryParams.d.ts +72 -0
- package/dist/types/index.d.ts +4 -108
- package/package.json +37 -37
- package/src/OINODb.ts +99 -348
- package/src/OINODbApi.test.ts +507 -498
- package/src/OINODbApi.ts +389 -667
- package/src/OINODbConstants.ts +32 -0
- package/src/OINODbDataModel.ts +191 -307
- package/src/OINODbFactory.ts +73 -68
- package/src/OINODbQueryParams.ts +203 -0
- package/src/index.ts +6 -118
- package/src/OINODbConfig.ts +0 -98
- package/src/OINODbDataField.ts +0 -405
- package/src/OINODbModelSet.ts +0 -353
- package/src/OINODbParser.ts +0 -438
- package/src/OINODbSqlParams.ts +0 -593
- package/src/OINODbSwagger.ts +0 -209
package/dist/types/index.d.ts
CHANGED
|
@@ -1,110 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
/// <reference types="node" />
|
|
3
|
-
import { Buffer } from "node:buffer";
|
|
4
|
-
import { OINODb } from "./OINODb.js";
|
|
5
|
-
import { OINODbDataField } from "./OINODbDataField.js";
|
|
6
|
-
import { OINODbSqlAggregate, OINODbSqlFilter, OINODbSqlLimit, OINODbSqlOrder, OINODbSqlSelect } from "./OINODbSqlParams.js";
|
|
7
|
-
export { OINODbApiResult, OINODbHtmlTemplate, OINODbApi, OINODbApiRequest, type OINODbApiRequestInit, type OINODbApiData } from "./OINODbApi.js";
|
|
1
|
+
export { OINODb } from "./OINODb.js";
|
|
8
2
|
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, OINODbSqlAggregate, OINODbSqlAggregateFunctions, OINODbSqlSelect, OINODbSqlNullCheck } from "./OINODbSqlParams.js";
|
|
13
|
-
export { OINODbConfig } from "./OINODbConfig.js";
|
|
14
3
|
export { OINODbFactory } from "./OINODbFactory.js";
|
|
15
|
-
export {
|
|
16
|
-
export {
|
|
17
|
-
|
|
18
|
-
export type OINODbApiParams = {
|
|
19
|
-
/** Name of the api */
|
|
20
|
-
apiName: string;
|
|
21
|
-
/** Name of the database table */
|
|
22
|
-
tableName: string;
|
|
23
|
-
/** Reject values that exceed field max length (behaviour on such is platform dependent) */
|
|
24
|
-
failOnOversizedValues?: Boolean;
|
|
25
|
-
/** Reject PUT-requests that contain values for autoinc-type fields */
|
|
26
|
-
failOnUpdateOnAutoinc?: boolean;
|
|
27
|
-
/** Reject POST-requests without primary key value (can work if DB-side ) */
|
|
28
|
-
failOnInsertWithoutKey?: boolean;
|
|
29
|
-
/** Reject POST-requests without primary key value (can work if DB-side ) */
|
|
30
|
-
failOnAnyInvalidRows?: boolean;
|
|
31
|
-
/** Treat date type fields as just strings and use the native formatting instead of the ISO 8601 format */
|
|
32
|
-
useDatesAsString?: Boolean;
|
|
33
|
-
/** Include given fields from the API and exclude rest (if defined) */
|
|
34
|
-
includeFields?: string[];
|
|
35
|
-
/** Exclude all fields with this prefix from the API */
|
|
36
|
-
excludeFieldPrefix?: string;
|
|
37
|
-
/** Exclude given fields from the API and include rest (if defined) */
|
|
38
|
-
excludeFields?: string[];
|
|
39
|
-
/** Enable hashids for numeric primarykeys by adding a 32 char key */
|
|
40
|
-
hashidKey?: string;
|
|
41
|
-
/** Set (minimum) length (12-32 chars) of the hashids */
|
|
42
|
-
hashidLength?: number;
|
|
43
|
-
/** Make hashids static per row/table */
|
|
44
|
-
hashidStaticIds?: boolean;
|
|
45
|
-
/** Name of field that has the modified field */
|
|
46
|
-
cacheModifiedField?: string;
|
|
47
|
-
/** Return inserted id values */
|
|
48
|
-
returnInsertedIds?: boolean;
|
|
49
|
-
};
|
|
50
|
-
/**
|
|
51
|
-
* Database class (constructor) type
|
|
52
|
-
* @param dbParams database parameters
|
|
53
|
-
*/
|
|
54
|
-
export type OINODbConstructor = new (dbParams: OINODbParams) => OINODb;
|
|
55
|
-
/** Database parameters */
|
|
56
|
-
export type OINODbParams = {
|
|
57
|
-
/** Name of the database class (e.g. OINODbPostgresql) */
|
|
58
|
-
type: string;
|
|
59
|
-
/** Connection URL, either file://-path or an IP-address or an HTTP-url */
|
|
60
|
-
url: string;
|
|
61
|
-
/** Name of the database */
|
|
62
|
-
database: string;
|
|
63
|
-
/** TCP port of the database */
|
|
64
|
-
port?: number;
|
|
65
|
-
/** Username used to authenticate */
|
|
66
|
-
user?: string;
|
|
67
|
-
/** Password used to authenticate */
|
|
68
|
-
password?: string;
|
|
69
|
-
};
|
|
70
|
-
/** Field parameters in database */
|
|
71
|
-
export type OINODbDataFieldParams = {
|
|
72
|
-
/** Is the field a primary key */
|
|
73
|
-
isPrimaryKey: Boolean;
|
|
74
|
-
/** Is the field a primary key */
|
|
75
|
-
isForeignKey: Boolean;
|
|
76
|
-
/** Is the field an auto inc type */
|
|
77
|
-
isAutoInc: Boolean;
|
|
78
|
-
/** Is the field allowed to have null values */
|
|
79
|
-
isNotNull: Boolean;
|
|
80
|
-
};
|
|
81
|
-
/**
|
|
82
|
-
* Callback to filter data fields
|
|
83
|
-
* @param field fields to filter
|
|
84
|
-
*/
|
|
85
|
-
export type OINODbDataFieldFilter = (field: OINODbDataField) => Boolean;
|
|
86
|
-
/** Request options */
|
|
87
|
-
export type OINODbSqlParams = {
|
|
88
|
-
/** Additional SQL select where-conditions */
|
|
89
|
-
filter?: OINODbSqlFilter;
|
|
90
|
-
/** SQL result ordering conditions */
|
|
91
|
-
order?: OINODbSqlOrder;
|
|
92
|
-
/** SQL result limit condition */
|
|
93
|
-
limit?: OINODbSqlLimit;
|
|
94
|
-
/** SQL aggregation functions */
|
|
95
|
-
aggregate?: OINODbSqlAggregate;
|
|
96
|
-
/** SQL select condition */
|
|
97
|
-
select?: OINODbSqlSelect;
|
|
98
|
-
};
|
|
99
|
-
/** A single column value of a data row */
|
|
100
|
-
export type OINODataCell = string | bigint | number | boolean | Date | Uint8Array | Buffer | null | undefined;
|
|
101
|
-
/** A single data row */
|
|
102
|
-
export type OINODataRow = Array<OINODataCell>;
|
|
103
|
-
/** Empty row instance */
|
|
104
|
-
export declare const OINODB_EMPTY_ROW: OINODataRow;
|
|
105
|
-
/** Empty row array instance */
|
|
106
|
-
export declare const OINODB_EMPTY_ROWS: OINODataRow[];
|
|
107
|
-
/** Constant for undefined values */
|
|
108
|
-
export declare const OINODB_UNDEFINED = "";
|
|
109
|
-
/** Key-value collection */
|
|
110
|
-
export type OINOValues = Record<string, string>;
|
|
4
|
+
export { OINODbApi } from "./OINODbApi.js";
|
|
5
|
+
export { OINODbQueryFilter, OINODbQueryOrder, OINODbQueryLimit, OINODbQueryAggregate } from "./OINODbQueryParams.js";
|
|
6
|
+
export { type OINODbConstructor, type OINODbParams, OINODB_UNDEFINED } from "./OINODbConstants.js";
|
package/package.json
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@oino-ts/db",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "OINO TS library package for publishing an SQL database tables as a REST API.",
|
|
5
|
-
"author": "Matias Kiviniemi (pragmatta)",
|
|
6
|
-
"license": "MPL-2.0",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "https://github.com/pragmatta/oino-ts.git"
|
|
10
|
-
},
|
|
11
|
-
"keywords": [
|
|
12
|
-
"sql",
|
|
13
|
-
"database",
|
|
14
|
-
"rest-api",
|
|
15
|
-
"typescript",
|
|
16
|
-
"library"
|
|
17
|
-
],
|
|
18
|
-
"main": "./dist/cjs/index.js",
|
|
19
|
-
"module": "./dist/esm/index.js",
|
|
20
|
-
"types": "./dist/types/index.d.ts",
|
|
21
|
-
"dependencies": {
|
|
22
|
-
"@oino-ts/common": "0.
|
|
23
|
-
"oino-ts": "file:.."
|
|
24
|
-
},
|
|
25
|
-
"devDependencies": {
|
|
26
|
-
"@oino-ts/types": "0.
|
|
27
|
-
"@types/bun": "^1.1.14",
|
|
28
|
-
"@types/node": "^
|
|
29
|
-
"typescript": "~5.9.0"
|
|
30
|
-
},
|
|
31
|
-
"files": [
|
|
32
|
-
"src/*.ts",
|
|
33
|
-
"dist/cjs/*.js",
|
|
34
|
-
"dist/esm/*.js",
|
|
35
|
-
"dist/types/*.d.ts"
|
|
36
|
-
]
|
|
37
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@oino-ts/db",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "OINO TS library package for publishing an SQL database tables as a REST API.",
|
|
5
|
+
"author": "Matias Kiviniemi (pragmatta)",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/pragmatta/oino-ts.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"sql",
|
|
13
|
+
"database",
|
|
14
|
+
"rest-api",
|
|
15
|
+
"typescript",
|
|
16
|
+
"library"
|
|
17
|
+
],
|
|
18
|
+
"main": "./dist/cjs/index.js",
|
|
19
|
+
"module": "./dist/esm/index.js",
|
|
20
|
+
"types": "./dist/types/index.d.ts",
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@oino-ts/common": "1.0.1",
|
|
23
|
+
"oino-ts": "file:.."
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@oino-ts/types": "1.0.1",
|
|
27
|
+
"@types/bun": "^1.1.14",
|
|
28
|
+
"@types/node": "^21.0.10",
|
|
29
|
+
"typescript": "~5.9.0"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"src/*.ts",
|
|
33
|
+
"dist/cjs/*.js",
|
|
34
|
+
"dist/esm/*.js",
|
|
35
|
+
"dist/types/*.d.ts"
|
|
36
|
+
]
|
|
37
|
+
}
|
package/src/OINODb.ts
CHANGED
|
@@ -1,348 +1,99 @@
|
|
|
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 {
|
|
8
|
-
import { OINODbParams
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Base class for database abstraction, implementing methods for connecting, making queries and parsing/formatting data
|
|
12
|
-
* between SQL and serialization formats.
|
|
13
|
-
*
|
|
14
|
-
*/
|
|
15
|
-
export abstract class OINODb {
|
|
16
|
-
|
|
17
|
-
protected
|
|
18
|
-
|
|
19
|
-
/** Name of the database */
|
|
20
|
-
readonly name:string
|
|
21
|
-
|
|
22
|
-
isConnected:boolean = false
|
|
23
|
-
isValidated:boolean = false
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Constructor for `OINODb`.
|
|
27
|
-
* @param params database parameters
|
|
28
|
-
*/
|
|
29
|
-
constructor(params:OINODbParams) {
|
|
30
|
-
|
|
31
|
-
this.
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
*
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
*
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* @param
|
|
56
|
-
*
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
*
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
*
|
|
101
|
-
*/
|
|
102
|
-
abstract sqlSelect(sql:string): Promise<OINODbDataSet>
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Execute other sql operations.
|
|
106
|
-
*
|
|
107
|
-
* @param sql SQL statement.
|
|
108
|
-
*
|
|
109
|
-
*/
|
|
110
|
-
abstract sqlExec(sql:string): Promise<OINODbDataSet>
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Initialize a data model by getting the SQL schema and populating OINODbDataFields of
|
|
114
|
-
* the model.
|
|
115
|
-
*
|
|
116
|
-
* @param api api which data model to initialize.
|
|
117
|
-
*
|
|
118
|
-
*/
|
|
119
|
-
abstract initializeApiDatamodel(api:OINODbApi): Promise<void>
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Print SQL select statement with DB specific formatting.
|
|
123
|
-
*
|
|
124
|
-
* @param tableName - The name of the table to select from.
|
|
125
|
-
* @param columnNames - The columns to be selected.
|
|
126
|
-
* @param whereCondition - The WHERE clause to filter the results.
|
|
127
|
-
* @param orderCondition - The ORDER BY clause to sort the results.
|
|
128
|
-
* @param limitCondition - The LIMIT clause to limit the number of results.
|
|
129
|
-
* @param groupByCondition - The GROUP BY clause to group the results.
|
|
130
|
-
*
|
|
131
|
-
*/
|
|
132
|
-
printSqlSelect(tableName:string, columnNames:string, whereCondition:string, orderCondition:string, limitCondition:string, groupByCondition: string): string {
|
|
133
|
-
let result:string = "SELECT " + columnNames + " FROM " + tableName;
|
|
134
|
-
if (whereCondition != "") {
|
|
135
|
-
result += " WHERE " + whereCondition
|
|
136
|
-
}
|
|
137
|
-
if (groupByCondition != "") {
|
|
138
|
-
result += " GROUP BY " + groupByCondition
|
|
139
|
-
}
|
|
140
|
-
if (orderCondition != "") {
|
|
141
|
-
result += " ORDER BY " + orderCondition
|
|
142
|
-
}
|
|
143
|
-
if (limitCondition != "") {
|
|
144
|
-
result += " LIMIT " + limitCondition
|
|
145
|
-
}
|
|
146
|
-
result += ";"
|
|
147
|
-
return result;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Print SQL select statement with DB specific formatting.
|
|
152
|
-
*
|
|
153
|
-
* @param tableName - The name of the table to select from.
|
|
154
|
-
* @param columns - The columns to be selected.
|
|
155
|
-
* @param values - The values to be inserted.
|
|
156
|
-
* @param returnIdFields - the id fields to return if returnIds is true (if supported by the database)
|
|
157
|
-
*
|
|
158
|
-
*/
|
|
159
|
-
printSqlInsert(tableName:string, columns:string, values:string, returnIdFields?:string[]): string {
|
|
160
|
-
let result = "INSERT INTO " + tableName + " (" + columns + ") VALUES (" + values + ")"
|
|
161
|
-
if (returnIdFields) {
|
|
162
|
-
result += " RETURNING " + returnIdFields.join(",")
|
|
163
|
-
}
|
|
164
|
-
result += ";"
|
|
165
|
-
return result;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Base class for SQL results that can be asynchronously iterated (but
|
|
172
|
-
* not necessarity rewinded). Idea is to handle database specific mechanisms
|
|
173
|
-
* for returning and formatting conventions in the database specific
|
|
174
|
-
* implementation. Data might be in memory or streamed in chunks and
|
|
175
|
-
* `OINODbDataSet` will serve it out consistently.
|
|
176
|
-
*
|
|
177
|
-
*/
|
|
178
|
-
|
|
179
|
-
export abstract class OINODbDataSet extends OINOResult {
|
|
180
|
-
private _data: unknown;
|
|
181
|
-
|
|
182
|
-
/** Error messages */
|
|
183
|
-
readonly messages: string[];
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Constructor for `OINODbDataSet`.
|
|
187
|
-
*
|
|
188
|
-
* @param data internal database specific data type (constructor will throw if invalid)
|
|
189
|
-
* @param messages error messages from SQL-query
|
|
190
|
-
*
|
|
191
|
-
*/
|
|
192
|
-
constructor(data: unknown, messages: string[] = []) {
|
|
193
|
-
super();
|
|
194
|
-
this._data = data;
|
|
195
|
-
this.messages = messages;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
/**
|
|
199
|
-
* Is data set empty.
|
|
200
|
-
*
|
|
201
|
-
*/
|
|
202
|
-
abstract isEmpty(): boolean;
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* Is there no more content, i.e. either dataset is empty or we have moved beyond last line
|
|
206
|
-
*
|
|
207
|
-
*/
|
|
208
|
-
abstract isEof(): boolean;
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
|
|
212
|
-
*
|
|
213
|
-
*/
|
|
214
|
-
abstract next(): Promise<boolean>;
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* Gets current row of data.
|
|
218
|
-
*
|
|
219
|
-
*/
|
|
220
|
-
abstract getRow(): OINODataRow;
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* Gets all rows of data.
|
|
224
|
-
*
|
|
225
|
-
* NOTE: This is left abstract instead of just using `getRow()` so that DB implementations can hopefully optimize not duplicating data *
|
|
226
|
-
*/
|
|
227
|
-
abstract getAllRows(): Promise<OINODataRow[]>;
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* Checks if the messages contain errors.
|
|
231
|
-
*
|
|
232
|
-
*/
|
|
233
|
-
hasErrors(): boolean {
|
|
234
|
-
for (let i = 0; i < this.messages.length; i++) {
|
|
235
|
-
if (this.messages[i].startsWith(OINO_ERROR_PREFIX)) {
|
|
236
|
-
return true;
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
241
|
-
/**
|
|
242
|
-
* Checks if the messages contain errors.
|
|
243
|
-
*
|
|
244
|
-
*/
|
|
245
|
-
getFirstError(): string {
|
|
246
|
-
for (let i = 0; i < this.messages.length; i++) {
|
|
247
|
-
if (this.messages[i].startsWith(OINO_ERROR_PREFIX)) {
|
|
248
|
-
return this.messages[i];
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
return "";
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Generic in memory implementation of a data set where data is an array of rows. Used
|
|
257
|
-
* by BunSqlite and automated testing. Can be rewinded.
|
|
258
|
-
*
|
|
259
|
-
*/
|
|
260
|
-
|
|
261
|
-
export class OINODbMemoryDataSet extends OINODbDataSet {
|
|
262
|
-
private _rows: OINODataRow[];
|
|
263
|
-
private _currentRow: number;
|
|
264
|
-
private _eof: boolean;
|
|
265
|
-
|
|
266
|
-
/**
|
|
267
|
-
* Constructor of `OINODbMemoryDataSet`.
|
|
268
|
-
*
|
|
269
|
-
* @param data data as OINODataRow[] (constructor will throw if invalid)
|
|
270
|
-
* @param errors error messages from SQL-query
|
|
271
|
-
*
|
|
272
|
-
*/
|
|
273
|
-
constructor(data: unknown, errors: string[] = []) {
|
|
274
|
-
super(data, errors);
|
|
275
|
-
if ((data == null) || !(Array.isArray(data))) {
|
|
276
|
-
throw new Error(OINO_ERROR_PREFIX + ": Data needs to be compatible with OINORow[]!"); // TODO: maybe check all rows
|
|
277
|
-
}
|
|
278
|
-
this._rows = data as OINODataRow[];
|
|
279
|
-
if (this.isEmpty()) {
|
|
280
|
-
this._currentRow = -1;
|
|
281
|
-
this._eof = true;
|
|
282
|
-
} else {
|
|
283
|
-
this._currentRow = 0;
|
|
284
|
-
this._eof = false;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* Is data set empty.
|
|
290
|
-
*
|
|
291
|
-
*/
|
|
292
|
-
isEmpty(): boolean {
|
|
293
|
-
return (this._rows.length == 0);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Is there no more content, i.e. either dataset is empty or we have moved beyond last line
|
|
298
|
-
*
|
|
299
|
-
*/
|
|
300
|
-
isEof(): boolean {
|
|
301
|
-
return (this._eof);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
|
|
306
|
-
*
|
|
307
|
-
*/
|
|
308
|
-
async next(): Promise<boolean> {
|
|
309
|
-
if (this._currentRow < this._rows.length - 1) {
|
|
310
|
-
this._currentRow = this._currentRow + 1;
|
|
311
|
-
} else {
|
|
312
|
-
this._eof = true;
|
|
313
|
-
}
|
|
314
|
-
return Promise.resolve(!this._eof);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* Gets current row of data.
|
|
319
|
-
*
|
|
320
|
-
*/
|
|
321
|
-
getRow(): OINODataRow {
|
|
322
|
-
if ((this._currentRow >= 0) && (this._currentRow < this._rows.length)) {
|
|
323
|
-
return this._rows[this._currentRow];
|
|
324
|
-
} else {
|
|
325
|
-
return OINODB_EMPTY_ROW;
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
/**
|
|
330
|
-
* Gets all rows of data.
|
|
331
|
-
*
|
|
332
|
-
*/
|
|
333
|
-
async getAllRows(): Promise<OINODataRow[]> {
|
|
334
|
-
return this._rows // at the moment theres no result streaming, so we can just return the rows
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* Rewinds data set to the first row, returns !isEof().
|
|
339
|
-
*
|
|
340
|
-
*/
|
|
341
|
-
first(): boolean {
|
|
342
|
-
this._currentRow = 0;
|
|
343
|
-
this._eof = this._rows.length == 0;
|
|
344
|
-
return !this._eof;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
|
|
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 { OINODataSet, OINODataSource } from "@oino-ts/common"
|
|
8
|
+
import { OINODbParams } from "./OINODbConstants.js"
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Base class for database abstraction, implementing methods for connecting, making queries and parsing/formatting data
|
|
12
|
+
* between SQL and serialization formats.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
export abstract class OINODb extends OINODataSource {
|
|
16
|
+
|
|
17
|
+
protected readonly dbParams:OINODbParams
|
|
18
|
+
|
|
19
|
+
/** Name of the database */
|
|
20
|
+
readonly name:string
|
|
21
|
+
|
|
22
|
+
isConnected:boolean = false
|
|
23
|
+
isValidated:boolean = false
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Constructor for `OINODb`.
|
|
27
|
+
* @param params database parameters
|
|
28
|
+
*/
|
|
29
|
+
constructor(params:OINODbParams) {
|
|
30
|
+
super()
|
|
31
|
+
this.dbParams = { ...params } // make a shallow copy of params so that changes to them do not affect the original object
|
|
32
|
+
this.name = this.dbParams.database
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Execute a select operation.
|
|
37
|
+
*
|
|
38
|
+
* @param sql SQL statement.
|
|
39
|
+
*
|
|
40
|
+
*/
|
|
41
|
+
abstract sqlSelect(sql:string): Promise<OINODataSet>
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Execute other sql operations.
|
|
45
|
+
*
|
|
46
|
+
* @param sql SQL statement.
|
|
47
|
+
*
|
|
48
|
+
*/
|
|
49
|
+
abstract sqlExec(sql:string): Promise<OINODataSet>
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Print SQL select statement with DB specific formatting.
|
|
53
|
+
*
|
|
54
|
+
* @param tableName - The name of the table to select from.
|
|
55
|
+
* @param columnNames - The columns to be selected.
|
|
56
|
+
* @param whereCondition - The WHERE clause to filter the results.
|
|
57
|
+
* @param orderCondition - The ORDER BY clause to sort the results.
|
|
58
|
+
* @param limitCondition - The LIMIT clause to limit the number of results.
|
|
59
|
+
* @param groupByCondition - The GROUP BY clause to group the results.
|
|
60
|
+
*
|
|
61
|
+
*/
|
|
62
|
+
printSqlSelect(tableName:string, columnNames:string, whereCondition:string, orderCondition:string, limitCondition:string, groupByCondition: string): string {
|
|
63
|
+
let result:string = "SELECT " + columnNames + " FROM " + tableName;
|
|
64
|
+
if (whereCondition != "") {
|
|
65
|
+
result += " WHERE " + whereCondition
|
|
66
|
+
}
|
|
67
|
+
if (groupByCondition != "") {
|
|
68
|
+
result += " GROUP BY " + groupByCondition
|
|
69
|
+
}
|
|
70
|
+
if (orderCondition != "") {
|
|
71
|
+
result += " ORDER BY " + orderCondition
|
|
72
|
+
}
|
|
73
|
+
if (limitCondition != "") {
|
|
74
|
+
result += " LIMIT " + limitCondition
|
|
75
|
+
}
|
|
76
|
+
result += ";"
|
|
77
|
+
return result;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Print SQL select statement with DB specific formatting.
|
|
82
|
+
*
|
|
83
|
+
* @param tableName - The name of the table to select from.
|
|
84
|
+
* @param columns - The columns to be selected.
|
|
85
|
+
* @param values - The values to be inserted.
|
|
86
|
+
* @param returnIdFields - the id fields to return if returnIds is true (if supported by the database)
|
|
87
|
+
*
|
|
88
|
+
*/
|
|
89
|
+
printSqlInsert(tableName:string, columns:string, values:string, returnIdFields?:string[]): string {
|
|
90
|
+
let result = "INSERT INTO " + tableName + " (" + columns + ") VALUES (" + values + ")"
|
|
91
|
+
if (returnIdFields) {
|
|
92
|
+
result += " RETURNING " + returnIdFields.join(",")
|
|
93
|
+
}
|
|
94
|
+
result += ";"
|
|
95
|
+
return result;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
}
|
|
99
|
+
|