directus

Форк
0
/
sanitize-schema.ts 
97 строк · 2.5 Кб
1
import type { Column } from '@directus/schema';
2
import type { Field, Relation } from '@directus/types';
3
import { pick } from 'lodash-es';
4
import type { Collection } from '../types/index.js';
5

6
/**
7
 * Pick certain database vendor specific collection properties that should be compared when performing diff
8
 *
9
 * @param collection collection to sanitize
10
 * @returns sanitized collection
11
 */
12

13
export function sanitizeCollection(collection: Collection | undefined) {
14
	if (!collection) return collection;
15

16
	return pick(collection, ['collection', 'fields', 'meta', 'schema.name']);
17
}
18

19
/**
20
 * Pick certain database vendor specific field properties that should be compared when performing diff
21
 *
22
 * @param field field to sanitize
23
 * @param sanitizeAllSchema Whether or not the whole field schema should be sanitized. Mainly used to prevent modifying autoincrement fields
24
 * @returns sanitized field
25
 */
26
export function sanitizeField(field: Field | undefined, sanitizeAllSchema = false) {
27
	if (!field) return field;
28

29
	const defaultPaths = ['collection', 'field', 'type', 'meta', 'name', 'children'];
30

31
	const pickedPaths = sanitizeAllSchema
32
		? defaultPaths
33
		: [
34
				...defaultPaths,
35
				'schema.name',
36
				'schema.table',
37
				'schema.data_type',
38
				'schema.default_value',
39
				'schema.max_length',
40
				'schema.numeric_precision',
41
				'schema.numeric_scale',
42
				'schema.is_nullable',
43
				'schema.is_unique',
44
				'schema.is_primary_key',
45
				'schema.is_generated',
46
				'schema.generation_expression',
47
				'schema.has_auto_increment',
48
				'schema.foreign_key_table',
49
				'schema.foreign_key_column',
50
		  ];
51

52
	return pick(field, pickedPaths);
53
}
54

55
export function sanitizeColumn(column: Column) {
56
	return pick(column, [
57
		'name',
58
		'table',
59
		'data_type',
60
		'default_value',
61
		'max_length',
62
		'numeric_precision',
63
		'numeric_scale',
64
		'is_nullable',
65
		'is_unique',
66
		'is_primary_key',
67
		'is_generated',
68
		'generation_expression',
69
		'has_auto_increment',
70
		'foreign_key_table',
71
		'foreign_key_column',
72
	]);
73
}
74

75
/**
76
 * Pick certain database vendor specific relation properties that should be compared when performing diff
77
 *
78
 * @param relation relation to sanitize
79
 * @returns sanitized relation
80
 */
81
export function sanitizeRelation(relation: Relation | undefined) {
82
	if (!relation) return relation;
83

84
	return pick(relation, [
85
		'collection',
86
		'field',
87
		'related_collection',
88
		'meta',
89
		'schema.table',
90
		'schema.column',
91
		'schema.foreign_key_table',
92
		'schema.foreign_key_column',
93
		'schema.constraint_name',
94
		'schema.on_update',
95
		'schema.on_delete',
96
	]);
97
}
98

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

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

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

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