|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { Client } from "@datocms/cli/lib/cma-client-node"; |
| 3 | + |
| 4 | +export default async function (client: Client) { |
| 5 | + console.log("Creating new fields/fieldsets"); |
| 6 | + |
| 7 | + const itemTypes = await client.itemTypes.list(); |
| 8 | + const inspireDatasetItemType = itemTypes.find( |
| 9 | + (itemType: any) => itemType.api_key === "inspire_dataset" |
| 10 | + ) || { id: "" }; |
| 11 | + |
| 12 | + const fields = await client.fields.list(inspireDatasetItemType.id); |
| 13 | + const findField = (label: string) => |
| 14 | + fields.find((field: any) => field.label === label); |
| 15 | + |
| 16 | + let doNotReorderFields: boolean = false; |
| 17 | + |
| 18 | + if (findField("Thesaurusdatum")) { |
| 19 | + console.log('Field "Thesaurusdatum" already exists'); |
| 20 | + doNotReorderFields = true; |
| 21 | + } else { |
| 22 | + console.log( |
| 23 | + 'Create Single-line string field "thesauruslink" (`thesauruslink`) in model "Inspire Dataset" (`inspire_dataset`)' |
| 24 | + ); |
| 25 | + await client.fields.create(inspireDatasetItemType.id, { |
| 26 | + id: "NS_cGHM9SGGcfP0KyG4vgw", |
| 27 | + label: "thesauruslink", |
| 28 | + field_type: "string", |
| 29 | + api_key: "thesauruslink", |
| 30 | + appearance: { |
| 31 | + addons: [], |
| 32 | + editor: "single_line", |
| 33 | + parameters: { heading: false, placeholder: null }, |
| 34 | + }, |
| 35 | + default_value: "", |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + if (findField("Thesaurusdatum")) { |
| 40 | + console.log('Field "Thesaurusdatum" already exists'); |
| 41 | + doNotReorderFields = true; |
| 42 | + } else { |
| 43 | + console.log( |
| 44 | + 'Create Date field "Thesaurusdatum" (`thesaurusdatum`) in model "Inspire Dataset" (`inspire_dataset`)' |
| 45 | + ); |
| 46 | + await client.fields.create(inspireDatasetItemType.id, { |
| 47 | + id: "FxL4Gm4WRNq9EA4d7tkmRA", |
| 48 | + label: "Thesaurusdatum", |
| 49 | + field_type: "date", |
| 50 | + api_key: "thesaurusdatum", |
| 51 | + appearance: { addons: [], editor: "date_picker", parameters: {} }, |
| 52 | + default_value: null, |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + if (findField("Thesaurusdatum type")) { |
| 57 | + console.log('Field "Thesaurusdatum type" already exists'); |
| 58 | + doNotReorderFields = true; |
| 59 | + } else { |
| 60 | + console.log( |
| 61 | + 'Create Single-line string field "Thesaurusdatum type" (`thesaurusdatum_type`) in model "Inspire Dataset" (`inspire_dataset`)' |
| 62 | + ); |
| 63 | + await client.fields.create(inspireDatasetItemType.id, { |
| 64 | + id: "RMgqrZGfQyKy5tMXDj6Srw", |
| 65 | + label: "Thesaurusdatum type", |
| 66 | + field_type: "string", |
| 67 | + api_key: "thesaurusdatum_type", |
| 68 | + appearance: { |
| 69 | + addons: [], |
| 70 | + editor: "single_line", |
| 71 | + parameters: { heading: false, placeholder: null }, |
| 72 | + }, |
| 73 | + default_value: "", |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + if (!doNotReorderFields) { |
| 78 | + console.log("Update existing fields/fieldsets"); |
| 79 | + |
| 80 | + console.log( |
| 81 | + 'Reorder fields/fieldsets for model "Inspire Dataset" (`inspire_dataset`)' |
| 82 | + ); |
| 83 | + try { |
| 84 | + await client.itemTypes.rawReorderFieldsAndFieldsets( |
| 85 | + inspireDatasetItemType.id, |
| 86 | + { |
| 87 | + data: [ |
| 88 | + { |
| 89 | + id: "NS_cGHM9SGGcfP0KyG4vgw", |
| 90 | + type: "field", |
| 91 | + attributes: { position: 3 }, |
| 92 | + relationships: { |
| 93 | + fieldset: { |
| 94 | + data: { id: "KbDtNCwNQAqEGh-9o5G-RA", type: "fieldset" }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + { |
| 99 | + id: "FxL4Gm4WRNq9EA4d7tkmRA", |
| 100 | + type: "field", |
| 101 | + attributes: { position: 4 }, |
| 102 | + relationships: { |
| 103 | + fieldset: { |
| 104 | + data: { id: "KbDtNCwNQAqEGh-9o5G-RA", type: "fieldset" }, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + { |
| 109 | + id: "RMgqrZGfQyKy5tMXDj6Srw", |
| 110 | + type: "field", |
| 111 | + attributes: { position: 5 }, |
| 112 | + relationships: { |
| 113 | + fieldset: { |
| 114 | + data: { id: "KbDtNCwNQAqEGh-9o5G-RA", type: "fieldset" }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + { |
| 119 | + id: "JdCdVG1LTHa5UmI8NHNGBw", |
| 120 | + type: "field", |
| 121 | + attributes: { position: 1 }, |
| 122 | + relationships: { |
| 123 | + fieldset: { |
| 124 | + data: { id: "TOSwC4esQFKcXNrW20gK1Q", type: "fieldset" }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + { |
| 129 | + id: "MciOlCyATDqKAeWCVthfgg", |
| 130 | + type: "field", |
| 131 | + attributes: { position: 0 }, |
| 132 | + relationships: { |
| 133 | + fieldset: { |
| 134 | + data: { id: "TOSwC4esQFKcXNrW20gK1Q", type: "fieldset" }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + { |
| 139 | + id: "fFAppifMRJeYDqnflH6d8w", |
| 140 | + type: "field", |
| 141 | + attributes: { position: 3 }, |
| 142 | + relationships: { |
| 143 | + fieldset: { |
| 144 | + data: { id: "IWFo-RIcRFGq75fjuAh35A", type: "fieldset" }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + { |
| 149 | + id: "EToHPBiYTSOZ80SxmAWcpg", |
| 150 | + type: "field", |
| 151 | + attributes: { position: 0 }, |
| 152 | + relationships: { |
| 153 | + fieldset: { |
| 154 | + data: { id: "INy1cspgRTCwgrfVa2fTDw", type: "fieldset" }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + { |
| 159 | + id: "bYdFNcOASiuFG5wVKfLYGw", |
| 160 | + type: "field", |
| 161 | + attributes: { position: 2 }, |
| 162 | + relationships: { |
| 163 | + fieldset: { |
| 164 | + data: { id: "VJVAflbjSxaaCl2OF4vaCA", type: "fieldset" }, |
| 165 | + }, |
| 166 | + }, |
| 167 | + }, |
| 168 | + { |
| 169 | + id: "BEF9VIoJRgiKa6SZSZQTDw", |
| 170 | + type: "field", |
| 171 | + attributes: { position: 1 }, |
| 172 | + relationships: { |
| 173 | + fieldset: { |
| 174 | + data: { id: "d5CCVL3pSxGMdchqXBBQJQ", type: "fieldset" }, |
| 175 | + }, |
| 176 | + }, |
| 177 | + }, |
| 178 | + { |
| 179 | + id: "LoXTVry5RTWfgDj2l9OAhw", |
| 180 | + type: "field", |
| 181 | + attributes: { position: 2 }, |
| 182 | + relationships: { |
| 183 | + fieldset: { |
| 184 | + data: { id: "KbDtNCwNQAqEGh-9o5G-RA", type: "fieldset" }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + }, |
| 188 | + { |
| 189 | + id: "NzxaGK5sSoyuErg9Tabo1Q", |
| 190 | + type: "field", |
| 191 | + attributes: { position: 2 }, |
| 192 | + relationships: { |
| 193 | + fieldset: { |
| 194 | + data: { id: "INy1cspgRTCwgrfVa2fTDw", type: "fieldset" }, |
| 195 | + }, |
| 196 | + }, |
| 197 | + }, |
| 198 | + { |
| 199 | + id: "ELSrYu9EQO60TyeUB_bDzA", |
| 200 | + type: "field", |
| 201 | + attributes: { position: 1 }, |
| 202 | + relationships: { |
| 203 | + fieldset: { |
| 204 | + data: { id: "INy1cspgRTCwgrfVa2fTDw", type: "fieldset" }, |
| 205 | + }, |
| 206 | + }, |
| 207 | + }, |
| 208 | + { |
| 209 | + id: "Ei2sKTDgSZO6RsFquxiiaw", |
| 210 | + type: "field", |
| 211 | + attributes: { position: 0 }, |
| 212 | + relationships: { |
| 213 | + fieldset: { |
| 214 | + data: { id: "d5CCVL3pSxGMdchqXBBQJQ", type: "fieldset" }, |
| 215 | + }, |
| 216 | + }, |
| 217 | + }, |
| 218 | + { |
| 219 | + id: "GbevT-_8T1O68xw1ijxFXQ", |
| 220 | + type: "field", |
| 221 | + attributes: { position: 0 }, |
| 222 | + relationships: { |
| 223 | + fieldset: { |
| 224 | + data: { id: "c4ZNV0cQTamwB4mBwbSFpg", type: "fieldset" }, |
| 225 | + }, |
| 226 | + }, |
| 227 | + }, |
| 228 | + { |
| 229 | + id: "Bw5WSGe9TGu_3LhSFjJobw", |
| 230 | + type: "field", |
| 231 | + attributes: { position: 7 }, |
| 232 | + relationships: { |
| 233 | + fieldset: { |
| 234 | + data: { id: "J7A-JpKBROyVXigOyRdNyQ", type: "fieldset" }, |
| 235 | + }, |
| 236 | + }, |
| 237 | + }, |
| 238 | + { |
| 239 | + id: "KbDtNCwNQAqEGh-9o5G-RA", |
| 240 | + type: "fieldset", |
| 241 | + attributes: { position: 5 }, |
| 242 | + }, |
| 243 | + { |
| 244 | + id: "LkE2P0h0RlG3N3kKty2fEQ", |
| 245 | + type: "fieldset", |
| 246 | + attributes: { position: 6 }, |
| 247 | + }, |
| 248 | + { |
| 249 | + id: "IWFo-RIcRFGq75fjuAh35A", |
| 250 | + type: "fieldset", |
| 251 | + attributes: { position: 2 }, |
| 252 | + }, |
| 253 | + { |
| 254 | + id: "VJVAflbjSxaaCl2OF4vaCA", |
| 255 | + type: "fieldset", |
| 256 | + attributes: { position: 3 }, |
| 257 | + }, |
| 258 | + { |
| 259 | + id: "INy1cspgRTCwgrfVa2fTDw", |
| 260 | + type: "fieldset", |
| 261 | + attributes: { position: 4 }, |
| 262 | + }, |
| 263 | + { |
| 264 | + id: "TOSwC4esQFKcXNrW20gK1Q", |
| 265 | + type: "fieldset", |
| 266 | + attributes: { position: 7 }, |
| 267 | + }, |
| 268 | + { |
| 269 | + id: "c4ZNV0cQTamwB4mBwbSFpg", |
| 270 | + type: "fieldset", |
| 271 | + attributes: { position: 8 }, |
| 272 | + }, |
| 273 | + { |
| 274 | + id: "d5CCVL3pSxGMdchqXBBQJQ", |
| 275 | + type: "fieldset", |
| 276 | + attributes: { position: 9 }, |
| 277 | + }, |
| 278 | + ], |
| 279 | + } |
| 280 | + ); |
| 281 | + } catch (error) { |
| 282 | + console.error(error); |
| 283 | + } |
| 284 | + } |
| 285 | +} |
0 commit comments