@punks/backend-entity-manager 0.0.334 → 0.0.336

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 (34) hide show
  1. package/dist/cjs/index.js +348 -51
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/cjs/types/integrations/cache/dynamo/converter.d.ts +4 -0
  4. package/dist/cjs/types/integrations/cache/dynamo/index.d.ts +2 -0
  5. package/dist/cjs/types/integrations/cache/dynamo/instance.d.ts +27 -0
  6. package/dist/cjs/types/integrations/cache/dynamo/types.d.ts +8 -0
  7. package/dist/cjs/types/integrations/cache/index.d.ts +1 -0
  8. package/dist/cjs/types/platforms/nest/__test__/server/app/appAuth/appAuth.actions.d.ts +1 -1
  9. package/dist/cjs/types/platforms/nest/extensions/authentication/handlers/index.d.ts +1 -1
  10. package/dist/cjs/types/platforms/nest/plugins/collections/aws-dynamodb/collection.d.ts +33 -0
  11. package/dist/cjs/types/platforms/nest/plugins/collections/aws-dynamodb/index.d.ts +2 -0
  12. package/dist/cjs/types/platforms/nest/plugins/collections/aws-dynamodb/module.d.ts +5 -0
  13. package/dist/cjs/types/platforms/nest/plugins/collections/aws-dynamodb/settings.d.ts +7 -0
  14. package/dist/cjs/types/platforms/nest/plugins/collections/aws-dynamodb/types.d.ts +66 -0
  15. package/dist/cjs/types/platforms/nest/plugins/collections/index.d.ts +1 -0
  16. package/dist/cjs/types/platforms/nest/plugins/index.d.ts +1 -0
  17. package/dist/esm/index.js +347 -52
  18. package/dist/esm/index.js.map +1 -1
  19. package/dist/esm/types/integrations/cache/dynamo/converter.d.ts +4 -0
  20. package/dist/esm/types/integrations/cache/dynamo/index.d.ts +2 -0
  21. package/dist/esm/types/integrations/cache/dynamo/instance.d.ts +27 -0
  22. package/dist/esm/types/integrations/cache/dynamo/types.d.ts +8 -0
  23. package/dist/esm/types/integrations/cache/index.d.ts +1 -0
  24. package/dist/esm/types/platforms/nest/__test__/server/app/appAuth/appAuth.actions.d.ts +1 -1
  25. package/dist/esm/types/platforms/nest/extensions/authentication/handlers/index.d.ts +1 -1
  26. package/dist/esm/types/platforms/nest/plugins/collections/aws-dynamodb/collection.d.ts +33 -0
  27. package/dist/esm/types/platforms/nest/plugins/collections/aws-dynamodb/index.d.ts +2 -0
  28. package/dist/esm/types/platforms/nest/plugins/collections/aws-dynamodb/module.d.ts +5 -0
  29. package/dist/esm/types/platforms/nest/plugins/collections/aws-dynamodb/settings.d.ts +7 -0
  30. package/dist/esm/types/platforms/nest/plugins/collections/aws-dynamodb/types.d.ts +66 -0
  31. package/dist/esm/types/platforms/nest/plugins/collections/index.d.ts +1 -0
  32. package/dist/esm/types/platforms/nest/plugins/index.d.ts +1 -0
  33. package/dist/index.d.ts +184 -38
  34. package/package.json +5 -1
package/dist/cjs/index.js CHANGED
@@ -3,8 +3,10 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var backendCore = require('@punks/backend-core');
6
- var typeorm = require('typeorm');
6
+ var clientDynamodb = require('@aws-sdk/client-dynamodb');
7
+ var utilDynamodb = require('@aws-sdk/util-dynamodb');
7
8
  var common = require('@nestjs/common');
9
+ var typeorm = require('typeorm');
8
10
  var core = require('@nestjs/core');
9
11
  var async_hooks = require('async_hooks');
10
12
  var bcrypt = require('bcrypt');
@@ -2578,6 +2580,349 @@ const PlatformEvents = {
2578
2580
  },
2579
2581
  };
2580
2582
 
2583
+ class AppInMemorySettings {
2584
+ constructor(name) {
2585
+ this.name = name;
2586
+ }
2587
+ initialize(value) {
2588
+ this._instance = value;
2589
+ }
2590
+ get value() {
2591
+ if (!this._instance) {
2592
+ throw new Error(`AppSettings ${this.name} not initialized`);
2593
+ }
2594
+ return this._instance;
2595
+ }
2596
+ }
2597
+
2598
+ const awsDynamoDbSettings = new AppInMemorySettings("awsDynamoDbSettings");
2599
+
2600
+ class DynamoDbCollection {
2601
+ constructor(settings) {
2602
+ this.settings = settings;
2603
+ // *** Utility methods ***
2604
+ this.deserializeItem = (item) => utilDynamodb.unmarshall(item);
2605
+ this.serializeItem = (item) => utilDynamodb.marshall(item, {
2606
+ removeUndefinedValues: true,
2607
+ });
2608
+ this.serializeCursor = (cursor) => cursor ? JSON.stringify(cursor) : undefined;
2609
+ this.deserializeCursor = (cursor) => cursor ? JSON.parse(cursor) : undefined;
2610
+ this.client = this.getClient();
2611
+ }
2612
+ async queryTableItems(input) {
2613
+ const results = [];
2614
+ await this.queryTable({
2615
+ ...input,
2616
+ callback: (items) => {
2617
+ results.push(...items);
2618
+ },
2619
+ });
2620
+ return results;
2621
+ }
2622
+ async queryTable(input) {
2623
+ const { callback, ...data } = input;
2624
+ return await this.rawQueryTable({
2625
+ ...data,
2626
+ callback: (items) => callback(items.map((x) => this.deserializeItem(x))),
2627
+ });
2628
+ }
2629
+ async rawQueryTable(input) {
2630
+ const { callback } = input;
2631
+ let cursor = undefined;
2632
+ while (true) {
2633
+ const buildCommand = new clientDynamodb.QueryCommand({
2634
+ TableName: this.settings.tableName,
2635
+ IndexName: input.index,
2636
+ Limit: input.pageSize,
2637
+ ExclusiveStartKey: cursor,
2638
+ KeyConditionExpression: input.keyFilter,
2639
+ FilterExpression: input.attributesFilter,
2640
+ ExpressionAttributeValues: input.filterAttributes,
2641
+ });
2642
+ const result = await this.client.send(buildCommand);
2643
+ callback(result.Items);
2644
+ cursor = result.LastEvaluatedKey;
2645
+ if (!result.LastEvaluatedKey) {
2646
+ return;
2647
+ }
2648
+ }
2649
+ }
2650
+ async queryTablePage(input) {
2651
+ const result = await this.queryTablePageRaw({
2652
+ ...input,
2653
+ filterAttributes: input.filterAttributes
2654
+ ? utilDynamodb.marshall(input.filterAttributes, {
2655
+ removeUndefinedValues: true,
2656
+ })
2657
+ : undefined,
2658
+ });
2659
+ return {
2660
+ items: result.items.map((x) => this.deserializeItem(x)),
2661
+ cursor: result.cursor,
2662
+ };
2663
+ }
2664
+ async queryTablePageRaw(input) {
2665
+ const result = await this.client.send(new clientDynamodb.QueryCommand({
2666
+ TableName: this.settings.tableName,
2667
+ IndexName: input.index,
2668
+ Limit: input.pageSize,
2669
+ ExclusiveStartKey: input.cursor
2670
+ ? this.deserializeCursor(input.cursor)
2671
+ : undefined,
2672
+ KeyConditionExpression: input.keyFilter,
2673
+ FilterExpression: input.dataFilter,
2674
+ ExpressionAttributeValues: input.filterAttributes,
2675
+ ScanIndexForward: input.direction !== "desc",
2676
+ }));
2677
+ return {
2678
+ items: result.Items ?? [],
2679
+ cursor: result.LastEvaluatedKey
2680
+ ? this.serializeCursor(result.LastEvaluatedKey)
2681
+ : undefined,
2682
+ };
2683
+ }
2684
+ async scanTableItems(input) {
2685
+ const items = [];
2686
+ await this.scanTable({
2687
+ ...input,
2688
+ callback: (x) => items.push(...x),
2689
+ });
2690
+ return items;
2691
+ }
2692
+ async scanTable(input) {
2693
+ const { callback, ...data } = input;
2694
+ return await this.rawScanTable({
2695
+ ...data,
2696
+ callback: (items) => callback(items.map((x) => this.deserializeItem(x))),
2697
+ });
2698
+ }
2699
+ async rawScanTable(input) {
2700
+ const { callback } = input;
2701
+ let cursor = undefined;
2702
+ while (true) {
2703
+ const result = await this.client.send(new clientDynamodb.ScanCommand({
2704
+ TableName: this.settings.tableName,
2705
+ IndexName: input.index,
2706
+ Limit: input.pageSize,
2707
+ FilterExpression: input.filter,
2708
+ ExclusiveStartKey: cursor,
2709
+ }));
2710
+ if (result.Items) {
2711
+ callback(result.Items);
2712
+ }
2713
+ cursor = result.LastEvaluatedKey;
2714
+ if (!result.LastEvaluatedKey) {
2715
+ return;
2716
+ }
2717
+ }
2718
+ }
2719
+ async itemExists(key) {
2720
+ return !!(await this.getItemRaw(key));
2721
+ }
2722
+ async getItem(key) {
2723
+ const item = await this.getItemRaw(key);
2724
+ const el = item ? this.deserializeItem(item) : undefined;
2725
+ return el;
2726
+ }
2727
+ async getItemRaw(key) {
2728
+ const result = await this.client.send(new clientDynamodb.GetItemCommand({
2729
+ TableName: this.settings.tableName,
2730
+ Key: key,
2731
+ }));
2732
+ return result.Item;
2733
+ }
2734
+ async putItem(item) {
2735
+ await this.putItemRaw(this.serializeItem(item));
2736
+ }
2737
+ async putItemRaw(item) {
2738
+ await this.getClient().send(new clientDynamodb.PutItemCommand({
2739
+ TableName: this.settings.tableName,
2740
+ Item: item,
2741
+ }));
2742
+ }
2743
+ async deleteItem(key) {
2744
+ await this.getClient().send(new clientDynamodb.DeleteItemCommand({
2745
+ TableName: this.settings.tableName,
2746
+ Key: key,
2747
+ }));
2748
+ }
2749
+ getClient() {
2750
+ return new clientDynamodb.DynamoDBClient({
2751
+ credentials: awsDynamoDbSettings.value.awsAccessKeyId &&
2752
+ awsDynamoDbSettings.value.awsSecretAccessKey
2753
+ ? {
2754
+ accessKeyId: awsDynamoDbSettings.value.awsAccessKeyId,
2755
+ secretAccessKey: awsDynamoDbSettings.value.awsSecretAccessKey,
2756
+ }
2757
+ : undefined,
2758
+ region: awsDynamoDbSettings.value.region,
2759
+ });
2760
+ }
2761
+ }
2762
+
2763
+ /******************************************************************************
2764
+ Copyright (c) Microsoft Corporation.
2765
+
2766
+ Permission to use, copy, modify, and/or distribute this software for any
2767
+ purpose with or without fee is hereby granted.
2768
+
2769
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
2770
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
2771
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
2772
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
2773
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
2774
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
2775
+ PERFORMANCE OF THIS SOFTWARE.
2776
+ ***************************************************************************** */
2777
+
2778
+ function __decorate(decorators, target, key, desc) {
2779
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
2780
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
2781
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
2782
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
2783
+ }
2784
+
2785
+ function __param(paramIndex, decorator) {
2786
+ return function (target, key) { decorator(target, key, paramIndex); }
2787
+ }
2788
+
2789
+ function __metadata(metadataKey, metadataValue) {
2790
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
2791
+ }
2792
+
2793
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
2794
+ var e = new Error(message);
2795
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
2796
+ };
2797
+
2798
+ var AwsDynamoDbModule_1;
2799
+ const ModuleData$a = {
2800
+ providers: [],
2801
+ exports: [],
2802
+ };
2803
+ exports.AwsDynamoDbModule = AwsDynamoDbModule_1 = class AwsDynamoDbModule {
2804
+ static forRoot(input) {
2805
+ awsDynamoDbSettings.initialize(input);
2806
+ return {
2807
+ module: AwsDynamoDbModule_1,
2808
+ ...ModuleData$a,
2809
+ };
2810
+ }
2811
+ };
2812
+ exports.AwsDynamoDbModule = AwsDynamoDbModule_1 = __decorate([
2813
+ common.Module({
2814
+ ...ModuleData$a,
2815
+ })
2816
+ ], exports.AwsDynamoDbModule);
2817
+
2818
+ const toCacheEntryInfo = (item) => ({
2819
+ instanceName: item.instance,
2820
+ key: item.key,
2821
+ expiration: new Date(item.expiration),
2822
+ createdOn: new Date(item.createdOn),
2823
+ updatedOn: new Date(item.updatedOn),
2824
+ });
2825
+ const toCacheEntryDetail = (item) => ({
2826
+ instanceName: item.instance,
2827
+ key: item.key,
2828
+ expiration: new Date(item.expiration),
2829
+ createdOn: new Date(item.createdOn),
2830
+ updatedOn: new Date(item.updatedOn),
2831
+ data: item.data,
2832
+ });
2833
+
2834
+ class DynamoDbCacheInstance {
2835
+ constructor(instanceName, dynamoDbOptions) {
2836
+ this.instanceName = instanceName;
2837
+ this.logger = backendCore.Log.getLogger(`DynamoDb Cache Instance ${instanceName}`);
2838
+ this.collection = new DynamoDbCollection({
2839
+ tableName: dynamoDbOptions.tableName,
2840
+ primaryKey: {
2841
+ partitionKey: dynamoDbOptions.partitionKey ?? "instance",
2842
+ sortKey: dynamoDbOptions.sortKey ?? "key",
2843
+ },
2844
+ });
2845
+ }
2846
+ async getEntries() {
2847
+ const items = await this.collection.queryTableItems({
2848
+ pageSize: 100,
2849
+ });
2850
+ return items.map(toCacheEntryInfo);
2851
+ }
2852
+ async getEntry(key) {
2853
+ const entry = await this.collection.getItem({
2854
+ instance: {
2855
+ S: this.instanceName,
2856
+ },
2857
+ key: {
2858
+ S: key,
2859
+ },
2860
+ });
2861
+ return entry && !this.isExpired(entry, new Date())
2862
+ ? toCacheEntryDetail(entry)
2863
+ : undefined;
2864
+ }
2865
+ async get(key) {
2866
+ const item = await this.getEntry(key);
2867
+ return item?.data;
2868
+ }
2869
+ async set(key, input) {
2870
+ const now = Date.now();
2871
+ await this.collection.putItem({
2872
+ key,
2873
+ data: input.value,
2874
+ createdOn: now,
2875
+ updatedOn: now,
2876
+ expiration: backendCore.addTime(new Date(), input.ttl).getTime(),
2877
+ instance: this.instanceName,
2878
+ });
2879
+ }
2880
+ async retrieve(key, input) {
2881
+ const item = await this.get(key);
2882
+ if (item) {
2883
+ return item;
2884
+ }
2885
+ const value = await input.valueFactory();
2886
+ await this.set(key, { value, ttl: input.ttl });
2887
+ return value;
2888
+ }
2889
+ async delete(key) {
2890
+ await this.collection.deleteItem({
2891
+ instance: {
2892
+ S: this.instanceName,
2893
+ },
2894
+ key: {
2895
+ S: key,
2896
+ },
2897
+ });
2898
+ }
2899
+ async clear() {
2900
+ await this.collection.scanTable({
2901
+ pageSize: 100,
2902
+ callback: (items) => {
2903
+ items.forEach((item) => {
2904
+ if (item.instance === this.instanceName) {
2905
+ this.collection.deleteItem({
2906
+ instance: {
2907
+ S: this.instanceName,
2908
+ },
2909
+ key: {
2910
+ S: item.key,
2911
+ },
2912
+ });
2913
+ }
2914
+ });
2915
+ },
2916
+ });
2917
+ }
2918
+ getInstanceName() {
2919
+ return this.instanceName;
2920
+ }
2921
+ isExpired(item, when) {
2922
+ return item.expiration < when.getTime();
2923
+ }
2924
+ }
2925
+
2581
2926
  class TypeormCacheInstance {
2582
2927
  constructor(instanceName) {
2583
2928
  this.instanceName = instanceName;
@@ -3849,41 +4194,6 @@ const AuthenticationEvents = {
3849
4194
  UserPasswordResetCompleted: `${AUTHENTICATION_EVENTS_NAMESPACE}:user.passwordResetCompleted`,
3850
4195
  };
3851
4196
 
3852
- /******************************************************************************
3853
- Copyright (c) Microsoft Corporation.
3854
-
3855
- Permission to use, copy, modify, and/or distribute this software for any
3856
- purpose with or without fee is hereby granted.
3857
-
3858
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
3859
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
3860
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
3861
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
3862
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
3863
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
3864
- PERFORMANCE OF THIS SOFTWARE.
3865
- ***************************************************************************** */
3866
-
3867
- function __decorate(decorators, target, key, desc) {
3868
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3869
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
3870
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
3871
- return c > 3 && r && Object.defineProperty(target, key, r), r;
3872
- }
3873
-
3874
- function __param(paramIndex, decorator) {
3875
- return function (target, key) { decorator(target, key, paramIndex); }
3876
- }
3877
-
3878
- function __metadata(metadataKey, metadataValue) {
3879
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
3880
- }
3881
-
3882
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
3883
- var e = new Error(message);
3884
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
3885
- };
3886
-
3887
4197
  var AuthGuard_1;
3888
4198
  exports.AuthGuard = AuthGuard_1 = class AuthGuard {
3889
4199
  constructor(reflector) {
@@ -22375,21 +22685,6 @@ AuthenticationServicesResolver = __decorate([
22375
22685
  common.Injectable()
22376
22686
  ], AuthenticationServicesResolver);
22377
22687
 
22378
- class AppInMemorySettings {
22379
- constructor(name) {
22380
- this.name = name;
22381
- }
22382
- initialize(value) {
22383
- this._instance = value;
22384
- }
22385
- get value() {
22386
- if (!this._instance) {
22387
- throw new Error(`AppSettings ${this.name} not initialized`);
22388
- }
22389
- return this._instance;
22390
- }
22391
- }
22392
-
22393
22688
  const authSettings = new AppInMemorySettings("authSettings");
22394
22689
 
22395
22690
  let JwtProvider = class JwtProvider {
@@ -43837,6 +44132,8 @@ exports.AwsS3BucketError = AwsS3BucketError;
43837
44132
  exports.AwsS3MediaError = AwsS3MediaError;
43838
44133
  exports.AwsSesEmailTemplate = AwsSesEmailTemplate;
43839
44134
  exports.CurrentUser = CurrentUser;
44135
+ exports.DynamoDbCacheInstance = DynamoDbCacheInstance;
44136
+ exports.DynamoDbCollection = DynamoDbCollection;
43840
44137
  exports.EntityManagerConfigurationError = EntityManagerConfigurationError;
43841
44138
  exports.EntityManagerException = EntityManagerException;
43842
44139
  exports.EntityManagerSymbols = EntityManagerSymbols;