@adobe/aio-lib-db 0.1.0-beta.5 → 0.1.0-beta.8

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
@@ -20,7 +20,7 @@ npm install @adobe/aio-lib-db
20
20
 
21
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.)
22
22
 
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.
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.
24
24
 
25
25
  ### Basic Usage
26
26
 
@@ -30,8 +30,12 @@ const libDb = require('@adobe/aio-lib-db');
30
30
  async function main() {
31
31
  let client;
32
32
  try {
33
- // initialize library in region is defined in app.config.yaml and defaults to amer
33
+ // initialize library with the default amer region or what is defined in AIO_DB_REGION
34
34
  const db = await libDb.init();
35
+
36
+ // initialize library with an explicit region
37
+ // const db = await libDb.init({region: "emea"});
38
+
35
39
  // connect to the database
36
40
  client = await db.connect();
37
41
 
@@ -53,7 +57,6 @@ async function main() {
53
57
  }
54
58
  }
55
59
  ```
56
-
57
60
  ---
58
61
 
59
62
  ## Collection Operations
@@ -272,6 +275,57 @@ const cursor = collection.aggregate()
272
275
 
273
276
  ## Advanced Features
274
277
 
278
+ ### Storage Statics
279
+
280
+ #### Individual database statistics:
281
+
282
+ ```javascript
283
+ // Get storage statistics for the database with the default scale factor (bytes)
284
+ const dbStats = client.dbStats()
285
+
286
+ // Get storage statistics for the database with a scale factor (e.g. KB)
287
+ const dbStatsKb = client.dbStats({ scale: 1024 })
288
+ ```
289
+ | field returned | description |
290
+ |----------------|-------------------------------------------------------------------------------------------------|
291
+ | collections | the number of collections |
292
+ | objects | the number of objects/documents |
293
+ | views | the number of views (not currently supported) |
294
+ | indexes | the number of indexes |
295
+ | dataSize | the actual amount of storage used (default bytes) |
296
+ | storageSize | space allocated for storage (default bytes) |
297
+ | indexSize | space allocated for indexes (default bytes) |
298
+ | ok | whether the request was successful |
299
+ | scaleFactor | the scale factor used for the size fields, ex: 1024 for kilobyte-scale (default is 1 for bytes) |
300
+ | lastUpdated | when the statistics were last updated |
301
+
302
+ #### Organization storage statistics:
303
+
304
+ ```javascript
305
+ // Get combined storage statistics across databases in the organization with the default scale factor (bytes)
306
+ const orgStats = client.orgStats()
307
+
308
+ // Get combined storage statistics across databases in the organization with a scale factor (e.g. MB)
309
+ const orgStatsMb = client.orgStats({ scale: 1024 * 1024 })
310
+ ```
311
+ | field returned | description |
312
+ |---------------------------|----------------------------------------------------------------------------------------------------------------------------|
313
+ | ok | whether the request was successful |
314
+ | databases | the number of databases in the organization |
315
+ | collections | the total number of collections across databases |
316
+ | dataSize | the total actual amount of storage used across databases (default bytes) |
317
+ | storageSize | space allocated for storage (default bytes) |
318
+ | indexSize | space allocated for indexes (default bytes) |
319
+ | scaleFactor | the scale factor used for the size fields, ex: 1024 for kilobyte-scale (default is 1 for bytes) |
320
+ | databaseStats | an array of statistics for individual databases in the organization |
321
+ | databaseStats.namespace | the runtime namespace the database corresponds to |
322
+ | databaseStats.dataSize | the actual amount of storage used by the database (default bytes) |
323
+ | databaseStats.storageSize | space allocated for storage for the database (default bytes) |
324
+ | databaseStats.indexSize | space allocated for indexes for the database (default bytes) |
325
+ | databaseStats.collections | the number of collections in the database |
326
+ | databaseStats.scaleFactor | the scale factor used for the size fields in the databaseStats array, ex: 1024 for kilobyte-scale (default is 1 for bytes) |
327
+ | databaseStats.lastUpdated | when the database statistics were last updated |
328
+
275
329
  ### Indexing
276
330
 
277
331
  ```javascript
@@ -353,12 +407,10 @@ const cursor = collection.find({ status: 'active' })
353
407
  ## Error Handling
354
408
 
355
409
  ```javascript
356
- const { DbError } = require('@adobe/aio-lib-db');
357
-
358
410
  try {
359
411
  await collection.insertOne({ email: 'invalid-email' });
360
412
  } catch (error) {
361
- if (error instanceof DbError) {
413
+ if (error.name == 'DbError') {
362
414
  console.error('Database error:', error.message);
363
415
  } else {
364
416
  console.error('Unexpected error:', error);
package/lib/DbBase.js CHANGED
@@ -81,7 +81,7 @@ class DbBase {
81
81
  * @param {Object=} config
82
82
  * @param {string=} config.namespace required here or as __OW_API_NAMESPACE in .env
83
83
  * @param {string=} config.apikey required here or as __OW_API_KEY in .env
84
- * @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'
85
85
  * @returns {Promise<DbBase>} a new DbBase instance
86
86
  * @memberof DbBase
87
87
  */
@@ -89,7 +89,7 @@ class DbBase {
89
89
  const namespace = config.namespace || process.env.__OW_NAMESPACE
90
90
  const apikey = config.apikey || process.env.__OW_API_KEY
91
91
  let aioAppRegion = null
92
-
92
+
93
93
  try {
94
94
  aioAppRegion = getRegionFromAppConfig(process.cwd())
95
95
  } catch (e) {
package/lib/DbClient.js CHANGED
@@ -9,7 +9,7 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
9
9
  OF ANY KIND, either express or implied. See the License for the specific language
10
10
  governing permissions and limitations under the License.
11
11
  */
12
- const { dbStatsApi, listCollectionsApi, createCollectionApi } = require('./api/client')
12
+ const { dbStatsApi, listCollectionsApi, createCollectionApi, orgStatsApi } = require('./api/client')
13
13
  const DbCollection = require('./DbCollection')
14
14
 
15
15
  class DbClient {
@@ -37,11 +37,23 @@ class DbClient {
37
37
  /**
38
38
  * Get the statistics for the scoped database
39
39
  *
40
+ * @param {Object=} options
41
+ * @returns {Promise<Object>}
42
+ * @throws {DbError}
43
+ */
44
+ async dbStats(options = {}) {
45
+ return dbStatsApi(this.db, options)
46
+ }
47
+
48
+ /**
49
+ * Gets the combined statistics for all databases that fall under the same organization as the scoped database
50
+ *
51
+ * @param {Object=} options
40
52
  * @returns {Promise<Object>}
41
53
  * @throws {DbError}
42
54
  */
43
- async dbStats() {
44
- return dbStatsApi(this.db)
55
+ async orgStats(options = {}) {
56
+ return orgStatsApi(this.db, options)
45
57
  }
46
58
 
47
59
  /**
package/lib/api/client.js CHANGED
@@ -43,11 +43,24 @@ async function getClientApi(db, endpoint) {
43
43
  * Gets the statistics for the scoped database
44
44
  *
45
45
  * @param {DbBase} db
46
+ * @param {Object=} options
47
+ * @returns {Promise<Object>}
48
+ * @throws {DbError}
49
+ */
50
+ async function dbStats(db, options = {}) {
51
+ return await postClientApi(db, 'dbStats', undefined, options)
52
+ }
53
+
54
+ /**
55
+ * Gets the combined statistics for all databases that fall under the same organization as the scoped database
56
+ *
57
+ * @param {DbBase} db
58
+ * @param {Object=} options
46
59
  * @returns {Promise<Object>}
47
60
  * @throws {DbError}
48
61
  */
49
- async function dbStats(db) {
50
- return await getClientApi(db, 'dbStats')
62
+ async function orgStats(db, options = {}) {
63
+ return await postClientApi(db, 'orgStats', undefined, options)
51
64
  }
52
65
 
53
66
  /**
@@ -90,6 +103,7 @@ async function close(db, axiosClient) {
90
103
 
91
104
  module.exports = {
92
105
  dbStatsApi: dbStats,
106
+ orgStatsApi: orgStats,
93
107
  closeApi: close,
94
108
  createCollectionApi: createCollection,
95
109
  listCollectionsApi: listCollections
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.5",
3
+ "version": "0.1.0-beta.8",
4
4
  "description": "An abstraction on top of Document DB storage",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,7 +26,7 @@
26
26
  "tough-cookie": "^5.1.2"
27
27
  },
28
28
  "devDependencies": {
29
- "jest": "^29.7.0",
29
+ "jest": "^30.2.0",
30
30
  "uuid": "^11.1.0"
31
31
  },
32
32
  "keywords": [