@adobe/spacecat-shared-data-access 1.61.16 → 1.61.17
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 +7 -0
- package/package.json +1 -1
- package/src/v2/models/base/base.collection.js +7 -2
- package/src/v2/models/base/schema.builder.js +23 -0
- package/src/v2/models/latest-audit/latest-audit.collection.js +10 -0
- package/src/v2/models/latest-audit/latest-audit.schema.js +1 -1
- package/src/v2/util/accessor.utils.js +5 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [@adobe/spacecat-shared-data-access-v1.61.17](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.61.16...@adobe/spacecat-shared-data-access-v1.61.17) (2025-01-07)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* update latest audit and allByIndexQuery ([#528](https://github.com/adobe/spacecat-shared/issues/528)) ([ff2c5c9](https://github.com/adobe/spacecat-shared/commit/ff2c5c9bfeae1d38c505653818184e5a040bb09c))
|
|
7
|
+
|
|
1
8
|
# [@adobe/spacecat-shared-data-access-v1.61.16](https://github.com/adobe/spacecat-shared/compare/@adobe/spacecat-shared-data-access-v1.61.15...@adobe/spacecat-shared-data-access-v1.61.16) (2025-01-06)
|
|
2
9
|
|
|
3
10
|
|
package/package.json
CHANGED
|
@@ -361,11 +361,13 @@ class BaseCollection {
|
|
|
361
361
|
* the entity.
|
|
362
362
|
* @async
|
|
363
363
|
* @param {Object} item - The data for the entity to be created.
|
|
364
|
+
* @param {Object} [options] - Additional options for the creation process.
|
|
365
|
+
* @param {boolean} [options.upsert=false] - Whether to perform an upsert operation.
|
|
364
366
|
* @returns {Promise<BaseModel>} - A promise that resolves to the created model instance.
|
|
365
367
|
* @throws {DataAccessError} - Throws an error if the data is invalid or if the
|
|
366
368
|
* creation process fails.
|
|
367
369
|
*/
|
|
368
|
-
async create(item) {
|
|
370
|
+
async create(item, { upsert = false } = {}) {
|
|
369
371
|
if (!isNonEmptyObject(item)) {
|
|
370
372
|
const message = `Failed to create [${this.entityName}]: data is required`;
|
|
371
373
|
this.log.error(message);
|
|
@@ -373,7 +375,10 @@ class BaseCollection {
|
|
|
373
375
|
}
|
|
374
376
|
|
|
375
377
|
try {
|
|
376
|
-
const record =
|
|
378
|
+
const record = upsert
|
|
379
|
+
? await this.entity.put(item).go()
|
|
380
|
+
: await this.entity.create(item).go();
|
|
381
|
+
|
|
377
382
|
const instance = this.#createInstance(record.data);
|
|
378
383
|
|
|
379
384
|
this.#invalidateCache();
|
|
@@ -209,6 +209,29 @@ class SchemaBuilder {
|
|
|
209
209
|
return this;
|
|
210
210
|
}
|
|
211
211
|
|
|
212
|
+
/**
|
|
213
|
+
* By default createdAt and updatedAt are readOnly. This method allows
|
|
214
|
+
* to disable this behavior and allow upserts.
|
|
215
|
+
*
|
|
216
|
+
* @param {boolean} allow - Whether to allow upserts.
|
|
217
|
+
* @returns {SchemaBuilder}
|
|
218
|
+
*/
|
|
219
|
+
withUpsertable(allow) {
|
|
220
|
+
if (!isBoolean(allow)) {
|
|
221
|
+
throw new SchemaBuilderError(this, 'allow must be a boolean.');
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (allow) {
|
|
225
|
+
this.addAttribute('createdAt', {
|
|
226
|
+
type: 'string',
|
|
227
|
+
required: true,
|
|
228
|
+
default: () => new Date().toISOString(),
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return this;
|
|
233
|
+
}
|
|
234
|
+
|
|
212
235
|
/**
|
|
213
236
|
* By default a schema allows removes. This method allows
|
|
214
237
|
* to disable removes for this entity. Note that this does
|
|
@@ -21,6 +21,16 @@ import { guardId, guardString } from '../../util/index.js';
|
|
|
21
21
|
* @extends AuditCollection
|
|
22
22
|
*/
|
|
23
23
|
class LatestAuditCollection extends BaseCollection {
|
|
24
|
+
async create(item) {
|
|
25
|
+
return super.create(item, { upsert: true });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async allByAuditType(auditType) {
|
|
29
|
+
guardString('auditType', auditType, this.entityName);
|
|
30
|
+
|
|
31
|
+
return this.all({ auditType });
|
|
32
|
+
}
|
|
33
|
+
|
|
24
34
|
async findById(siteId, auditType) {
|
|
25
35
|
guardId('siteId', siteId, this.entityName);
|
|
26
36
|
guardString('auditType', auditType, this.entityName);
|
|
@@ -28,11 +28,11 @@ Indexes Doc: https://electrodb.dev/en/modeling/indexes/
|
|
|
28
28
|
const schema = new SchemaBuilder(LatestAudit, LatestAuditCollection)
|
|
29
29
|
.withPrimaryPartitionKeys(['siteId'])
|
|
30
30
|
.withPrimarySortKeys(['auditType'])
|
|
31
|
+
.withUpsertable(true)
|
|
31
32
|
.addReference('belongs_to', 'Site', ['auditType'])
|
|
32
33
|
.addReference('belongs_to', 'Audit', ['auditType'])
|
|
33
34
|
.addReference('has_many', 'Opportunities')
|
|
34
35
|
.addAllIndex(['auditType'])
|
|
35
|
-
.allowUpdates(false)
|
|
36
36
|
.allowRemove(false)
|
|
37
37
|
.addAttribute('auditResult', {
|
|
38
38
|
type: 'any',
|
|
@@ -103,6 +103,10 @@ export function createAccessor(config) { /* eslint-disable no-underscore-dangle
|
|
|
103
103
|
});
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
+
if (context[name]) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
106
110
|
const foreignKeys = {
|
|
107
111
|
...isNonEmptyObject(foreignKey) && { [foreignKey.name]: foreignKey.value },
|
|
108
112
|
};
|
|
@@ -150,9 +154,8 @@ export function createAccessor(config) { /* eslint-disable no-underscore-dangle
|
|
|
150
154
|
);
|
|
151
155
|
}
|
|
152
156
|
|
|
153
|
-
export function createAccessors(configs
|
|
157
|
+
export function createAccessors(configs) {
|
|
154
158
|
configs.forEach((config) => {
|
|
155
159
|
createAccessor(config);
|
|
156
|
-
log.debug(`Created accessor ${config.name} for ${config.context.schema.getModelName()} to ${config.collection.schema.getModelName()}`);
|
|
157
160
|
});
|
|
158
161
|
}
|