@squiz/db-lib 1.68.0 → 1.69.0

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Change Log
2
2
 
3
+ ## 1.69.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 0079f07: allow fields to be stored as json string in dynamodb
8
+
3
9
  ## 1.68.0
4
10
 
5
11
  ### Minor Changes
@@ -26,6 +26,7 @@ export type KeysFormat = Record<keyof TableKeys | keyof TableIndexes, string>;
26
26
  export type EntityDefinition = {
27
27
  keys: TableKeys;
28
28
  indexes: TableIndexes;
29
+ fieldsAsJsonString: string[];
29
30
  };
30
31
  export declare abstract class AbstractDynamoDbRepository<SHAPE extends object, DATA_CLASS extends SHAPE> implements Reader<SHAPE>, Writer<SHAPE> {
31
32
  protected tableName: string;
@@ -39,6 +40,7 @@ export declare abstract class AbstractDynamoDbRepository<SHAPE extends object, D
39
40
  protected keys: TableKeys;
40
41
  protected indexes: TableIndexes;
41
42
  protected keysFormat: KeysFormat;
43
+ protected fieldsAsJsonString: string[];
42
44
  constructor(tableName: string, dbManager: DynamoDbManager<Repositories>, entityName: string, entityDefinition: EntityDefinition, classRef: {
43
45
  new (data?: Record<string, unknown>): DATA_CLASS;
44
46
  });
@@ -99,6 +101,7 @@ export declare abstract class AbstractDynamoDbRepository<SHAPE extends object, D
99
101
  * @returns
100
102
  */
101
103
  protected hydrateItem(item: Record<string, unknown>): DATA_CLASS;
104
+ protected convertSelectedValuesToJsonString(item: Record<string, unknown>): void;
102
105
  /**
103
106
  * Evaluate the partition key value from the partial item
104
107
  * @param item
@@ -6,12 +6,14 @@ interface ITestItem {
6
6
  age: number;
7
7
  country: string;
8
8
  data?: object;
9
+ data2?: object;
9
10
  }
10
11
  declare class TestItem implements ITestItem {
11
12
  name: string;
12
13
  age: number;
13
14
  country: string;
14
15
  data: object;
16
+ data2?: object;
15
17
  constructor(data?: Partial<ITestItem>);
16
18
  }
17
19
  export type TestRepositories = {
@@ -0,0 +1,5 @@
1
+ import { InternalServerError } from '@squiz/dx-common-lib';
2
+ export declare class InvalidDataFormatError extends InternalServerError {
3
+ name: string;
4
+ constructor(message: string);
5
+ }
package/lib/index.js CHANGED
@@ -328,12 +328,12 @@ var require_InternalServerError = __commonJS({
328
328
  Object.defineProperty(exports, "__esModule", { value: true });
329
329
  exports.InternalServerError = void 0;
330
330
  var ErrorWithHttpStatusCode_1 = require_ErrorWithHttpStatusCode();
331
- var InternalServerError4 = class extends ErrorWithHttpStatusCode_1.ErrorWithHttpStatusCode {
331
+ var InternalServerError5 = class extends ErrorWithHttpStatusCode_1.ErrorWithHttpStatusCode {
332
332
  constructor(message) {
333
333
  super(500, message);
334
334
  }
335
335
  };
336
- exports.InternalServerError = InternalServerError4;
336
+ exports.InternalServerError = InternalServerError5;
337
337
  }
338
338
  });
339
339
 
@@ -56131,6 +56131,15 @@ var DynamoDbManager = class {
56131
56131
  }
56132
56132
  };
56133
56133
 
56134
+ // src/error/InvalidDataFormatError.ts
56135
+ var import_dx_common_lib3 = __toESM(require_lib5());
56136
+ var InvalidDataFormatError = class extends import_dx_common_lib3.InternalServerError {
56137
+ constructor(message) {
56138
+ super(message);
56139
+ this.name = "InvalidDataFormatError";
56140
+ }
56141
+ };
56142
+
56134
56143
  // src/dynamodb/AbstractDynamoDbRepository.ts
56135
56144
  var AbstractDynamoDbRepository = class {
56136
56145
  constructor(tableName, dbManager, entityName2, entityDefinition, classRef) {
@@ -56142,6 +56151,7 @@ var AbstractDynamoDbRepository = class {
56142
56151
  this.client = dbManager.client;
56143
56152
  this.keys = entityDefinition.keys;
56144
56153
  this.indexes = entityDefinition.indexes;
56154
+ this.fieldsAsJsonString = entityDefinition.fieldsAsJsonString;
56145
56155
  this.keysFormat = {
56146
56156
  [this.keys.pk.attributeName]: this.keys.pk.format,
56147
56157
  [this.keys.sk.attributeName]: this.keys.sk.format
@@ -56233,7 +56243,9 @@ var AbstractDynamoDbRepository = class {
56233
56243
  if (oldValue === void 0) {
56234
56244
  return void 0;
56235
56245
  }
56236
- this.assertValueMatchesModel({ ...oldValue, ...newValue });
56246
+ const value = { ...oldValue, ...newValue };
56247
+ this.assertValueMatchesModel(value);
56248
+ this.convertSelectedValuesToJsonString(newValue);
56237
56249
  const updateExpression = [];
56238
56250
  const expressionAttributeNames = {};
56239
56251
  const expressionAttributeValues = {};
@@ -56259,7 +56271,7 @@ var AbstractDynamoDbRepository = class {
56259
56271
  this.dbManager.addWriteTransactionItem(transaction.id, {
56260
56272
  Update: updateCommandInput
56261
56273
  });
56262
- return new this.classRef({ ...oldValue, ...newValue });
56274
+ return new this.classRef(value);
56263
56275
  }
56264
56276
  let output;
56265
56277
  try {
@@ -56296,6 +56308,7 @@ var AbstractDynamoDbRepository = class {
56296
56308
  for (const modelProperty of Object.keys(value)) {
56297
56309
  columns[modelProperty] = value[modelProperty];
56298
56310
  }
56311
+ this.convertSelectedValuesToJsonString(columns);
56299
56312
  const keyFields = {
56300
56313
  [this.keys.pk.attributeName]: this.getPk(value),
56301
56314
  [this.keys.sk.attributeName]: this.getSk(value)
@@ -56362,8 +56375,24 @@ var AbstractDynamoDbRepository = class {
56362
56375
  * @returns
56363
56376
  */
56364
56377
  hydrateItem(item) {
56378
+ for (const fieldName of Object.keys(item)) {
56379
+ if (this.fieldsAsJsonString.includes(fieldName)) {
56380
+ if (typeof item[fieldName] === "string") {
56381
+ item[fieldName] = JSON.parse(item[fieldName]);
56382
+ } else {
56383
+ throw new InvalidDataFormatError(`Field '${fieldName}' defined as JSON String has a non-string data`);
56384
+ }
56385
+ }
56386
+ }
56365
56387
  return new this.classRef(item);
56366
56388
  }
56389
+ convertSelectedValuesToJsonString(item) {
56390
+ for (const fieldName of Object.keys(item)) {
56391
+ if (this.fieldsAsJsonString.includes(fieldName)) {
56392
+ item[fieldName] = JSON.stringify(item[fieldName]);
56393
+ }
56394
+ }
56395
+ }
56367
56396
  /**
56368
56397
  * Evaluate the partition key value from the partial item
56369
56398
  * @param item
@@ -56502,8 +56531,8 @@ async function getSecret(id) {
56502
56531
  }
56503
56532
 
56504
56533
  // src/error/DuplicateItemError.ts
56505
- var import_dx_common_lib3 = __toESM(require_lib5());
56506
- var DuplicateItemError = class extends import_dx_common_lib3.InternalServerError {
56534
+ var import_dx_common_lib4 = __toESM(require_lib5());
56535
+ var DuplicateItemError = class extends import_dx_common_lib4.InternalServerError {
56507
56536
  constructor(message) {
56508
56537
  super(message);
56509
56538
  this.name = "DuplicateItemError";
@@ -56511,8 +56540,8 @@ var DuplicateItemError = class extends import_dx_common_lib3.InternalServerError
56511
56540
  };
56512
56541
 
56513
56542
  // src/error/MissingKeyValuesError.ts
56514
- var import_dx_common_lib4 = __toESM(require_lib5());
56515
- var MissingKeyValuesError = class extends import_dx_common_lib4.InternalServerError {
56543
+ var import_dx_common_lib5 = __toESM(require_lib5());
56544
+ var MissingKeyValuesError = class extends import_dx_common_lib5.InternalServerError {
56516
56545
  constructor(message) {
56517
56546
  super(message);
56518
56547
  this.name = "MissingKeyValuesError";
@@ -56520,8 +56549,8 @@ var MissingKeyValuesError = class extends import_dx_common_lib4.InternalServerEr
56520
56549
  };
56521
56550
 
56522
56551
  // src/error/InvalidDbSchemaError.ts
56523
- var import_dx_common_lib5 = __toESM(require_lib5());
56524
- var InvalidDbSchemaError = class extends import_dx_common_lib5.BadRequestError {
56552
+ var import_dx_common_lib6 = __toESM(require_lib5());
56553
+ var InvalidDbSchemaError = class extends import_dx_common_lib6.BadRequestError {
56525
56554
  constructor(message) {
56526
56555
  super(message);
56527
56556
  this.name = "InvalidDbSchemaError";