@oino-ts/db-bunsqlite 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +222 -0
- package/dist/cjs/OINODbBunSqlite.js +264 -0
- package/dist/cjs/index.js +5 -0
- package/dist/esm/OINODbBunSqlite.js +260 -0
- package/dist/esm/index.js +1 -0
- package/dist/types/OINODbBunSqlite.d.ts +76 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +35 -0
- package/src/OINODbBunSqlite.ts +275 -0
- package/src/index.ts +1 -0
package/README.md
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
# OINO TS
|
|
2
|
+
OINO Is Not an ORM but it's trying to solve a similar problem for API development. Instead of mirroring your DB schema in code that needs manual updates, OINO will get the data schema from DBMS using SQL in real time. Every time your app starts, it has an updated data model which enables automatic (de)serialize SQL results to JSON/CSV and back. OINO works on the level below routing where you pass the method, URL ID, body and request parameters to the API-object. OINO will parse and validate the data against the data model and generate proper SQL for your DB. Because OINO knows how data is serialized (e.g. JSON), what column it belongs to (e.g. floating point number) and what the target database is, it knows how to parse, format and escape the value as valid SQL.
|
|
3
|
+
|
|
4
|
+
```
|
|
5
|
+
const result:OINOApiResult = await api_orderdetails.doRequest("GET", id, body, params)
|
|
6
|
+
return new Response(result.modelset.writeString(OINOContentType.json))
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# GETTING STARTED
|
|
11
|
+
|
|
12
|
+
### Setup
|
|
13
|
+
Install the `@oino-ts/core` npm package and necessary database packages and import them in your code.
|
|
14
|
+
```
|
|
15
|
+
bun install @oino-ts/core
|
|
16
|
+
bun install @oino-ts/bunsqlite
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
import { OINODb, OINOApi, OINOFactory } from "@oino-ts/core";
|
|
21
|
+
import { OINODbBunSqlite } from "@oino-ts/bunsqlite"
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### Register database and logger
|
|
25
|
+
Register your database implementation and logger (see [`OINOConsoleLog`](https://pragmatta.github.io/oino-ts/classes/core_src.OINOConsoleLog.html) how to implement your own)
|
|
26
|
+
|
|
27
|
+
```
|
|
28
|
+
OINOLog.setLogger(new OINOConsoleLog())
|
|
29
|
+
OINOFactory.registerDb("OINODbBunSqlite", OINODbBunSqlite)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Create a database
|
|
33
|
+
Creating a database connection [`OINODb`](https://pragmatta.github.io/oino-ts/classes/core_src_OINODb.OINODb.html) is done by passing [`OINODbParams`](https://pragmatta.github.io/oino-ts/types/core_src.OINODbParams.html) to the factory method. For [`OINODbBunSqlite`](https://pragmatta.github.io/oino-ts/classes/bunsqlite_OINODbBunSqlite.OINODbBunSqlite.html) that means a file url for the database file, for others network host, port, credentials etc.
|
|
34
|
+
```
|
|
35
|
+
const db:OINODb = await OINOFactory.createDb( { type: "OINODbBunSqlite", url: "file://../localDb/northwind.sqlite" } )
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Create an API
|
|
39
|
+
From a database you can create an [`OINOApi`](https://pragmatta.github.io/oino-ts/classes/core_src_OINOApi.OINOApi.html) by passing [`OINOApiParams`](https://pragmatta.github.io/oino-ts/types/core_src.OINOApiParams.html) with table name and preferences to the factory method.
|
|
40
|
+
```
|
|
41
|
+
const api_employees:OINOApi = await OINOFactory.createApi(db, { tableName: "Employees", excludeFields:["BirthDate"] })
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Pass HTTP requests to API
|
|
45
|
+
When you receive a HTTP request, just pass the method, URL ID, body and params to the correct API, which will parse and validate input and return results.
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
const result:OINOApiResult = await api_orderdetails.doRequest("GET", id, body, params)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Write results back to HTTP Response
|
|
52
|
+
The results for a GET request will contain [`OINOModelSet`](https://pragmatta.github.io/oino-ts/classes/core_src_OINOModelSet.OINOModelSet.html) data that can be written out as JSON or CSV as needed. For other requests result is just success or error with messages.
|
|
53
|
+
```
|
|
54
|
+
return new Response(result.modelset.writeString(OINOContentType.json))
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# FEATURES
|
|
59
|
+
|
|
60
|
+
## RESTfull
|
|
61
|
+
OINO maps HTTP methods GET/POST/PUT/DELETE to SQL operations SELECT/INSERT/UPDATE/DELETE. The GET/POST requests can be made without URL ID to get all rows or insert new ones and others target a single row using URL ID.
|
|
62
|
+
|
|
63
|
+
### HTTP GET
|
|
64
|
+
```
|
|
65
|
+
Request and response:
|
|
66
|
+
> curl.exe -X GET http://localhost:3001/orderdetails/11077:77
|
|
67
|
+
[
|
|
68
|
+
{"_OINOID_":"11077:77","OrderID":11077,"ProductID":77,"UnitPrice":13,"Quantity":2,"Discount":0}
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
SQL:
|
|
72
|
+
SELECT "OrderID","ProductID","UnitPrice","Quantity","Discount" FROM [OrderDetails] WHERE ("OrderID"=11077 AND "ProductID"=77);
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### HTTP POST
|
|
76
|
+
```
|
|
77
|
+
Request and response:
|
|
78
|
+
> curl.exe -X POST http://localhost:3001/orderdetails -H "Content-Type: application/json" --data '[{\"OrderID\":11077,\"ProductID\":99,\"UnitPrice\":19,\"Quantity\":1,\"Discount\":0}]'
|
|
79
|
+
{"success":true,"statusCode":200,"statusMessage":"OK","messages":[]}
|
|
80
|
+
|
|
81
|
+
SQL:
|
|
82
|
+
INSERT INTO [OrderDetails] ("OrderID","ProductID","UnitPrice","Quantity","Discount") VALUES (11077,99,19,1,0);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### HTTP PUT
|
|
86
|
+
```
|
|
87
|
+
Request and response:
|
|
88
|
+
> curl.exe -X PUT http://localhost:3001/orderdetails/11077:99 -H "Content-Type: application/json" --data '[{\"UnitPrice\":20}]'
|
|
89
|
+
{"success":true,"statusCode":200,"statusMessage":"OK","messages":[]}
|
|
90
|
+
|
|
91
|
+
SQL:
|
|
92
|
+
UPDATE [OrderDetails] SET "UnitPrice"=20 WHERE ("OrderID"=11077 AND "ProductID"=99);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### HTTP DELETE
|
|
96
|
+
```
|
|
97
|
+
Request and response:
|
|
98
|
+
> curl.exe -X DELETE http://localhost:3001/orderdetails/11077:99
|
|
99
|
+
{"success":true,"statusCode":200,"statusMessage":"OK","messages":[]}
|
|
100
|
+
|
|
101
|
+
SQL:
|
|
102
|
+
DELETE FROM [OrderDetails] WHERE ("OrderID"=11077 AND "ProductID"=99);
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## Universal Serialization
|
|
106
|
+
OINO handles serialization of data to JSON/CSV/etc. and back based on the data model. It knows what columns exist, what is their data type and how to convert each to JSON/CSV and back. This allows also partial data to be sent, i.e. you can send only columns that need updating or even send extra columns and have them ignored.
|
|
107
|
+
|
|
108
|
+
### Features
|
|
109
|
+
- Files can be sent to BLOB fields using BASE64 encoding.
|
|
110
|
+
- Datetimes are (optionally) normalized to ISO 8601 format.
|
|
111
|
+
- Extended JSON-encoding
|
|
112
|
+
- Unquoted literal `undefined` can be used to represent non-existent values (leaving property out works too but preserving structure might be easier e.g. when translating data).
|
|
113
|
+
- CSV
|
|
114
|
+
- Comma-separated, doublequotes.
|
|
115
|
+
- Unquoted literal `null` represents null values.
|
|
116
|
+
- Unquoted empty string represents undefined values.
|
|
117
|
+
- Form data
|
|
118
|
+
- Multipart-mixed and binary files not supported.
|
|
119
|
+
- Non-existent value line (i.e. nothing after the empty line) treated as a null value.
|
|
120
|
+
- Url-encoded
|
|
121
|
+
- No null values, missing properties treated as undefined.
|
|
122
|
+
- Multiple lines could be used to post multiple rows.
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
## Database Abstraction
|
|
126
|
+
OINO functions as a database abstraction, providing a consistent interface for working with different databases. It abstracts out different conventions in connecting, making queries and formatting data.
|
|
127
|
+
|
|
128
|
+
Currently supported databases:
|
|
129
|
+
- Bun Sqlite through Bun native implementation
|
|
130
|
+
- Postgresql through [pg](https://www.npmjs.com/package/pg)-package
|
|
131
|
+
- Mariadb / Mysql-support through [mariadb](https://www.npmjs.com/package/mariadb)-package
|
|
132
|
+
|
|
133
|
+
## Complex Keys
|
|
134
|
+
To support tables with multipart primary keys OINO generates a composite key `_OINOID_` that is included in the result and can be used as the REST ID. For example in the example above table `OrderDetails` has two primary keys `OrderID` and `ProductID` making the `_OINOID_` of form `11077:99`.
|
|
135
|
+
|
|
136
|
+
## Power Of SQL
|
|
137
|
+
Since OINO controls the SQL, WHERE-conditions can be defined with [`OINOSqlFilter`](https://pragmatta.github.io/oino-ts/classes/core_src.OINOSqlFilter.html) and order with [`OINOSqlOrder`](https://pragmatta.github.io/oino-ts/classes/core_src.OINOSqlOrder.html) that are passed as HTTP request parameters. No more API development where you make unique API endpoints for each filter that fetch all data with original API and filter in backend code. Every API can be filtered when and as needed without unnessecary data tranfer and utilizing SQL indexing when available.
|
|
138
|
+
|
|
139
|
+
## Swagger Support
|
|
140
|
+
Swagger is great as long as the definitions are updated and with OINO you can automatically get a Swagger definition including a data model schema.
|
|
141
|
+
```
|
|
142
|
+
if (url.pathname == "/swagger.json") {
|
|
143
|
+
return new Response(JSON.stringify(OINOSwagger.getApiDefinition(api_array)))
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+

|
|
147
|
+
|
|
148
|
+
## Node support
|
|
149
|
+
OINO is developped Typescript first but compiles to standard CommonJS and the NPM packages should work on either ESM / CommonJS. Checkout sample apps `readmeApp` (ESM) and `nodeApp` (CommonJS).
|
|
150
|
+
|
|
151
|
+
## HTMX support
|
|
152
|
+
OINO is [htmx.org](https://htmx.org) friendly, allowing easy translation of [`OINODataRow`](https://pragmatta.github.io/oino-ts/types/core_src.OINODataRow.html) to HTML output using templates (cf. the [htmx sample app](https://github.com/pragmatta/oino-ts/tree/main/samples/htmxApp)).
|
|
153
|
+
|
|
154
|
+
### Hashids
|
|
155
|
+
Autoinc numeric id's are very pragmatic and fit well with OINO (e.g. using a form without primary key fields to insert new rows with database assigned ids). However it's not always sensible to share information about the sequence. Hashids solve this by masking the original values by encrypting the ids using AES-128 and some randomness. Length of the hashid can be chosen from 12-32 characters where longer ids provide more security. However this should not be considereded a cryptographic solution for keeping ids secret but rather making it infeasible to iterate all ids.
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
# STATUS
|
|
159
|
+
OINO is currently a hobby project which should and should considered in alpha status. That also means compatibility breaking changes can be made without prior notice when architectual issues are discovered.
|
|
160
|
+
|
|
161
|
+
## Beta
|
|
162
|
+
For a beta status following milestones are planned:
|
|
163
|
+
|
|
164
|
+
### Realistic app
|
|
165
|
+
There needs to be a realistic app built on top of OINO to get a better grasp of the edge cases.
|
|
166
|
+
|
|
167
|
+
### Security review
|
|
168
|
+
Handling of SQL-injection attacks needs a thorough review, what are the relevant attack vectors are for OINO and what protections are still needed.
|
|
169
|
+
|
|
170
|
+
## Roadmap
|
|
171
|
+
Things that need to happen in some order before beta-status are at least following:
|
|
172
|
+
|
|
173
|
+
### Support for views
|
|
174
|
+
Simple cases of views would work already in some databases but edge cases might get complicated. For example
|
|
175
|
+
- How to handle a view which does not have a complete private key?
|
|
176
|
+
- What edge cases exist in updating views?
|
|
177
|
+
|
|
178
|
+
### Batch updates
|
|
179
|
+
Supporting batch updates similar to batch inserts is slightly bending the RESTfull principles but would still be a useful optional feature.
|
|
180
|
+
|
|
181
|
+
### Aggregation and limits
|
|
182
|
+
Similar to filtering and ordering, aggregation and limits can be implemented as HTTP request parameters telling what column is aggregated or used for ordering or how many results to return.
|
|
183
|
+
|
|
184
|
+
### Streaming
|
|
185
|
+
One core idea is to be efficient in not making unnecessary copies of the data and minimizing garbage collection debt. This can be taken further by implementing streaming, allowing large dataset to be written to HTTP response as SQL result rows are received.
|
|
186
|
+
|
|
187
|
+
### SQL generation callbacks
|
|
188
|
+
It would be useful to allow developer to validate / override SQL generation to cover cases OINO does not support or even workaround issues.
|
|
189
|
+
|
|
190
|
+
### Transactions
|
|
191
|
+
Even though the basic case for OINO is executing SQL operations on individual rows, having an option to use SQL transactions could make sense at least for batch operations.
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
# HELP
|
|
195
|
+
|
|
196
|
+
## Bug reports
|
|
197
|
+
Fixing bugs is a priority and getting good quality bug reports helps. It's recommended to use the sample Northwind database included with project to replicate issues or make an SQL script export of the relevant table.
|
|
198
|
+
|
|
199
|
+
## Feedback
|
|
200
|
+
Understanding and prioritizing the use cases for OINO is also important and feedback about how you'd use OINO is interesting. Feel free to raise issues and feature requests in Github, but understand that short term most of the effort goes towards reaching the beta stage.
|
|
201
|
+
|
|
202
|
+
## Typescript / Javascript architecture
|
|
203
|
+
Typescript building with different targets and module-systemts and a ton of configuration is a complex domain and something I have little experience un so help in fixing problems and how thing ought to be done is appreciated.
|
|
204
|
+
|
|
205
|
+
# LINKS
|
|
206
|
+
- [Github repository](https://github.com/pragmatta/oino-ts)
|
|
207
|
+
- [NPM repository](https://www.npmjs.com/org/oino-ts)
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
# ACKNOWLEDGEMENTS
|
|
211
|
+
|
|
212
|
+
## Libraries
|
|
213
|
+
OINO uses the following open source libraries and npm packages and I would like to thank everyone for their contributions:
|
|
214
|
+
- Postgresql support by [node-postgres package](https://www.npmjs.com/package/pg)
|
|
215
|
+
- Mariadb / Mysql-support by [mariadb package](https://www.npmjs.com/package/mariadb)
|
|
216
|
+
|
|
217
|
+
## Bun
|
|
218
|
+
OINO has been developed using the Bun runtime, not because of the speed improvements but for the first class Typescript support and integrated developper experience. Kudos on the bun team for making Typescript work more exiting again.
|
|
219
|
+
|
|
220
|
+
## SQL Scripts
|
|
221
|
+
The SQL scripts for creating the sample Northwind database are based on [Google Code archive](https://code.google.com/archive/p/northwindextended/downloads) and have been further customized to ensure they would have identical data (in the scope of the automated testing).
|
|
222
|
+
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
4
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
5
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.OINODbBunSqlite = void 0;
|
|
9
|
+
const db_1 = require("@oino-ts/db");
|
|
10
|
+
const bun_sqlite_1 = require("bun:sqlite");
|
|
11
|
+
/**
|
|
12
|
+
* Implmentation of OINODbDataSet for BunSqlite.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
class OINOBunSqliteDataset extends db_1.OINODbMemoryDataSet {
|
|
16
|
+
constructor(data, messages = []) {
|
|
17
|
+
super(data, messages);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Implementation of BunSqlite-database.
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
class OINODbBunSqlite extends db_1.OINODb {
|
|
25
|
+
static _tableDescriptionRegex = /^CREATE TABLE\s*[\"\[]?\w+[\"\]]?\s*\(\s*(.*)\s*\)\s*(WITHOUT ROWID)?$/msi;
|
|
26
|
+
static _tablePrimarykeyRegex = /PRIMARY KEY \(([^\)]+)\)/i;
|
|
27
|
+
static _tableFieldTypeRegex = /[\"\[\s]?(\w+)[\"\]\s]\s?(INTEGER|REAL|DOUBLE|NUMERIC|DECIMAL|TEXT|BLOB|VARCHAR|DATETIME|DATE|BOOLEAN)(\s?\((\d+)\s?\,?\s?(\d*)?\))?/i;
|
|
28
|
+
_db;
|
|
29
|
+
/**
|
|
30
|
+
* OINODbBunSqlite constructor
|
|
31
|
+
* @param params database parameters
|
|
32
|
+
*/
|
|
33
|
+
constructor(params) {
|
|
34
|
+
super(params);
|
|
35
|
+
this._db = null;
|
|
36
|
+
if (!this._params.url.startsWith("file://")) {
|
|
37
|
+
throw new Error(db_1.OINO_ERROR_PREFIX + ": OINODbBunSqlite url must be a file://-url!");
|
|
38
|
+
}
|
|
39
|
+
db_1.OINOLog.debug("OINODbBunSqlite.constructor", { params: params });
|
|
40
|
+
if (this._params.type !== "OINODbBunSqlite") {
|
|
41
|
+
throw new Error(db_1.OINO_ERROR_PREFIX + ": Not OINODbBunSqlite-type: " + this._params.type);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
_parseDbFieldParams(fieldStr) {
|
|
45
|
+
const result = {
|
|
46
|
+
isPrimaryKey: fieldStr.indexOf("PRIMARY KEY") >= 0,
|
|
47
|
+
isAutoInc: fieldStr.indexOf("AUTOINCREMENT") >= 0,
|
|
48
|
+
isNotNull: fieldStr.indexOf("NOT NULL") >= 0
|
|
49
|
+
};
|
|
50
|
+
// OINOLog.debug("OINODbBunSqlite._parseDbFieldParams", {fieldStr:fieldStr, result:result})
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Print a table name using database specific SQL escaping.
|
|
55
|
+
*
|
|
56
|
+
* @param sqlTable name of the table
|
|
57
|
+
*
|
|
58
|
+
*/
|
|
59
|
+
printSqlTablename(sqlTable) {
|
|
60
|
+
return "[" + sqlTable + "]";
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Print a column name with correct SQL escaping.
|
|
64
|
+
*
|
|
65
|
+
* @param sqlColumn name of the column
|
|
66
|
+
*
|
|
67
|
+
*/
|
|
68
|
+
printSqlColumnname(sqlColumn) {
|
|
69
|
+
return "\"" + sqlColumn + "\"";
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Print a single data value from serialization using the context of the native data
|
|
73
|
+
* type with the correct SQL escaping.
|
|
74
|
+
*
|
|
75
|
+
* @param cellValue data from sql results
|
|
76
|
+
* @param sqlType native type name for table column
|
|
77
|
+
*
|
|
78
|
+
*/
|
|
79
|
+
printCellAsSqlValue(cellValue, sqlType) {
|
|
80
|
+
// OINOLog.debug("OINODbBunSqlite.printCellAsSqlValue", {cellValue:cellValue, sqlType:sqlType, type:typeof(cellValue)})
|
|
81
|
+
if (cellValue === null) {
|
|
82
|
+
return "NULL";
|
|
83
|
+
}
|
|
84
|
+
else if (cellValue === undefined) {
|
|
85
|
+
return "UNDEFINED";
|
|
86
|
+
}
|
|
87
|
+
else if ((sqlType == "INTEGER") || (sqlType == "REAL") || (sqlType == "DOUBLE" || (sqlType == "NUMERIC") || (sqlType == "DECIMAL"))) {
|
|
88
|
+
return cellValue.toString();
|
|
89
|
+
}
|
|
90
|
+
else if (sqlType == "BLOB") {
|
|
91
|
+
return "X\'" + Buffer.from(cellValue).toString('hex') + "\'";
|
|
92
|
+
}
|
|
93
|
+
else if (((sqlType == "DATETIME") || (sqlType == "DATE")) && (cellValue instanceof Date)) {
|
|
94
|
+
return "\'" + cellValue.toISOString() + "\'";
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
return "\"" + cellValue.toString().replaceAll("\"", "\"\"") + "\"";
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Parse a single SQL result value for serialization using the context of the native data
|
|
102
|
+
* type.
|
|
103
|
+
*
|
|
104
|
+
* @param sqlValue data from serialization
|
|
105
|
+
* @param sqlType native type name for table column
|
|
106
|
+
*
|
|
107
|
+
*/
|
|
108
|
+
parseSqlValueAsCell(sqlValue, sqlType) {
|
|
109
|
+
if ((sqlValue === null) || (sqlValue == "NULL")) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
else if (sqlValue === undefined) {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
else if (((sqlType == "DATETIME") || (sqlType == "DATE")) && (typeof (sqlValue) == "string")) {
|
|
116
|
+
return new Date(sqlValue);
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
return sqlValue;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Connect to database.
|
|
124
|
+
*
|
|
125
|
+
*/
|
|
126
|
+
connect() {
|
|
127
|
+
const filepath = this._params.url.substring(7);
|
|
128
|
+
try {
|
|
129
|
+
db_1.OINOLog.debug("OINODbBunSqlite.connect", { params: this._params });
|
|
130
|
+
this._db = bun_sqlite_1.Database.open(filepath, { create: true, readonly: false, readwrite: true });
|
|
131
|
+
// OINOLog.debug("OINODbBunSqlite.connect done")
|
|
132
|
+
return Promise.resolve(true);
|
|
133
|
+
}
|
|
134
|
+
catch (err) {
|
|
135
|
+
throw new Error(db_1.OINO_ERROR_PREFIX + ": Error connecting to Sqlite database (" + filepath + "): " + err);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Execute a select operation.
|
|
140
|
+
*
|
|
141
|
+
* @param sql SQL statement.
|
|
142
|
+
*
|
|
143
|
+
*/
|
|
144
|
+
async sqlSelect(sql) {
|
|
145
|
+
db_1.OINOBenchmark.start("sqlSelect");
|
|
146
|
+
let result;
|
|
147
|
+
try {
|
|
148
|
+
result = new OINOBunSqliteDataset(this._db?.query(sql).values(), []);
|
|
149
|
+
// OINOLog.debug("OINODbBunSqlite.sqlSelect", {result:result})
|
|
150
|
+
}
|
|
151
|
+
catch (e) {
|
|
152
|
+
result = new OINOBunSqliteDataset([[]], ["OINODbBunSqlite.sqlSelect exception in _db.query: " + e.message]);
|
|
153
|
+
}
|
|
154
|
+
db_1.OINOBenchmark.end("sqlSelect");
|
|
155
|
+
return Promise.resolve(result);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Execute other sql operations.
|
|
159
|
+
*
|
|
160
|
+
* @param sql SQL statement.
|
|
161
|
+
*
|
|
162
|
+
*/
|
|
163
|
+
async sqlExec(sql) {
|
|
164
|
+
db_1.OINOBenchmark.start("sqlExec");
|
|
165
|
+
let result;
|
|
166
|
+
try {
|
|
167
|
+
this._db?.exec(sql);
|
|
168
|
+
result = new OINOBunSqliteDataset([[]], []);
|
|
169
|
+
}
|
|
170
|
+
catch (e) {
|
|
171
|
+
result = new OINOBunSqliteDataset([[]], [db_1.OINO_ERROR_PREFIX + "(sqlExec): exception in _db.exec [" + e.message + "]"]);
|
|
172
|
+
}
|
|
173
|
+
db_1.OINOBenchmark.end("sqlExec");
|
|
174
|
+
return Promise.resolve(result);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Initialize a data model by getting the SQL schema and populating OINODbDataFields of
|
|
178
|
+
* the model.
|
|
179
|
+
*
|
|
180
|
+
* @param api api which data model to initialize.
|
|
181
|
+
*
|
|
182
|
+
*/
|
|
183
|
+
async initializeApiDatamodel(api) {
|
|
184
|
+
const res = await this.sqlSelect("select sql from sqlite_schema WHERE name='" + api.params.tableName + "'");
|
|
185
|
+
const sql_desc = (res?.getRow()[0]);
|
|
186
|
+
// OINOLog.debug("OINODbBunSqlite.initDatamodel.sql_desc=" + sql_desc)
|
|
187
|
+
let table_matches = OINODbBunSqlite._tableDescriptionRegex.exec(sql_desc);
|
|
188
|
+
// OINOLog.debug("OINODbBunSqlite.initDatamodel", {table_matches:table_matches})
|
|
189
|
+
if (!table_matches || table_matches?.length < 2) {
|
|
190
|
+
throw new Error("Table " + api.params.tableName + " not recognized as a valid Sqlite table!");
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
// OINOBenchmark.start("OINODbBunSqlite.initDatamodel")
|
|
194
|
+
let field_strings = db_1.OINOStr.splitExcludingBrackets(table_matches[1], ',', '(', ')');
|
|
195
|
+
// OINOLog.debug("OINODbBunSqlite.initDatamodel", {table_match:table_matches[1], field_strings:field_strings})
|
|
196
|
+
for (let field_str of field_strings) {
|
|
197
|
+
field_str = field_str.trim();
|
|
198
|
+
let field_params = this._parseDbFieldParams(field_str);
|
|
199
|
+
let field_match = OINODbBunSqlite._tableFieldTypeRegex.exec(field_str);
|
|
200
|
+
// OINOLog.debug("initDatamodel next field", {field_str:field_str, field_match:field_match, field_params:field_params})
|
|
201
|
+
if ((!field_match) || (field_match.length < 3)) {
|
|
202
|
+
let primarykey_match = OINODbBunSqlite._tablePrimarykeyRegex.exec(field_str);
|
|
203
|
+
// OINOLog.debug("initDatamodel non-field definition", {primarykey_match:primarykey_match})
|
|
204
|
+
if (primarykey_match && primarykey_match.length >= 2) {
|
|
205
|
+
const primary_keys = primarykey_match[1].replaceAll("\"", "").split(','); // not sure if will have space or not so split by comma and trim later
|
|
206
|
+
for (let i = 0; i < primary_keys.length; i++) {
|
|
207
|
+
const pk = primary_keys[i].trim(); //..the trim
|
|
208
|
+
for (let j = 0; j < api.datamodel.fields.length; j++) {
|
|
209
|
+
if (api.datamodel.fields[j].name == pk) {
|
|
210
|
+
api.datamodel.fields[j].fieldParams.isPrimaryKey = true;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
db_1.OINOLog.info("OINODbBunSqlite.initializeApiDatamodel: Unsupported field definition skipped.", { field: field_str });
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
else {
|
|
220
|
+
// field_str = "NAME TYPE (M, N)" -> 1:NAME, 2:TYPE, 4:M, 5:N
|
|
221
|
+
// OINOLog.debug("OINODbBunSqlite.initializeApiDatamodel: field regex matches", { field_match: field_match })
|
|
222
|
+
const field_name = field_match[1];
|
|
223
|
+
const sql_type = field_match[2];
|
|
224
|
+
const field_length = parseInt(field_match[4]) || 0;
|
|
225
|
+
// OINOLog.debug("OINODbBunSqlite.initializeApiDatamodel: field regex matches", { api.params: api.params, field_name:field_name })
|
|
226
|
+
if (((api.params.excludeFieldPrefix) && field_name.startsWith(api.params.excludeFieldPrefix)) || ((api.params.excludeFields) && (api.params.excludeFields.indexOf(field_name) < 0))) {
|
|
227
|
+
db_1.OINOLog.info("OINODbBunSqlite.initializeApiDatamodel: field excluded in API parameters.", { field: field_name });
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
if ((sql_type == "INTEGER") || (sql_type == "REAL") || (sql_type == "DOUBLE") || (sql_type == "NUMERIC") || (sql_type == "DECIMAL")) {
|
|
231
|
+
api.datamodel.addField(new db_1.OINONumberDataField(this, field_name, sql_type, field_params));
|
|
232
|
+
}
|
|
233
|
+
else if ((sql_type == "BLOB")) {
|
|
234
|
+
api.datamodel.addField(new db_1.OINOBlobDataField(this, field_name, sql_type, field_params, field_length));
|
|
235
|
+
}
|
|
236
|
+
else if ((sql_type == "TEXT")) {
|
|
237
|
+
api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, field_length));
|
|
238
|
+
}
|
|
239
|
+
else if ((sql_type == "DATETIME") || (sql_type == "DATE")) {
|
|
240
|
+
if (api.params.useDatesAsString) {
|
|
241
|
+
api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, 0));
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
api.datamodel.addField(new db_1.OINODatetimeDataField(this, field_name, sql_type, field_params));
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
else if ((sql_type == "BOOLEAN")) {
|
|
248
|
+
api.datamodel.addField(new db_1.OINOBooleanDataField(this, field_name, sql_type, field_params));
|
|
249
|
+
}
|
|
250
|
+
else {
|
|
251
|
+
db_1.OINOLog.info("OINODbBunSqlite.initializeApiDatamodel: unrecognized field type treated as string", { field_name: field_name, sql_type: sql_type, field_length: field_length, field_params: field_params });
|
|
252
|
+
api.datamodel.addField(new db_1.OINOStringDataField(this, field_name, sql_type, field_params, 0));
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
;
|
|
258
|
+
// OINOBenchmark.end("OINODbBunSqlite.initializeApiDatamodel")
|
|
259
|
+
db_1.OINOLog.debug("OINODbBunSqlite.initializeDatasetModel:\n" + api.datamodel.printDebug("\n"));
|
|
260
|
+
return Promise.resolve();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
exports.OINODbBunSqlite = OINODbBunSqlite;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OINODbBunSqlite = void 0;
|
|
4
|
+
var OINODbBunSqlite_js_1 = require("./OINODbBunSqlite.js");
|
|
5
|
+
Object.defineProperty(exports, "OINODbBunSqlite", { enumerable: true, get: function () { return OINODbBunSqlite_js_1.OINODbBunSqlite; } });
|
|
@@ -0,0 +1,260 @@
|
|
|
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
|
+
import { OINODb, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINO_ERROR_PREFIX, OINODbMemoryDataSet, OINOBenchmark, OINOBlobDataField, OINODatetimeDataField, OINOStr, OINOLog } from "@oino-ts/db";
|
|
7
|
+
import { Database as BunSqliteDb } from "bun:sqlite";
|
|
8
|
+
/**
|
|
9
|
+
* Implmentation of OINODbDataSet for BunSqlite.
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
class OINOBunSqliteDataset extends OINODbMemoryDataSet {
|
|
13
|
+
constructor(data, messages = []) {
|
|
14
|
+
super(data, messages);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Implementation of BunSqlite-database.
|
|
19
|
+
*
|
|
20
|
+
*/
|
|
21
|
+
export class OINODbBunSqlite extends OINODb {
|
|
22
|
+
static _tableDescriptionRegex = /^CREATE TABLE\s*[\"\[]?\w+[\"\]]?\s*\(\s*(.*)\s*\)\s*(WITHOUT ROWID)?$/msi;
|
|
23
|
+
static _tablePrimarykeyRegex = /PRIMARY KEY \(([^\)]+)\)/i;
|
|
24
|
+
static _tableFieldTypeRegex = /[\"\[\s]?(\w+)[\"\]\s]\s?(INTEGER|REAL|DOUBLE|NUMERIC|DECIMAL|TEXT|BLOB|VARCHAR|DATETIME|DATE|BOOLEAN)(\s?\((\d+)\s?\,?\s?(\d*)?\))?/i;
|
|
25
|
+
_db;
|
|
26
|
+
/**
|
|
27
|
+
* OINODbBunSqlite constructor
|
|
28
|
+
* @param params database parameters
|
|
29
|
+
*/
|
|
30
|
+
constructor(params) {
|
|
31
|
+
super(params);
|
|
32
|
+
this._db = null;
|
|
33
|
+
if (!this._params.url.startsWith("file://")) {
|
|
34
|
+
throw new Error(OINO_ERROR_PREFIX + ": OINODbBunSqlite url must be a file://-url!");
|
|
35
|
+
}
|
|
36
|
+
OINOLog.debug("OINODbBunSqlite.constructor", { params: params });
|
|
37
|
+
if (this._params.type !== "OINODbBunSqlite") {
|
|
38
|
+
throw new Error(OINO_ERROR_PREFIX + ": Not OINODbBunSqlite-type: " + this._params.type);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
_parseDbFieldParams(fieldStr) {
|
|
42
|
+
const result = {
|
|
43
|
+
isPrimaryKey: fieldStr.indexOf("PRIMARY KEY") >= 0,
|
|
44
|
+
isAutoInc: fieldStr.indexOf("AUTOINCREMENT") >= 0,
|
|
45
|
+
isNotNull: fieldStr.indexOf("NOT NULL") >= 0
|
|
46
|
+
};
|
|
47
|
+
// OINOLog.debug("OINODbBunSqlite._parseDbFieldParams", {fieldStr:fieldStr, result:result})
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Print a table name using database specific SQL escaping.
|
|
52
|
+
*
|
|
53
|
+
* @param sqlTable name of the table
|
|
54
|
+
*
|
|
55
|
+
*/
|
|
56
|
+
printSqlTablename(sqlTable) {
|
|
57
|
+
return "[" + sqlTable + "]";
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Print a column name with correct SQL escaping.
|
|
61
|
+
*
|
|
62
|
+
* @param sqlColumn name of the column
|
|
63
|
+
*
|
|
64
|
+
*/
|
|
65
|
+
printSqlColumnname(sqlColumn) {
|
|
66
|
+
return "\"" + sqlColumn + "\"";
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Print a single data value from serialization using the context of the native data
|
|
70
|
+
* type with the correct SQL escaping.
|
|
71
|
+
*
|
|
72
|
+
* @param cellValue data from sql results
|
|
73
|
+
* @param sqlType native type name for table column
|
|
74
|
+
*
|
|
75
|
+
*/
|
|
76
|
+
printCellAsSqlValue(cellValue, sqlType) {
|
|
77
|
+
// OINOLog.debug("OINODbBunSqlite.printCellAsSqlValue", {cellValue:cellValue, sqlType:sqlType, type:typeof(cellValue)})
|
|
78
|
+
if (cellValue === null) {
|
|
79
|
+
return "NULL";
|
|
80
|
+
}
|
|
81
|
+
else if (cellValue === undefined) {
|
|
82
|
+
return "UNDEFINED";
|
|
83
|
+
}
|
|
84
|
+
else if ((sqlType == "INTEGER") || (sqlType == "REAL") || (sqlType == "DOUBLE" || (sqlType == "NUMERIC") || (sqlType == "DECIMAL"))) {
|
|
85
|
+
return cellValue.toString();
|
|
86
|
+
}
|
|
87
|
+
else if (sqlType == "BLOB") {
|
|
88
|
+
return "X\'" + Buffer.from(cellValue).toString('hex') + "\'";
|
|
89
|
+
}
|
|
90
|
+
else if (((sqlType == "DATETIME") || (sqlType == "DATE")) && (cellValue instanceof Date)) {
|
|
91
|
+
return "\'" + cellValue.toISOString() + "\'";
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
return "\"" + cellValue.toString().replaceAll("\"", "\"\"") + "\"";
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Parse a single SQL result value for serialization using the context of the native data
|
|
99
|
+
* type.
|
|
100
|
+
*
|
|
101
|
+
* @param sqlValue data from serialization
|
|
102
|
+
* @param sqlType native type name for table column
|
|
103
|
+
*
|
|
104
|
+
*/
|
|
105
|
+
parseSqlValueAsCell(sqlValue, sqlType) {
|
|
106
|
+
if ((sqlValue === null) || (sqlValue == "NULL")) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
else if (sqlValue === undefined) {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
else if (((sqlType == "DATETIME") || (sqlType == "DATE")) && (typeof (sqlValue) == "string")) {
|
|
113
|
+
return new Date(sqlValue);
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
return sqlValue;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Connect to database.
|
|
121
|
+
*
|
|
122
|
+
*/
|
|
123
|
+
connect() {
|
|
124
|
+
const filepath = this._params.url.substring(7);
|
|
125
|
+
try {
|
|
126
|
+
OINOLog.debug("OINODbBunSqlite.connect", { params: this._params });
|
|
127
|
+
this._db = BunSqliteDb.open(filepath, { create: true, readonly: false, readwrite: true });
|
|
128
|
+
// OINOLog.debug("OINODbBunSqlite.connect done")
|
|
129
|
+
return Promise.resolve(true);
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
throw new Error(OINO_ERROR_PREFIX + ": Error connecting to Sqlite database (" + filepath + "): " + err);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Execute a select operation.
|
|
137
|
+
*
|
|
138
|
+
* @param sql SQL statement.
|
|
139
|
+
*
|
|
140
|
+
*/
|
|
141
|
+
async sqlSelect(sql) {
|
|
142
|
+
OINOBenchmark.start("sqlSelect");
|
|
143
|
+
let result;
|
|
144
|
+
try {
|
|
145
|
+
result = new OINOBunSqliteDataset(this._db?.query(sql).values(), []);
|
|
146
|
+
// OINOLog.debug("OINODbBunSqlite.sqlSelect", {result:result})
|
|
147
|
+
}
|
|
148
|
+
catch (e) {
|
|
149
|
+
result = new OINOBunSqliteDataset([[]], ["OINODbBunSqlite.sqlSelect exception in _db.query: " + e.message]);
|
|
150
|
+
}
|
|
151
|
+
OINOBenchmark.end("sqlSelect");
|
|
152
|
+
return Promise.resolve(result);
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Execute other sql operations.
|
|
156
|
+
*
|
|
157
|
+
* @param sql SQL statement.
|
|
158
|
+
*
|
|
159
|
+
*/
|
|
160
|
+
async sqlExec(sql) {
|
|
161
|
+
OINOBenchmark.start("sqlExec");
|
|
162
|
+
let result;
|
|
163
|
+
try {
|
|
164
|
+
this._db?.exec(sql);
|
|
165
|
+
result = new OINOBunSqliteDataset([[]], []);
|
|
166
|
+
}
|
|
167
|
+
catch (e) {
|
|
168
|
+
result = new OINOBunSqliteDataset([[]], [OINO_ERROR_PREFIX + "(sqlExec): exception in _db.exec [" + e.message + "]"]);
|
|
169
|
+
}
|
|
170
|
+
OINOBenchmark.end("sqlExec");
|
|
171
|
+
return Promise.resolve(result);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Initialize a data model by getting the SQL schema and populating OINODbDataFields of
|
|
175
|
+
* the model.
|
|
176
|
+
*
|
|
177
|
+
* @param api api which data model to initialize.
|
|
178
|
+
*
|
|
179
|
+
*/
|
|
180
|
+
async initializeApiDatamodel(api) {
|
|
181
|
+
const res = await this.sqlSelect("select sql from sqlite_schema WHERE name='" + api.params.tableName + "'");
|
|
182
|
+
const sql_desc = (res?.getRow()[0]);
|
|
183
|
+
// OINOLog.debug("OINODbBunSqlite.initDatamodel.sql_desc=" + sql_desc)
|
|
184
|
+
let table_matches = OINODbBunSqlite._tableDescriptionRegex.exec(sql_desc);
|
|
185
|
+
// OINOLog.debug("OINODbBunSqlite.initDatamodel", {table_matches:table_matches})
|
|
186
|
+
if (!table_matches || table_matches?.length < 2) {
|
|
187
|
+
throw new Error("Table " + api.params.tableName + " not recognized as a valid Sqlite table!");
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
// OINOBenchmark.start("OINODbBunSqlite.initDatamodel")
|
|
191
|
+
let field_strings = OINOStr.splitExcludingBrackets(table_matches[1], ',', '(', ')');
|
|
192
|
+
// OINOLog.debug("OINODbBunSqlite.initDatamodel", {table_match:table_matches[1], field_strings:field_strings})
|
|
193
|
+
for (let field_str of field_strings) {
|
|
194
|
+
field_str = field_str.trim();
|
|
195
|
+
let field_params = this._parseDbFieldParams(field_str);
|
|
196
|
+
let field_match = OINODbBunSqlite._tableFieldTypeRegex.exec(field_str);
|
|
197
|
+
// OINOLog.debug("initDatamodel next field", {field_str:field_str, field_match:field_match, field_params:field_params})
|
|
198
|
+
if ((!field_match) || (field_match.length < 3)) {
|
|
199
|
+
let primarykey_match = OINODbBunSqlite._tablePrimarykeyRegex.exec(field_str);
|
|
200
|
+
// OINOLog.debug("initDatamodel non-field definition", {primarykey_match:primarykey_match})
|
|
201
|
+
if (primarykey_match && primarykey_match.length >= 2) {
|
|
202
|
+
const primary_keys = primarykey_match[1].replaceAll("\"", "").split(','); // not sure if will have space or not so split by comma and trim later
|
|
203
|
+
for (let i = 0; i < primary_keys.length; i++) {
|
|
204
|
+
const pk = primary_keys[i].trim(); //..the trim
|
|
205
|
+
for (let j = 0; j < api.datamodel.fields.length; j++) {
|
|
206
|
+
if (api.datamodel.fields[j].name == pk) {
|
|
207
|
+
api.datamodel.fields[j].fieldParams.isPrimaryKey = true;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
OINOLog.info("OINODbBunSqlite.initializeApiDatamodel: Unsupported field definition skipped.", { field: field_str });
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
// field_str = "NAME TYPE (M, N)" -> 1:NAME, 2:TYPE, 4:M, 5:N
|
|
218
|
+
// OINOLog.debug("OINODbBunSqlite.initializeApiDatamodel: field regex matches", { field_match: field_match })
|
|
219
|
+
const field_name = field_match[1];
|
|
220
|
+
const sql_type = field_match[2];
|
|
221
|
+
const field_length = parseInt(field_match[4]) || 0;
|
|
222
|
+
// OINOLog.debug("OINODbBunSqlite.initializeApiDatamodel: field regex matches", { api.params: api.params, field_name:field_name })
|
|
223
|
+
if (((api.params.excludeFieldPrefix) && field_name.startsWith(api.params.excludeFieldPrefix)) || ((api.params.excludeFields) && (api.params.excludeFields.indexOf(field_name) < 0))) {
|
|
224
|
+
OINOLog.info("OINODbBunSqlite.initializeApiDatamodel: field excluded in API parameters.", { field: field_name });
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
if ((sql_type == "INTEGER") || (sql_type == "REAL") || (sql_type == "DOUBLE") || (sql_type == "NUMERIC") || (sql_type == "DECIMAL")) {
|
|
228
|
+
api.datamodel.addField(new OINONumberDataField(this, field_name, sql_type, field_params));
|
|
229
|
+
}
|
|
230
|
+
else if ((sql_type == "BLOB")) {
|
|
231
|
+
api.datamodel.addField(new OINOBlobDataField(this, field_name, sql_type, field_params, field_length));
|
|
232
|
+
}
|
|
233
|
+
else if ((sql_type == "TEXT")) {
|
|
234
|
+
api.datamodel.addField(new OINOStringDataField(this, field_name, sql_type, field_params, field_length));
|
|
235
|
+
}
|
|
236
|
+
else if ((sql_type == "DATETIME") || (sql_type == "DATE")) {
|
|
237
|
+
if (api.params.useDatesAsString) {
|
|
238
|
+
api.datamodel.addField(new OINOStringDataField(this, field_name, sql_type, field_params, 0));
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
api.datamodel.addField(new OINODatetimeDataField(this, field_name, sql_type, field_params));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
else if ((sql_type == "BOOLEAN")) {
|
|
245
|
+
api.datamodel.addField(new OINOBooleanDataField(this, field_name, sql_type, field_params));
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
OINOLog.info("OINODbBunSqlite.initializeApiDatamodel: unrecognized field type treated as string", { field_name: field_name, sql_type: sql_type, field_length: field_length, field_params: field_params });
|
|
249
|
+
api.datamodel.addField(new OINOStringDataField(this, field_name, sql_type, field_params, 0));
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
;
|
|
255
|
+
// OINOBenchmark.end("OINODbBunSqlite.initializeApiDatamodel")
|
|
256
|
+
OINOLog.debug("OINODbBunSqlite.initializeDatasetModel:\n" + api.datamodel.printDebug("\n"));
|
|
257
|
+
return Promise.resolve();
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { OINODbBunSqlite } from "./OINODbBunSqlite.js";
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINODataCell } from "@oino-ts/db";
|
|
2
|
+
/**
|
|
3
|
+
* Implementation of BunSqlite-database.
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export declare class OINODbBunSqlite extends OINODb {
|
|
7
|
+
private static _tableDescriptionRegex;
|
|
8
|
+
private static _tablePrimarykeyRegex;
|
|
9
|
+
private static _tableFieldTypeRegex;
|
|
10
|
+
private _db;
|
|
11
|
+
/**
|
|
12
|
+
* OINODbBunSqlite constructor
|
|
13
|
+
* @param params database parameters
|
|
14
|
+
*/
|
|
15
|
+
constructor(params: OINODbParams);
|
|
16
|
+
private _parseDbFieldParams;
|
|
17
|
+
/**
|
|
18
|
+
* Print a table name using database specific SQL escaping.
|
|
19
|
+
*
|
|
20
|
+
* @param sqlTable name of the table
|
|
21
|
+
*
|
|
22
|
+
*/
|
|
23
|
+
printSqlTablename(sqlTable: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* Print a column name with correct SQL escaping.
|
|
26
|
+
*
|
|
27
|
+
* @param sqlColumn name of the column
|
|
28
|
+
*
|
|
29
|
+
*/
|
|
30
|
+
printSqlColumnname(sqlColumn: string): string;
|
|
31
|
+
/**
|
|
32
|
+
* Print a single data value from serialization using the context of the native data
|
|
33
|
+
* type with the correct SQL escaping.
|
|
34
|
+
*
|
|
35
|
+
* @param cellValue data from sql results
|
|
36
|
+
* @param sqlType native type name for table column
|
|
37
|
+
*
|
|
38
|
+
*/
|
|
39
|
+
printCellAsSqlValue(cellValue: OINODataCell, sqlType: string): string;
|
|
40
|
+
/**
|
|
41
|
+
* Parse a single SQL result value for serialization using the context of the native data
|
|
42
|
+
* type.
|
|
43
|
+
*
|
|
44
|
+
* @param sqlValue data from serialization
|
|
45
|
+
* @param sqlType native type name for table column
|
|
46
|
+
*
|
|
47
|
+
*/
|
|
48
|
+
parseSqlValueAsCell(sqlValue: OINODataCell, sqlType: string): OINODataCell;
|
|
49
|
+
/**
|
|
50
|
+
* Connect to database.
|
|
51
|
+
*
|
|
52
|
+
*/
|
|
53
|
+
connect(): Promise<boolean>;
|
|
54
|
+
/**
|
|
55
|
+
* Execute a select operation.
|
|
56
|
+
*
|
|
57
|
+
* @param sql SQL statement.
|
|
58
|
+
*
|
|
59
|
+
*/
|
|
60
|
+
sqlSelect(sql: string): Promise<OINODbDataSet>;
|
|
61
|
+
/**
|
|
62
|
+
* Execute other sql operations.
|
|
63
|
+
*
|
|
64
|
+
* @param sql SQL statement.
|
|
65
|
+
*
|
|
66
|
+
*/
|
|
67
|
+
sqlExec(sql: string): Promise<OINODbDataSet>;
|
|
68
|
+
/**
|
|
69
|
+
* Initialize a data model by getting the SQL schema and populating OINODbDataFields of
|
|
70
|
+
* the model.
|
|
71
|
+
*
|
|
72
|
+
* @param api api which data model to initialize.
|
|
73
|
+
*
|
|
74
|
+
*/
|
|
75
|
+
initializeApiDatamodel(api: OINODbApi): Promise<void>;
|
|
76
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { OINODbBunSqlite } from "./OINODbBunSqlite.js";
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oino-ts/db-bunsqlite",
|
|
3
|
+
"version": "0.0.11",
|
|
4
|
+
"description": "OINO TS package for using Bun Sqlite databases.",
|
|
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
|
+
"sqlite"
|
|
18
|
+
],
|
|
19
|
+
"main": "./dist/cjs/index.js",
|
|
20
|
+
"module": "./dist/esm/index.js",
|
|
21
|
+
"types": "./dist/types/index.d.ts",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@oino-ts/db": "^0.0.11"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/node": "^20.12.7",
|
|
27
|
+
"@types/bun": "latest"
|
|
28
|
+
},
|
|
29
|
+
"files": [
|
|
30
|
+
"src/*.ts",
|
|
31
|
+
"dist/cjs/*.js",
|
|
32
|
+
"dist/esm/*.js",
|
|
33
|
+
"dist/types/*.d.ts"
|
|
34
|
+
]
|
|
35
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
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 { OINODb, OINODbParams, OINODbDataSet, OINODbApi, OINOBooleanDataField, OINONumberDataField, OINOStringDataField, OINODbDataFieldParams, OINO_ERROR_PREFIX, OINODbMemoryDataSet, OINODataCell, OINOBenchmark, OINOBlobDataField, OINODatetimeDataField, OINOStr, OINOLog } from "@oino-ts/db";
|
|
8
|
+
|
|
9
|
+
import { Database as BunSqliteDb } from "bun:sqlite";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Implmentation of OINODbDataSet for BunSqlite.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
class OINOBunSqliteDataset extends OINODbMemoryDataSet {
|
|
16
|
+
constructor(data: unknown, messages:string[]=[]) {
|
|
17
|
+
super(data, messages)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Implementation of BunSqlite-database.
|
|
23
|
+
*
|
|
24
|
+
*/
|
|
25
|
+
export class OINODbBunSqlite extends OINODb {
|
|
26
|
+
private static _tableDescriptionRegex = /^CREATE TABLE\s*[\"\[]?\w+[\"\]]?\s*\(\s*(.*)\s*\)\s*(WITHOUT ROWID)?$/msi
|
|
27
|
+
private static _tablePrimarykeyRegex = /PRIMARY KEY \(([^\)]+)\)/i
|
|
28
|
+
private static _tableFieldTypeRegex = /[\"\[\s]?(\w+)[\"\]\s]\s?(INTEGER|REAL|DOUBLE|NUMERIC|DECIMAL|TEXT|BLOB|VARCHAR|DATETIME|DATE|BOOLEAN)(\s?\((\d+)\s?\,?\s?(\d*)?\))?/i
|
|
29
|
+
|
|
30
|
+
private _db:BunSqliteDb|null
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* OINODbBunSqlite constructor
|
|
34
|
+
* @param params database parameters
|
|
35
|
+
*/
|
|
36
|
+
constructor(params:OINODbParams) {
|
|
37
|
+
super(params)
|
|
38
|
+
this._db = null
|
|
39
|
+
if (!this._params.url.startsWith("file://")) {
|
|
40
|
+
throw new Error(OINO_ERROR_PREFIX + ": OINODbBunSqlite url must be a file://-url!")
|
|
41
|
+
}
|
|
42
|
+
OINOLog.debug("OINODbBunSqlite.constructor", {params:params})
|
|
43
|
+
|
|
44
|
+
if (this._params.type !== "OINODbBunSqlite") {
|
|
45
|
+
throw new Error(OINO_ERROR_PREFIX + ": Not OINODbBunSqlite-type: " + this._params.type)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private _parseDbFieldParams(fieldStr:string): OINODbDataFieldParams {
|
|
50
|
+
const result:OINODbDataFieldParams = {
|
|
51
|
+
isPrimaryKey: fieldStr.indexOf("PRIMARY KEY") >= 0,
|
|
52
|
+
isAutoInc: fieldStr.indexOf("AUTOINCREMENT") >= 0,
|
|
53
|
+
isNotNull: fieldStr.indexOf("NOT NULL") >= 0
|
|
54
|
+
}
|
|
55
|
+
// OINOLog.debug("OINODbBunSqlite._parseDbFieldParams", {fieldStr:fieldStr, result:result})
|
|
56
|
+
return result
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Print a table name using database specific SQL escaping.
|
|
61
|
+
*
|
|
62
|
+
* @param sqlTable name of the table
|
|
63
|
+
*
|
|
64
|
+
*/
|
|
65
|
+
printSqlTablename(sqlTable:string): string {
|
|
66
|
+
return "["+sqlTable+"]"
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Print a column name with correct SQL escaping.
|
|
71
|
+
*
|
|
72
|
+
* @param sqlColumn name of the column
|
|
73
|
+
*
|
|
74
|
+
*/
|
|
75
|
+
printSqlColumnname(sqlColumn:string): string {
|
|
76
|
+
return "\""+sqlColumn+"\""
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Print a single data value from serialization using the context of the native data
|
|
81
|
+
* type with the correct SQL escaping.
|
|
82
|
+
*
|
|
83
|
+
* @param cellValue data from sql results
|
|
84
|
+
* @param sqlType native type name for table column
|
|
85
|
+
*
|
|
86
|
+
*/
|
|
87
|
+
printCellAsSqlValue(cellValue:OINODataCell, sqlType: string): string {
|
|
88
|
+
// OINOLog.debug("OINODbBunSqlite.printCellAsSqlValue", {cellValue:cellValue, sqlType:sqlType, type:typeof(cellValue)})
|
|
89
|
+
if (cellValue === null) {
|
|
90
|
+
return "NULL"
|
|
91
|
+
|
|
92
|
+
} else if (cellValue === undefined) {
|
|
93
|
+
return "UNDEFINED"
|
|
94
|
+
|
|
95
|
+
} else if ((sqlType == "INTEGER") || (sqlType == "REAL") || (sqlType == "DOUBLE" || (sqlType == "NUMERIC") || (sqlType == "DECIMAL"))) {
|
|
96
|
+
return cellValue.toString()
|
|
97
|
+
|
|
98
|
+
} else if (sqlType == "BLOB") {
|
|
99
|
+
return "X\'" + Buffer.from(cellValue as Uint8Array).toString('hex') + "\'"
|
|
100
|
+
|
|
101
|
+
} else if (((sqlType == "DATETIME") || (sqlType == "DATE")) && (cellValue instanceof Date)) {
|
|
102
|
+
return "\'" + cellValue.toISOString() + "\'"
|
|
103
|
+
|
|
104
|
+
} else {
|
|
105
|
+
return "\"" + cellValue.toString().replaceAll("\"", "\"\"") + "\""
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Parse a single SQL result value for serialization using the context of the native data
|
|
111
|
+
* type.
|
|
112
|
+
*
|
|
113
|
+
* @param sqlValue data from serialization
|
|
114
|
+
* @param sqlType native type name for table column
|
|
115
|
+
*
|
|
116
|
+
*/
|
|
117
|
+
parseSqlValueAsCell(sqlValue:OINODataCell, sqlType: string): OINODataCell {
|
|
118
|
+
if ((sqlValue === null) || (sqlValue == "NULL")) {
|
|
119
|
+
return null
|
|
120
|
+
|
|
121
|
+
} else if (sqlValue === undefined) {
|
|
122
|
+
return undefined
|
|
123
|
+
|
|
124
|
+
} else if (((sqlType == "DATETIME") || (sqlType == "DATE")) && (typeof(sqlValue) == "string")) {
|
|
125
|
+
return new Date(sqlValue)
|
|
126
|
+
|
|
127
|
+
} else {
|
|
128
|
+
return sqlValue
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Connect to database.
|
|
136
|
+
*
|
|
137
|
+
*/
|
|
138
|
+
connect(): Promise<boolean> {
|
|
139
|
+
const filepath:string = this._params.url.substring(7)
|
|
140
|
+
try {
|
|
141
|
+
OINOLog.debug("OINODbBunSqlite.connect", {params:this._params})
|
|
142
|
+
this._db = BunSqliteDb.open(filepath, { create: true, readonly: false, readwrite: true })
|
|
143
|
+
// OINOLog.debug("OINODbBunSqlite.connect done")
|
|
144
|
+
return Promise.resolve(true)
|
|
145
|
+
} catch (err) {
|
|
146
|
+
throw new Error(OINO_ERROR_PREFIX + ": Error connecting to Sqlite database ("+ filepath +"): " + err)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Execute a select operation.
|
|
152
|
+
*
|
|
153
|
+
* @param sql SQL statement.
|
|
154
|
+
*
|
|
155
|
+
*/
|
|
156
|
+
async sqlSelect(sql:string): Promise<OINODbDataSet> {
|
|
157
|
+
OINOBenchmark.start("sqlSelect")
|
|
158
|
+
let result:OINODbDataSet
|
|
159
|
+
try {
|
|
160
|
+
result = new OINOBunSqliteDataset(this._db?.query(sql).values(), [])
|
|
161
|
+
// OINOLog.debug("OINODbBunSqlite.sqlSelect", {result:result})
|
|
162
|
+
|
|
163
|
+
} catch (e:any) {
|
|
164
|
+
result = new OINOBunSqliteDataset([[]], ["OINODbBunSqlite.sqlSelect exception in _db.query: " + e.message])
|
|
165
|
+
}
|
|
166
|
+
OINOBenchmark.end("sqlSelect")
|
|
167
|
+
return Promise.resolve(result)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Execute other sql operations.
|
|
172
|
+
*
|
|
173
|
+
* @param sql SQL statement.
|
|
174
|
+
*
|
|
175
|
+
*/
|
|
176
|
+
async sqlExec(sql:string): Promise<OINODbDataSet> {
|
|
177
|
+
OINOBenchmark.start("sqlExec")
|
|
178
|
+
let result:OINODbDataSet
|
|
179
|
+
try {
|
|
180
|
+
this._db?.exec(sql)
|
|
181
|
+
result = new OINOBunSqliteDataset([[]], [])
|
|
182
|
+
|
|
183
|
+
} catch (e:any) {
|
|
184
|
+
result = new OINOBunSqliteDataset([[]], [OINO_ERROR_PREFIX + "(sqlExec): exception in _db.exec [" + e.message + "]"])
|
|
185
|
+
}
|
|
186
|
+
OINOBenchmark.end("sqlExec")
|
|
187
|
+
return Promise.resolve(result)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Initialize a data model by getting the SQL schema and populating OINODbDataFields of
|
|
192
|
+
* the model.
|
|
193
|
+
*
|
|
194
|
+
* @param api api which data model to initialize.
|
|
195
|
+
*
|
|
196
|
+
*/
|
|
197
|
+
async initializeApiDatamodel(api:OINODbApi): Promise<void> {
|
|
198
|
+
const res:OINODbDataSet|null = await this.sqlSelect("select sql from sqlite_schema WHERE name='" + api.params.tableName + "'")
|
|
199
|
+
const sql_desc:string = (res?.getRow()[0]) as string
|
|
200
|
+
// OINOLog.debug("OINODbBunSqlite.initDatamodel.sql_desc=" + sql_desc)
|
|
201
|
+
let table_matches = OINODbBunSqlite._tableDescriptionRegex.exec(sql_desc)
|
|
202
|
+
// OINOLog.debug("OINODbBunSqlite.initDatamodel", {table_matches:table_matches})
|
|
203
|
+
if (!table_matches || table_matches?.length < 2) {
|
|
204
|
+
throw new Error("Table " + api.params.tableName + " not recognized as a valid Sqlite table!")
|
|
205
|
+
|
|
206
|
+
} else {
|
|
207
|
+
// OINOBenchmark.start("OINODbBunSqlite.initDatamodel")
|
|
208
|
+
let field_strings:string[] = OINOStr.splitExcludingBrackets(table_matches[1], ',', '(', ')')
|
|
209
|
+
// OINOLog.debug("OINODbBunSqlite.initDatamodel", {table_match:table_matches[1], field_strings:field_strings})
|
|
210
|
+
for (let field_str of field_strings) {
|
|
211
|
+
field_str = field_str.trim()
|
|
212
|
+
let field_params = this._parseDbFieldParams(field_str)
|
|
213
|
+
let field_match = OINODbBunSqlite._tableFieldTypeRegex.exec(field_str)
|
|
214
|
+
// OINOLog.debug("initDatamodel next field", {field_str:field_str, field_match:field_match, field_params:field_params})
|
|
215
|
+
if ((!field_match) || (field_match.length < 3)) {
|
|
216
|
+
let primarykey_match = OINODbBunSqlite._tablePrimarykeyRegex.exec(field_str)
|
|
217
|
+
// OINOLog.debug("initDatamodel non-field definition", {primarykey_match:primarykey_match})
|
|
218
|
+
if (primarykey_match && primarykey_match.length >= 2) {
|
|
219
|
+
const primary_keys:string[] = primarykey_match[1].replaceAll("\"", "").split(',') // not sure if will have space or not so split by comma and trim later
|
|
220
|
+
for (let i:number=0; i<primary_keys.length; i++) {
|
|
221
|
+
const pk:string = primary_keys[i].trim() //..the trim
|
|
222
|
+
for (let j:number=0; j<api.datamodel.fields.length; j++) {
|
|
223
|
+
if (api.datamodel.fields[j].name == pk) {
|
|
224
|
+
api.datamodel.fields[j].fieldParams.isPrimaryKey = true
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
} else {
|
|
230
|
+
OINOLog.info("OINODbBunSqlite.initializeApiDatamodel: Unsupported field definition skipped.", { field: field_str })
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
} else {
|
|
234
|
+
// field_str = "NAME TYPE (M, N)" -> 1:NAME, 2:TYPE, 4:M, 5:N
|
|
235
|
+
// OINOLog.debug("OINODbBunSqlite.initializeApiDatamodel: field regex matches", { field_match: field_match })
|
|
236
|
+
const field_name:string = field_match[1]
|
|
237
|
+
const sql_type:string = field_match[2]
|
|
238
|
+
const field_length:number = parseInt(field_match[4]) || 0
|
|
239
|
+
// OINOLog.debug("OINODbBunSqlite.initializeApiDatamodel: field regex matches", { api.params: api.params, field_name:field_name })
|
|
240
|
+
if (((api.params.excludeFieldPrefix) && field_name.startsWith(api.params.excludeFieldPrefix)) || ((api.params.excludeFields) && (api.params.excludeFields.indexOf(field_name) < 0))) {
|
|
241
|
+
OINOLog.info("OINODbBunSqlite.initializeApiDatamodel: field excluded in API parameters.", {field:field_name})
|
|
242
|
+
|
|
243
|
+
} else {
|
|
244
|
+
if ((sql_type == "INTEGER") || (sql_type == "REAL") || (sql_type == "DOUBLE") || (sql_type == "NUMERIC") || (sql_type == "DECIMAL")) {
|
|
245
|
+
api.datamodel.addField(new OINONumberDataField(this, field_name, sql_type, field_params ))
|
|
246
|
+
|
|
247
|
+
} else if ((sql_type == "BLOB") ) {
|
|
248
|
+
api.datamodel.addField(new OINOBlobDataField(this, field_name, sql_type, field_params, field_length))
|
|
249
|
+
|
|
250
|
+
} else if ((sql_type == "TEXT")) {
|
|
251
|
+
api.datamodel.addField(new OINOStringDataField(this, field_name, sql_type, field_params, field_length))
|
|
252
|
+
|
|
253
|
+
} else if ((sql_type == "DATETIME") || (sql_type == "DATE")) {
|
|
254
|
+
if (api.params.useDatesAsString) {
|
|
255
|
+
api.datamodel.addField(new OINOStringDataField(this, field_name, sql_type, field_params, 0))
|
|
256
|
+
} else {
|
|
257
|
+
api.datamodel.addField(new OINODatetimeDataField(this, field_name, sql_type, field_params))
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
} else if ((sql_type == "BOOLEAN")) {
|
|
261
|
+
api.datamodel.addField(new OINOBooleanDataField(this, field_name, sql_type, field_params))
|
|
262
|
+
|
|
263
|
+
} else {
|
|
264
|
+
OINOLog.info("OINODbBunSqlite.initializeApiDatamodel: unrecognized field type treated as string", {field_name: field_name, sql_type:sql_type, field_length:field_length, field_params:field_params })
|
|
265
|
+
api.datamodel.addField(new OINOStringDataField(this, field_name, sql_type, field_params, 0))
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
// OINOBenchmark.end("OINODbBunSqlite.initializeApiDatamodel")
|
|
271
|
+
OINOLog.debug("OINODbBunSqlite.initializeDatasetModel:\n" + api.datamodel.printDebug("\n"))
|
|
272
|
+
return Promise.resolve()
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { OINODbBunSqlite } from "./OINODbBunSqlite.js"
|