@adobe/spacecat-shared-data-access 2.109.0 → 3.0.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/src/index.js CHANGED
@@ -14,8 +14,6 @@ import { createDataAccess } from './service/index.js';
14
14
 
15
15
  export * from './service/index.js';
16
16
 
17
- const TABLE_NAME_DATA = 'spacecat-services-data';
18
-
19
17
  /**
20
18
  * Wrapper for data access layer
21
19
  * @param {function} fn - The function to wrap
@@ -25,7 +23,7 @@ export default function dataAccessWrapper(fn) {
25
23
  /**
26
24
  * Wrapper for data access layer. This wrapper will create a data access layer if it is not
27
25
  * already created. It requires the context to have a log object. It will also use the
28
- * DYNAMO_TABLE_NAME_DATA environment variable to create the data access layer.
26
+ * POSTGREST_URL environment variable to create the data access layer.
29
27
  * Optionally, it will use the ENV and AWS_REGION environment variables
30
28
  *
31
29
  * @param {object} request - The request object
@@ -37,18 +35,25 @@ export default function dataAccessWrapper(fn) {
37
35
  const { log } = context;
38
36
 
39
37
  const {
40
- DYNAMO_TABLE_NAME_DATA = TABLE_NAME_DATA,
38
+ POSTGREST_URL: postgrestUrl,
39
+ POSTGREST_SCHEMA: postgrestSchema,
40
+ POSTGREST_API_KEY: postgrestApiKey,
41
41
  S3_CONFIG_BUCKET: s3Bucket,
42
42
  AWS_REGION: region,
43
43
  S2S_ALLOWED_IMS_ORG_IDS: s2sAllowedImsOrgIdsRaw,
44
44
  } = context.env;
45
45
 
46
+ if (!postgrestUrl) {
47
+ throw new Error('POSTGREST_URL is required');
48
+ }
46
49
  const s2sAllowedImsOrgIds = s2sAllowedImsOrgIdsRaw
47
50
  ? s2sAllowedImsOrgIdsRaw.split(',').map((id) => id.trim()).filter(Boolean)
48
51
  : [];
49
52
 
50
53
  context.dataAccess = createDataAccess({
51
- tableNameData: DYNAMO_TABLE_NAME_DATA,
54
+ postgrestUrl,
55
+ postgrestSchema,
56
+ postgrestApiKey,
52
57
  s3Bucket,
53
58
  region,
54
59
  s2sAllowedImsOrgIds,
@@ -22,30 +22,16 @@ import BaseCollection from '../base/base.collection.js';
22
22
  class AuditCollection extends BaseCollection {
23
23
  static COLLECTION_NAME = 'AuditCollection';
24
24
 
25
- // create a copy of the audit as a LatestAudit entity
25
+ // LatestAudit is derived from audits in v3; no copy table writes.
26
+ // eslint-disable-next-line class-methods-use-this,no-unused-vars
26
27
  async _onCreate(item) {
27
- const collection = this.entityRegistry.getCollection('LatestAuditCollection');
28
- await collection.create(item.toJSON());
28
+ // no-op
29
29
  }
30
30
 
31
- // of the created audits, find the latest per site and auditType
32
- // and create a LatestAudit copy for each
31
+ // LatestAudit is derived from audits in v3; no copy table writes.
32
+ // eslint-disable-next-line class-methods-use-this,no-unused-vars
33
33
  async _onCreateMany(items) {
34
- const collection = this.entityRegistry.getCollection('LatestAuditCollection');
35
- const latestAudits = items.createdItems.reduce((acc, audit) => {
36
- const siteId = audit.getSiteId();
37
- const auditType = audit.getAuditType();
38
- const auditedAt = audit.getAuditedAt();
39
- const key = `${siteId}-${auditType}`;
40
-
41
- if (!acc[key] || acc[key].getAuditedAt() < auditedAt) {
42
- acc[key] = audit;
43
- }
44
-
45
- return acc;
46
- }, {});
47
-
48
- await collection.createMany(Object.values(latestAudits).map((audit) => audit.toJSON()));
34
+ // no-op
49
35
  }
50
36
  }
51
37