oak-domain 2.4.4 → 2.5.2

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 (37) hide show
  1. package/lib/actions/relation.d.ts +3 -3
  2. package/lib/actions/relation.js +1 -1
  3. package/lib/base-app-domain/ActionDefDict.d.ts +4 -1
  4. package/lib/base-app-domain/ActionDefDict.js +3 -1
  5. package/lib/base-app-domain/EntityDict.d.ts +2 -0
  6. package/lib/base-app-domain/Modi/Action.d.ts +4 -4
  7. package/lib/base-app-domain/ModiEntity/Schema.d.ts +31 -4
  8. package/lib/base-app-domain/ModiEntity/Storage.js +1 -1
  9. package/lib/base-app-domain/OperEntity/Schema.d.ts +31 -4
  10. package/lib/base-app-domain/OperEntity/Storage.js +1 -1
  11. package/lib/base-app-domain/Storage.js +3 -1
  12. package/lib/base-app-domain/User/Action.d.ts +10 -0
  13. package/lib/base-app-domain/User/Action.js +12 -0
  14. package/lib/base-app-domain/User/Schema.d.ts +60 -8
  15. package/lib/base-app-domain/User/Storage.js +12 -1
  16. package/lib/base-app-domain/UserEntityGrant/Schema.d.ts +121 -0
  17. package/lib/base-app-domain/UserEntityGrant/Schema.js +2 -0
  18. package/lib/base-app-domain/UserEntityGrant/Storage.d.ts +3 -0
  19. package/lib/base-app-domain/UserEntityGrant/Storage.js +28 -0
  20. package/lib/base-app-domain/_SubQuery.d.ts +8 -0
  21. package/lib/checkers/index.js +1 -1
  22. package/lib/compiler/schemalBuilder.js +50 -6
  23. package/lib/entities/User.d.ts +1 -0
  24. package/lib/entities/User.js +16 -0
  25. package/lib/entities/UserEntityGrant.d.ts +7 -0
  26. package/lib/entities/UserEntityGrant.js +12 -0
  27. package/lib/store/CascadeStore.js +121 -108
  28. package/lib/store/checker.d.ts +1 -1
  29. package/lib/store/checker.js +151 -9
  30. package/lib/store/modi.js +33 -15
  31. package/lib/types/Action.d.ts +3 -1
  32. package/lib/types/Auth.d.ts +4 -1
  33. package/lib/types/Trigger.d.ts +1 -0
  34. package/lib/types/Trigger.js +2 -1
  35. package/package.json +1 -1
  36. package/src/entities/User.ts +26 -1
  37. package/src/entities/UserEntityGrant.ts +24 -0
@@ -5,10 +5,12 @@ var tslib_1 = require("tslib");
5
5
  var assert_1 = tslib_1.__importDefault(require("assert"));
6
6
  var filter_1 = require("../store/filter");
7
7
  var Exception_1 = require("../types/Exception");
8
+ var types_1 = require("../types");
8
9
  var actionDef_1 = require("./actionDef");
9
10
  var string_1 = require("../utils/string");
10
11
  var lodash_1 = require("../utils/lodash");
11
12
  var relation_1 = require("./relation");
13
+ var uuid_1 = require("../utils/uuid");
12
14
  function translateCheckerInAsyncContext(checker) {
13
15
  var _this = this;
14
16
  var entity = checker.entity, type = checker.type, action = checker.action;
@@ -406,12 +408,12 @@ function createAuthCheckers(schema, authDict) {
406
408
  var _b = authDict[entity], relationAuth = _b.relationAuth, actionAuth = _b.actionAuth;
407
409
  if (relationAuth) {
408
410
  var raFilterMakerDict_1 = {};
411
+ var userEntityName_1 = "user".concat((0, string_1.firstLetterUpperCase)(entity));
409
412
  for (var r in relationAuth) {
410
413
  Object.assign(raFilterMakerDict_1, (_a = {},
411
414
  _a[r] = translateActionAuthFilterMaker(schema, relationAuth[r], entity),
412
415
  _a));
413
416
  }
414
- var userEntityName_1 = "user".concat((0, string_1.firstLetterUpperCase)(entity));
415
417
  var entityIdAttr_1 = "".concat(entity, "Id");
416
418
  checkers.push({
417
419
  entity: userEntityName_1,
@@ -508,7 +510,7 @@ exports.createAuthCheckers = createAuthCheckers;
508
510
  * @returns
509
511
  * 如果有的对象允许删除,需要使用trigger来处理其相关联的外键对象,这些trigger写作before,则会在checker之前执行,仍然可以删除成功
510
512
  */
511
- function createRemoveCheckers(schema) {
513
+ function createRemoveCheckers(schema, authDict) {
512
514
  var e_1, _a;
513
515
  var checkers = [];
514
516
  // 先建立所有的一对多的关系
@@ -546,8 +548,8 @@ function createRemoveCheckers(schema) {
546
548
  }
547
549
  };
548
550
  for (var entity in schema) {
549
- if (['operEntity'].includes(entity)) {
550
- continue; // OperEntity会指向每一个对象,不必处理
551
+ if (['operEntity', 'modiEntity', 'userEntityGrant'].includes(entity)) {
552
+ continue; // 系统功能性数据,不用处理
551
553
  }
552
554
  var attributes = schema[entity].attributes;
553
555
  for (var attr in attributes) {
@@ -565,7 +567,7 @@ function createRemoveCheckers(schema) {
565
567
  }
566
568
  }
567
569
  // 当删除一时,要确认多上面没有指向一的数据
568
- var entities = (0, lodash_1.intersection)(Object.keys(OneToManyMatrix), Object.keys(OneToManyOnEntityMatrix));
570
+ var entities = (0, lodash_1.union)(Object.keys(OneToManyMatrix), Object.keys(OneToManyOnEntityMatrix));
569
571
  var _loop_3 = function (entity) {
570
572
  checkers.push({
571
573
  entity: entity,
@@ -575,7 +577,7 @@ function createRemoveCheckers(schema) {
575
577
  var e_3, _a, e_4, _b;
576
578
  var promises = [];
577
579
  if (OneToManyMatrix[entity]) {
578
- var _loop_4 = function (otm) {
580
+ var _loop_5 = function (otm) {
579
581
  var _g, _h, _j, _k;
580
582
  var _l = tslib_1.__read(otm, 2), e = _l[0], attr = _l[1];
581
583
  var proj = (_g = {
@@ -627,7 +629,7 @@ function createRemoveCheckers(schema) {
627
629
  try {
628
630
  for (var _c = (e_3 = void 0, tslib_1.__values(OneToManyMatrix[entity])), _d = _c.next(); !_d.done; _d = _c.next()) {
629
631
  var otm = _d.value;
630
- _loop_4(otm);
632
+ _loop_5(otm);
631
633
  }
632
634
  }
633
635
  catch (e_3_1) { e_3 = { error: e_3_1 }; }
@@ -639,7 +641,7 @@ function createRemoveCheckers(schema) {
639
641
  }
640
642
  }
641
643
  if (OneToManyOnEntityMatrix[entity]) {
642
- var _loop_5 = function (otm) {
644
+ var _loop_6 = function (otm) {
643
645
  var _o, _p, _q;
644
646
  var proj = {
645
647
  id: 1,
@@ -690,7 +692,7 @@ function createRemoveCheckers(schema) {
690
692
  try {
691
693
  for (var _e = (e_4 = void 0, tslib_1.__values(OneToManyOnEntityMatrix[entity])), _f = _e.next(); !_f.done; _f = _e.next()) {
692
694
  var otm = _f.value;
693
- _loop_5(otm);
695
+ _loop_6(otm);
694
696
  }
695
697
  }
696
698
  catch (e_4_1) { e_4 = { error: e_4_1 }; }
@@ -720,6 +722,146 @@ function createRemoveCheckers(schema) {
720
722
  }
721
723
  finally { if (e_1) throw e_1.error; }
722
724
  }
725
+ var _loop_4 = function (entity) {
726
+ var e_5, _b;
727
+ var cascadeRemove = authDict[entity].cascadeRemove;
728
+ if (cascadeRemove) {
729
+ var entitiesOnEntityAttr = [];
730
+ var hasAllEntity = false;
731
+ var _loop_7 = function (attr) {
732
+ if (attr === '@entity') {
733
+ hasAllEntity = true;
734
+ return "continue";
735
+ }
736
+ var rel = (0, relation_1.judgeRelation)(schema, entity, attr);
737
+ if (rel === 2) {
738
+ entitiesOnEntityAttr.push(attr);
739
+ checkers.push({
740
+ entity: attr,
741
+ action: 'remove',
742
+ type: 'logical',
743
+ priority: types_1.REMOVE_CASCADE_PRIORITY,
744
+ checker: function (operation, context) {
745
+ var _a, _b;
746
+ var filter = operation.filter;
747
+ if (cascadeRemove[attr] === 'remove') {
748
+ return context.operate(entity, {
749
+ id: (0, uuid_1.generateNewId)(),
750
+ action: 'remove',
751
+ data: {},
752
+ filter: filter ? (_a = {},
753
+ _a[attr] = filter,
754
+ _a) : undefined,
755
+ }, { dontCollect: true });
756
+ }
757
+ return context.operate(entity, {
758
+ id: (0, uuid_1.generateNewId)(),
759
+ action: 'update',
760
+ data: {
761
+ entity: null,
762
+ entityId: null,
763
+ },
764
+ filter: filter ? (_b = {},
765
+ _b[attr] = filter,
766
+ _b) : undefined,
767
+ }, { dontCollect: true });
768
+ }
769
+ });
770
+ }
771
+ else {
772
+ (0, assert_1.default)(typeof rel === 'string');
773
+ checkers.push({
774
+ entity: rel,
775
+ action: 'remove',
776
+ type: 'logical',
777
+ priority: types_1.REMOVE_CASCADE_PRIORITY,
778
+ checker: function (operation, context) {
779
+ var _a, _b, _c;
780
+ var filter = operation.filter;
781
+ if (cascadeRemove[attr] === 'remove') {
782
+ return context.operate(entity, {
783
+ id: (0, uuid_1.generateNewId)(),
784
+ action: 'remove',
785
+ data: {},
786
+ filter: filter ? (_a = {},
787
+ _a[attr] = filter,
788
+ _a) : undefined,
789
+ }, { dontCollect: true });
790
+ }
791
+ return context.operate(entity, {
792
+ id: (0, uuid_1.generateNewId)(),
793
+ action: 'update',
794
+ data: (_b = {},
795
+ _b["".concat(attr, "Id")] = null,
796
+ _b),
797
+ filter: filter ? (_c = {},
798
+ _c[attr] = filter,
799
+ _c) : undefined,
800
+ }, { dontCollect: true });
801
+ }
802
+ });
803
+ }
804
+ };
805
+ for (var attr in cascadeRemove) {
806
+ _loop_7(attr);
807
+ }
808
+ if (hasAllEntity) {
809
+ var attributes = schema[entity].attributes;
810
+ var ref = attributes.entity.ref;
811
+ var restEntities = (0, lodash_1.difference)(ref, entitiesOnEntityAttr);
812
+ var _loop_8 = function (e) {
813
+ checkers.push({
814
+ entity: e,
815
+ action: 'remove',
816
+ type: 'logical',
817
+ priority: types_1.REMOVE_CASCADE_PRIORITY,
818
+ checker: function (operation, context) {
819
+ var _a, _b;
820
+ var filter = operation.filter;
821
+ if (cascadeRemove['@entity'] === 'remove') {
822
+ return context.operate(entity, {
823
+ id: (0, uuid_1.generateNewId)(),
824
+ action: 'remove',
825
+ data: {},
826
+ filter: filter ? (_a = {},
827
+ _a[e] = filter,
828
+ _a) : undefined,
829
+ }, { dontCollect: true });
830
+ }
831
+ return context.operate(entity, {
832
+ id: (0, uuid_1.generateNewId)(),
833
+ action: 'update',
834
+ data: {
835
+ entity: null,
836
+ entityId: null,
837
+ },
838
+ filter: filter ? (_b = {},
839
+ _b[e] = filter,
840
+ _b) : undefined,
841
+ }, { dontCollect: true });
842
+ }
843
+ });
844
+ };
845
+ try {
846
+ for (var restEntities_1 = (e_5 = void 0, tslib_1.__values(restEntities)), restEntities_1_1 = restEntities_1.next(); !restEntities_1_1.done; restEntities_1_1 = restEntities_1.next()) {
847
+ var e = restEntities_1_1.value;
848
+ _loop_8(e);
849
+ }
850
+ }
851
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
852
+ finally {
853
+ try {
854
+ if (restEntities_1_1 && !restEntities_1_1.done && (_b = restEntities_1.return)) _b.call(restEntities_1);
855
+ }
856
+ finally { if (e_5) throw e_5.error; }
857
+ }
858
+ }
859
+ }
860
+ };
861
+ // 注入声明的cascade删除时的外键处理动作
862
+ for (var entity in authDict) {
863
+ _loop_4(entity);
864
+ }
723
865
  return checkers;
724
866
  }
725
867
  exports.createRemoveCheckers = createRemoveCheckers;
package/lib/store/modi.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.createModiRelatedTriggers = exports.createModiRelatedCheckers = exports.abandonModis = exports.applyModis = exports.createOperationsFromModies = void 0;
4
4
  var tslib_1 = require("tslib");
5
+ var types_1 = require("../types");
5
6
  var action_1 = require("../actions/action");
6
7
  var lodash_1 = require("../utils/lodash");
7
8
  var uuid_1 = require("../utils/uuid");
@@ -154,35 +155,52 @@ function createModiRelatedTriggers(schema) {
154
155
  if (inModi) {
155
156
  // 当关联modi的对象被删除时,对应的modi也删除
156
157
  triggers.push({
157
- name: "\u5F53\u5220\u9664".concat(entity, "\u5BF9\u8C61\u65F6\uFF0C\u5220\u9664\u76F8\u5173\u8054\u8FD8\u6D3B\u8DC3\u7684modi"),
158
+ name: "\u5F53\u5220\u9664".concat(entity, "\u5BF9\u8C61\u65F6\uFF0C\u5220\u9664\u76F8\u5173\u8054\u7684modi\u7684modiEntity"),
158
159
  action: 'remove',
159
160
  entity: entity,
160
161
  when: 'after',
162
+ priority: types_1.REMOVE_CASCADE_PRIORITY,
161
163
  fn: function (_a, context, option) {
162
164
  var operation = _a.operation;
163
165
  return tslib_1.__awaiter(_this, void 0, void 0, function () {
164
- var data, id, _b, _c, _d;
165
- var _e;
166
- return tslib_1.__generator(this, function (_f) {
167
- switch (_f.label) {
166
+ var data, id, _b, _c, _d, _e, _f, _g;
167
+ var _h, _j;
168
+ return tslib_1.__generator(this, function (_k) {
169
+ switch (_k.label) {
168
170
  case 0:
169
171
  data = operation.data;
170
172
  id = data.id;
171
173
  _c = (_b = context).operate;
172
- _d = ['modi'];
173
- _e = {};
174
+ _d = ['modiEntity'];
175
+ _h = {};
174
176
  return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
175
- case 1: return [4 /*yield*/, _c.apply(_b, _d.concat([(_e.id = _f.sent(),
176
- _e.action = 'remove',
177
- _e.data = {},
178
- _e.filter = {
177
+ case 1: return [4 /*yield*/, _c.apply(_b, _d.concat([(_h.id = _k.sent(),
178
+ _h.action = 'remove',
179
+ _h.data = {},
180
+ _h.filter = {
181
+ modi: {
182
+ entity: entity,
183
+ entityId: id,
184
+ },
185
+ },
186
+ _h), { dontCollect: true }]))];
187
+ case 2:
188
+ _k.sent();
189
+ _f = (_e = context).operate;
190
+ _g = ['modi'];
191
+ _j = {};
192
+ return [4 /*yield*/, (0, uuid_1.generateNewIdAsync)()];
193
+ case 3: return [4 /*yield*/, _f.apply(_e, _g.concat([(_j.id = _k.sent(),
194
+ _j.action = 'remove',
195
+ _j.data = {},
196
+ _j.filter = {
179
197
  entity: entity,
180
198
  entityId: id,
181
199
  },
182
- _e), option]))];
183
- case 2:
184
- _f.sent();
185
- return [2 /*return*/, 1];
200
+ _j), { dontCollect: true }]))];
201
+ case 4:
202
+ _k.sent();
203
+ return [2 /*return*/, 0];
186
204
  }
187
205
  });
188
206
  });
@@ -13,6 +13,8 @@ export declare type ActionDictOfEntityDict<E extends EntityDict> = {
13
13
  [A in keyof E[T]['OpSchema']]?: ActionDef<string, string>;
14
14
  };
15
15
  };
16
+ export declare type CascadeActionItem = CascadeRelationItem;
16
17
  export declare type CascadeActionAuth<A extends Action = ''> = {
17
- [K in A | GenericAction]?: CascadeRelationItem | (CascadeRelationItem | CascadeRelationItem[])[];
18
+ [K in A | GenericAction]?: CascadeActionItem | (CascadeActionItem | CascadeActionItem[])[];
18
19
  };
20
+ export declare type ActionOnRemove = 'setNull' | 'remove';
@@ -1,4 +1,4 @@
1
- import { CascadeActionAuth, CascadeRelationAuth } from ".";
1
+ import { CascadeActionAuth, CascadeRelationAuth, ActionOnRemove } from ".";
2
2
  import { AsyncContext } from "../store/AsyncRowStore";
3
3
  import { SyncContext } from "../store/SyncRowStore";
4
4
  import { EntityDict, OperateOption, SelectOption } from "../types/Entity";
@@ -60,6 +60,9 @@ export declare type Checker<ED extends EntityDict, T extends keyof ED, Cxt exten
60
60
  export declare type AuthDef<ED extends EntityDict, T extends keyof ED> = {
61
61
  relationAuth?: CascadeRelationAuth<NonNullable<ED[T]['Relation']>>;
62
62
  actionAuth?: CascadeActionAuth<ED[T]['Action']>;
63
+ cascadeRemove?: {
64
+ [E in keyof ED[T]['OpSchema'] | '@entity']?: ActionOnRemove;
65
+ };
63
66
  };
64
67
  export declare type AuthDefDict<ED extends EntityDict> = {
65
68
  [K in keyof ED]?: AuthDef<ED, K>;
@@ -12,6 +12,7 @@ export declare const TRIGGER_MIN_PRIORITY = 1;
12
12
  export declare const TRIGGER_MAX_PRIORITY = 99;
13
13
  export declare const DATA_CHECKER_DEFAULT_PRIORITY = 60;
14
14
  export declare const CHECKER_DEFAULT_PRIORITY = 99;
15
+ export declare const REMOVE_CASCADE_PRIORITY = 70;
15
16
  interface TriggerBase<ED extends EntityDict, T extends keyof ED> {
16
17
  checkerType?: CheckerType;
17
18
  entity: T;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CHECKER_DEFAULT_PRIORITY = exports.DATA_CHECKER_DEFAULT_PRIORITY = exports.TRIGGER_MAX_PRIORITY = exports.TRIGGER_MIN_PRIORITY = exports.TRIGGER_DEFAULT_PRIORITY = void 0;
3
+ exports.REMOVE_CASCADE_PRIORITY = exports.CHECKER_DEFAULT_PRIORITY = exports.DATA_CHECKER_DEFAULT_PRIORITY = exports.TRIGGER_MAX_PRIORITY = exports.TRIGGER_MIN_PRIORITY = exports.TRIGGER_DEFAULT_PRIORITY = void 0;
4
4
  /**
5
5
  * 优先级越小,越早执行。定义在1~99之间
6
6
  */
@@ -9,6 +9,7 @@ exports.TRIGGER_MIN_PRIORITY = 1;
9
9
  exports.TRIGGER_MAX_PRIORITY = 99;
10
10
  exports.DATA_CHECKER_DEFAULT_PRIORITY = 60;
11
11
  exports.CHECKER_DEFAULT_PRIORITY = 99;
12
+ exports.REMOVE_CASCADE_PRIORITY = 70;
12
13
  ;
13
14
  ;
14
15
  ;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oak-domain",
3
- "version": "2.4.4",
3
+ "version": "2.5.2",
4
4
  "author": {
5
5
  "name": "XuChang"
6
6
  },
@@ -1,20 +1,45 @@
1
1
  import { String, Int, Text, Image, Datetime } from '../types/DataType';
2
2
  import { LocaleDef } from '../types/Locale';
3
3
  import { EntityShape } from '../types/Entity';
4
+ import { ActionDef } from '../types/Action';
4
5
 
5
6
  export interface Schema extends EntityShape {
6
7
  name?: String<16>;
7
8
  nickname?: String<64>;
8
9
  password?: Text;
10
+ ref?: Schema;
9
11
  };
10
12
 
13
+ type UserAction = 'mergeTo';
14
+ type UserState = 'normal' | 'merged';
11
15
 
12
- const locale: LocaleDef<Schema, '', '', {}> = {
16
+ type Action = UserAction;
17
+
18
+ const UserActionDef: ActionDef<UserAction, UserState> = {
19
+ stm: {
20
+ mergeTo: ['normal', 'merged'],
21
+ },
22
+ };
23
+
24
+ const locale: LocaleDef<Schema, Action, '', {
25
+ userState: UserState;
26
+ }> = {
13
27
  zh_CN: {
14
28
  attr: {
15
29
  name: '姓名',
16
30
  nickname: '昵称',
17
31
  password: '密码',
32
+ ref: '指向用户',
33
+ userState: '状态',
34
+ },
35
+ action: {
36
+ mergeTo: '合并',
18
37
  },
38
+ v: {
39
+ userState: {
40
+ normal: '正常',
41
+ merged: '已被合并',
42
+ },
43
+ }
19
44
  },
20
45
  };
@@ -0,0 +1,24 @@
1
+ import { String } from '../types/DataType';
2
+ import { LocaleDef } from '../types/Locale';
3
+ import { EntityShape } from '../types/Entity';
4
+
5
+ export interface Schema extends EntityShape {
6
+ entity: String<32>;
7
+ entityId: String<64>;
8
+ relation: String<32>;
9
+ };
10
+
11
+ const locale: LocaleDef<
12
+ Schema,
13
+ '',
14
+ '',
15
+ {}
16
+ > = {
17
+ zh_CN: {
18
+ attr: {
19
+ relation: '关系',
20
+ entity: '关联对象',
21
+ entityId: '关联对象id',
22
+ },
23
+ },
24
+ };