@rayondigital/nest-dapr 0.9.2 → 0.9.4

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/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![npm version](https://badge.fury.io/js/@rayondigital%2Fnest-dapr.svg)](https://badge.fury.io/js/@rayondigital%2Fnest-dapr)
2
+
1
3
  # NestJS + Dapr
2
4
  Develop NestJs microservices using [Dapr](https://dapr.io/) pubsub, actors and bindings.
3
5
 
@@ -1,11 +1,16 @@
1
- import { DaprClient } from '@dapr/dapr';
1
+ import { ActorId, DaprClient } from '@dapr/dapr';
2
2
  import { Type } from '@nestjs/common';
3
3
  export declare class DaprActorClient {
4
4
  private readonly daprClient;
5
5
  private actorClients;
6
+ private interfaces;
7
+ private prefix;
8
+ private delimiter;
6
9
  constructor(daprClient: DaprClient);
10
+ setPrefix(prefix: string, delimiter?: string): void;
7
11
  register<T>(actorTypeName: string, actorType: Type<T> | Function, daprClient?: DaprClient): void;
8
12
  registerInterface<T>(actorType: Type<T> | Function, interfaceType: Type<T> | Function, daprClient?: DaprClient): void;
13
+ getActorId(actorId: string): ActorId;
9
14
  getActor<TActorInterface>(actorType: Type<TActorInterface> | Function, actorId: string): TActorInterface;
10
15
  getActorByTypeName<TActorInterface>(actorTypeName: string, actorId: string): TActorInterface;
11
16
  contains(actorTypeName: string): boolean;
@@ -16,15 +16,31 @@ let DaprActorClient = class DaprActorClient {
16
16
  constructor(daprClient) {
17
17
  this.daprClient = daprClient;
18
18
  this.actorClients = new Map();
19
+ this.interfaces = new Map();
20
+ this.prefix = '';
21
+ this.delimiter = '-';
22
+ }
23
+ setPrefix(prefix, delimiter = '-') {
24
+ this.prefix = prefix;
25
+ this.delimiter = delimiter;
19
26
  }
20
27
  register(actorTypeName, actorType, daprClient) {
28
+ this.interfaces.set(this.formatActorTypeName(actorTypeName), actorType);
21
29
  this.actorClients.set(this.formatActorTypeName(actorTypeName), new dapr_1.ActorProxyBuilder(actorType, daprClient !== null && daprClient !== void 0 ? daprClient : this.daprClient));
22
30
  }
23
31
  registerInterface(actorType, interfaceType, daprClient) {
24
32
  var _a;
25
33
  const interfaceTypeName = (_a = interfaceType.name) !== null && _a !== void 0 ? _a : interfaceType.constructor.name;
34
+ this.interfaces.set(this.formatActorTypeName(interfaceTypeName), actorType);
26
35
  this.actorClients.set(this.formatActorTypeName(interfaceTypeName), new dapr_1.ActorProxyBuilder(actorType, daprClient !== null && daprClient !== void 0 ? daprClient : this.daprClient));
27
36
  }
37
+ getActorId(actorId) {
38
+ var _a;
39
+ if (this.prefix) {
40
+ return new dapr_1.ActorId(`${this.prefix}${(_a = this.delimiter) !== null && _a !== void 0 ? _a : '-'}${actorId}`);
41
+ }
42
+ return new dapr_1.ActorId(actorId);
43
+ }
28
44
  getActor(actorType, actorId) {
29
45
  var _a;
30
46
  const actorTypeName = (_a = actorType.name) !== null && _a !== void 0 ? _a : actorType.constructor.name;
@@ -35,7 +51,8 @@ let DaprActorClient = class DaprActorClient {
35
51
  throw new Error(`Actor ${actorTypeName} not found`);
36
52
  }
37
53
  const actorClient = this.getActorClient(actorTypeName);
38
- return actorClient.build(new dapr_1.ActorId(actorId));
54
+ const fullActorId = this.getActorId(actorId);
55
+ return actorClient.build(fullActorId);
39
56
  }
40
57
  getActorByTypeName(actorTypeName, actorId) {
41
58
  if (!actorTypeName) {
@@ -45,7 +62,8 @@ let DaprActorClient = class DaprActorClient {
45
62
  throw new Error(`Actor ${actorTypeName} not found`);
46
63
  }
47
64
  const actorClient = this.getActorClient(actorTypeName);
48
- return actorClient.build(new dapr_1.ActorId(actorId));
65
+ const fullActorId = this.getActorId(actorId);
66
+ return actorClient.build(fullActorId);
49
67
  }
50
68
  contains(actorTypeName) {
51
69
  return this.actorClients.has(this.formatActorTypeName(actorTypeName));
@@ -1,7 +1,12 @@
1
1
  import { AbstractActor } from '@dapr/dapr';
2
2
  export declare class StatefulActor extends AbstractActor {
3
+ private stateProperties;
3
4
  setState<T>(stateName: string, value: T): Promise<void>;
4
5
  removeState(stateName: string): Promise<void>;
5
6
  saveState(): Promise<void>;
7
+ clearState(): Promise<void>;
8
+ onActivate(): Promise<void>;
9
+ getAllState(): Promise<void>;
10
+ setAllStateFromProperties(): Promise<void>;
6
11
  getState<T>(stateName: string, defaultValue?: T): Promise<T | undefined>;
7
12
  }
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.StatefulActor = void 0;
13
13
  const dapr_1 = require("@dapr/dapr");
14
+ const constants_1 = require("../constants");
14
15
  class StatefulActor extends dapr_1.AbstractActor {
15
16
  setState(stateName, value) {
16
17
  return __awaiter(this, void 0, void 0, function* () {
@@ -24,9 +25,53 @@ class StatefulActor extends dapr_1.AbstractActor {
24
25
  }
25
26
  saveState() {
26
27
  return __awaiter(this, void 0, void 0, function* () {
28
+ yield this.setAllStateFromProperties();
27
29
  yield this.getStateManager().saveState();
28
30
  });
29
31
  }
32
+ clearState() {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ for (const property of this.stateProperties) {
35
+ yield this.removeState(property.name);
36
+ }
37
+ });
38
+ }
39
+ onActivate() {
40
+ const _super = Object.create(null, {
41
+ onActivate: { get: () => super.onActivate }
42
+ });
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ this.stateProperties =
45
+ Reflect.getMetadata(constants_1.DAPR_ACTOR_STATE, this.constructor) || [];
46
+ yield _super.onActivate.call(this);
47
+ yield this.getAllState();
48
+ });
49
+ }
50
+ getAllState() {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ for (const property of this.stateProperties) {
53
+ const value = yield this.getState(property.name);
54
+ if (value === undefined) {
55
+ if (typeof property.defaultValue === 'function') {
56
+ this[property.key] = property.defaultValue();
57
+ }
58
+ else {
59
+ this[property.key] = property.defaultValue;
60
+ }
61
+ }
62
+ else {
63
+ this[property.key] = value;
64
+ }
65
+ }
66
+ });
67
+ }
68
+ setAllStateFromProperties() {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ for (const property of this.stateProperties) {
71
+ yield this.setState(property.name, this[property.key]);
72
+ }
73
+ });
74
+ }
30
75
  getState(stateName, defaultValue = undefined) {
31
76
  return __awaiter(this, void 0, void 0, function* () {
32
77
  try {
@@ -1,3 +1,4 @@
1
1
  export declare const DAPR_BINDING_METADATA = "DAPR_BINDING_METADATA";
2
2
  export declare const DAPR_PUBSUB_METADATA = "DAPR_PUBSUB_METADATA";
3
3
  export declare const DAPR_ACTOR_METADATA = "DAPR_ACTOR_METADATA";
4
+ export declare const DAPR_ACTOR_STATE = "DAPR_ACTOR_STATE";
package/dist/constants.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DAPR_ACTOR_METADATA = exports.DAPR_PUBSUB_METADATA = exports.DAPR_BINDING_METADATA = void 0;
3
+ exports.DAPR_ACTOR_STATE = exports.DAPR_ACTOR_METADATA = exports.DAPR_PUBSUB_METADATA = exports.DAPR_BINDING_METADATA = void 0;
4
4
  exports.DAPR_BINDING_METADATA = 'DAPR_BINDING_METADATA';
5
5
  exports.DAPR_PUBSUB_METADATA = 'DAPR_PUBSUB_METADATA';
6
6
  exports.DAPR_ACTOR_METADATA = 'DAPR_ACTOR_METADATA';
7
+ exports.DAPR_ACTOR_STATE = 'DAPR_ACTOR_STATE';
@@ -0,0 +1,10 @@
1
+ export interface StateProperty {
2
+ key: string | symbol;
3
+ name: string;
4
+ type: Function;
5
+ defaultValue?: any;
6
+ }
7
+ export declare function State(options?: {
8
+ defaultValue?: any;
9
+ name?: string;
10
+ }): PropertyDecorator;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.State = void 0;
4
+ const constants_1 = require("./constants");
5
+ function State(options) {
6
+ return (target, propertyKey) => {
7
+ var _a;
8
+ const properties = Reflect.getMetadata(constants_1.DAPR_ACTOR_STATE, target.constructor) || [];
9
+ const type = Reflect.getMetadata('design:type', target, propertyKey);
10
+ properties.push({
11
+ key: propertyKey,
12
+ name: (_a = options === null || options === void 0 ? void 0 : options.name) !== null && _a !== void 0 ? _a : propertyKey.toString(),
13
+ type,
14
+ defaultValue: options === null || options === void 0 ? void 0 : options.defaultValue,
15
+ });
16
+ Reflect.defineMetadata(constants_1.DAPR_ACTOR_STATE, properties, target.constructor);
17
+ };
18
+ }
19
+ exports.State = State;
@@ -42,7 +42,11 @@ let DaprLoader = DaprLoader_1 = class DaprLoader {
42
42
  this.logger = new common_1.Logger(DaprLoader_1.name);
43
43
  }
44
44
  onApplicationBootstrap() {
45
+ var _a, _b, _c, _d;
45
46
  return __awaiter(this, void 0, void 0, function* () {
47
+ if (this.options.actorOptions) {
48
+ this.daprActorClient.setPrefix((_b = (_a = this.options.actorOptions) === null || _a === void 0 ? void 0 : _a.prefix) !== null && _b !== void 0 ? _b : '', (_d = (_c = this.options.actorOptions) === null || _c === void 0 ? void 0 : _c.delimiter) !== null && _d !== void 0 ? _d : '-');
49
+ }
46
50
  (0, nest_actor_manager_1.patchActorManagerForNest)(this.moduleRef);
47
51
  yield this.daprServer.actor.init();
48
52
  this.loadDaprHandlers();
@@ -8,6 +8,10 @@ export interface DaprModuleOptions {
8
8
  communicationProtocol?: CommunicationProtocolEnum;
9
9
  clientOptions?: DaprClientOptions;
10
10
  onError?: (name: string, topicName: string, error: any) => DaprPubSubStatusEnum;
11
+ actorOptions?: {
12
+ prefix?: string;
13
+ delimiter?: string;
14
+ };
11
15
  }
12
16
  export interface DaprModuleOptionsFactory {
13
17
  createDaprModuleOptions(): Promise<DaprModuleOptions> | DaprModuleOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rayondigital/nest-dapr",
3
- "version": "0.9.2",
3
+ "version": "0.9.4",
4
4
  "description": "Develop NestJs microservices using Dapr pubsub, actors and other bindings",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -15,7 +15,7 @@
15
15
  "type": "git",
16
16
  "url": "git+https://github.com/rayondigital/nest-dapr.git"
17
17
  },
18
- "author": "Neil Dobson/Jeremy Carter",
18
+ "author": "Jeremy Carter/Neil Dobson/Contibutors",
19
19
  "license": "MIT",
20
20
  "bugs": {
21
21
  "url": "https://github.com/rayondigital/nest-dapr/issues"