@adobe/spacecat-shared-data-access 1.15.6 → 1.16.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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [@adobe/spacecat-shared-data-access-v1.16.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.15.6...@adobe/spacecat-shared-data-access-v1.16.0) (2024-02-14)
2
+
3
+
4
+ ### Features
5
+
6
+ * add configurations data access ([65bdc25](https://github.com/adobe/spacecat-shared/commit/65bdc2581e150cb32eb51d4d2bf03c8172ec2247))
7
+
1
8
  # [@adobe/spacecat-shared-data-access-v1.15.6](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.15.5...@adobe/spacecat-shared-data-access-v1.15.6) (2024-02-12)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-data-access",
3
- "version": "1.15.6",
3
+ "version": "1.16.0",
4
4
  "description": "Shared modules of the Spacecat Services - Data Access",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -0,0 +1,33 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import { createConfiguration } from '../models/configuration.js';
14
+
15
+ /**
16
+ * Data transfer object for Configuration.
17
+ */
18
+ export const ConfigurationDto = {
19
+ /**
20
+ * Converts a DynamoDB item into a Configuration object.
21
+ * @param {object } dynamoItem - DynamoDB item.
22
+ * @returns {Readonly<Configuration>} Configuration object.
23
+ */
24
+ fromDynamoItem: (dynamoItem) => {
25
+ const configurationData = {
26
+ version: dynamoItem.version,
27
+ queues: dynamoItem.queues,
28
+ jobs: dynamoItem.jobs,
29
+ };
30
+
31
+ return createConfiguration(configurationData);
32
+ },
33
+ };
package/src/index.d.ts CHANGED
@@ -315,6 +315,27 @@ export interface Organization {
315
315
  getConfig: () => Config;
316
316
  }
317
317
 
318
+ export interface Configuration {
319
+ /**
320
+ * Retrieves the configuration version.
321
+ * @returns {string} The configuration version.
322
+ */
323
+ getVersion: () => string;
324
+
325
+ /**
326
+ * Retrieves the queues configuration.
327
+ * @returns {object} The queues configuration.
328
+ */
329
+ getQueues: () => object;
330
+
331
+ /**
332
+ * Retrieves the jobs configuration.
333
+ * @returns {Array} The jobs configurations.
334
+ */
335
+ getJobs: () => Array<object>;
336
+
337
+ }
338
+
318
339
  export interface DataAccess {
319
340
  getAuditForSite: (
320
341
  sitedId: string,
@@ -407,6 +428,10 @@ export interface DataAccess {
407
428
  upsertSiteCandidate: (siteCandidateDate: object) => Promise<SiteCandidate>;
408
429
  siteCandidateExists: (baseURL: string) => Promise<boolean>;
409
430
  updateSiteCandidate: (siteCandidate: SiteCandidate) => Promise<SiteCandidate>;
431
+
432
+ // configuration functions
433
+ getConfiguration: () => Promise<Configuration>
434
+ getConfigurationByVersion: (version: string) => Promise<Configuration>
410
435
  }
411
436
 
412
437
  interface DataAccessConfig {
@@ -415,6 +440,7 @@ interface DataAccessConfig {
415
440
  tableNameOrganizations: string,
416
441
  tableNameSites: string;
417
442
  tableNameSiteCandidates: string;
443
+ tableNameConfigurations: string;
418
444
  indexNameAllSites: string;
419
445
  indexNameAllSitesOrganizations: string,
420
446
  indexNameAllSitesByDeliveryType: string;
@@ -423,6 +449,7 @@ interface DataAccessConfig {
423
449
  pkAllSites: string;
424
450
  pkAllLatestAudits: string;
425
451
  pkAllOrganizations: string;
452
+ pkAllConfigurations: string;
426
453
  }
427
454
 
428
455
  export function createDataAccess(
package/src/index.js CHANGED
@@ -17,6 +17,7 @@ const TABLE_NAME_LATEST_AUDITS = 'spacecat-services-latest-audits-dev';
17
17
  const TABLE_NAME_SITES = 'spacecat-services-sites-dev';
18
18
  const TABLE_NAME_SITE_CANDIDATES = 'spacecat-services-site-candidates-dev';
19
19
  const TABLE_NAME_ORGANIZATIONS = 'spacecat-services-organizations-dev';
20
+ const TABLE_NAME_CONFIGURATIONS = 'spacecat-services-configurations-dev';
20
21
 
21
22
  const INDEX_NAME_ALL_SITES = 'spacecat-services-all-sites-dev';
22
23
  const INDEX_NAME_ALL_ORGANIZATIONS = 'spacecat-services-all-organizations-dev';
@@ -25,6 +26,7 @@ const INDEX_NAME_ALL_LATEST_AUDIT_SCORES = 'spacecat-services-all-latest-audit-s
25
26
  const INDEX_NAME_ALL_SITES_ORGANIZATIONS = 'spacecat-services-all-sites-organizations-dev';
26
27
 
27
28
  const PK_ALL_SITES = 'ALL_SITES';
29
+ const PK_ALL_CONFIGURATIONS = 'ALL_CONFIGURATIONS';
28
30
  const PK_ALL_ORGANIZATIONS = 'ALL_ORGANIZATIONS';
29
31
  const PK_ALL_LATEST_AUDITS = 'ALL_LATEST_AUDITS';
30
32
 
@@ -39,6 +41,7 @@ export default function dataAccessWrapper(fn) {
39
41
  DYNAMO_TABLE_NAME_SITES = TABLE_NAME_SITES,
40
42
  DYNAMO_TABLE_NAME_SITE_CANDIDATES = TABLE_NAME_SITE_CANDIDATES,
41
43
  DYNAMO_TABLE_NAME_ORGANIZATIONS = TABLE_NAME_ORGANIZATIONS,
44
+ DYNAMO_TABLE_NAME_CONFIGURATIONS = TABLE_NAME_CONFIGURATIONS,
42
45
  DYNAMO_INDEX_NAME_ALL_SITES = INDEX_NAME_ALL_SITES,
43
46
  DYNAMO_INDEX_NAME_ALL_SITES_BY_DELIVERY_TYPE = INDEX_NAME_ALL_SITES_BY_DELIVERY_TYPE,
44
47
  DYNAMO_INDEX_NAME_ALL_LATEST_AUDIT_SCORES = INDEX_NAME_ALL_LATEST_AUDIT_SCORES,
@@ -52,6 +55,7 @@ export default function dataAccessWrapper(fn) {
52
55
  tableNameOrganizations: DYNAMO_TABLE_NAME_ORGANIZATIONS,
53
56
  tableNameSites: DYNAMO_TABLE_NAME_SITES,
54
57
  tableNameSiteCandidates: DYNAMO_TABLE_NAME_SITE_CANDIDATES,
58
+ tableNameConfigurations: DYNAMO_TABLE_NAME_CONFIGURATIONS,
55
59
  indexNameAllSites: DYNAMO_INDEX_NAME_ALL_SITES,
56
60
  indexNameAllOrganizations: DYNAMO_INDEX_NAME_ALL_ORGANIZATIONS,
57
61
  indexNameAllSitesByDeliveryType: DYNAMO_INDEX_NAME_ALL_SITES_BY_DELIVERY_TYPE,
@@ -60,6 +64,7 @@ export default function dataAccessWrapper(fn) {
60
64
  pkAllSites: PK_ALL_SITES,
61
65
  pkAllOrganizations: PK_ALL_ORGANIZATIONS,
62
66
  pkAllLatestAudits: PK_ALL_LATEST_AUDITS,
67
+ pkAllConfigurations: PK_ALL_CONFIGURATIONS,
63
68
  }, log);
64
69
  }
65
70
 
@@ -0,0 +1,49 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import Joi from 'joi';
14
+
15
+ const Configuration = (data = {}) => {
16
+ const self = { ...data };
17
+ self.getJobs = () => self.jobs;
18
+ self.getVersion = () => self.version;
19
+ self.getQueues = () => self.queues;
20
+
21
+ return Object.freeze(self);
22
+ };
23
+
24
+ export const checkConfiguration = (configuration) => {
25
+ const schema = Joi.object({
26
+ version: Joi.string().required(),
27
+ queues: Joi.object().required(),
28
+ jobs: Joi.array().required(),
29
+ }).unknown(true);
30
+ const { error, value } = schema.validate(configuration);
31
+
32
+ if (error) {
33
+ throw new Error(`Configuration validation error: ${error.message}`);
34
+ }
35
+
36
+ return value; // Validated and sanitized configuration
37
+ };
38
+
39
+ /**
40
+ * Creates a new Configuration.
41
+ *
42
+ * @param {object} data - configuration data
43
+ * @returns {Readonly<Configuration>} configuration - new configuration
44
+ */
45
+ export const createConfiguration = (data) => {
46
+ const value = checkConfiguration(data);
47
+ const newState = { ...value };
48
+ return Configuration(newState);
49
+ };
@@ -0,0 +1,63 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+
13
+ import { isObject } from '@adobe/spacecat-shared-utils';
14
+
15
+ import { ConfigurationDto } from '../../dto/configuration.js';
16
+
17
+ /**
18
+ * Retrieves configuration with latest version.
19
+ *
20
+ * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
21
+ * @param {DataAccessConfig} config - The data access config.
22
+ * @returns {Promise<Readonly<Configuration>>} A promise that resolves to the configuration
23
+ * object if found, otherwise null.
24
+ */
25
+ export const getConfiguration = async (
26
+ dynamoClient,
27
+ config,
28
+ ) => {
29
+ const dynamoItems = await dynamoClient.query({
30
+ TableName: config.tableNameConfigurations,
31
+ KeyConditionExpression: 'PK = :pk',
32
+ ExpressionAttributeValues: {
33
+ ':pk': config.pkAllConfigurations,
34
+ },
35
+ Limit: 1,
36
+ ScanIndexForward: false, // Sorts ascending if true, descending if false
37
+ });
38
+
39
+ if (dynamoItems.length === 0) return null;
40
+ return ConfigurationDto.fromDynamoItem(dynamoItems[0]);
41
+ };
42
+
43
+ /**
44
+ * Retrieves a site by its version.
45
+ *
46
+ * @param {DynamoDbClient} dynamoClient - The DynamoDB client.
47
+ * @param {DataAccessConfig} config - The data access config.
48
+ * @param {string} version - The version of the configuration to retrieve.
49
+ * @returns {Promise<Readonly<Configuration>|null>} A promise that resolves to the configuration
50
+ * object if found, otherwise null.
51
+ */
52
+ export const getConfigurationByVersion = async (
53
+ dynamoClient,
54
+ config,
55
+ version,
56
+ ) => {
57
+ const dynamoItem = await dynamoClient.getItem(config.tableNameConfigurations, {
58
+ PK: config.pkAllConfigurations,
59
+ version,
60
+ });
61
+
62
+ return isObject(dynamoItem) ? ConfigurationDto.fromDynamoItem(dynamoItem) : null;
63
+ };
@@ -0,0 +1,24 @@
1
+ /*
2
+ * Copyright 2024 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import { getConfiguration, getConfigurationByVersion } from './accessPatterns.js';
13
+
14
+ export const configurationFunctions = (dynamoClient, config) => ({
15
+ getConfiguration: () => getConfiguration(
16
+ dynamoClient,
17
+ config,
18
+ ),
19
+ getConfigurationByVersion: (version) => getConfigurationByVersion(
20
+ dynamoClient,
21
+ config,
22
+ version,
23
+ ),
24
+ });
@@ -15,6 +15,7 @@ import { auditFunctions } from './audits/index.js';
15
15
  import { siteFunctions } from './sites/index.js';
16
16
  import { siteCandidateFunctions } from './site-candidates/index.js';
17
17
  import { organizationFunctions } from './organizations/index.js';
18
+ import { configurationFunctions } from './configurations/index.js';
18
19
 
19
20
  /**
20
21
  * Creates a data access object.
@@ -33,11 +34,13 @@ export const createDataAccess = (config, log = console) => {
33
34
  const siteFuncs = siteFunctions(dynamoClient, config, log);
34
35
  const siteCandidateFuncs = siteCandidateFunctions(dynamoClient, config, log);
35
36
  const organizationFuncs = organizationFunctions(dynamoClient, config, log);
37
+ const configurationFuncs = configurationFunctions(dynamoClient, config);
36
38
 
37
39
  return {
38
40
  ...auditFuncs,
39
41
  ...siteFuncs,
40
42
  ...siteCandidateFuncs,
41
43
  ...organizationFuncs,
44
+ ...configurationFuncs,
42
45
  };
43
46
  };