@adobe/aio-lib-db 0.1.0-beta.4 → 0.1.0-beta.6

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 CHANGED
@@ -12,49 +12,32 @@ 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
- First, set your credentials in your `.env` file:
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
- ```env
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 be initialized in the region the workspace database was provisioned. Otherwise, the connection will fail. To explicitly initialize the library in a specific region, pass the `{region: "<region>"}` argument to the `libDb.init()` method. Called with no arguments, `libDb.init()` will initialize the library either in the default `amer` region or in the region defined in the `AIO_DB_REGION` environment variable.
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
- // Initialize and connect
56
- const db = await libDb.init({ region: 'amer' });
57
- const client = await db.connect();
33
+ // initialize library with the default amer region or what is defined in AIO_DB_REGION
34
+ const db = await libDb.init();
35
+
36
+ // initialize library with an explicit region
37
+ // const db = await libDb.init({region: "emea"});
38
+
39
+ // connect to the database
40
+ client = await db.connect();
58
41
 
59
42
  // Get a collection
60
43
  const users = client.collection('users');
@@ -67,12 +50,13 @@ async function main() {
67
50
  const results = await cursor.toArray();
68
51
  }
69
52
  finally {
70
- // Close any open cursors when the application is done
71
- await client.close();
53
+ if (client) {
54
+ // Close any open cursors when the application is done
55
+ await client.close();
56
+ }
72
57
  }
73
58
  }
74
59
  ```
75
-
76
60
  ---
77
61
 
78
62
  ## Collection Operations
@@ -291,6 +275,24 @@ const cursor = collection.aggregate()
291
275
 
292
276
  ## Advanced Features
293
277
 
278
+ ### Storage Statics
279
+
280
+ ```javascript
281
+ // Get storage statistics for the database
282
+ const dbStats = client.dbStats()
283
+ ```
284
+ | field returned | description |
285
+ |----------------|-----------------------------------------------|
286
+ | collections | the number of collections |
287
+ | objects | the number of objects/documents |
288
+ | views | the number of views (not currently supported) |
289
+ | indexes | the number of indexes |
290
+ | dataSize | the actual amount of storage used in bytes |
291
+ | storageSize | space allocated for storage in bytes |
292
+ | indexSize | space allocated for indexes in bytes |
293
+ | ok | whether the request was successful |
294
+ | lastUpdated | when the statistics where last updated |
295
+
294
296
  ### Indexing
295
297
 
296
298
  ```javascript
@@ -372,12 +374,10 @@ const cursor = collection.find({ status: 'active' })
372
374
  ## Error Handling
373
375
 
374
376
  ```javascript
375
- const { DbError } = require('@adobe/aio-lib-db');
376
-
377
377
  try {
378
378
  await collection.insertOne({ email: 'invalid-email' });
379
379
  } catch (error) {
380
- if (error instanceof DbError) {
380
+ if (error.name == 'DbError') {
381
381
  console.error('Database error:', error.message);
382
382
  } else {
383
383
  console.error('Unexpected error:', error);
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 {
@@ -80,7 +81,7 @@ class DbBase {
80
81
  * @param {Object=} config
81
82
  * @param {string=} config.namespace required here or as __OW_API_NAMESPACE in .env
82
83
  * @param {string=} config.apikey required here or as __OW_API_KEY in .env
83
- * @param {string=} config.region optional, default is 'amer'. Allowed prod values are 'amer', 'emea', 'apac'
84
+ * @param {string=} config.region optional, default is 'amer'. Allowed prod values are 'amer', 'emea', 'apac', 'aus'
84
85
  * @returns {Promise<DbBase>} a new DbBase instance
85
86
  * @memberof DbBase
86
87
  */
@@ -88,7 +89,7 @@ class DbBase {
88
89
  const namespace = config.namespace || process.env.__OW_NAMESPACE
89
90
  const apikey = config.apikey || process.env.__OW_API_KEY
90
91
  let aioAppRegion = null
91
-
92
+
92
93
  try {
93
94
  aioAppRegion = getRegionFromAppConfig(process.cwd())
94
95
  } catch (e) {
package/lib/constants.js CHANGED
@@ -21,7 +21,7 @@ const STAGE_ENV = 'stage'
21
21
 
22
22
  const ALLOWED_REGIONS = {
23
23
  // First region in an environment's array is the default
24
- [PROD_ENV]: ['amer', 'emea', 'apac'],
24
+ [PROD_ENV]: ['amer', 'emea', 'apac', 'aus'],
25
25
  [STAGE_ENV]: ['amer', 'amer2']
26
26
  }
27
27
 
package/lib/init.js CHANGED
@@ -21,7 +21,7 @@ require('dotenv/config')
21
21
  * `__OW_NAMESPACE` and `__OW_API_KEY`.
22
22
  *
23
23
  * @param {Object=} [config] used to init the sdk
24
- * @param {('amer'|'apac'|'emea')=} [config.region] optional region to use, default: `amer`
24
+ * @param {('amer'|'apac'|'emea'|'aus')=} [config.region] optional region to use, default: `amer`
25
25
  * @param {Object=} [config.ow] Set those if you want to use ootb credentials to access the database service
26
26
  * @param {string=} [config.ow.namespace]
27
27
  * @param {string=} [config.ow.auth]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/aio-lib-db",
3
- "version": "0.1.0-beta.4",
3
+ "version": "0.1.0-beta.6",
4
4
  "description": "An abstraction on top of Document DB storage",
5
5
  "main": "index.js",
6
6
  "scripts": {