directus
1import type { Column } from '@directus/schema';2import type { Field, Relation } from '@directus/types';3import { pick } from 'lodash-es';4import 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
13export function sanitizeCollection(collection: Collection | undefined) {14if (!collection) return collection;15
16return 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*/
26export function sanitizeField(field: Field | undefined, sanitizeAllSchema = false) {27if (!field) return field;28
29const defaultPaths = ['collection', 'field', 'type', 'meta', 'name', 'children'];30
31const pickedPaths = sanitizeAllSchema32? defaultPaths33: [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
52return pick(field, pickedPaths);53}
54
55export function sanitizeColumn(column: Column) {56return 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*/
81export function sanitizeRelation(relation: Relation | undefined) {82if (!relation) return relation;83
84return 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