@adobe/spacecat-shared-data-access 1.60.3 → 1.61.1

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.
Files changed (47) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/package.json +3 -3
  3. package/src/service/index.js +4 -3
  4. package/src/v2/errors/data-access.error.js +24 -0
  5. package/src/v2/errors/index.d.ts +5 -1
  6. package/src/v2/errors/index.js +11 -1
  7. package/src/v2/errors/reference.error.js +22 -0
  8. package/src/v2/{models/base/constants.js → errors/schema-validation.error.js} +4 -6
  9. package/src/v2/errors/schema.builder.error.js +27 -0
  10. package/src/v2/errors/schema.error.js +19 -0
  11. package/src/v2/errors/validation.error.js +3 -1
  12. package/src/v2/models/api-key/api-key.schema.js +2 -0
  13. package/src/v2/models/api-key/index.d.ts +15 -2
  14. package/src/v2/models/audit/audit.collection.js +25 -1
  15. package/src/v2/models/audit/audit.schema.js +3 -0
  16. package/src/v2/models/audit/index.d.ts +16 -11
  17. package/src/v2/models/base/base.collection.js +148 -85
  18. package/src/v2/models/base/base.model.js +73 -14
  19. package/src/v2/models/base/entity.registry.js +7 -2
  20. package/src/v2/models/base/index.d.ts +30 -11
  21. package/src/v2/models/base/reference.js +81 -28
  22. package/src/v2/models/base/schema.builder.js +96 -24
  23. package/src/v2/models/base/schema.js +78 -10
  24. package/src/v2/models/configuration/configuration.schema.js +2 -2
  25. package/src/v2/models/configuration/index.d.ts +24 -90
  26. package/src/v2/models/experiment/experiment.schema.js +12 -3
  27. package/src/v2/models/experiment/index.d.ts +11 -3
  28. package/src/v2/models/import-job/index.d.ts +10 -3
  29. package/src/v2/models/import-url/index.d.ts +6 -3
  30. package/src/v2/models/index.d.ts +3 -0
  31. package/src/v2/models/index.js +1 -0
  32. package/src/v2/models/key-event/index.d.ts +5 -1
  33. package/src/v2/models/latest-audit/index.d.ts +43 -0
  34. package/src/v2/models/latest-audit/index.js +19 -0
  35. package/src/v2/models/latest-audit/latest-audit.collection.js +32 -0
  36. package/src/v2/models/latest-audit/latest-audit.model.js +26 -0
  37. package/src/v2/models/latest-audit/latest-audit.schema.js +72 -0
  38. package/src/v2/models/opportunity/index.d.ts +17 -1
  39. package/src/v2/models/opportunity/opportunity.schema.js +1 -0
  40. package/src/v2/models/organization/index.d.ts +3 -1
  41. package/src/v2/models/site/index.d.ts +43 -6
  42. package/src/v2/models/site/site.model.js +0 -6
  43. package/src/v2/models/site/site.schema.js +2 -0
  44. package/src/v2/models/site-candidate/index.d.ts +5 -7
  45. package/src/v2/models/site-top-page/index.d.ts +16 -2
  46. package/src/v2/models/suggestion/index.d.ts +2 -0
  47. package/src/v2/util/patcher.js +11 -0
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ # [@adobe/spacecat-shared-data-access-v1.61.1](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.61.0...@adobe/spacecat-shared-data-access-v1.61.1) (2024-12-24)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * schema and db init ([#505](https://github.com/adobe/spacecat-shared/issues/505)) ([9b67b9a](https://github.com/adobe/spacecat-shared/commit/9b67b9a9f40f643c648cc5043018bfbc1d2a9c12))
7
+
8
+ # [@adobe/spacecat-shared-data-access-v1.61.0](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.60.3...@adobe/spacecat-shared-data-access-v1.61.0) (2024-12-23)
9
+
10
+
11
+ ### Features
12
+
13
+ * latest audit entity ([#503](https://github.com/adobe/spacecat-shared/issues/503)) ([2d01b09](https://github.com/adobe/spacecat-shared/commit/2d01b0969c0c046cdbffa480f8e40991e5abf91e))
14
+
1
15
  # [@adobe/spacecat-shared-data-access-v1.60.3](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.60.2...@adobe/spacecat-shared-data-access-v1.60.3) (2024-12-22)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/spacecat-shared-data-access",
3
- "version": "1.60.3",
3
+ "version": "1.61.1",
4
4
  "description": "Shared modules of the Spacecat Services - Data Access",
5
5
  "type": "module",
6
6
  "engines": {
@@ -34,8 +34,8 @@
34
34
  "access": "public"
35
35
  },
36
36
  "dependencies": {
37
- "@adobe/spacecat-shared-dynamo": "1.4.0",
38
- "@adobe/spacecat-shared-utils": "1.23.1",
37
+ "@adobe/spacecat-shared-dynamo": "1.4.2",
38
+ "@adobe/spacecat-shared-utils": "1.24.1",
39
39
  "@aws-sdk/client-dynamodb": "3.716.0",
40
40
  "@aws-sdk/lib-dynamodb": "3.716.0",
41
41
  "@types/joi": "17.2.3",
@@ -31,8 +31,8 @@ import { importUrlFunctions } from './import-url/index.js';
31
31
  import { experimentFunctions } from './experiments/index.js';
32
32
  import { apiKeyFunctions } from './api-key/index.js';
33
33
 
34
- const createRawClient = () => {
35
- const dbClient = AWSXray.captureAWSv3Client(new DynamoDB());
34
+ const createRawClient = (client = undefined) => {
35
+ const dbClient = client || AWSXray.captureAWSv3Client(new DynamoDB());
36
36
  return DynamoDBDocument.from(dbClient, {
37
37
  marshallOptions: {
38
38
  convertEmptyValues: true,
@@ -70,6 +70,7 @@ const createElectroService = (client, config, log) => {
70
70
  * tableNameSiteTopPages: string, indexNameAllOrganizations: string,
71
71
  * indexNameAllOrganizationsByImsOrgId: string, pkAllOrganizations: string}} config configuration
72
72
  * @param {Logger} log log
73
+ * @param client custom dynamo client
73
74
  * @returns {object} data access object
74
75
  */
75
76
  export const createDataAccess = (config, log = console, client = undefined) => {
@@ -88,7 +89,7 @@ export const createDataAccess = (config, log = console, client = undefined) => {
88
89
  const apiKeyFuncs = apiKeyFunctions(dynamoClient, config, log);
89
90
 
90
91
  // electro-based data access objects
91
- const rawClient = createRawClient();
92
+ const rawClient = createRawClient(client);
92
93
  const electroService = createElectroService(rawClient, config, log);
93
94
  const entityRegistry = new EntityRegistry(electroService, log);
94
95
  const collections = entityRegistry.getCollections();
@@ -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
+
13
+ export default class DataAccessError extends Error {
14
+ constructor(message, details = {}, cause = null) {
15
+ super(message);
16
+ this.name = this.constructor.name;
17
+ this.details = details;
18
+ this.cause = cause;
19
+
20
+ if (cause?.stack) {
21
+ this.stack += `\nCaused by: ${cause.stack}`;
22
+ }
23
+ }
24
+ }
@@ -10,4 +10,8 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- export type ValidationError = Error
13
+ export type DataAccessError = Error
14
+ export type SchemaBuilderError = DataAccessError
15
+ export type SchemaError = DataAccessError
16
+ export type SchemaValidationError = DataAccessError
17
+ export type ValidationError = DataAccessError
@@ -10,6 +10,16 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
+ import DataAccessError from './data-access.error.js';
14
+ import SchemaBuilderError from './schema.builder.error.js';
15
+ import SchemaError from './schema.error.js';
16
+ import SchemaValidationError from './schema-validation.error.js';
13
17
  import ValidationError from './validation.error.js';
14
18
 
15
- export { ValidationError };
19
+ export {
20
+ DataAccessError,
21
+ SchemaBuilderError,
22
+ SchemaError,
23
+ SchemaValidationError,
24
+ ValidationError,
25
+ };
@@ -0,0 +1,22 @@
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 DataAccessError from './data-access.error.js';
14
+
15
+ export default class ReferenceError extends DataAccessError {
16
+ constructor(reference, message, cause) {
17
+ const { type, target } = reference;
18
+ const prefix = type && target ? `[${type} -> ${target}] ` : '';
19
+
20
+ super(`${prefix}${message}`, { reference }, cause);
21
+ }
22
+ }
@@ -9,9 +9,7 @@
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
- export const INDEX_TYPES = {
13
- PRIMARY: 'primary',
14
- ALL: 'all',
15
- BELONGS_TO: 'belongs_to',
16
- OTHER: 'other',
17
- };
12
+
13
+ import DataAccessError from './data-access.error.js';
14
+
15
+ export default class SchemaValidationError extends DataAccessError {}
@@ -0,0 +1,27 @@
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 { hasText } from '@adobe/spacecat-shared-utils';
14
+
15
+ import DataAccessError from './data-access.error.js';
16
+
17
+ export default class SchemaBuilderError extends DataAccessError {
18
+ constructor(builder, message, cause) {
19
+ const { serviceName, entityName } = builder;
20
+ const prefix = hasText(serviceName) && hasText(entityName)
21
+ ? `[${serviceName} -> ${entityName}] `
22
+ : '';
23
+
24
+ super(`${prefix}${message}`, { builder }, cause);
25
+ this.name = this.constructor.name;
26
+ }
27
+ }
@@ -0,0 +1,19 @@
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 DataAccessError from './data-access.error.js';
14
+
15
+ export default class SchemaError extends DataAccessError {
16
+ constructor(schema, message, cause) {
17
+ super(`[${schema.getModelName()}] ${message}`, { schema }, cause);
18
+ }
19
+ }
@@ -10,4 +10,6 @@
10
10
  * governing permissions and limitations under the License.
11
11
  */
12
12
 
13
- export default class ValidationError extends Error {}
13
+ import DataAccessError from './data-access.error.js';
14
+
15
+ export default class ValidationError extends DataAccessError {}
@@ -31,9 +31,11 @@ const schema = new SchemaBuilder(ApiKey, ApiKeyCollection)
31
31
  })
32
32
  .addAttribute('imsUserId', {
33
33
  type: 'string',
34
+ default: 'default',
34
35
  })
35
36
  .addAttribute('imsOrgId', {
36
37
  type: 'string',
38
+ default: 'default',
37
39
  })
38
40
  .addAttribute('name', {
39
41
  type: 'string',
@@ -13,6 +13,7 @@
13
13
  import type { BaseCollection, BaseModel } from '../base';
14
14
 
15
15
  export interface ApiKey extends BaseModel {
16
+ getApiKeyId(): string;
16
17
  getDeletedAt(): string | undefined;
17
18
  getExpiresAt(): string | undefined;
18
19
  getHashedApiKey(): string;
@@ -32,6 +33,18 @@ export interface ApiKey extends BaseModel {
32
33
  }
33
34
 
34
35
  export interface ApiKeyCollection extends BaseCollection<ApiKey> {
35
- allByImsOrgIdAndImsUserId: (imsUserId: string, imsOrgId: string) => Promise<ApiKey[]>;
36
- findByHashedApiKey: (hashedApiKey: string) => Promise<ApiKey | null>;
36
+ allByHashedApiKey(hashedApiKey: string): Promise<ApiKey[]>;
37
+ allByHashedApiKeyAndUpdatedAt(hashedApiKey: string, updatedAt: string): Promise<ApiKey[]>;
38
+ allByImsOrgId(imsOrgId: string): Promise<ApiKey[]>;
39
+ allByImsOrgIdAndImsUserId(imsOrgId: string, imsUserId: string): Promise<ApiKey[]>;
40
+ allByImsOrgIdAndImsUserIdAndUpdatedAt(
41
+ imsOrgId: string, imsUserId: string, updatedAt: string
42
+ ): Promise<ApiKey[]>;
43
+ findByHashedApiKey(hashedApiKey: string): Promise<ApiKey | null>;
44
+ findByHashedApiKeyAndUpdatedAt(hashedApiKey: string, updatedAt: string): Promise<ApiKey | null>;
45
+ findByImsOrgId(imsOrgId: string): Promise<ApiKey | null>;
46
+ findByImsOrgIdAndImsUserId(imsOrgId: string, imsUserId: string): Promise<ApiKey | null>;
47
+ findByImsOrgIdAndImsUserIdAndUpdatedAt(
48
+ imsOrgId: string, imsUserId: string, updatedAt: string
49
+ ): Promise<ApiKey | null>;
37
50
  }
@@ -20,7 +20,31 @@ import BaseCollection from '../base/base.collection.js';
20
20
  * @extends BaseCollection
21
21
  */
22
22
  class AuditCollection extends BaseCollection {
23
- // add custom methods here
23
+ // create a copy of the audit as a LatestAudit entity
24
+ async _onCreate(item) {
25
+ const collection = this.entityRegistry.getCollection('LatestAuditCollection');
26
+ await collection.create(item.toJSON());
27
+ }
28
+
29
+ // of the created audits, find the latest per site and auditType
30
+ // and create a LatestAudit copy for each
31
+ async _onCreateMany(items) {
32
+ const collection = this.entityRegistry.getCollection('LatestAuditCollection');
33
+ const latestAudits = items.createdItems.reduce((acc, audit) => {
34
+ const siteId = audit.getSiteId();
35
+ const auditType = audit.getAuditType();
36
+ const auditedAt = audit.getAuditedAt();
37
+ const key = `${siteId}-${auditType}`;
38
+
39
+ if (!acc[key] || acc[key].getAuditedAt() < auditedAt) {
40
+ acc[key] = audit;
41
+ }
42
+
43
+ return acc;
44
+ }, {});
45
+
46
+ await collection.createMany(Object.values(latestAudits).map((audit) => audit.toJSON()));
47
+ }
24
48
  }
25
49
 
26
50
  export default AuditCollection;
@@ -26,7 +26,10 @@ Indexes Doc: https://electrodb.dev/en/modeling/indexes/
26
26
 
27
27
  const schema = new SchemaBuilder(Audit, AuditCollection)
28
28
  .addReference('belongs_to', 'Site', ['auditType', 'auditedAt'])
29
+ .addReference('has_one', 'LatestAudit', ['auditType'], { required: false })
29
30
  .addReference('has_many', 'Opportunities')
31
+ .allowUpdates(false)
32
+ .allowRemove(false)
30
33
  .addAttribute('auditResult', {
31
34
  type: 'any',
32
35
  required: true,
@@ -11,30 +11,35 @@
11
11
  */
12
12
 
13
13
  import type {
14
- BaseCollection, BaseModel, Opportunity, Site,
14
+ BaseCollection, BaseModel, LatestAudit, Opportunity, Site,
15
15
  } from '../index';
16
16
 
17
17
  export interface Audit extends BaseModel {
18
+ getAuditedAt(): number;
19
+ getAuditId(): string;
18
20
  getAuditResult(): object;
19
21
  getAuditType(): string;
20
- getAuditedAt(): number;
21
22
  getFullAuditRef(): string;
22
23
  getIsError(): boolean;
23
24
  getIsLive(): boolean;
25
+ getLatestAudit(): Promise<LatestAudit | null>;
26
+ getLatestAuditByAuditType(auditType: string): Promise<LatestAudit | null>;
24
27
  getOpportunities(): Promise<Opportunity[]>;
28
+ getOpportunitiesByUpdatedAt(updatedAt: string): Promise<Opportunity[]>;
29
+ getScores(): object | undefined;
25
30
  getSite(): Promise<Site>;
26
31
  getSiteId(): string;
27
- setAuditResult(auditResult: object): Audit;
28
- setAuditType(auditType: string): Audit;
29
- setAuditedAt(auditedAt: number): Audit;
30
- setFullAuditRef(fullAuditRef: string): Audit;
31
- setIsError(isError: boolean): Audit;
32
- setIsLive(isLive: boolean): Audit;
33
- setSiteId(siteId: string): Audit;
34
- toggleLive(): Audit;
35
32
  }
36
33
 
37
34
  export interface AuditCollection extends BaseCollection<Audit> {
38
35
  allBySiteId(siteId: string): Promise<Audit[]>;
39
- allBySiteAndType(siteId: string, auditType: string): Promise<Audit[]>;
36
+ allBySiteIdAndAuditType(siteId: string, auditType: string): Promise<Audit[]>;
37
+ allBySiteIdAndAuditTypeAndAuditedAt(
38
+ siteId: string, auditType: string, auditedAt: string
39
+ ): Promise<Audit[]>;
40
+ findBySiteId(siteId: string): Promise<Audit | null>;
41
+ findBySiteIdAndAuditType(siteId: string, auditType: string): Promise<Audit | null>;
42
+ findBySiteIdAndAuditTypeAndAuditedAt(
43
+ siteId: string, auditType: string, auditedAt: string
44
+ ): Promise<Audit | null>;
40
45
  }