@oino-ts/blob 1.0.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.
@@ -0,0 +1,66 @@
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
+ OINODataModel,
9
+ OINOMemoryDataset,
10
+ OINODataRow,
11
+ } from "@oino-ts/common"
12
+ import { OINOBlobApi } from "./OINOBlobApi.js"
13
+ import { OINOBlobEntry } from "./OINOBlobConstants.js"
14
+
15
+ /**
16
+ * Static data model for blob listings.
17
+ *
18
+ * Fields are added by the blob implementation's `initializeApiDatamodel`
19
+ * method, so the exact set depends on what the storage backend supports.
20
+ * The canonical order is:
21
+ * 1. `name` – full blob name (primary key, string)
22
+ * 2. `etag` – entity tag (string)
23
+ * 3. `lastModified` – last modification timestamp (datetime)
24
+ * 4. `contentLength` – size in bytes (number)
25
+ * 5. `contentType` – MIME type (string) – omitted when not supported
26
+ */
27
+ export class OINOBlobDataModel extends OINODataModel {
28
+
29
+ /** Reference to the owning blob API */
30
+ readonly blobApi: OINOBlobApi
31
+
32
+ /**
33
+ * Constructor. Fields are added externally by the blob implementation
34
+ * via `initializeApiDatamodel`.
35
+ *
36
+ * @param api the `OINOBlobApi` that owns this data model
37
+ */
38
+ constructor(api: OINOBlobApi) {
39
+ super(api)
40
+ this.blobApi = api
41
+ }
42
+
43
+ /**
44
+ * Convert an array of blob entries into an in-memory dataset whose
45
+ * columns match the fields present in this model.
46
+ *
47
+ * @param entries blob entries from the storage backend
48
+ */
49
+ entriesToDataset(entries: OINOBlobEntry[]): OINOMemoryDataset {
50
+ const fieldNames = this.fields.map(f => f.name)
51
+ const rows: OINODataRow[] = entries.map(e => {
52
+ const row: OINODataRow = []
53
+ for (const name of fieldNames) {
54
+ switch (name) {
55
+ case "name": row.push(e.name); break
56
+ case "etag": row.push(e.etag); break
57
+ case "lastModified": row.push(e.lastModified); break
58
+ case "contentLength": row.push(e.contentLength); break
59
+ case "contentType": row.push(e.contentType); break
60
+ }
61
+ }
62
+ return row
63
+ })
64
+ return new OINOMemoryDataset(rows)
65
+ }
66
+ }
@@ -0,0 +1,80 @@
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 { OINOApiParams } from "@oino-ts/common"
8
+ import { OINOBlobParams, OINOBlobConstructor } from "./OINOBlobConstants.js"
9
+ import { OINOBlob } from "./OINOBlob.js"
10
+ import { OINOBlobApi } from "./OINOBlobApi.js"
11
+ import { OINOBlobDataModel } from "./OINOBlobDataModel.js"
12
+
13
+ /**
14
+ * Static factory for creating `OINOBlob` instances and `OINOBlobApi` instances
15
+ * from registered provider classes.
16
+ *
17
+ * Usage:
18
+ * ```ts
19
+ * OINOBlobFactory.registerBlob("OINOBlobAzureTable", OINOBlobAzureTable)
20
+ * const blob = await OINOBlobFactory.createBlob({ type: "OINOBlobAzureTable", ... })
21
+ * const api = await OINOBlobFactory.createApi(blob, { apiName: "files", tableName: "uploads/" })
22
+ * ```
23
+ */
24
+ export class OINOBlobFactory {
25
+ private static _registry: Record<string, OINOBlobConstructor> = {}
26
+
27
+ /**
28
+ * Register a blob provider class under the given name.
29
+ *
30
+ * @param name name used in `OINOBlobParams.type`
31
+ * @param blobClass constructor of the provider
32
+ */
33
+ static registerBlob(name: string, blobClass: OINOBlobConstructor): void {
34
+ this._registry[name] = blobClass
35
+ }
36
+
37
+ /**
38
+ * Create and optionally connect/validate a blob backend from params.
39
+ *
40
+ * @param params connection parameters
41
+ * @param connect if true, calls `connect()` on the backend
42
+ * @param validate if true, calls `validate()` on the backend
43
+ */
44
+ static async createBlob(
45
+ params: OINOBlobParams,
46
+ connect: boolean = true,
47
+ validate: boolean = true
48
+ ): Promise<OINOBlob> {
49
+ const BlobClass = this._registry[params.type]
50
+ if (!BlobClass) {
51
+ throw new Error("Unsupported blob type: " + params.type)
52
+ }
53
+ const blob: OINOBlob = new BlobClass(params)
54
+ if (connect) {
55
+ const connect_res = await blob.connect()
56
+ if (!connect_res.success) {
57
+ throw new Error("Blob connection failed: " + connect_res.statusText)
58
+ }
59
+ }
60
+ if (validate) {
61
+ const validate_res = await blob.validate()
62
+ if (!validate_res.success) {
63
+ throw new Error("Blob validation failed: " + validate_res.statusText)
64
+ }
65
+ }
66
+ return blob
67
+ }
68
+
69
+ /**
70
+ * Create an `OINOBlobApi` and initialise its data model.
71
+ *
72
+ * @param blob blob backend to use
73
+ * @param params API parameters (`tableName` is used as the blob prefix)
74
+ */
75
+ static async createApi(blob: OINOBlob, params: OINOApiParams): Promise<OINOBlobApi> {
76
+ const api = new OINOBlobApi(blob, params)
77
+ await blob.initializeApiDatamodel(api)
78
+ return api
79
+ }
80
+ }
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export { OINOBlob } from "./OINOBlob.js"
2
+ export { OINOBlobDataModel } from "./OINOBlobDataModel.js"
3
+ export { OINOBlobFactory } from "./OINOBlobFactory.js"
4
+ export { OINOBlobApi, OINOBlobApiResult } from "./OINOBlobApi.js"
5
+ export { type OINOBlobConstructor, type OINOBlobParams, type OINOBlobEntry, type OINOBlobFetchResult } from "./OINOBlobConstants.js"