oak-domain 6.1.0 → 6.1.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 (161) hide show
  1. package/es/base-app-domain/ActionDefDict.d.ts +1 -0
  2. package/es/base-app-domain/ActionDefDict.js +3 -1
  3. package/es/base-app-domain/EntityDict.d.ts +86 -1
  4. package/es/base-app-domain/Log/Storage.js +4 -0
  5. package/es/base-app-domain/Log/_baseSchema.d.ts +5 -0
  6. package/es/base-app-domain/Storage.js +3 -1
  7. package/es/base-app-domain/StyleDict.js +3 -1
  8. package/es/base-app-domain/Version/Action.d.ts +6 -0
  9. package/es/base-app-domain/Version/Action.js +2 -0
  10. package/es/base-app-domain/Version/Schema.d.ts +19 -0
  11. package/es/base-app-domain/Version/Schema.js +1 -0
  12. package/es/base-app-domain/Version/Storage.d.ts +3 -0
  13. package/es/base-app-domain/Version/Storage.js +54 -0
  14. package/es/base-app-domain/Version/Style.d.ts +3 -0
  15. package/es/base-app-domain/Version/Style.js +6 -0
  16. package/es/base-app-domain/Version/_baseSchema.d.ts +57 -0
  17. package/es/base-app-domain/Version/_baseSchema.js +1 -0
  18. package/es/compiler/dependencyBuilder.d.ts +1 -0
  19. package/es/compiler/dependencyBuilder.js +173 -55
  20. package/es/compiler/routerBuilder.d.ts +4 -1
  21. package/es/compiler/routerBuilder.js +22 -19
  22. package/es/compiler/schemaBuilder.d.ts +5 -1
  23. package/es/compiler/schemaBuilder.js +10 -0
  24. package/es/context/BackendRuntimeContext.d.ts +9 -1
  25. package/es/context/BackendRuntimeContext.js +13 -1
  26. package/es/entities/Log.d.ts +2 -0
  27. package/es/entities/Log.js +2 -0
  28. package/es/entities/Version.d.ts +14 -0
  29. package/es/entities/Version.js +42 -0
  30. package/es/store/AsyncRowStore.d.ts +4 -1
  31. package/es/store/AsyncRowStore.js +7 -0
  32. package/es/store/CascadeStore.js +22 -0
  33. package/es/store/RelationAuth/authorizationFilter.d.ts +11 -0
  34. package/es/store/RelationAuth/authorizationFilter.js +104 -0
  35. package/es/store/RelationAuth/continuation.d.ts +9 -0
  36. package/es/store/RelationAuth/continuation.js +33 -0
  37. package/es/store/RelationAuth/index.d.ts +141 -0
  38. package/es/store/RelationAuth/index.js +1440 -0
  39. package/es/store/RelationAuth/model.d.ts +38 -0
  40. package/es/store/RelationAuth/model.js +24 -0
  41. package/es/store/RelationAuth/userLookup.d.ts +21 -0
  42. package/es/store/RelationAuth/userLookup.js +102 -0
  43. package/es/store/SyncRowStore.d.ts +2 -1
  44. package/es/store/SyncRowStore.js +3 -0
  45. package/es/store/filter.fix.d.ts +128 -0
  46. package/es/store/filter.fix.js +2862 -0
  47. package/es/store/filter.js +14 -9
  48. package/es/store/triggers.js +239 -44
  49. package/es/types/Context.d.ts +2 -1
  50. package/es/types/Demand.d.ts +11 -1
  51. package/es/types/Environment.d.ts +13 -2
  52. package/es/types/Exception.d.ts +3 -3
  53. package/es/types/Exception.js +25 -22
  54. package/es/types/Expression.d.ts +4 -1
  55. package/es/types/Expression.js +1 -1
  56. package/es/types/Storage.d.ts +15 -2
  57. package/es/utils/SimpleConnector.js +5 -2
  58. package/es/utils/decimal.js +2 -1
  59. package/es/utils/fetch/index.ali.d.ts +3 -0
  60. package/es/utils/fetch/index.ali.js +31 -0
  61. package/es/utils/fetch/index.d.ts +3 -0
  62. package/es/utils/fetch/index.js +5 -0
  63. package/es/utils/fetch/index.mp.d.ts +5 -3
  64. package/es/utils/fetch/index.mp.js +5 -280
  65. package/es/utils/fetch/index.wechat.d.ts +5 -0
  66. package/es/utils/fetch/index.wechat.js +174 -0
  67. package/es/utils/fetch/mini-program.shared.d.ts +37 -0
  68. package/es/utils/fetch/mini-program.shared.js +85 -0
  69. package/es/utils/geo.d.ts +1 -1
  70. package/es/utils/geo.js +1 -1
  71. package/es/utils/random/random.ali.d.ts +6 -0
  72. package/es/utils/random/random.ali.js +21 -0
  73. package/es/utils/random/random.mp.d.ts +5 -1
  74. package/es/utils/random/random.mp.js +11 -5
  75. package/es/utils/random/random.native.d.ts +1 -1
  76. package/es/utils/random/random.native.js +7 -4
  77. package/es/utils/random/random.wechat.d.ts +1 -0
  78. package/es/utils/random/random.wechat.js +10 -0
  79. package/lib/base-app-domain/ActionDefDict.d.ts +1 -0
  80. package/lib/base-app-domain/ActionDefDict.js +3 -1
  81. package/lib/base-app-domain/EntityDict.d.ts +86 -1
  82. package/lib/base-app-domain/Log/Storage.js +4 -0
  83. package/lib/base-app-domain/Log/_baseSchema.d.ts +5 -0
  84. package/lib/base-app-domain/Storage.js +3 -1
  85. package/lib/base-app-domain/StyleDict.js +3 -1
  86. package/lib/base-app-domain/Version/Action.d.ts +6 -0
  87. package/lib/base-app-domain/Version/Action.js +5 -0
  88. package/lib/base-app-domain/Version/Schema.d.ts +19 -0
  89. package/lib/base-app-domain/Version/Schema.js +4 -0
  90. package/lib/base-app-domain/Version/Storage.d.ts +3 -0
  91. package/lib/base-app-domain/Version/Storage.js +57 -0
  92. package/lib/base-app-domain/Version/Style.d.ts +3 -0
  93. package/lib/base-app-domain/Version/Style.js +9 -0
  94. package/lib/base-app-domain/Version/_baseSchema.d.ts +57 -0
  95. package/lib/base-app-domain/Version/_baseSchema.js +2 -0
  96. package/lib/compiler/dependencyBuilder.d.ts +1 -0
  97. package/lib/compiler/dependencyBuilder.js +173 -54
  98. package/lib/compiler/routerBuilder.d.ts +4 -1
  99. package/lib/compiler/routerBuilder.js +22 -19
  100. package/lib/compiler/schemaBuilder.d.ts +5 -1
  101. package/lib/compiler/schemaBuilder.js +10 -0
  102. package/lib/context/BackendRuntimeContext.d.ts +9 -1
  103. package/lib/context/BackendRuntimeContext.js +13 -1
  104. package/lib/entities/Log.d.ts +2 -0
  105. package/lib/entities/Log.js +2 -0
  106. package/lib/entities/Version.d.ts +14 -0
  107. package/lib/entities/Version.js +43 -0
  108. package/lib/store/AsyncRowStore.d.ts +4 -1
  109. package/lib/store/AsyncRowStore.js +7 -0
  110. package/lib/store/CascadeStore.js +22 -0
  111. package/lib/store/RelationAuth/authorizationFilter.d.ts +11 -0
  112. package/lib/store/RelationAuth/authorizationFilter.js +110 -0
  113. package/lib/store/RelationAuth/continuation.d.ts +9 -0
  114. package/lib/store/RelationAuth/continuation.js +37 -0
  115. package/lib/store/RelationAuth/index.d.ts +141 -0
  116. package/lib/store/RelationAuth/index.js +1445 -0
  117. package/lib/store/RelationAuth/model.d.ts +38 -0
  118. package/lib/store/RelationAuth/model.js +27 -0
  119. package/lib/store/RelationAuth/userLookup.d.ts +21 -0
  120. package/lib/store/RelationAuth/userLookup.js +105 -0
  121. package/lib/store/SyncRowStore.d.ts +2 -1
  122. package/lib/store/SyncRowStore.js +3 -0
  123. package/lib/store/filter.fix.d.ts +128 -0
  124. package/lib/store/filter.fix.js +2876 -0
  125. package/lib/store/filter.js +14 -9
  126. package/lib/store/triggers.js +239 -44
  127. package/lib/types/Context.d.ts +2 -1
  128. package/lib/types/Demand.d.ts +11 -1
  129. package/lib/types/Environment.d.ts +13 -2
  130. package/lib/types/Exception.d.ts +3 -3
  131. package/lib/types/Exception.js +25 -22
  132. package/lib/types/Expression.d.ts +4 -1
  133. package/lib/types/Expression.js +1 -1
  134. package/lib/types/Storage.d.ts +15 -2
  135. package/lib/utils/SimpleConnector.js +5 -2
  136. package/lib/utils/decimal.js +2 -1
  137. package/lib/utils/fetch/index.ali.d.ts +3 -0
  138. package/lib/utils/fetch/index.ali.js +36 -0
  139. package/lib/utils/fetch/index.d.ts +3 -0
  140. package/lib/utils/fetch/index.js +7 -0
  141. package/lib/utils/fetch/index.mp.d.ts +5 -3
  142. package/lib/utils/fetch/index.mp.js +5 -282
  143. package/lib/utils/fetch/index.wechat.d.ts +5 -0
  144. package/lib/utils/fetch/index.wechat.js +180 -0
  145. package/lib/utils/fetch/mini-program.shared.d.ts +37 -0
  146. package/lib/utils/fetch/mini-program.shared.js +96 -0
  147. package/lib/utils/geo.d.ts +1 -1
  148. package/lib/utils/geo.js +1 -1
  149. package/lib/utils/random/random.ali.d.ts +6 -0
  150. package/lib/utils/random/random.ali.js +24 -0
  151. package/lib/utils/random/random.mp.d.ts +5 -1
  152. package/lib/utils/random/random.mp.js +11 -5
  153. package/lib/utils/random/random.native.d.ts +1 -1
  154. package/lib/utils/random/random.native.js +7 -5
  155. package/lib/utils/random/random.wechat.d.ts +1 -0
  156. package/lib/utils/random/random.wechat.js +13 -0
  157. package/package.json +8 -5
  158. package/es/store/RelationAuth.d.ts +0 -115
  159. package/es/store/RelationAuth.js +0 -1340
  160. package/lib/store/RelationAuth.d.ts +0 -115
  161. package/lib/store/RelationAuth.js +0 -1345
@@ -254,7 +254,7 @@ function unionFilterSegment(entity, schema, ...filters) {
254
254
  }
255
255
  Object.assign(f1, {
256
256
  [attr]: {
257
- $in: f1[attr][op1] instanceof Array ? f1[attr][op1].concat(f2[attr]) : [f1[op1][attr], f2[attr]],
257
+ $in: f1[attr][op1] instanceof Array ? f1[attr][op1].concat(f2[attr]) : [f1[attr][op1], f2[attr]],
258
258
  },
259
259
  });
260
260
  return true;
@@ -371,7 +371,7 @@ function unionFilterSegment(entity, schema, ...filters) {
371
371
  }
372
372
  else if (pca1 === pca2) {
373
373
  if (pca1 === '$and') {
374
- assert(false, '只一个属性的时候不应该用$and');
374
+ return false;
375
375
  }
376
376
  else if (pca1 === '$not') {
377
377
  // 先not后or 等于先and后not
@@ -449,8 +449,13 @@ function unionFilterSegment(entity, schema, ...filters) {
449
449
  if (Object.keys(filter).length === 0) {
450
450
  Object.assign(filter, f);
451
451
  }
452
- else if (filter.$or) {
453
- filter.$or.push(f);
452
+ else if (filter.$or && Object.keys(filter).length === 1) {
453
+ if (f && Object.keys(f).length === 1 && f.$or) {
454
+ filter.$or.push(...f.$or);
455
+ }
456
+ else {
457
+ filter.$or.push(f);
458
+ }
454
459
  }
455
460
  else {
456
461
  filter = {
@@ -540,7 +545,7 @@ export function judgeValueRelation(value1, value2, contained) {
540
545
  const c = attr2 && ((['$gte'].includes(attr2) && value2[attr2] <= value1.$gte
541
546
  || ['$gt'].includes(attr2) && value2[attr2] < value1.$gte) || (attr2 === '$exists' && value2[attr2] === true));
542
547
  const r = (attr2 && (['$lt'].includes(attr2) && value2[attr2] <= value1.$gte
543
- || ['$eq', '$lte'].includes(attr2) && value2[attr2] < value1.gte
548
+ || ['$eq', '$lte'].includes(attr2) && value2[attr2] < value1.$gte
544
549
  || attr2 === '$in' && value2[attr2] instanceof Array && !(value2[attr2]).find((ele) => typeof ele !== typeof value1.$gte || ele >= value1.$gte) || (attr2 === '$exists' && value2[attr2] === false))) || (['string', 'number'].includes(typeof value2) && value2 < value1.$gte);
545
550
  if (contained) {
546
551
  // 包容
@@ -561,7 +566,7 @@ export function judgeValueRelation(value1, value2, contained) {
561
566
  const attr2 = (typeof value2 === 'object') && Object.keys(value2)[0];
562
567
  const c = attr2 && (['$lt', '$lte'].includes(attr2) && value2[attr2] >= value1.$lt || attr2 === '$exists' && value2[attr2] === true);
563
568
  const r = (attr2 && (['$gt', '$gte', '$eq'].includes(attr2) && value2[attr2] >= value1.$lt
564
- || attr2 === '$in' && value2[attr2] instanceof Array && !(value2[attr2]).find((ele) => typeof ele !== typeof value1.$gt || ele < value1.$lt) || (attr2 === '$exists' && value2[attr2] === false))) || (['string', 'number'].includes(typeof value2) && value2 >= value1.$lt);
569
+ || attr2 === '$in' && value2[attr2] instanceof Array && !(value2[attr2]).find((ele) => typeof ele !== typeof value1.$lt || ele < value1.$lt) || (attr2 === '$exists' && value2[attr2] === false))) || (['string', 'number'].includes(typeof value2) && value2 >= value1.$lt);
565
570
  if (contained) {
566
571
  if (c) {
567
572
  return true;
@@ -582,7 +587,7 @@ export function judgeValueRelation(value1, value2, contained) {
582
587
  const c = attr2 && (['$lte'].includes(attr2) && value2[attr2] >= value1.$lte
583
588
  || ['$lt'].includes(attr2) && value2[attr2] > value1.$lte) || (attr2 === '$exists' && value2[attr2] === true);
584
589
  const r = (attr2 && (['$gt'].includes(attr2) && value2[attr2] >= value1.$lte
585
- || ['$eq', '$gte'].includes(attr2) && value2[attr2] > value1.lte
590
+ || ['$eq', '$gte'].includes(attr2) && value2[attr2] > value1.$lte
586
591
  || attr2 === '$in' && value2[attr2] instanceof Array && !(value2[attr2]).find((ele) => typeof ele !== typeof value1.$lte || ele <= value1.$lte) || (attr2 === '$exists' && value2[attr2] === false))) || (['string', 'number'].includes(typeof value2) && value2 > value1.$lte);
587
592
  if (contained) {
588
593
  // 包容
@@ -620,7 +625,7 @@ export function judgeValueRelation(value1, value2, contained) {
620
625
  || attr2 === '$includes' && !value1.$eq.includes(value2[attr2])
621
626
  || attr2 === '$in' && value2[attr2] instanceof Array && !value2[attr2].includes(value1.$eq)
622
627
  || attr2 === '$between' && (value2[attr2][0] > value1.$eq || value2[attr2][1] < value1.$eq)
623
- || attr2 === '$exists' && value2[attr2] === false)) || value2 !== value1.$eq;
628
+ || attr2 === '$exists' && value2[attr2] === false)) || (!attr2 && value2 !== value1.$eq);
624
629
  if (contained) {
625
630
  // 相容
626
631
  if (c) {
@@ -1407,7 +1412,7 @@ export function getRelevantIds(filter) {
1407
1412
  }
1408
1413
  // 因为要准确判定id,如果有其它的过滤条件,可能会使实际处理的行数少于id指向的行数,只能返回空数组
1409
1414
  const attrs = Object.keys(filter);
1410
- if (intersection(attrs, ['id', '$and', '$or']).length > 3) {
1415
+ if (difference(attrs, ['id', '$and', '$or']).length > 0) {
1411
1416
  return [];
1412
1417
  }
1413
1418
  if (filter?.$and) {
@@ -29,8 +29,11 @@ export const actionAuthTriggers = [
29
29
  * @param context
30
30
  */
31
31
  async function undoLog(log, context) {
32
- const { iState, oper$log } = log;
33
- for (const oper of oper$log) {
32
+ const { oper$log } = log;
33
+ // 撤销一条 Log 时必须先撤销最后执行的 Oper。尤其是 cascade 更新中,后面的子操作
34
+ // 可能依赖前面创建或修改的数据,若按正序撤销会在中间状态破坏外键或业务约束。
35
+ const opers = [...(oper$log || [])].sort((a, b) => (b[SeqAttribute] || 0) - (a[SeqAttribute] || 0));
36
+ for (const oper of opers) {
34
37
  const { action, targetEntity, filter, undoData, iState } = oper;
35
38
  assert(iState === 'normal', '非正常状态的oper怎能回滚');
36
39
  switch (action) {
@@ -82,26 +85,21 @@ async function undoLog(log, context) {
82
85
  }
83
86
  }
84
87
  }
85
- await context.operate('log', {
88
+ await context.operate('oper', {
86
89
  id: 'dummy',
87
90
  action: 'undo',
88
- data: {
89
- iState: 'rollbacked',
90
- oper$log: [{
91
- id: 'dummy',
92
- action: 'undo',
93
- data: {
94
- iState: 'rollbacked',
95
- },
96
- }],
97
- },
98
- filter: {
99
- id: log.id,
100
- },
91
+ data: { iState: 'rollbacked' },
92
+ filter: { log: { id: log.id } },
101
93
  }, {
102
94
  blockTrigger: true,
103
95
  dontCreateOper: true,
104
96
  });
97
+ await context.operate('log', {
98
+ id: 'dummy',
99
+ action: 'update',
100
+ data: { iState: 'rollbacked' },
101
+ filter: { id: log.id },
102
+ }, { blockTrigger: true, dontCreateOper: true });
105
103
  }
106
104
  /**
107
105
  * 重做某条日志的修改
@@ -110,7 +108,9 @@ async function undoLog(log, context) {
110
108
  */
111
109
  async function redoLog(log, context) {
112
110
  const { oper$log } = log;
113
- for (const oper of oper$log) {
111
+ // Redo 与 Undo 相反,必须严格恢复原执行顺序,确保后续 Oper 所依赖的数据已经就绪。
112
+ const opers = [...(oper$log || [])].sort((a, b) => (a[SeqAttribute] || 0) - (b[SeqAttribute] || 0));
113
+ for (const oper of opers) {
114
114
  const { action, targetEntity, filter, data, iState } = oper;
115
115
  assert(iState === 'rollbacked', '非正常状态的oper怎能回滚');
116
116
  switch (action) {
@@ -151,33 +151,194 @@ async function redoLog(log, context) {
151
151
  }
152
152
  }
153
153
  }
154
- await context.operate('log', {
154
+ await context.operate('oper', {
155
155
  id: 'dummy',
156
156
  action: 'redo',
157
- data: {
158
- iState: 'normal',
159
- oper$log: [{
160
- id: 'dummy',
161
- action: 'redo',
162
- data: {
163
- iState: 'normal',
164
- },
165
- }],
166
- },
167
- filter: {
168
- id: log.id,
169
- },
157
+ data: { iState: 'normal' },
158
+ filter: { log: { id: log.id } },
170
159
  }, {
171
160
  blockTrigger: true,
172
161
  dontCreateOper: true,
173
162
  });
163
+ await context.operate('log', {
164
+ id: 'dummy',
165
+ action: 'update',
166
+ data: { iState: 'normal' },
167
+ filter: { id: log.id },
168
+ }, { blockTrigger: true, dontCreateOper: true });
174
169
  }
175
170
  export const triggers = [
176
171
  {
177
- name: '当log被undo时,同一entity上所有在其之后的log全部回滚',
172
+ name: '创建version时确定当前日志锚点并归档未归属日志',
173
+ entity: 'version',
174
+ action: 'create',
175
+ when: 'before',
176
+ fn: async ({ operation }, context) => {
177
+ const data = operation.data;
178
+ assert(!(data instanceof Array));
179
+ const { entity, entityId } = data;
180
+ assert(entity && entityId);
181
+ const latest = await context.select('log', {
182
+ data: { id: 1, [SeqAttribute]: 1 },
183
+ filter: { entity, entityId, iState: 'normal' },
184
+ sorter: [{ $attr: { [SeqAttribute]: 1 }, $direction: 'desc' }],
185
+ count: 1,
186
+ }, { dontCollect: true });
187
+ const latestLog = latest[0];
188
+ const logId = data.log?.id || latestLog?.id;
189
+ // Version 只能描述对象当前状态,不能插入历史 Log 流中。应用显式传入锚点时,
190
+ // 也必须与当前最大的 normal Log 一致;未传锚点则由框架自动补上该 Log。
191
+ if (data.log?.id) {
192
+ assert(latestLog?.id === data.log.id, 'Version只能创建在当前最大的normal Log上');
193
+ }
194
+ if (!logId) {
195
+ return 0;
196
+ }
197
+ if (!data.log) {
198
+ data.log = { id: logId };
199
+ }
200
+ return 0;
201
+ },
202
+ },
203
+ {
204
+ name: '创建version后归档未归属日志',
205
+ entity: 'version',
206
+ action: 'create',
207
+ when: 'after',
208
+ fn: async ({ operation }, context) => {
209
+ const data = operation.data;
210
+ assert(!(data instanceof Array));
211
+ const { entity, entityId } = data;
212
+ const [version] = await context.select('version', {
213
+ data: { id: 1, log: { id: 1, [SeqAttribute]: 1 } },
214
+ filter: { id: data.id },
215
+ }, { dontCollect: true });
216
+ const anchorSeq = version?.log?.[SeqAttribute];
217
+ // 只归档尚未属于其它 Version、且不超过当前锚点的 Log。该内部更新不产生 Oper/Log,
218
+ // 避免“维护历史成员关系”本身再次污染历史记录。
219
+ const result = await context.operate('log', {
220
+ id: 'dummy',
221
+ action: 'update',
222
+ data: { versionId: data.id },
223
+ filter: {
224
+ entity,
225
+ entityId,
226
+ iState: 'normal',
227
+ versionId: { $exists: false },
228
+ ...(anchorSeq === undefined ? {} : { [SeqAttribute]: { $lte: anchorSeq } }),
229
+ },
230
+ }, { blockTrigger: true, dontCreateOper: true });
231
+ return result.log?.update || 0;
232
+ },
233
+ },
234
+ {
235
+ name: 'undo到指定Version完成时的状态',
236
+ entity: 'version',
237
+ action: 'undo',
238
+ when: 'before',
239
+ fn: async ({ operation }, context) => {
240
+ const ids = getRelevantIds(operation.filter);
241
+ assert(ids.length === 1);
242
+ const [version] = await context.select('version', {
243
+ data: { id: 1, entity: 1, entityId: 1, iState: 1, log: { id: 1, [SeqAttribute]: 1 } },
244
+ filter: { id: ids[0] },
245
+ }, { dontCollect: true });
246
+ assert(version);
247
+ assert(version.iState === 'normal', '只能undo到当前有效的Version');
248
+ const anchorSeq = version.log?.[SeqAttribute];
249
+ const logs = await context.select('log', {
250
+ data: {
251
+ id: 1,
252
+ entity: 1,
253
+ entityId: 1,
254
+ [SeqAttribute]: 1,
255
+ oper$log: {
256
+ $entity: 'oper',
257
+ data: { id: 1, undoData: 1, action: 1, targetEntity: 1, filter: 1, iState: 1, [SeqAttribute]: 1 },
258
+ },
259
+ },
260
+ filter: {
261
+ entity: version.entity,
262
+ entityId: version.entityId,
263
+ iState: 'normal',
264
+ ...(anchorSeq === undefined ? {} : { [SeqAttribute]: { $gt: anchorSeq } }),
265
+ },
266
+ sorter: [{ $attr: { [SeqAttribute]: 1 }, $direction: 'desc' }],
267
+ }, { dontCollect: true });
268
+ for (const log of logs) {
269
+ await undoLog(log, context);
270
+ }
271
+ await context.operate('version', {
272
+ id: 'dummy',
273
+ action: 'update',
274
+ data: { iState: 'rollbacked' },
275
+ filter: {
276
+ id: { $ne: ids[0] },
277
+ entity: version.entity,
278
+ entityId: version.entityId,
279
+ iState: 'normal',
280
+ ...(anchorSeq === undefined ? {} : { log: { [SeqAttribute]: { $gt: anchorSeq } } }),
281
+ },
282
+ }, { blockTrigger: true, dontCreateOper: true });
283
+ return logs.length;
284
+ },
285
+ },
286
+ {
287
+ name: 'redo到指定Version完成时的状态',
288
+ entity: 'version',
289
+ action: 'redo',
290
+ when: 'before',
291
+ fn: async ({ operation }, context) => {
292
+ const ids = getRelevantIds(operation.filter);
293
+ assert(ids.length === 1);
294
+ const [version] = await context.select('version', {
295
+ data: { id: 1, entity: 1, entityId: 1, iState: 1, log: { id: 1, [SeqAttribute]: 1 } },
296
+ filter: { id: ids[0] },
297
+ }, { dontCollect: true });
298
+ assert(version);
299
+ assert(version.iState === 'rollbacked', '只能redo到已回滚的Version');
300
+ const anchorSeq = version.log?.[SeqAttribute];
301
+ const logs = anchorSeq === undefined ? [] : await context.select('log', {
302
+ data: {
303
+ id: 1,
304
+ entity: 1,
305
+ entityId: 1,
306
+ [SeqAttribute]: 1,
307
+ oper$log: {
308
+ $entity: 'oper',
309
+ data: { id: 1, data: 1, action: 1, targetEntity: 1, filter: 1, iState: 1, [SeqAttribute]: 1 },
310
+ },
311
+ },
312
+ filter: {
313
+ entity: version.entity,
314
+ entityId: version.entityId,
315
+ iState: 'rollbacked',
316
+ [SeqAttribute]: { $lte: anchorSeq },
317
+ },
318
+ sorter: [{ $attr: { [SeqAttribute]: 1 }, $direction: 'asc' }],
319
+ }, { dontCollect: true, forUpdate: true });
320
+ for (const log of logs) {
321
+ await redoLog(log, context);
322
+ }
323
+ const result = await context.operate('version', {
324
+ id: 'dummy',
325
+ action: 'update',
326
+ data: { iState: 'normal' },
327
+ filter: {
328
+ entity: version.entity,
329
+ entityId: version.entityId,
330
+ iState: 'rollbacked',
331
+ ...(anchorSeq === undefined ? { id: version.id } : { log: { [SeqAttribute]: { $lte: anchorSeq } } }),
332
+ },
333
+ }, { blockTrigger: true, dontCreateOper: true });
334
+ return logs.length + (result.version?.update || 0);
335
+ },
336
+ },
337
+ {
338
+ name: 'undo到指定Log完成时的状态',
178
339
  entity: 'log',
179
340
  action: 'undo',
180
- when: 'after',
341
+ when: 'before',
181
342
  fn: async ({ operation }, context) => {
182
343
  const { filter } = operation;
183
344
  assert(filter);
@@ -186,6 +347,7 @@ export const triggers = [
186
347
  const [log] = await context.select('log', {
187
348
  data: {
188
349
  id: 1,
350
+ iState: 1,
189
351
  oper$log: {
190
352
  $entity: 'oper',
191
353
  data: {
@@ -194,6 +356,7 @@ export const triggers = [
194
356
  action: 1,
195
357
  targetEntity: 1,
196
358
  filter: 1,
359
+ iState: 1,
197
360
  [SeqAttribute]: 1,
198
361
  },
199
362
  },
@@ -205,6 +368,7 @@ export const triggers = [
205
368
  id: ids[0],
206
369
  },
207
370
  }, { dontCollect: true });
371
+ assert(log.iState === 'normal', '只能undo到当前有效的Log');
208
372
  const logs = await context.select('log', {
209
373
  data: {
210
374
  id: 1,
@@ -217,6 +381,7 @@ export const triggers = [
217
381
  targetEntity: 1,
218
382
  filter: 1,
219
383
  iState: 1,
384
+ [SeqAttribute]: 1,
220
385
  },
221
386
  },
222
387
  iState: 1,
@@ -229,7 +394,7 @@ export const triggers = [
229
394
  entity: log.entity,
230
395
  entityId: log.entityId,
231
396
  [SeqAttribute]: {
232
- $gte: log[SeqAttribute],
397
+ $gt: log[SeqAttribute],
233
398
  },
234
399
  },
235
400
  sorter: [
@@ -241,18 +406,28 @@ export const triggers = [
241
406
  }
242
407
  ]
243
408
  }, { dontCollect: true });
244
- assert(logs.length > 0);
245
409
  for (const log2 of logs) {
246
410
  await undoLog(log2, context);
247
411
  }
248
- return logs.length;
412
+ const result = await context.operate('version', {
413
+ id: 'dummy',
414
+ action: 'update',
415
+ data: { iState: 'rollbacked' },
416
+ filter: {
417
+ entity: log.entity,
418
+ entityId: log.entityId,
419
+ iState: 'normal',
420
+ log: { [SeqAttribute]: { $gt: log[SeqAttribute] } },
421
+ },
422
+ }, { blockTrigger: true, dontCreateOper: true });
423
+ return logs.length + (result.version?.update || 0);
249
424
  }
250
425
  },
251
426
  {
252
- name: '当log被redo时,同一entity上所有在其之前的log全部重做',
427
+ name: 'redo到指定Log完成时的状态',
253
428
  entity: 'log',
254
429
  action: 'redo',
255
- when: 'after',
430
+ when: 'before',
256
431
  fn: async ({ operation }, context) => {
257
432
  const { filter } = operation;
258
433
  assert(filter);
@@ -281,6 +456,7 @@ export const triggers = [
281
456
  id: ids[0],
282
457
  },
283
458
  }, { dontCollect: true });
459
+ assert(log.iState === 'rollbacked', '只能redo到已回滚的Log');
284
460
  const logs = await context.select('log', {
285
461
  data: {
286
462
  id: 1,
@@ -293,6 +469,7 @@ export const triggers = [
293
469
  targetEntity: 1,
294
470
  filter: 1,
295
471
  iState: 1,
472
+ [SeqAttribute]: 1,
296
473
  },
297
474
  },
298
475
  iState: 1,
@@ -304,9 +481,7 @@ export const triggers = [
304
481
  iState: 'rollbacked',
305
482
  entity: log.entity,
306
483
  entityId: log.entityId,
307
- [SeqAttribute]: {
308
- $lte: log[SeqAttribute],
309
- },
484
+ [SeqAttribute]: { $lte: log[SeqAttribute] },
310
485
  },
311
486
  sorter: [
312
487
  {
@@ -317,11 +492,21 @@ export const triggers = [
317
492
  }
318
493
  ]
319
494
  }, { dontCollect: true, forUpdate: true });
320
- assert(logs.length > 0);
321
495
  for (const log2 of logs) {
322
496
  await redoLog(log2, context);
323
497
  }
324
- return logs.length;
498
+ const result = await context.operate('version', {
499
+ id: 'dummy',
500
+ action: 'update',
501
+ data: { iState: 'normal' },
502
+ filter: {
503
+ entity: log.entity,
504
+ entityId: log.entityId,
505
+ iState: 'rollbacked',
506
+ log: { [SeqAttribute]: { $lte: log[SeqAttribute] } },
507
+ },
508
+ }, { blockTrigger: true, dontCreateOper: true });
509
+ return logs.length + (result.version?.update || 0);
325
510
  }
326
511
  },
327
512
  {
@@ -333,6 +518,16 @@ export const triggers = [
333
518
  const { data } = operation;
334
519
  assert(!(data instanceof Array));
335
520
  const { entity, entityId } = data;
521
+ const versionResult = await context.operate('version', {
522
+ id: 'dummy',
523
+ action: 'update',
524
+ data: { log: null },
525
+ filter: {
526
+ entity: entity,
527
+ entityId: entityId,
528
+ iState: 'rollbacked',
529
+ },
530
+ }, { blockTrigger: true, dontCreateOper: true });
336
531
  const result = await context.operate('oper', {
337
532
  id: 'dummy',
338
533
  action: 'remove',
@@ -355,7 +550,7 @@ export const triggers = [
355
550
  iState: 'rollbacked',
356
551
  },
357
552
  }, {});
358
- return (result.oper?.remove || 0) + (result2.log?.remove || 0);
553
+ return (versionResult.version?.update || 0) + (result.oper?.remove || 0) + (result2.log?.remove || 0);
359
554
  }
360
555
  },
361
556
  // 在删除log时,删除与此log关联的oper
@@ -1,6 +1,7 @@
1
1
  export interface Context {
2
+ getAspectName(): string | undefined;
2
3
  getCurrentTxnId(): string | undefined;
3
- getCurrentUserId(allowUnloggedIn?: boolean): string | undefined;
4
+ getCurrentUserId<T extends boolean = false>(allowUnloggedIn?: T): T extends true ? string | undefined : string;
4
5
  isRoot(): boolean;
5
6
  allowUserUpdate(): boolean;
6
7
  toString(): Promise<string>;
@@ -56,11 +56,21 @@ export type Q_ExistsValue = {
56
56
  $exists: boolean;
57
57
  };
58
58
  export type Q_GeoOperand = Geo | Point[];
59
+ export type Q_DistanceUnit = 'm' | 'km';
60
+ export type Q_DistanceRadius = {
61
+ value: number | {
62
+ $expr: RefOrExpression<string>;
63
+ };
64
+ unit: Q_DistanceUnit;
65
+ };
66
+ export type Q_DistanceComparisonValue = Q_NumberComparisonValue | {
67
+ $lte: Q_DistanceRadius;
68
+ };
59
69
  export type Q_GeoValue = Q_ExistsValue | OneOf<{
60
70
  $contains: Q_GeoOperand;
61
71
  $within: Q_GeoOperand;
62
72
  $intersects: Q_GeoOperand;
63
- $distance: [Q_GeoOperand, Q_NumberComparisonValue];
73
+ $distance: [Q_GeoOperand, Q_DistanceComparisonValue];
64
74
  }>;
65
75
  export type Q_NumberValue = Q_NumberComparisonValue | Q_ExistsValue;
66
76
  export type Q_DecimalValue = Q_DecimalComparisonValue | Q_ExistsValue;
@@ -36,6 +36,13 @@ export type WechatMpEnv = {
36
36
  };
37
37
  localStorageEnabled: true;
38
38
  };
39
+ /**
40
+ * Alipay exposes the same core device/window fields used by Oak. Provider-only
41
+ * authorization fields stay optional until Oak consumes them explicitly.
42
+ */
43
+ export type AlipayMpEnv = Omit<WechatMpEnv, 'type'> & {
44
+ type: 'alipayMp';
45
+ };
39
46
  export type WebEnv = {
40
47
  type: 'web';
41
48
  visitorId: string;
@@ -83,8 +90,9 @@ export type NativeEnv = {
83
90
  visitorId: string;
84
91
  type: 'native';
85
92
  };
86
- export type Environment = WechatMpEnv | WebEnv | ServerEnv | NativeEnv;
87
- export type ClientRuntime = 'browser' | 'tauri' | 'electron' | 'wechatMp' | 'native';
93
+ export type MiniProgramEnv = WechatMpEnv | AlipayMpEnv;
94
+ export type Environment = MiniProgramEnv | WebEnv | ServerEnv | NativeEnv;
95
+ export type ClientRuntime = 'browser' | 'tauri' | 'electron' | 'wechatMp' | 'alipayMp' | 'native';
88
96
  export type BriefClientEnvironment = {
89
97
  runtime: ClientRuntime;
90
98
  runtimeVersion?: string;
@@ -104,6 +112,9 @@ export type BriefEnv = {
104
112
  system: string;
105
113
  brand?: string;
106
114
  model?: string;
115
+ /** Provider-neutral mini-program runtime summary. */
116
+ miniProgram?: string;
117
+ /** @deprecated Use `miniProgram`; retained for WeChat payload compatibility. */
107
118
  wechat?: string;
108
119
  explorer?: string;
109
120
  client?: BriefClientEnvironment;
@@ -5,8 +5,8 @@ declare const OAK_EXCEPTION_SYMBOL: unique symbol;
5
5
  /**
6
6
  * 判断一个对象是否为 OakException 实例,作用和 `instanceof` 类似,但更安全可靠。
7
7
  * 如OakUserException继承自OakException,使用时可传入OakUserException以进一步确认类型。
8
- * @param obj 需要判断的对象
9
- * @param exceptionName 可选参数,指定异常类名以进一步确认
8
+ * @param value 需要判断的对象
9
+ * @param errType 可选参数,指定异常类型以进一步确认
10
10
  *
11
11
  * ```ts
12
12
  * if (isOakException(e, OakUserException)) {
@@ -16,7 +16,7 @@ declare const OAK_EXCEPTION_SYMBOL: unique symbol;
16
16
  *
17
17
  * @returns 如果对象是 OakException 实例则返回 true,否则返回 false
18
18
  */
19
- export declare function isOakException<T extends OakException<any>>(obj: any, errType?: new () => T): obj is T;
19
+ export declare function isOakException<T extends OakException<any> = OakException<any>>(value: unknown, errType?: new (...args: any[]) => T): value is T;
20
20
  export declare class OakException<ED extends EntityDict & BaseEntityDict> extends Error {
21
21
  opRecords: OpRecord<ED>[];
22
22
  _module?: string;
@@ -4,8 +4,8 @@ const OAK_EXCEPTION_SYMBOL = Symbol.for('oak-domain:exception');
4
4
  /**
5
5
  * 判断一个对象是否为 OakException 实例,作用和 `instanceof` 类似,但更安全可靠。
6
6
  * 如OakUserException继承自OakException,使用时可传入OakUserException以进一步确认类型。
7
- * @param obj 需要判断的对象
8
- * @param exceptionName 可选参数,指定异常类名以进一步确认
7
+ * @param value 需要判断的对象
8
+ * @param errType 可选参数,指定异常类型以进一步确认
9
9
  *
10
10
  * ```ts
11
11
  * if (isOakException(e, OakUserException)) {
@@ -15,39 +15,42 @@ const OAK_EXCEPTION_SYMBOL = Symbol.for('oak-domain:exception');
15
15
  *
16
16
  * @returns 如果对象是 OakException 实例则返回 true,否则返回 false
17
17
  */
18
- export function isOakException(obj, errType) {
19
- if (!obj) {
18
+ export function isOakException(value, errType) {
19
+ if (typeof value !== 'object' || value === null) {
20
20
  return false;
21
21
  }
22
- const isOakException = !!obj[OAK_EXCEPTION_SYMBOL];
23
- if (isOakException) {
22
+ try {
23
+ if (value[OAK_EXCEPTION_SYMBOL] !== true) {
24
+ return false;
25
+ }
24
26
  if (!errType) {
25
27
  return true;
26
28
  }
27
- }
28
- else {
29
- return false;
30
- }
31
- try {
32
- let proto = Object.getPrototypeOf(obj);
29
+ if (value instanceof errType) {
30
+ return true;
31
+ }
33
32
  const expectedName = errType.name;
34
- let depth = 0;
35
- const maxDepth = 10; // 防止无限循环
36
- while (proto && depth < maxDepth) {
37
- const constructorName = proto.constructor?.name;
38
- if (constructorName === expectedName) {
33
+ if (!expectedName) {
34
+ return false;
35
+ }
36
+ const visited = new Set();
37
+ let proto = Object.getPrototypeOf(value);
38
+ while (proto && !visited.has(proto)) {
39
+ visited.add(proto);
40
+ const constructor = Object
41
+ .getOwnPropertyDescriptor(proto, 'constructor')
42
+ ?.value;
43
+ if (typeof constructor === 'function'
44
+ && constructor.name === expectedName) {
39
45
  return true;
40
46
  }
41
47
  proto = Object.getPrototypeOf(proto);
42
- depth++;
43
48
  }
44
- return false;
45
49
  }
46
- catch (e) {
47
- console.warn(e);
48
- // 如果检查过程出错,安全返回 false
50
+ catch {
49
51
  return false;
50
52
  }
53
+ return false;
51
54
  }
52
55
  export class OakException extends Error {
53
56
  opRecords;
@@ -135,7 +135,10 @@ interface GeoContains<A> {
135
135
  interface GeoDistance<A> {
136
136
  $distance: [RefOrExpression<A> | Geo, RefOrExpression<A> | Geo];
137
137
  }
138
- type GeoExpression<A> = GeoContains<A> | GeoDistance<A>;
138
+ interface GeoNearest<A> {
139
+ $nearest: [RefOrExpression<A> | Geo, RefOrExpression<A> | Geo];
140
+ }
141
+ type GeoExpression<A> = GeoContains<A> | GeoDistance<A> | GeoNearest<A>;
139
142
  interface AggrCountExpression<A> {
140
143
  $$count: RefOrExpression<A>;
141
144
  }