|
1 | | -const helper = require('../helper/helper.js'); |
2 | | -const schemaHelper = require('../helper/schemaHelper.js'); |
| 1 | +const { getFieldsSchema } = require('./helpers/getFieldsSchema'); |
| 2 | +const { getMappingScript } = require('./helpers/getMappingScript'); |
| 3 | +const { getTypeSchema } = require('./helpers/getTypeSchema'); |
| 4 | +const { getCurlScript } = require('./helpers/getCurlScript'); |
| 5 | +const { getKibanaScript } = require('./helpers/getKibanaScript'); |
3 | 6 |
|
4 | 7 | module.exports = { |
5 | 8 | generateScript(data, logger, cb) { |
6 | | - const { jsonSchema, modelData, entityData, isUpdateScript } = data; |
7 | | - const containerData = data.containerData || {}; |
| 9 | + const { |
| 10 | + jsonSchema, |
| 11 | + modelData, |
| 12 | + entityData, |
| 13 | + isUpdateScript, |
| 14 | + pluginConfiguration, |
| 15 | + internalDefinitions, |
| 16 | + modelDefinitions, |
| 17 | + externalDefinitions, |
| 18 | + containerData = {}, |
| 19 | + } = data; |
| 20 | + |
8 | 21 | let result = ''; |
9 | | - let fieldsSchema = this.getFieldsSchema({ |
| 22 | + |
| 23 | + const fieldsSchema = getFieldsSchema({ |
10 | 24 | jsonSchema: JSON.parse(jsonSchema), |
11 | | - internalDefinitions: JSON.parse(data.internalDefinitions), |
12 | | - modelDefinitions: JSON.parse(data.modelDefinitions), |
13 | | - externalDefinitions: JSON.parse(data.externalDefinitions), |
| 25 | + internalDefinitions: JSON.parse(internalDefinitions), |
| 26 | + modelDefinitions: JSON.parse(modelDefinitions), |
| 27 | + externalDefinitions: JSON.parse(externalDefinitions), |
| 28 | + fieldLevelConfig: pluginConfiguration.fieldLevelConfig, |
14 | 29 | }); |
15 | | - let typeSchema = this.getTypeSchema(entityData, fieldsSchema); |
16 | | - let mappingScript = this.getMappingScript(containerData, typeSchema); |
| 30 | + |
| 31 | + const typeSchema = getTypeSchema(entityData, fieldsSchema); |
| 32 | + |
| 33 | + const mappingScript = getMappingScript(containerData, typeSchema, pluginConfiguration.containerLevelConfig); |
17 | 34 |
|
18 | 35 | if (isUpdateScript) { |
19 | | - result = this.getCurlScript(mappingScript, modelData, containerData); |
| 36 | + result = getCurlScript(mappingScript, modelData, containerData); |
20 | 37 | } else { |
21 | | - result += this.getKibanaScript(mappingScript, containerData); |
| 38 | + result += getKibanaScript(mappingScript, containerData); |
22 | 39 | } |
23 | 40 |
|
24 | 41 | cb(null, result); |
25 | 42 | }, |
26 | | - |
27 | | - getCurlScript(mapping, modelData, indexData) { |
28 | | - const host = modelData.host || 'localhost'; |
29 | | - const port = modelData.port || 9200; |
30 | | - const indexName = indexData.name || ''; |
31 | | - const majorVersion = +(modelData.dbVersion || '').split('.').shift(); |
32 | | - const includeTypeName = majorVersion >= 7 ? '&include_type_name=true' : ''; |
33 | | - |
34 | | - return `curl -XPUT '${host}:${port}/${indexName.toLowerCase()}?pretty${includeTypeName}' -H 'Content-Type: application/json' -d '\n${JSON.stringify(mapping, null, 4)}\n'`; |
35 | | - }, |
36 | | - |
37 | | - getKibanaScript(mapping, indexData) { |
38 | | - const indexName = indexData.name || ''; |
39 | | - |
40 | | - return `PUT /${indexName.toLowerCase()}\n${JSON.stringify(mapping, null, 4)}`; |
41 | | - }, |
42 | | - |
43 | | - getFieldsSchema(data) { |
44 | | - const { jsonSchema } = data; |
45 | | - let schema = {}; |
46 | | - |
47 | | - if (!(jsonSchema.properties && jsonSchema.properties._source && jsonSchema.properties._source.properties)) { |
48 | | - return schema; |
49 | | - } |
50 | | - |
51 | | - schema = this.getSchemaByItem(jsonSchema.properties._source.properties, data); |
52 | | - |
53 | | - return schema; |
54 | | - }, |
55 | | - |
56 | | - getSchemaByItem(properties, data) { |
57 | | - let schema = {}; |
58 | | - |
59 | | - for (let fieldName in properties) { |
60 | | - let field = properties[fieldName]; |
61 | | - |
62 | | - schema[fieldName] = this.getField(field, data); |
63 | | - } |
64 | | - |
65 | | - return schema; |
66 | | - }, |
67 | | - |
68 | | - getField(field, data) { |
69 | | - let schema = {}; |
70 | | - const fieldProperties = helper.getFieldProperties(field.type, field, {}); |
71 | | - let type = this.getFieldType(field); |
72 | | - |
73 | | - if (type !== 'object' && type !== 'array') { |
74 | | - schema.type = type; |
75 | | - } |
76 | | - |
77 | | - if (type === 'object') { |
78 | | - schema.properties = {}; |
79 | | - } |
80 | | - |
81 | | - this.setProperties(schema, fieldProperties, data); |
82 | | - |
83 | | - if (type === 'alias') { |
84 | | - return { ...schema, ...this.getAliasSchema(field, data) }; |
85 | | - } else if (type === 'join') { |
86 | | - return { ...schema, ...this.getJoinSchema(field) }; |
87 | | - } else if ( |
88 | | - [ |
89 | | - 'completion', |
90 | | - 'sparse_vector', |
91 | | - 'dense_vector', |
92 | | - 'geo_shape', |
93 | | - 'geo_point', |
94 | | - 'rank_feature', |
95 | | - 'rank_features', |
96 | | - ].includes(type) |
97 | | - ) { |
98 | | - return schema; |
99 | | - } else if (field.properties) { |
100 | | - schema.properties = this.getSchemaByItem(field.properties, data); |
101 | | - } else if (field.items) { |
102 | | - let arrData = field.items; |
103 | | - |
104 | | - if (Array.isArray(field.items)) { |
105 | | - arrData = field.items[0]; |
106 | | - } |
107 | | - |
108 | | - schema = { ...schema, ...this.getField(arrData, data) }; |
109 | | - } |
110 | | - |
111 | | - return schema; |
112 | | - }, |
113 | | - |
114 | | - getFieldType(field) { |
115 | | - switch (field.type) { |
116 | | - case 'geo-shape': |
117 | | - return 'geo_shape'; |
118 | | - case 'geo-point': |
119 | | - return 'geo_point'; |
120 | | - case 'number': |
121 | | - return field.mode || 'long'; |
122 | | - case 'string': |
123 | | - return field.mode || 'text'; |
124 | | - case 'range': |
125 | | - return field.mode || 'integer_range'; |
126 | | - case 'null': |
127 | | - return 'long'; |
128 | | - default: |
129 | | - return field.type; |
130 | | - } |
131 | | - }, |
132 | | - |
133 | | - setProperties(schema, properties, data) { |
134 | | - for (let propName in properties) { |
135 | | - if (propName === 'stringfields') { |
136 | | - try { |
137 | | - schema['fields'] = JSON.parse(properties[propName]); |
138 | | - } catch (e) {} |
139 | | - } else if (this.isFieldList(properties[propName])) { |
140 | | - const names = schemaHelper.getNamesByIds( |
141 | | - properties[propName].map(item => item.keyId), |
142 | | - [data.jsonSchema, data.internalDefinitions, data.modelDefinitions, data.externalDefinitions], |
143 | | - ); |
144 | | - if (names.length) { |
145 | | - schema[propName] = names.length === 1 ? names[0] : names; |
146 | | - } |
147 | | - } else { |
148 | | - schema[propName] = properties[propName]; |
149 | | - } |
150 | | - } |
151 | | - |
152 | | - return schema; |
153 | | - }, |
154 | | - |
155 | | - getTypeSchema(typeData, fieldsSchema) { |
156 | | - let script = {}; |
157 | | - |
158 | | - if (typeData.dynamic) { |
159 | | - script.dynamic = typeData.dynamic; |
160 | | - } |
161 | | - |
162 | | - script.properties = fieldsSchema; |
163 | | - |
164 | | - return { |
165 | | - [(typeData.collectionName || '').toLowerCase()]: script, |
166 | | - }; |
167 | | - }, |
168 | | - |
169 | | - getMappingScript(indexData, typeSchema) { |
170 | | - let mappingScript = {}; |
171 | | - let settings = this.getSettings(indexData); |
172 | | - let aliases = this.getAliases(indexData); |
173 | | - |
174 | | - if (settings) { |
175 | | - mappingScript.settings = settings; |
176 | | - } |
177 | | - |
178 | | - if (aliases) { |
179 | | - mappingScript.aliases = aliases; |
180 | | - } |
181 | | - |
182 | | - mappingScript.mappings = typeSchema; |
183 | | - |
184 | | - return mappingScript; |
185 | | - }, |
186 | | - |
187 | | - getSettings(indexData) { |
188 | | - let settings; |
189 | | - let properties = helper.getContainerLevelProperties(); |
190 | | - |
191 | | - properties.forEach(propertyName => { |
192 | | - if (indexData[propertyName]) { |
193 | | - if (!settings) { |
194 | | - settings = {}; |
195 | | - } |
196 | | - |
197 | | - settings[propertyName] = indexData[propertyName]; |
198 | | - } |
199 | | - }); |
200 | | - |
201 | | - return settings; |
202 | | - }, |
203 | | - |
204 | | - getAliases(indexData) { |
205 | | - let aliases; |
206 | | - |
207 | | - if (!indexData.aliases) { |
208 | | - return aliases; |
209 | | - } |
210 | | - |
211 | | - indexData.aliases.forEach(alias => { |
212 | | - if (alias.name) { |
213 | | - if (!aliases) { |
214 | | - aliases = {}; |
215 | | - } |
216 | | - |
217 | | - aliases[alias.name] = {}; |
218 | | - |
219 | | - if (alias.filter) { |
220 | | - let filterData = ''; |
221 | | - try { |
222 | | - filterData = JSON.parse(alias.filter); |
223 | | - } catch (e) {} |
224 | | - |
225 | | - aliases[alias.name].filter = { |
226 | | - term: filterData, |
227 | | - }; |
228 | | - } |
229 | | - |
230 | | - if (alias.routing) { |
231 | | - aliases[alias.name].routing = alias.routing; |
232 | | - } |
233 | | - } |
234 | | - }); |
235 | | - |
236 | | - return aliases; |
237 | | - }, |
238 | | - |
239 | | - isFieldList(property) { |
240 | | - if (!Array.isArray(property)) { |
241 | | - return false; |
242 | | - } |
243 | | - |
244 | | - if (!property[0]) { |
245 | | - return false; |
246 | | - } |
247 | | - |
248 | | - if (property[0].keyId) { |
249 | | - return true; |
250 | | - } |
251 | | - |
252 | | - return false; |
253 | | - }, |
254 | | - |
255 | | - getJoinSchema(field) { |
256 | | - if (!Array.isArray(field.relations)) { |
257 | | - return {}; |
258 | | - } |
259 | | - |
260 | | - const relations = field.relations.reduce((result, item) => { |
261 | | - if (!item.parent) { |
262 | | - return result; |
263 | | - } |
264 | | - |
265 | | - if (!Array.isArray(item.children)) { |
266 | | - return result; |
267 | | - } |
268 | | - |
269 | | - if (item.children.length === 1) { |
270 | | - return { |
271 | | - ...result, |
272 | | - [item.parent]: item.children?.[0]?.name |
273 | | - }; |
274 | | - } |
275 | | - |
276 | | - return { |
277 | | - ...result, |
278 | | - [item.parent]: item.children.map(item => item.name || '') |
279 | | - }; |
280 | | - }, {}); |
281 | | - |
282 | | - return { relations }; |
283 | | - }, |
284 | | - |
285 | | - getAliasSchema(field, data) { |
286 | | - if (!Array.isArray(field.path)) { |
287 | | - return {}; |
288 | | - } |
289 | | - |
290 | | - if (field.path.length === 0) { |
291 | | - return {}; |
292 | | - } |
293 | | - |
294 | | - const pathName = schemaHelper.getPathName(field.path[0].keyId, [ |
295 | | - data.jsonSchema, |
296 | | - data.internalDefinitions, |
297 | | - data.modelDefinitions, |
298 | | - data.externalDefinitions, |
299 | | - ]); |
300 | | - |
301 | | - return { path: pathName }; |
302 | | - }, |
303 | 43 | }; |
0 commit comments