directus

Форк
0
/
get-snapshot-diff.ts 
115 строк · 4.0 Кб
1
import deepDiff from 'deep-diff';
2
import type { Snapshot, SnapshotDiff } from '../types/index.js';
3
import { DiffKind } from '../types/index.js';
4
import { sanitizeCollection, sanitizeField, sanitizeRelation } from './sanitize-schema.js';
5

6
export function getSnapshotDiff(current: Snapshot, after: Snapshot): SnapshotDiff {
7
	const diffedSnapshot: SnapshotDiff = {
8
		collections: [
9
			...current.collections.map((currentCollection) => {
10
				const afterCollection = after.collections.find(
11
					(afterCollection) => afterCollection.collection === currentCollection.collection,
12
				);
13

14
				return {
15
					collection: currentCollection.collection,
16
					diff: deepDiff.diff(sanitizeCollection(currentCollection), sanitizeCollection(afterCollection)),
17
				};
18
			}),
19
			...after.collections
20
				.filter((afterCollection) => {
21
					const currentCollection = current.collections.find(
22
						(currentCollection) => currentCollection.collection === afterCollection.collection,
23
					);
24

25
					return !!currentCollection === false;
26
				})
27
				.map((afterCollection) => ({
28
					collection: afterCollection.collection,
29
					diff: deepDiff.diff(undefined, sanitizeCollection(afterCollection)),
30
				})),
31
		].filter((obj) => Array.isArray(obj.diff)) as SnapshotDiff['collections'],
32
		fields: [
33
			...current.fields.map((currentField) => {
34
				const afterField = after.fields.find(
35
					(afterField) => afterField.collection === currentField.collection && afterField.field === currentField.field,
36
				);
37

38
				const isAutoIncrementPrimaryKey =
39
					!!currentField.schema?.is_primary_key && !!currentField.schema?.has_auto_increment;
40

41
				return {
42
					collection: currentField.collection,
43
					field: currentField.field,
44
					diff: deepDiff.diff(
45
						sanitizeField(currentField, isAutoIncrementPrimaryKey),
46
						sanitizeField(afterField, isAutoIncrementPrimaryKey),
47
					),
48
				};
49
			}),
50
			...after.fields
51
				.filter((afterField) => {
52
					const currentField = current.fields.find(
53
						(currentField) =>
54
							currentField.collection === afterField.collection && afterField.field === currentField.field,
55
					);
56

57
					return !!currentField === false;
58
				})
59
				.map((afterField) => ({
60
					collection: afterField.collection,
61
					field: afterField.field,
62
					diff: deepDiff.diff(undefined, sanitizeField(afterField)),
63
				})),
64
		].filter((obj) => Array.isArray(obj.diff)) as SnapshotDiff['fields'],
65

66
		relations: [
67
			...current.relations.map((currentRelation) => {
68
				const afterRelation = after.relations.find(
69
					(afterRelation) =>
70
						afterRelation.collection === currentRelation.collection && afterRelation.field === currentRelation.field,
71
				);
72

73
				return {
74
					collection: currentRelation.collection,
75
					field: currentRelation.field,
76
					related_collection: currentRelation.related_collection,
77
					diff: deepDiff.diff(sanitizeRelation(currentRelation), sanitizeRelation(afterRelation)),
78
				};
79
			}),
80
			...after.relations
81
				.filter((afterRelation) => {
82
					const currentRelation = current.relations.find(
83
						(currentRelation) =>
84
							currentRelation.collection === afterRelation.collection && afterRelation.field === currentRelation.field,
85
					);
86

87
					return !!currentRelation === false;
88
				})
89
				.map((afterRelation) => ({
90
					collection: afterRelation.collection,
91
					field: afterRelation.field,
92
					related_collection: afterRelation.related_collection,
93
					diff: deepDiff.diff(undefined, sanitizeRelation(afterRelation)),
94
				})),
95
		].filter((obj) => Array.isArray(obj.diff)) as SnapshotDiff['relations'],
96
	};
97

98
	/**
99
	 * When you delete a collection, we don't have to individually drop all the fields/relations as well
100
	 */
101

102
	const deletedCollections = diffedSnapshot.collections
103
		.filter((collection) => collection.diff?.[0]?.kind === DiffKind.DELETE)
104
		.map(({ collection }) => collection);
105

106
	diffedSnapshot.fields = diffedSnapshot.fields.filter(
107
		(field) => deletedCollections.includes(field.collection) === false,
108
	);
109

110
	diffedSnapshot.relations = diffedSnapshot.relations.filter(
111
		(relation) => deletedCollections.includes(relation.collection) === false,
112
	);
113

114
	return diffedSnapshot;
115
}
116

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

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

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

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