@squiz/db-lib 1.67.0 → 1.69.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,5 +1,17 @@
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
+
9
+ ## 1.68.0
10
+
11
+ ### Minor Changes
12
+
13
+ - bd553fe: fix dynamdb integration test issues
14
+
3
15
  ## 1.67.0
4
16
 
5
17
  ### 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,12 @@
1
+ export declare const getDynamoDbOptions: (awsRegion: string, nodeEnv: 'production' | 'development') => {
2
+ region: string;
3
+ credentials?: undefined;
4
+ endpoint?: undefined;
5
+ } | {
6
+ credentials: {
7
+ accessKeyId: string;
8
+ secretAccessKey: string;
9
+ };
10
+ endpoint: string;
11
+ region?: undefined;
12
+ };
@@ -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.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from './AbstractRepository';
2
2
  export * from './ConnectionManager';
3
3
  export * from './dynamodb/DynamoDbManager';
4
4
  export * from './dynamodb/AbstractDynamoDbRepository';
5
+ export * from './dynamodb/getDynamoDbOptions';
5
6
  export * from './Migrator';
6
7
  export * from './Repositories';
7
8
  export * from './getConnectionInfo';
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
 
@@ -55678,7 +55678,8 @@ __export(src_exports, {
55678
55678
  PoolClient: () => import_pg2.PoolClient,
55679
55679
  PostgresErrorCode: () => PostgresErrorCode,
55680
55680
  TransactionError: () => TransactionError,
55681
- getConnectionInfo: () => getConnectionInfo
55681
+ getConnectionInfo: () => getConnectionInfo,
55682
+ getDynamoDbOptions: () => getDynamoDbOptions
55682
55683
  });
55683
55684
  module.exports = __toCommonJS(src_exports);
55684
55685
 
@@ -56130,6 +56131,15 @@ var DynamoDbManager = class {
56130
56131
  }
56131
56132
  };
56132
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
+
56133
56143
  // src/dynamodb/AbstractDynamoDbRepository.ts
56134
56144
  var AbstractDynamoDbRepository = class {
56135
56145
  constructor(tableName, dbManager, entityName2, entityDefinition, classRef) {
@@ -56141,6 +56151,7 @@ var AbstractDynamoDbRepository = class {
56141
56151
  this.client = dbManager.client;
56142
56152
  this.keys = entityDefinition.keys;
56143
56153
  this.indexes = entityDefinition.indexes;
56154
+ this.fieldsAsJsonString = entityDefinition.fieldsAsJsonString;
56144
56155
  this.keysFormat = {
56145
56156
  [this.keys.pk.attributeName]: this.keys.pk.format,
56146
56157
  [this.keys.sk.attributeName]: this.keys.sk.format
@@ -56232,7 +56243,9 @@ var AbstractDynamoDbRepository = class {
56232
56243
  if (oldValue === void 0) {
56233
56244
  return void 0;
56234
56245
  }
56235
- this.assertValueMatchesModel({ ...oldValue, ...newValue });
56246
+ const value = { ...oldValue, ...newValue };
56247
+ this.assertValueMatchesModel(value);
56248
+ this.convertSelectedValuesToJsonString(newValue);
56236
56249
  const updateExpression = [];
56237
56250
  const expressionAttributeNames = {};
56238
56251
  const expressionAttributeValues = {};
@@ -56258,7 +56271,7 @@ var AbstractDynamoDbRepository = class {
56258
56271
  this.dbManager.addWriteTransactionItem(transaction.id, {
56259
56272
  Update: updateCommandInput
56260
56273
  });
56261
- return new this.classRef({ ...oldValue, ...newValue });
56274
+ return new this.classRef(value);
56262
56275
  }
56263
56276
  let output;
56264
56277
  try {
@@ -56295,6 +56308,7 @@ var AbstractDynamoDbRepository = class {
56295
56308
  for (const modelProperty of Object.keys(value)) {
56296
56309
  columns[modelProperty] = value[modelProperty];
56297
56310
  }
56311
+ this.convertSelectedValuesToJsonString(columns);
56298
56312
  const keyFields = {
56299
56313
  [this.keys.pk.attributeName]: this.getPk(value),
56300
56314
  [this.keys.sk.attributeName]: this.getSk(value)
@@ -56361,8 +56375,24 @@ var AbstractDynamoDbRepository = class {
56361
56375
  * @returns
56362
56376
  */
56363
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
+ }
56364
56387
  return new this.classRef(item);
56365
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
+ }
56366
56396
  /**
56367
56397
  * Evaluate the partition key value from the partial item
56368
56398
  * @param item
@@ -56462,6 +56492,17 @@ var AbstractDynamoDbRepository = class {
56462
56492
  }
56463
56493
  };
56464
56494
 
56495
+ // src/dynamodb/getDynamoDbOptions.ts
56496
+ var getDynamoDbOptions = (awsRegion, nodeEnv) => {
56497
+ if (nodeEnv === "production") {
56498
+ return { region: awsRegion };
56499
+ }
56500
+ return {
56501
+ credentials: { accessKeyId: "key", secretAccessKey: "key" },
56502
+ endpoint: process.env.CI ? "http://dynamodb-local:8000" : "http://localhost:8000"
56503
+ };
56504
+ };
56505
+
56465
56506
  // src/getConnectionInfo.ts
56466
56507
  var import_client_secrets_manager = __toESM(require_dist_cjs53());
56467
56508
  async function getConnectionInfo(config) {
@@ -56490,8 +56531,8 @@ async function getSecret(id) {
56490
56531
  }
56491
56532
 
56492
56533
  // src/error/DuplicateItemError.ts
56493
- var import_dx_common_lib3 = __toESM(require_lib5());
56494
- 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 {
56495
56536
  constructor(message) {
56496
56537
  super(message);
56497
56538
  this.name = "DuplicateItemError";
@@ -56499,8 +56540,8 @@ var DuplicateItemError = class extends import_dx_common_lib3.InternalServerError
56499
56540
  };
56500
56541
 
56501
56542
  // src/error/MissingKeyValuesError.ts
56502
- var import_dx_common_lib4 = __toESM(require_lib5());
56503
- 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 {
56504
56545
  constructor(message) {
56505
56546
  super(message);
56506
56547
  this.name = "MissingKeyValuesError";
@@ -56508,8 +56549,8 @@ var MissingKeyValuesError = class extends import_dx_common_lib4.InternalServerEr
56508
56549
  };
56509
56550
 
56510
56551
  // src/error/InvalidDbSchemaError.ts
56511
- var import_dx_common_lib5 = __toESM(require_lib5());
56512
- 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 {
56513
56554
  constructor(message) {
56514
56555
  super(message);
56515
56556
  this.name = "InvalidDbSchemaError";
@@ -56804,7 +56845,8 @@ var import_pg2 = __toESM(require_lib7());
56804
56845
  PoolClient,
56805
56846
  PostgresErrorCode,
56806
56847
  TransactionError,
56807
- getConnectionInfo
56848
+ getConnectionInfo,
56849
+ getDynamoDbOptions
56808
56850
  });
56809
56851
  /*! Bundled license information:
56810
56852