@steedos/service-metadata-objects 2.1.76 → 2.1.84

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.
@@ -1,6 +1,6 @@
1
1
  import _ = require("lodash");
2
2
  import { METADATA_TYPE } from ".";
3
- import { getObjectServiceName, getOriginalObject, refreshObject } from "./objects";
3
+ import { getObjectServiceName, getOriginalObject, refreshObject, MONGO_BASE_OBJECT, SQL_BASE_OBJECT } from "./objects";
4
4
  export type SObject = {
5
5
  name: string,
6
6
  [x: string]: any
@@ -26,7 +26,7 @@ export class ActionHandlers {
26
26
  onRegister: any = null;
27
27
  onDestroy: any = null;
28
28
  onRegistered: any = null;
29
- registerObjectMemEntry: Map<string, number>;
29
+ registerObjectMemEntry: Map<string, any>;
30
30
 
31
31
  constructor(onRegister, onDestroy, onRegistered){
32
32
  this.onRegister = onRegister;
@@ -36,11 +36,13 @@ export class ActionHandlers {
36
36
  }
37
37
 
38
38
  async registerObject(ctx, objectApiName, data, meta) {
39
- if(this.onRegister && _.isFunction(this.onRegister)){
39
+ if (objectApiName != MONGO_BASE_OBJECT &&
40
+ objectApiName != SQL_BASE_OBJECT && this.onRegister && _.isFunction(this.onRegister)) {
40
41
  await this.onRegister(data)
41
42
  }
42
43
  await ctx.broker.call('metadata.add', {key: cacherKey(objectApiName), data: data}, {meta: meta});
43
- if(this.onRegistered && _.isFunction(this.onRegistered)){
44
+ if (objectApiName != MONGO_BASE_OBJECT &&
45
+ objectApiName != SQL_BASE_OBJECT && this.onRegistered && _.isFunction(this.onRegistered)) {
44
46
  await this.onRegistered(data)
45
47
  }
46
48
  // 为每个对象 setTimeout 延时执行
@@ -51,7 +53,7 @@ export class ActionHandlers {
51
53
  }
52
54
  timeoutId = setTimeout(function(){
53
55
  ctx.broker.emit("metadata.objects.inserted", {objectApiName: objectApiName, isInsert: true});
54
- ctx.broker.emit(`@${objectApiName}.metadata.objects.inserted`, {objectApiName: objectApiName, isInsert: true, data: data});
56
+ ctx.broker.broadcast(`${data.datasource}.@${objectApiName}.metadata.objects.inserted`, { objectApiName: objectApiName, isInsert: true, data: data });
55
57
  registerObjectMemEntry.delete(objectApiName);
56
58
  }, DELAY_MESSAGE_OF_OBJECT_CHANGED);
57
59
  registerObjectMemEntry.set(objectApiName, timeoutId);
@@ -95,19 +97,24 @@ export class ActionHandlers {
95
97
  metadataType: METADATA_TYPE,
96
98
  metadataApiName: metadataApiName,
97
99
  }, {meta: ctx.meta});
98
-
99
- if(metadataConfig && metadataConfig.metadata){
100
+
101
+ if (metadataConfig && metadataConfig.metadata) {
100
102
  config.list_views = _.defaultsDeep(metadataConfig.metadata.list_views || {}, config.list_views || {});
101
103
  config = _.defaultsDeep(config, metadataConfig.metadata);
102
104
  }
103
105
  }
104
- await ctx.broker.call('metadata.addServiceMetadata', {key: cacherKey(metadataApiName), data: config}, {meta: Object.assign({}, ctx.meta, {metadataType: METADATA_TYPE, metadataApiName: metadataApiName})})
106
+
107
+ await ctx.broker.call('metadata.addServiceMetadata', { key: cacherKey(metadataApiName), data: config }, { meta: Object.assign({}, ctx.meta, { metadataType: METADATA_TYPE, metadataApiName: metadataApiName }) })
108
+
105
109
  const objectConfig = await refreshObject(ctx, metadataApiName);
106
- if(!objectConfig){
107
- return ;
110
+
111
+ if (!objectConfig) {
112
+ return;
108
113
  }
114
+
109
115
  const objectServiceName = getObjectServiceName(metadataApiName);
110
- return await this.registerObject(ctx, metadataApiName, objectConfig, {
116
+
117
+ const result = await this.registerObject(ctx, metadataApiName, objectConfig, {
111
118
  caller: {
112
119
  // nodeID: broker.nodeID,
113
120
  service: {
@@ -117,6 +124,44 @@ export class ActionHandlers {
117
124
  }
118
125
  }
119
126
  });
127
+ return result;
128
+ }
129
+
130
+ async addConfigs(ctx: any): Promise<boolean> {
131
+ let configs = ctx.params.data;
132
+ for (let config of configs) {
133
+ if (config.extend) {
134
+ config.name = config.extend
135
+ }
136
+ const metadataApiName = config.name;
137
+ if (!config.isMain) {
138
+ const metadataConfig = await ctx.broker.call('metadata.getServiceMetadata', {
139
+ serviceName: ctx.meta.metadataServiceName,
140
+ metadataType: METADATA_TYPE,
141
+ metadataApiName: metadataApiName,
142
+ }, { meta: ctx.meta });
143
+ if (metadataConfig && metadataConfig.metadata) {
144
+ config.list_views = _.defaultsDeep(metadataConfig.metadata.list_views || {}, config.list_views || {});
145
+ config = _.defaultsDeep(config, metadataConfig.metadata);
146
+ }
147
+ }
148
+ await ctx.broker.call('metadata.addServiceMetadata', { key: cacherKey(metadataApiName), data: config }, { meta: Object.assign({}, ctx.meta, { metadataType: METADATA_TYPE, metadataApiName: metadataApiName }) })
149
+ const objectConfig = await refreshObject(ctx, metadataApiName);
150
+ if (!objectConfig) {
151
+ return;
152
+ }
153
+ const objectServiceName = getObjectServiceName(metadataApiName);
154
+ await this.registerObject(ctx, metadataApiName, objectConfig, {
155
+ caller: {
156
+ // nodeID: broker.nodeID,
157
+ service: {
158
+ name: objectServiceName,
159
+ // version: broker.service.version, TODO
160
+ // fullName: broker.service.fullName, TODO
161
+ }
162
+ }
163
+ });
164
+ }
120
165
  }
121
166
 
122
167
  async change(ctx: any): Promise<boolean> {
@@ -83,6 +83,12 @@ export class LookupActionHandler {
83
83
  })
84
84
  return _.uniq(detailName);
85
85
  }
86
+
87
+ getDetailsInfoKey(objectApiName: string) {
88
+ return this.getDetailKey(objectApiName, "*");
89
+ }
90
+
91
+
86
92
  async getDetailsInfo(objectApiName: string){
87
93
  const detailsInfo = [];
88
94
  let records = (await this.broker.call('metadata.filter', { key: this.getDetailKey(objectApiName, "*") }, { meta: {} })) || {};
@@ -215,6 +215,9 @@ export class MasterDetailActionHandler{
215
215
  return _.uniq(mastersName);
216
216
  }
217
217
 
218
+ getMastersInfoKey(objectApiName: string) {
219
+ return this.getMasterKey(objectApiName, "*");
220
+ }
218
221
  async getMastersInfo(objectApiName: string){
219
222
  const mastersInfo = [];
220
223
  let records = (await this.broker.call('metadata.filter', { key: this.getMasterKey(objectApiName, "*") }, { meta: {} })) || {};
@@ -257,6 +260,10 @@ export class MasterDetailActionHandler{
257
260
  return _.uniq(detailsName);
258
261
  }
259
262
 
263
+ getDetailsInfoKey(objectApiName: string) {
264
+ return this.getDetailKey(objectApiName, "*", "*");
265
+ }
266
+
260
267
  async getDetailsInfo(objectApiName: string){
261
268
  const detailsInfo = [];
262
269
  let records = (await this.broker.call('metadata.filter', { key: this.getDetailKey(objectApiName, "*", "*") }, { meta: {} })) || {};
@@ -11,6 +11,19 @@ export function getObjectServiceName(objectApiName: string) {
11
11
  return `@${objectApiName}`;
12
12
  }
13
13
 
14
+ function isExpried(expiredAt: number) {
15
+ return expiredAt <= new Date().getTime();
16
+ }
17
+
18
+ const BASE_OBJECT_CACHE = {
19
+ [MONGO_BASE_OBJECT]: {
20
+ data: null,
21
+ },
22
+ [SQL_BASE_OBJECT]: {
23
+ data: null,
24
+ }
25
+ };
26
+
14
27
  async function getBaseObjectConfig(ctx, datasourceName) {
15
28
  const serviceName = "*";
16
29
  const metadataType = METADATA_TYPE;
@@ -18,12 +31,25 @@ async function getBaseObjectConfig(ctx, datasourceName) {
18
31
  if (datasourceName === "default" || datasourceName === "meteor") {
19
32
  metadataApiName = MONGO_BASE_OBJECT;
20
33
  }
34
+
35
+ const cacheData = BASE_OBJECT_CACHE[metadataApiName];
36
+ if (cacheData && cacheData.data && isExpried(cacheData.expiredAt)) {
37
+ return cacheData.data;
38
+ }
39
+
21
40
  const configs = await ctx.broker.call(`metadata.getServiceMetadatas`, {
22
41
  serviceName,
23
42
  metadataType,
24
43
  metadataApiName: metadataApiName,
25
44
  }, {meta: ctx.meta});
26
- return configs && configs.length > 0 ? configs[0]?.metadata : null;
45
+
46
+ const config = configs && configs.length > 0 ? configs[0]?.metadata : null;
47
+ BASE_OBJECT_CACHE[metadataApiName] = {
48
+ data: config,
49
+ expiredAt: new Date().getTime() + 10 * 1000
50
+ }
51
+
52
+ return config;
27
53
  }
28
54
 
29
55
  async function getObjectConfigs(ctx, objectApiName) {
@@ -5,6 +5,8 @@ import { FormulaActionHandler } from './formula/formulaActionHandler';
5
5
  import { SummaryActionHandler } from './summary/summaryActionHandler';
6
6
  import { LookupActionHandler } from './lookup/LookupActionHandler';
7
7
  import { METADATA_TYPE } from '.';
8
+ import * as _ from 'lodash';
9
+
8
10
  module.exports = {
9
11
  name: "objects",
10
12
  /**
@@ -70,6 +72,11 @@ module.exports = {
70
72
  return await this.objetActionHandlers.addConfig(ctx);
71
73
  }
72
74
  },
75
+ addConfigs: {
76
+ async handler(ctx) {
77
+ return await this.objetActionHandlers.addConfigs(ctx);
78
+ }
79
+ },
73
80
  change: {
74
81
  async handler(ctx) {
75
82
  return await this.objetActionHandlers.change(ctx);
@@ -168,6 +175,21 @@ module.exports = {
168
175
  return await this.lookupActionHandler.getDetailsInfo(objectApiName);
169
176
  }
170
177
  },
178
+ getRelationsInfo: {
179
+ async handler(ctx) {
180
+ const { objectApiName } = ctx.params;
181
+ const detailsInfoKey = this.masterDetailActionHandler.getDetailsInfoKey(objectApiName);
182
+ const mastersInfo = this.masterDetailActionHandler.getMastersInfoKey(objectApiName);
183
+ const lookupDetailsInfo = this.lookupActionHandler.getDetailsInfoKey(objectApiName);
184
+
185
+ const results = await ctx.broker.call('metadata.mfilter', { keys: [detailsInfoKey, mastersInfo, lookupDetailsInfo] });
186
+ return {
187
+ details: _.compact(_.map(results[0], 'metadata')),
188
+ masters: _.compact(_.map(results[1], 'metadata')),
189
+ lookup_details: _.compact(_.map(results[2], 'metadata')),
190
+ }
191
+ }
192
+ },
171
193
  getMasterPaths: {
172
194
  async handler(ctx) {
173
195
  const { objectApiName } = ctx.params;