@simitgroup/simpleapp-generator 2.0.0-x-alpha → 2.0.0-y-alpha

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/ReleaseNote.md CHANGED
@@ -1,3 +1,7 @@
1
+ [2.0.0y-alpha]
2
+
3
+ 1. Allow mini app adjust built in schema
4
+
1
5
  [2.0.0x-alpha]
2
6
 
3
7
  1. Fix patch many
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simitgroup/simpleapp-generator",
3
- "version": "2.0.0x-alpha",
3
+ "version": "2.0.0y-alpha",
4
4
  "description": "frontend nuxtjs and backend nests code generator using jsonschema",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -82,16 +82,17 @@ export class <%= upperFirstCase(it.name)%>Client extends SimpleAppClient<openapi
82
82
 
83
83
  <% if(it.customField.isEnable) { %>
84
84
  // FLAG::CUSTOM_FIELD
85
- const { $customFieldStore, $miniAppStore } = useNuxtApp();
86
- if (!this._customFieldWatch) {
87
- this._customFieldWatch = watch(
88
- [() => $customFieldStore.data, () => $miniAppStore.installedMiniApps],
89
- ([newCustomField, newMiniApps], [oldCustomField, oldMiniApps]) => {
90
- this.prepareCustomFieldJsonSchema();
91
- },
92
- { immediate: false, deep: true },
93
- );
94
- }
85
+ // const { $customFieldStore, $miniAppStore } = useNuxtApp();
86
+ // if (!this._customFieldWatch) {
87
+ // this._customFieldWatch = watch(
88
+ // [() => $customFieldStore.data, () => $miniAppStore.installedMiniApps],
89
+ // ([newCustomField, newMiniApps], [oldCustomField, oldMiniApps]) => {
90
+ // console.log("Run");
91
+ // this.prepareCustomFieldJsonSchema();
92
+ // },
93
+ // { immediate: false, deep: true },
94
+ // );
95
+ // }
95
96
  <% } %>
96
97
 
97
98
  }
@@ -20,6 +20,7 @@ import { WatchHandle } from "vue";
20
20
  import { CustomFieldService } from "../features/customField/services/CustomFieldService";
21
21
  import { CustomFieldDataMode } from "../features/customField/types/common";
22
22
  import { CustomFieldDataModeEnum } from "../features/customField/enums/common";
23
+ import { cloneDeep, isEmpty } from "lodash";
23
24
 
24
25
  // import { useToast, } from 'primevue/usetoast';
25
26
  // import type { ToastMessageOptions } from 'primevue/toast';
@@ -68,7 +69,8 @@ export class SimpleAppClient<
68
69
  getDocType = () => this.doctype;
69
70
  getDocName = (capFirst: boolean = false) =>
70
71
  capFirst ? upperFirst(this.docname) : this.docname;
71
- getResourceName = ()=>upperFirst(this.schema["x-simpleapp-config"]?.resourceName??'');
72
+ getResourceName = () =>
73
+ upperFirst(this.schema["x-simpleapp-config"]?.resourceName ?? "");
72
74
  setNew = () => {};
73
75
  isNew = () => this.data.value?.created == "";
74
76
  setSchema = (schema: SchemaType) => (this.schema = schema);
@@ -85,7 +87,51 @@ export class SimpleAppClient<
85
87
  Object.assign(this.data.value, data);
86
88
  };
87
89
 
90
+ /**
91
+ * Adjust built in standard field validation rules
92
+ */
93
+ private prepareSchemaPatches() {
94
+ const patches = this._customFieldService.prepareSchemaPatches(this.docname);
95
+ if (patches && !isEmpty(patches)) {
96
+ const schema = this.applySchemaPatch(patches);
97
+ this.schema = schema;
98
+ }
99
+ }
100
+
101
+ private applySchemaPatch(patches: { [key: string]: string | number }) {
102
+ const allowedPatchAttributes = ["required", "minLength", "maxLength"];
103
+ const schema = cloneDeep(this.schema);
104
+ for (const [path, value] of Object.entries(patches)) {
105
+ const keys = path.split(".");
106
+ let target: any = schema;
107
+
108
+ // Traverse into nested path (e.g. properties.studentGroup.properties._id)
109
+ for (let i = 0; i < keys.length - 1; i++) {
110
+ const key = keys[i];
111
+ if (!(key in target)) target[key] = {};
112
+ target = target[key];
113
+ }
114
+
115
+ const attributeName = keys[keys.length - 1];
116
+ if (allowedPatchAttributes.includes(attributeName)) {
117
+ if (attributeName === "required" && Array.isArray(value)) {
118
+ target.required = Array.from(
119
+ new Set([...(target.required ?? []), ...value]),
120
+ );
121
+ } else {
122
+ target[attributeName] = value;
123
+ }
124
+ } else {
125
+ console.error("Found invalid schema patch: ", path, value);
126
+ }
127
+ }
128
+
129
+ return schema;
130
+ }
131
+
88
132
  prepareCustomFieldJsonSchema() {
133
+ this.prepareSchemaPatches();
134
+
89
135
  const resp = this._customFieldService.prepareCustomFieldJsonSchema(
90
136
  this.docname,
91
137
  );
@@ -1,11 +1,11 @@
1
- import _ from 'lodash';
1
+ import _ from "lodash";
2
2
  import {
3
3
  CustomFieldDataMode,
4
4
  CustomFieldMoreSchema,
5
- CustomFieldMoreSchemaGroup
6
- } from '../types/common';
7
- import { CustomFieldDataModeEnum } from '../enums/common';
8
- import { DynamicObject, SimpleAppJSONSchema7Definition } from '~/types';
5
+ CustomFieldMoreSchemaGroup,
6
+ } from "../types/common";
7
+ import { CustomFieldDataModeEnum } from "../enums/common";
8
+ import { DynamicObject, SimpleAppJSONSchema7Definition } from "~/types";
9
9
 
10
10
  export class CustomFieldService {
11
11
  // =============================== Prepare Schema Function ===============================
@@ -18,16 +18,20 @@ export class CustomFieldService {
18
18
  }
19
19
 
20
20
  const schema: CustomFieldMoreSchema = {
21
- type: 'object',
21
+ type: "object",
22
22
  properties: {
23
23
  ...customFieldFromDB,
24
- ...customFieldFromMiniApp
25
- }
24
+ ...customFieldFromMiniApp,
25
+ },
26
26
  };
27
27
 
28
28
  return schema;
29
29
  }
30
30
 
31
+ prepareSchemaPatches(collectionName: string) {
32
+ return this.findSchemaPatchFromMiniApp(collectionName);
33
+ }
34
+
31
35
  private findCustomFieldSchemaFromDB(collectionName: string) {
32
36
  const { $customFieldStore } = useNuxtApp();
33
37
  if (!$customFieldStore.data || _.isEmpty($customFieldStore.data)) {
@@ -48,9 +52,9 @@ export class CustomFieldService {
48
52
 
49
53
  const schema: CustomFieldMoreSchemaGroup = {
50
54
  default: {
51
- title: 'Custom Field',
52
- ...customField.form.jsonSchema
53
- }
55
+ title: "Custom Field",
56
+ ...customField.form.jsonSchema,
57
+ },
54
58
  };
55
59
 
56
60
  return schema;
@@ -76,12 +80,32 @@ export class CustomFieldService {
76
80
  return schema;
77
81
  }
78
82
 
83
+ private findSchemaPatchFromMiniApp(collectionName: string) {
84
+ const { $miniAppStore } = useNuxtApp();
85
+
86
+ const miniAppForms = $miniAppStore.getForm(collectionName);
87
+ if (!miniAppForms || miniAppForms.length <= 0) {
88
+ return {};
89
+ }
90
+
91
+ for (let i = 0; i < miniAppForms.length; i++) {
92
+ const miniAppItem = miniAppForms[i];
93
+ const { patches } = miniAppItem;
94
+
95
+ if (patches && !_.isEmpty(patches)) {
96
+ return patches;
97
+ }
98
+ }
99
+
100
+ return {};
101
+ }
102
+
79
103
  // ================================ Prepare Data Function ================================
80
104
  prepareCustomFieldData<T extends DynamicObject>(
81
105
  collectionName: string,
82
106
  mode: CustomFieldDataMode,
83
107
  customSchema: CustomFieldMoreSchema | undefined,
84
- resourceData: T | undefined
108
+ resourceData: T | undefined,
85
109
  ): T | undefined {
86
110
  if (
87
111
  !customSchema ||
@@ -103,18 +127,18 @@ export class CustomFieldService {
103
127
 
104
128
  return this.prepareCustomFieldWithExistingData(
105
129
  customSchema,
106
- resourceData
130
+ resourceData,
107
131
  );
108
132
  break;
109
133
  }
110
134
  }
111
135
 
112
136
  private prepareCustomFieldWithDefaultData<T extends DynamicObject>(
113
- customSchema: CustomFieldMoreSchema
137
+ customSchema: CustomFieldMoreSchema,
114
138
  ) {
115
139
  const data: DynamicObject = {};
116
140
  for (const [groupName, groupItem] of Object.entries(
117
- customSchema.properties
141
+ customSchema.properties,
118
142
  )) {
119
143
  const defaultData = this.generateDefaultData(groupItem);
120
144
 
@@ -126,16 +150,16 @@ export class CustomFieldService {
126
150
 
127
151
  private prepareCustomFieldWithExistingData<T extends DynamicObject>(
128
152
  customSchema: CustomFieldMoreSchema,
129
- resourceData: T
153
+ resourceData: T,
130
154
  ) {
131
155
  const data: DynamicObject = {};
132
156
  for (const [groupName, groupItem] of Object.entries(
133
- customSchema.properties
157
+ customSchema.properties,
134
158
  )) {
135
159
  const defaultData = this.generateDefaultData(groupItem);
136
160
  const mergedData = this.mergeWithDefault(
137
161
  defaultData,
138
- resourceData?.[groupName]
162
+ resourceData?.[groupName],
139
163
  );
140
164
 
141
165
  data[groupName] = mergedData;
@@ -146,13 +170,13 @@ export class CustomFieldService {
146
170
 
147
171
  private generateDefaultData(schema: SimpleAppJSONSchema7Definition): any {
148
172
  if (!_.isObject(schema)) {
149
- return '';
173
+ return "";
150
174
  }
151
175
 
152
- if (schema.type === 'object') {
176
+ if (schema.type === "object") {
153
177
  const result: any = {};
154
178
 
155
- if (schema?.['x-foreignkey']) {
179
+ if (schema?.["x-foreignkey"]) {
156
180
  return undefined;
157
181
  }
158
182
 
@@ -163,31 +187,33 @@ export class CustomFieldService {
163
187
  return result;
164
188
  }
165
189
 
166
- if (schema.type === 'array') {
190
+ if (schema.type === "array") {
167
191
  const itemSchema = schema.items;
168
192
  const minItems = schema.minItems || 0;
169
193
  const arr = [];
170
194
  const itemsCount = minItems > 0 ? 1 : 0;
171
195
  for (let i = 0; i < itemsCount; i++) {
172
196
  arr.push(
173
- this.generateDefaultData(itemSchema as SimpleAppJSONSchema7Definition)
197
+ this.generateDefaultData(
198
+ itemSchema as SimpleAppJSONSchema7Definition,
199
+ ),
174
200
  );
175
201
  }
176
202
  return arr;
177
203
  }
178
204
 
179
- if (typeof schema.default !== 'undefined') {
205
+ if (typeof schema.default !== "undefined") {
180
206
  return schema.default;
181
207
  }
182
208
 
183
209
  // handle primitive types fallback
184
210
  switch (schema.type) {
185
- case 'string':
186
- return '';
187
- case 'number':
188
- case 'integer':
211
+ case "string":
212
+ return "";
213
+ case "number":
214
+ case "integer":
189
215
  return 0;
190
- case 'boolean':
216
+ case "boolean":
191
217
  return false;
192
218
  default:
193
219
  return null;
@@ -196,7 +222,7 @@ export class CustomFieldService {
196
222
 
197
223
  private mergeWithDefault<T extends DynamicObject>(
198
224
  defaultData: T,
199
- collectionData: T | undefined
225
+ collectionData: T | undefined,
200
226
  ) {
201
227
  if (Array.isArray(defaultData)) {
202
228
  if (Array.isArray(collectionData)) {
@@ -213,17 +239,17 @@ export class CustomFieldService {
213
239
  // return defaultData;
214
240
  return [];
215
241
  }
216
- } else if (typeof defaultData === 'object' && defaultData !== null) {
242
+ } else if (typeof defaultData === "object" && defaultData !== null) {
217
243
  const result: any = {};
218
244
  for (const key of Object.keys(defaultData)) {
219
245
  result[key] = this.mergeWithDefault(
220
246
  defaultData[key],
221
- collectionData?.[key]
247
+ collectionData?.[key],
222
248
  );
223
249
  }
224
250
  return result;
225
251
  } else {
226
- return typeof collectionData !== 'undefined'
252
+ return typeof collectionData !== "undefined"
227
253
  ? collectionData
228
254
  : defaultData;
229
255
  }