directus

Форк
0
/
20231009B-update-panel-options.ts 
102 строки · 2.7 Кб
1
import type { Knex } from 'knex';
2

3
export async function up(knex: Knex) {
4
	const panels = await knex('directus_panels').where('type', '=', 'metric').select();
5

6
	const updates: Promise<number>[] = [];
7

8
	for (const panel of panels) {
9
		let options = panel.options;
10

11
		// Check if the options are stringified and parse them
12
		const wasStringified = typeof options === 'string';
13

14
		if (wasStringified) {
15
			options = JSON.parse(options);
16
		}
17

18
		// Not expected, just to be on the safe side
19
		if (!options) continue;
20

21
		let needsUpdate = false;
22

23
		// Check and update abbreviate -> notation
24
		if (options.abbreviate === true) {
25
			options.notation = 'compact';
26
			delete options.abbreviate;
27
			needsUpdate = true;
28
		}
29

30
		// Check and update decimals -> minimumFractionDigits and maximumFractionDigits
31
		if (typeof options.decimals === 'number') {
32
			options.minimumFractionDigits = options.decimals;
33
			options.maximumFractionDigits = options.decimals;
34
			delete options.decimals;
35
			needsUpdate = true;
36
		}
37

38
		// Update the row with modified options if necessary
39
		if (needsUpdate) {
40
			// Convert the options back to string if they were stringified initially
41
			if (wasStringified) {
42
				options = JSON.stringify(options);
43
			}
44

45
			updates.push(knex('directus_panels').update({ options }).where('id', panel.id));
46
		}
47
	}
48

49
	return Promise.all(updates);
50
}
51

52
export async function down(knex: Knex) {
53
	const panels = await knex('directus_panels').where('type', '=', 'metric').select();
54

55
	const updates: Promise<number>[] = [];
56

57
	for (const panel of panels) {
58
		let options = panel.options;
59

60
		// Check if the options are stringified and parse them
61
		const wasStringified = typeof options === 'string';
62

63
		if (wasStringified) {
64
			options = JSON.parse(options);
65
		}
66

67
		// Not expected, just to be on the safe side
68
		if (!options) continue;
69

70
		let needsUpdate = false;
71

72
		// Revert notation -> abbreviate
73
		if (options.notation === 'compact') {
74
			options.abbreviate = true;
75
			delete options.notation;
76
			needsUpdate = true;
77
		}
78

79
		// Revert minimumFractionDigits and maximumFractionDigits -> decimals
80
		if (
81
			typeof options.minimumFractionDigits === 'number' &&
82
			options.minimumFractionDigits === options.maximumFractionDigits
83
		) {
84
			options.decimals = options.minimumFractionDigits;
85
			delete options.minimumFractionDigits;
86
			delete options.maximumFractionDigits;
87
			needsUpdate = true;
88
		}
89

90
		// Update the row with reverted options if necessary
91
		if (needsUpdate) {
92
			// Convert the options back to string if they were stringified initially
93
			if (wasStringified) {
94
				options = JSON.stringify(options);
95
			}
96

97
			updates.push(knex('directus_panels').update({ options }).where('id', panel.id));
98
		}
99
	}
100

101
	return Promise.all(updates);
102
}
103

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.