@oino-ts/nosql-aws 1.0.7 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +4 -4
- package/readme.md +209 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oino-ts/nosql-aws",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "OINO TS package for using AWS DynamoDB as a REST API.",
|
|
5
5
|
"author": "Matias Kiviniemi (pragmatta)",
|
|
6
6
|
"license": "MPL-2.0",
|
|
@@ -23,11 +23,11 @@
|
|
|
23
23
|
"dependencies": {
|
|
24
24
|
"@aws-sdk/client-dynamodb": "^3.0.0",
|
|
25
25
|
"@aws-sdk/lib-dynamodb": "^3.0.0",
|
|
26
|
-
"@oino-ts/nosql": "1.0
|
|
27
|
-
"@oino-ts/common": "1.0
|
|
26
|
+
"@oino-ts/nosql": "1.1.0",
|
|
27
|
+
"@oino-ts/common": "1.1.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@oino-ts/types": "1.0
|
|
30
|
+
"@oino-ts/types": "1.1.0",
|
|
31
31
|
"@types/bun": "^1.1.14",
|
|
32
32
|
"@types/node": "^22.0.00",
|
|
33
33
|
"typescript": "~5.9.0"
|
package/readme.md
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
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
|
+
## Create Datasources
|
|
13
|
+
|
|
14
|
+
### Create an SQL DB
|
|
15
|
+
|
|
16
|
+
First install the `@oino-ts/db` npm package and necessary database packages and import them in your code.
|
|
17
|
+
```
|
|
18
|
+
bun install @oino-ts/db
|
|
19
|
+
bun install @oino-ts/db-bunsqlite
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```
|
|
23
|
+
import { OINODb, OINODbFactory } from "@oino-ts/db";
|
|
24
|
+
import { OINOApi } from "@oino-ts/db";
|
|
25
|
+
import { OINODbBunSqlite } from "@oino-ts/db-bunsqlite"
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Next register your database implementation and logger (see [`OINOConsoleLog`](https://pragmatta.github.io/oino-ts/classes/common_src.OINOConsoleLog.html) how to implement your own)
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
OINOLog.setLogger(new OINOConsoleLog())
|
|
32
|
+
OINODbFactory.registerDb("OINODbBunSqlite", OINODbBunSqlite)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Finally creating a database connection [`OINODb`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODb.html) is done by passing [`OINODbParams`](https://pragmatta.github.io/oino-ts/types/db_src.OINODbParams.html) to the factory method. For [`OINODbBunSqlite`](https://pragmatta.github.io/oino-ts/classes/db_bunsqlite_src.OINODbBunSqlite.html) that means a file url for the database file, for others network host, port, credentials etc.
|
|
36
|
+
```
|
|
37
|
+
const db:OINODb = await OINODbFactory.createDb( { type: "OINODbBunSqlite", url: "file://../localDb/northwind.sqlite" } )
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Create NoSQL datasources
|
|
41
|
+
Creating NoSQL datasources works similarly by importing the [`OINONoSql`](https://pragmatta.github.io/oino-ts/modules/nosql_src.html) package and either the [`OINONoSqlAws`](https://pragmatta.github.io/oino-ts/modules/nosql-aws_src.html) or [`OINONoSqlAzure`](https://pragmatta.github.io/oino-ts/modules/nosql-azure_src.html), registering the implementation with the factory
|
|
42
|
+
```
|
|
43
|
+
OINONoSqlFactory.registerNoSql("OINONoSqlAzureTable", OINONoSqlAzureTable)
|
|
44
|
+
const nosql_azure_params = { type: "OINONoSqlAzureTable", table: "NorthwindOrders", credentials: { connectionStr: process.env.OINOCLOUD_TEST_BLOB_AZURE_CONSTR } }
|
|
45
|
+
const nosql_azure = await OINONoSqlFactory.createNoSql(nosql_azure_params)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
NOTE! Format of the credentials varies by platform and might require extra authorization.
|
|
49
|
+
|
|
50
|
+
### Create Blob datasources
|
|
51
|
+
Creating Blob datasources works similarly by importing the [`OINOBlob`](https://pragmatta.github.io/oino-ts/classes/blob_src.OINOBlob.html) package and either the [`OINOBlobAws`](https://pragmatta.github.io/oino-ts/modules/blob-aws_src.html) or [`OINOBlobAzure`](https://pragmatta.github.io/oino-ts/modules/blob-azure_src.html), registering the implementation with the factory
|
|
52
|
+
```
|
|
53
|
+
OINOBlobFactory.registerBlob("OINOBlobAzureTable", OINOBlobAzureTable)
|
|
54
|
+
const Blob_azure_params = { type: "OINOBlobAzureTable", table: "NorthwindOrders", credentials: { connectionStr: process.env.OINOCLOUD_TEST_BLOB_AZURE_CONSTR } }
|
|
55
|
+
const Blob_azure = await OINOBlobFactory.createBlob(nosql_azure_params)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
NOTE! Format of the credentials varies by platform and might require extra authorization.
|
|
59
|
+
|
|
60
|
+
## Create an API
|
|
61
|
+
From a datasource you can create an [`OINOApi`](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbApi.html) by passing [`OINOApiParams`](https://pragmatta.github.io/oino-ts/types/db_src.OINODbApiParams.html) with table name and preferences to the factory method.
|
|
62
|
+
```
|
|
63
|
+
const api_employees:OINOApi = await OINOFactory.createApi(db, { tableName: "Employees", excludeFields:["BirthDate"] })
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Pass HTTP requests to API
|
|
67
|
+
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.
|
|
68
|
+
|
|
69
|
+
```
|
|
70
|
+
const result:OINOApiResult = await api_orderdetails.doRequest("GET", id, body, params)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Write results back to HTTP Response
|
|
74
|
+
The results for a GET request will contain [`OINOModelSet`](https://pragmatta.github.io/oino-ts/classes/common_src.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.
|
|
75
|
+
```
|
|
76
|
+
return new Response(result.data.writeString(OINOContentType.json))
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
# MAIN FEATURES
|
|
81
|
+
|
|
82
|
+
## RESTfull
|
|
83
|
+
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.
|
|
84
|
+
|
|
85
|
+
For example HTTP POST
|
|
86
|
+
```
|
|
87
|
+
Request and response:
|
|
88
|
+
> curl.exe -X POST http://localhost:3001/orderdetails -H "Content-Type: application/json" --data '[{\"OrderID\":11077,\"ProductID\":99,\"UnitPrice\":19,\"Quantity\":1,\"Discount\":0}]'
|
|
89
|
+
{"success":true,"statusCode":200,"statusMessage":"OK","messages":[]}
|
|
90
|
+
|
|
91
|
+
SQL:
|
|
92
|
+
INSERT INTO [OrderDetails] ("OrderID","ProductID","UnitPrice","Quantity","Discount") VALUES (11077,99,19,1,0);
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
## Universal Serialization
|
|
97
|
+
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.
|
|
98
|
+
|
|
99
|
+
- Files can be sent to BLOB fields using BASE64 or MIME multipart encoding. Also supports standard HTML form file submission to blob fields and returning them data url images.
|
|
100
|
+
- Datetimes are (optionally) normalized to ISO 8601 format.
|
|
101
|
+
- Extended JSON-encoding
|
|
102
|
+
- 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).
|
|
103
|
+
- CSV
|
|
104
|
+
- Comma-separated, doublequotes.
|
|
105
|
+
- Unquoted literal `null` represents null values.
|
|
106
|
+
- Unquoted empty string represents undefined values.
|
|
107
|
+
- Form data
|
|
108
|
+
- Multipart-mixed and binary files not supported.
|
|
109
|
+
- Non-existent value line (i.e. nothing after the empty line) treated as a null value.
|
|
110
|
+
- Url-encoded
|
|
111
|
+
- No null values, missing properties treated as undefined.
|
|
112
|
+
- Multiple lines could be used to post multiple rows.
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
## Datasource Abstraction
|
|
116
|
+
OINO functions as a datasource abstraction for SQL, NoSQL and Blob storages, providing a consistent interface for working with different datasources. It abstracts out different conventions in connecting, making queries and formatting data.
|
|
117
|
+
|
|
118
|
+
Currently supported datasources:
|
|
119
|
+
- SQL
|
|
120
|
+
- Bun Sqlite through Bun native implementation
|
|
121
|
+
- Postgresql through [pg](https://www.npmjs.com/package/pg)-package
|
|
122
|
+
- Mariadb / Mysql-support through [mariadb](https://www.npmjs.com/package/mariadb)-package
|
|
123
|
+
- Sql Server through [mssql](https://www.npmjs.com/package/mssql)-package
|
|
124
|
+
- NoSQL
|
|
125
|
+
- AWS DynamoDb through [@aws-sdk/client-dynamodb](https://www.npmjs.com/package/@aws-sdk/client-dynamodb)-package
|
|
126
|
+
- Azure Tables through [@azure/data-tables](https://www.npmjs.com/package/@azure/data-tables)-package
|
|
127
|
+
- Blob
|
|
128
|
+
- AWS S3 through [@aws-sdk/client-s3](https://www.npmjs.com/package/@aws-sdk/client-s3)-package
|
|
129
|
+
- Azure Blobs through [@azure/storage-blob](https://www.npmjs.com/package/@azure/storage-blob)-package
|
|
130
|
+
|
|
131
|
+
## Composite Keys
|
|
132
|
+
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`.
|
|
133
|
+
|
|
134
|
+
## Power Of SQL
|
|
135
|
+
Since OINO is just generating SQL, WHERE-conditions can be defined with [`OINOQueryFilter`](https://pragmatta.github.io/oino-ts/classes/common_src.OINOQueryFilter.html), order with [`OINOQueryOrder`](https://pragmatta.github.io/oino-ts/classes/common_src.OINOQueryOrder.html), limits/paging with [`OINOQueryLimit`](https://pragmatta.github.io/oino-ts/classes/common_src.OINOQueryLimit.html) and aggregation with [`OINOQueryAggregate`](https://pragmatta.github.io/oino-ts/classes/common_src.OINOQueryAggregate.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.
|
|
136
|
+
|
|
137
|
+
Most of the filtering also works with NoSQL and Blob datasources but might less performant depending if the service supports it or if we result in software filtering the results.
|
|
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/db_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
|
+
### Batch updates
|
|
158
|
+
Batch updates slight bend the RESTfull principles but there are separate `doBatchUpdate` endpoints (e.g. [OINODbApi.dobatchupdate](https://pragmatta.github.io/oino-ts/classes/db_src.OINODbApi.html#dobatchupdate)).
|
|
159
|
+
|
|
160
|
+
## Schema Management
|
|
161
|
+
OINO has endpoints for reading, creating and deleting table and column schemas.
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
# STATUS
|
|
165
|
+
OINO v1.1 is the first release considered production status. Architecture has now survived introduction NoSQL and Blov datasources and we feel comfortable saying it's stable now. Also we have been using it in [oino.cloud](https://oino.cloud) for a while without issues.
|
|
166
|
+
|
|
167
|
+
## Roadmap
|
|
168
|
+
Major features that are considered in future releases
|
|
169
|
+
|
|
170
|
+
### Views
|
|
171
|
+
It would be interesting to combine multiple datasources as OINO-views like multiple NoSQL-tables.
|
|
172
|
+
|
|
173
|
+
### Streaming
|
|
174
|
+
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.
|
|
175
|
+
|
|
176
|
+
### SQL generation callbacks
|
|
177
|
+
It would be useful to allow developer to validate / override SQL generation to cover cases OINO does not support or even workaround issues.
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
# HELP
|
|
181
|
+
|
|
182
|
+
## Bug reports
|
|
183
|
+
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.
|
|
184
|
+
|
|
185
|
+
## Feedback
|
|
186
|
+
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.
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
# LINKS
|
|
190
|
+
- [Github repository](https://github.com/pragmatta/oino-ts)
|
|
191
|
+
- [NPM repository](https://www.npmjs.com/org/oino-ts)
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
# ACKNOWLEDGEMENTS
|
|
195
|
+
|
|
196
|
+
## Libraries
|
|
197
|
+
OINO uses the following open source libraries and npm packages and I would like to thank everyone for their contributions:
|
|
198
|
+
- Postgresql [node-postgres package](https://github.com/brianc/node-postgres)
|
|
199
|
+
- Mariadb / Mysql [mariadb package](https://github.com/mariadb-corporation/mariadb-connector-nodejs)
|
|
200
|
+
- Sql Server [mssql package](https://github.com/tediousjs/node-mssql)
|
|
201
|
+
- Custom base encoding [base-x package](https://github.com/cryptocoinjs/base-x)
|
|
202
|
+
- AWS JS SDK [aws-sdk](https://github.com/aws/aws-sdk-js-v3)
|
|
203
|
+
- Azure JS SDK [azure](https://github.com/Azure/azure-sdk-for-js)
|
|
204
|
+
|
|
205
|
+
## Bun
|
|
206
|
+
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.
|
|
207
|
+
|
|
208
|
+
## SQL Scripts
|
|
209
|
+
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).
|