@adobe/aio-lib-db 0.1.0-beta.3 → 0.1.0-beta.5
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 +11 -30
- package/lib/DbBase.js +3 -2
- package/package.json +1 -1
- package/utils/ejsonHandler.js +4 -0
package/README.md
CHANGED
|
@@ -12,49 +12,28 @@ Install `aio-lib-db` from npm:
|
|
|
12
12
|
npm install @adobe/aio-lib-db
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
Or add it to your `package.json`:
|
|
16
|
-
|
|
17
|
-
```json
|
|
18
|
-
{
|
|
19
|
-
"dependencies": {
|
|
20
|
-
"@adobe/aio-lib-db": "^0.1.0"
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
```
|
|
24
|
-
|
|
25
15
|
---
|
|
26
16
|
|
|
27
17
|
## Quick Start
|
|
28
18
|
|
|
29
19
|
### Setup
|
|
30
20
|
|
|
31
|
-
|
|
21
|
+
**aio-lib-db** is intended to be used by AIO Runtime Actions and the DB Plugin for the AIO CLI, and these are always executed within a specific runtime namespace. Before use, a Workspace Database must be provisioned. (See [Provisioning a Workspace Database](https://developer.adobe.com/app-builder/docs/guides/app_builder_guides/storage/database#provisioning-a-workspace-database) in the [Getting Started with Database Storage](https://developer.adobe.com/app-builder/docs/guides/app_builder_guides/storage/database) guide for details.)
|
|
32
22
|
|
|
33
|
-
|
|
34
|
-
__OW_NAMESPACE=your_namespace
|
|
35
|
-
__OW_API_KEY=user:password
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
> To find runtime namespace and credentials, click "Download all" in the Adobe Developer Console for your project workspace and the values will be under `project.workspace.details.runtime.namespaces`.
|
|
23
|
+
**aio-lib-db** must always be initialized in a specific region. This region is normally defined in the `app.config.yaml` application manifest. If it is not, it will default to the `amer` region. Another option is to pass `{ region: '<region>' }` to the `libDb.init()` method to override the default.
|
|
39
24
|
|
|
40
25
|
### Basic Usage
|
|
41
26
|
|
|
42
|
-
> When calling `libDb.init()`, you can pass `{ region: '<region>>' }` to specify the region where your database is provisioned, or if region is defined in `app.config.yaml` of aio app, then `libDb.init()` will initialize in specified region.
|
|
43
|
-
>
|
|
44
|
-
> **Note:** region defined in `app.config.yaml` holds preference over passed in config param.
|
|
45
|
-
>
|
|
46
|
-
> If region is not specified in any of the above ways, it falls back to default.
|
|
47
|
-
>
|
|
48
|
-
> Valid regions are `amer` (default), `emea`, and `apac`.
|
|
49
|
-
|
|
50
27
|
```javascript
|
|
51
28
|
const libDb = require('@adobe/aio-lib-db');
|
|
52
29
|
|
|
53
30
|
async function main() {
|
|
31
|
+
let client;
|
|
54
32
|
try {
|
|
55
|
-
//
|
|
56
|
-
const db = await libDb.init(
|
|
57
|
-
|
|
33
|
+
// initialize library in region is defined in app.config.yaml and defaults to amer
|
|
34
|
+
const db = await libDb.init();
|
|
35
|
+
// connect to the database
|
|
36
|
+
client = await db.connect();
|
|
58
37
|
|
|
59
38
|
// Get a collection
|
|
60
39
|
const users = client.collection('users');
|
|
@@ -67,8 +46,10 @@ async function main() {
|
|
|
67
46
|
const results = await cursor.toArray();
|
|
68
47
|
}
|
|
69
48
|
finally {
|
|
70
|
-
|
|
71
|
-
|
|
49
|
+
if (client) {
|
|
50
|
+
// Close any open cursors when the application is done
|
|
51
|
+
await client.close();
|
|
52
|
+
}
|
|
72
53
|
}
|
|
73
54
|
}
|
|
74
55
|
```
|
package/lib/DbBase.js
CHANGED
|
@@ -58,8 +58,9 @@ class DbBase {
|
|
|
58
58
|
// Stage environment does not have a separate runtime endpoint
|
|
59
59
|
serviceUrl = STAGE_ENDPOINT
|
|
60
60
|
}
|
|
61
|
-
else if (process.env.__OW_ACTIVATION_ID) {
|
|
62
|
-
// If __OW_ACTIVATION_ID is set, the sdk is being used from inside a runtime action
|
|
61
|
+
else if (process.env.__OW_ACTIVATION_ID && !process.env.AIO_DEV) {
|
|
62
|
+
// If __OW_ACTIVATION_ID is set, the sdk is being used from inside a runtime action and should use the internal
|
|
63
|
+
// endpoint unless it's being executed by "aio app dev" (AIO_DEV is set)
|
|
63
64
|
serviceUrl = PROD_ENDPOINT_RUNTIME
|
|
64
65
|
}
|
|
65
66
|
else {
|
package/package.json
CHANGED
package/utils/ejsonHandler.js
CHANGED
|
@@ -35,6 +35,10 @@ function transformBsonPrimitives(obj) {
|
|
|
35
35
|
})
|
|
36
36
|
}
|
|
37
37
|
else if (typeof obj === 'object') {
|
|
38
|
+
// If the type was recognized by the parser, any properties should also have been handled so return it as-is
|
|
39
|
+
const objName = obj?.constructor?.name
|
|
40
|
+
if (objName && objName !== 'Object') return obj
|
|
41
|
+
|
|
38
42
|
const mapped = {}
|
|
39
43
|
Object.keys(obj).forEach((key) => {
|
|
40
44
|
mapped[key] = transformBsonPrimitives(obj[key])
|