@oino-ts/types 0.0.15 → 0.0.17
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 +18 -47
- package/dist/cjs/OINOResult.js +7 -0
- package/dist/esm/OINOResult.js +7 -0
- package/dist/types/OINOResult.d.ts +5 -0
- package/dist/types/OINOStr.d.ts +6 -6
- package/package.json +1 -1
- package/src/OINOResult.ts +8 -0
- package/src/OINOStr.ts +6 -6
package/README.md
CHANGED
|
@@ -10,15 +10,15 @@
|
|
|
10
10
|
# GETTING STARTED
|
|
11
11
|
|
|
12
12
|
### Setup
|
|
13
|
-
Install the `@oino-ts/
|
|
13
|
+
Install the `@oino-ts/db` npm package and necessary database packages and import them in your code.
|
|
14
14
|
```
|
|
15
|
-
bun install @oino-ts/
|
|
16
|
-
bun install @oino-ts/bunsqlite
|
|
15
|
+
bun install @oino-ts/db
|
|
16
|
+
bun install @oino-ts/db-bunsqlite
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
```
|
|
20
|
-
import { OINODb, OINOApi, OINOFactory } from "@oino-ts/
|
|
21
|
-
import { OINODbBunSqlite } from "@oino-ts/bunsqlite"
|
|
20
|
+
import { OINODb, OINOApi, OINOFactory } from "@oino-ts/db";
|
|
21
|
+
import { OINODbBunSqlite } from "@oino-ts/db-bunsqlite"
|
|
22
22
|
```
|
|
23
23
|
|
|
24
24
|
### Register database and logger
|
|
@@ -51,7 +51,7 @@
|
|
|
51
51
|
### Write results back to HTTP Response
|
|
52
52
|
The results for a GET request will contain [`OINOModelSet`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbModelSet.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
53
|
```
|
|
54
|
-
return new Response(result.
|
|
54
|
+
return new Response(result.data.writeString(OINOContentType.json))
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
|
|
@@ -59,20 +59,8 @@
|
|
|
59
59
|
|
|
60
60
|
## RESTfull
|
|
61
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
62
|
|
|
75
|
-
|
|
63
|
+
For example HTTP POST
|
|
76
64
|
```
|
|
77
65
|
Request and response:
|
|
78
66
|
> curl.exe -X POST http://localhost:3001/orderdetails -H "Content-Type: application/json" --data '[{\"OrderID\":11077,\"ProductID\":99,\"UnitPrice\":19,\"Quantity\":1,\"Discount\":0}]'
|
|
@@ -82,31 +70,12 @@
|
|
|
82
70
|
INSERT INTO [OrderDetails] ("OrderID","ProductID","UnitPrice","Quantity","Discount") VALUES (11077,99,19,1,0);
|
|
83
71
|
```
|
|
84
72
|
|
|
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
73
|
|
|
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
74
|
## Universal Serialization
|
|
106
75
|
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
76
|
|
|
108
77
|
### Features
|
|
109
|
-
- Files can be sent to BLOB fields using BASE64 encoding.
|
|
78
|
+
- Files can be sent to BLOB fields using BASE64 or MIME multipart encoding.
|
|
110
79
|
- Datetimes are (optionally) normalized to ISO 8601 format.
|
|
111
80
|
- Extended JSON-encoding
|
|
112
81
|
- 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).
|
|
@@ -129,12 +98,13 @@
|
|
|
129
98
|
- Bun Sqlite through Bun native implementation
|
|
130
99
|
- Postgresql through [pg](https://www.npmjs.com/package/pg)-package
|
|
131
100
|
- Mariadb / Mysql-support through [mariadb](https://www.npmjs.com/package/mariadb)-package
|
|
101
|
+
- Sql Server through [mssql](https://www.npmjs.com/package/mssql)-package
|
|
132
102
|
|
|
133
103
|
## Complex Keys
|
|
134
104
|
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
105
|
|
|
136
106
|
## Power Of SQL
|
|
137
|
-
Since OINO
|
|
107
|
+
Since OINO is just generating SQL, WHERE-conditions can be defined with [`OINOSqlFilter`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlFilter.html), order with [`OINOSqlOrder`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlOrder.html) and limits/paging with [`OINOSqlOrder`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbSqlLimit.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
108
|
|
|
139
109
|
## Swagger Support
|
|
140
110
|
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.
|
|
@@ -158,7 +128,7 @@
|
|
|
158
128
|
# STATUS
|
|
159
129
|
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
130
|
|
|
161
|
-
## Beta
|
|
131
|
+
## Beta
|
|
162
132
|
For a beta status following milestones are planned:
|
|
163
133
|
|
|
164
134
|
### Realistic app
|
|
@@ -178,8 +148,8 @@
|
|
|
178
148
|
### Batch updates
|
|
179
149
|
Supporting batch updates similar to batch inserts is slightly bending the RESTfull principles but would still be a useful optional feature.
|
|
180
150
|
|
|
181
|
-
### Aggregation
|
|
182
|
-
Similar to filtering and
|
|
151
|
+
### Aggregation
|
|
152
|
+
Similar to filtering, ordering and limits, aggregation could be implemented as HTTP request parameters telling what column is aggregated or used for ordering or how many results to return.
|
|
183
153
|
|
|
184
154
|
### Streaming
|
|
185
155
|
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.
|
|
@@ -205,18 +175,19 @@
|
|
|
205
175
|
# LINKS
|
|
206
176
|
- [Github repository](https://github.com/pragmatta/oino-ts)
|
|
207
177
|
- [NPM repository](https://www.npmjs.com/org/oino-ts)
|
|
208
|
-
|
|
178
|
+
|
|
209
179
|
|
|
210
180
|
# ACKNOWLEDGEMENTS
|
|
211
181
|
|
|
212
182
|
## Libraries
|
|
213
183
|
OINO uses the following open source libraries and npm packages and I would like to thank everyone for their contributions:
|
|
214
|
-
- Postgresql
|
|
215
|
-
- Mariadb / Mysql
|
|
184
|
+
- Postgresql [node-postgres package](https://www.npmjs.com/package/pg)
|
|
185
|
+
- Mariadb / Mysql [mariadb package](https://www.npmjs.com/package/mariadb)
|
|
186
|
+
- Sql Server [mssql package](https://www.npmjs.com/package/mssql)
|
|
187
|
+
- Custom base encoding [base-x package](https://www.npmjs.com/package/base-x)
|
|
216
188
|
|
|
217
189
|
## Bun
|
|
218
190
|
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
191
|
|
|
220
192
|
## SQL Scripts
|
|
221
193
|
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
|
-
|
package/dist/cjs/OINOResult.js
CHANGED
|
@@ -148,6 +148,13 @@ class OINOResult {
|
|
|
148
148
|
}
|
|
149
149
|
}
|
|
150
150
|
}
|
|
151
|
+
/**
|
|
152
|
+
* Print result for logging.
|
|
153
|
+
*
|
|
154
|
+
*/
|
|
155
|
+
printLog() {
|
|
156
|
+
return "OINOResult: statusCode=" + this.statusCode + ", statusMessage=" + this.statusMessage + ", messages=[" + this.messages.join(", ") + "]";
|
|
157
|
+
}
|
|
151
158
|
}
|
|
152
159
|
exports.OINOResult = OINOResult;
|
|
153
160
|
/**
|
package/dist/esm/OINOResult.js
CHANGED
|
@@ -145,6 +145,13 @@ export class OINOResult {
|
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
147
|
}
|
|
148
|
+
/**
|
|
149
|
+
* Print result for logging.
|
|
150
|
+
*
|
|
151
|
+
*/
|
|
152
|
+
printLog() {
|
|
153
|
+
return "OINOResult: statusCode=" + this.statusCode + ", statusMessage=" + this.statusMessage + ", messages=[" + this.messages.join(", ") + "]";
|
|
154
|
+
}
|
|
148
155
|
}
|
|
149
156
|
/**
|
|
150
157
|
* Specialized result for HTTP responses.
|
|
@@ -72,6 +72,11 @@ export declare class OINOResult {
|
|
|
72
72
|
*
|
|
73
73
|
*/
|
|
74
74
|
copyMessagesToHeaders(headers: Headers, copyErrors?: boolean, copyWarnings?: boolean, copyInfos?: boolean, copyDebug?: boolean): void;
|
|
75
|
+
/**
|
|
76
|
+
* Print result for logging.
|
|
77
|
+
*
|
|
78
|
+
*/
|
|
79
|
+
printLog(): string;
|
|
75
80
|
}
|
|
76
81
|
/**
|
|
77
82
|
* Specialized result for HTTP responses.
|
package/dist/types/OINOStr.d.ts
CHANGED
|
@@ -40,7 +40,7 @@ export declare class OINOStr {
|
|
|
40
40
|
*
|
|
41
41
|
* @param str string to decode
|
|
42
42
|
*/
|
|
43
|
-
static decodeJSON(str: string): string
|
|
43
|
+
static decodeJSON(str: string): string;
|
|
44
44
|
/**
|
|
45
45
|
* Encode OINO serialized strings as valid CSV.
|
|
46
46
|
*
|
|
@@ -52,7 +52,7 @@ export declare class OINOStr {
|
|
|
52
52
|
*
|
|
53
53
|
* @param str string to decode
|
|
54
54
|
*/
|
|
55
|
-
static decodeCSV(str: string): string
|
|
55
|
+
static decodeCSV(str: string): string;
|
|
56
56
|
/**
|
|
57
57
|
* Encode OINO serialized strings as valid Formdata.
|
|
58
58
|
*
|
|
@@ -64,7 +64,7 @@ export declare class OINOStr {
|
|
|
64
64
|
*
|
|
65
65
|
* @param str string to decode
|
|
66
66
|
*/
|
|
67
|
-
static decodeFormdata(str: string): string
|
|
67
|
+
static decodeFormdata(str: string): string;
|
|
68
68
|
/**
|
|
69
69
|
* Encode OINO serialized strings as valid Urlencode.
|
|
70
70
|
*
|
|
@@ -76,7 +76,7 @@ export declare class OINOStr {
|
|
|
76
76
|
*
|
|
77
77
|
* @param str string to decode
|
|
78
78
|
*/
|
|
79
|
-
static decodeUrlencode(str: string): string
|
|
79
|
+
static decodeUrlencode(str: string): string;
|
|
80
80
|
/**
|
|
81
81
|
* Encode OINO serialized strings as valid HTML content.
|
|
82
82
|
*
|
|
@@ -88,7 +88,7 @@ export declare class OINOStr {
|
|
|
88
88
|
*
|
|
89
89
|
* @param str string to encode
|
|
90
90
|
*/
|
|
91
|
-
static decodeHtml(str: string): string
|
|
91
|
+
static decodeHtml(str: string): string;
|
|
92
92
|
/**
|
|
93
93
|
* Decode content type formatted string as OINO serialization.
|
|
94
94
|
*
|
|
@@ -96,7 +96,7 @@ export declare class OINOStr {
|
|
|
96
96
|
* @param contentType content type for serialization
|
|
97
97
|
*
|
|
98
98
|
*/
|
|
99
|
-
static decode(str: string, contentType: OINOContentType): string
|
|
99
|
+
static decode(str: string, contentType: OINOContentType): string;
|
|
100
100
|
/**
|
|
101
101
|
* Encode OINO serialized string to the content type formatting.
|
|
102
102
|
*
|
package/package.json
CHANGED
package/src/OINOResult.ts
CHANGED
|
@@ -157,6 +157,14 @@ export class OINOResult {
|
|
|
157
157
|
}
|
|
158
158
|
}
|
|
159
159
|
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Print result for logging.
|
|
163
|
+
*
|
|
164
|
+
*/
|
|
165
|
+
printLog() {
|
|
166
|
+
return "OINOResult: statusCode=" + this.statusCode + ", statusMessage=" + this.statusMessage + ", messages=[" + this.messages.join(", ") + "]"
|
|
167
|
+
}
|
|
160
168
|
}
|
|
161
169
|
|
|
162
170
|
/**
|
package/src/OINOStr.ts
CHANGED
|
@@ -109,7 +109,7 @@ export class OINOStr {
|
|
|
109
109
|
*
|
|
110
110
|
* @param str string to decode
|
|
111
111
|
*/
|
|
112
|
-
static decodeJSON(str:string):string
|
|
112
|
+
static decodeJSON(str:string):string {
|
|
113
113
|
return str // JSON parsing using JS methods, no need to decode anything
|
|
114
114
|
}
|
|
115
115
|
|
|
@@ -133,7 +133,7 @@ export class OINOStr {
|
|
|
133
133
|
*
|
|
134
134
|
* @param str string to decode
|
|
135
135
|
*/
|
|
136
|
-
static decodeCSV(str:string):string
|
|
136
|
+
static decodeCSV(str:string):string {
|
|
137
137
|
return str.replaceAll("\"\"", "\"")
|
|
138
138
|
}
|
|
139
139
|
|
|
@@ -157,7 +157,7 @@ export class OINOStr {
|
|
|
157
157
|
*
|
|
158
158
|
* @param str string to decode
|
|
159
159
|
*/
|
|
160
|
-
static decodeFormdata(str:string):string
|
|
160
|
+
static decodeFormdata(str:string):string {
|
|
161
161
|
return str
|
|
162
162
|
}
|
|
163
163
|
/**
|
|
@@ -180,7 +180,7 @@ export class OINOStr {
|
|
|
180
180
|
*
|
|
181
181
|
* @param str string to decode
|
|
182
182
|
*/
|
|
183
|
-
static decodeUrlencode(str:string):string
|
|
183
|
+
static decodeUrlencode(str:string):string {
|
|
184
184
|
return decodeURIComponent(str)
|
|
185
185
|
}
|
|
186
186
|
|
|
@@ -204,7 +204,7 @@ export class OINOStr {
|
|
|
204
204
|
*
|
|
205
205
|
* @param str string to encode
|
|
206
206
|
*/
|
|
207
|
-
static decodeHtml(str:string):string
|
|
207
|
+
static decodeHtml(str:string):string {
|
|
208
208
|
return str.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll(''', "'")
|
|
209
209
|
}
|
|
210
210
|
/**
|
|
@@ -214,7 +214,7 @@ export class OINOStr {
|
|
|
214
214
|
* @param contentType content type for serialization
|
|
215
215
|
*
|
|
216
216
|
*/
|
|
217
|
-
static decode(str:string, contentType:OINOContentType):string
|
|
217
|
+
static decode(str:string, contentType:OINOContentType):string {
|
|
218
218
|
if (contentType == OINOContentType.csv) {
|
|
219
219
|
return this.decodeCSV(str)
|
|
220
220
|
} else if (contentType == OINOContentType.json) {
|