Merge pull request #276 from directus/csv

Add csv special type, return type based on special flag
This commit is contained in:
Rijk van Zanten
2020-09-10 14:15:19 -04:00
committed by GitHub
4 changed files with 25 additions and 3 deletions

View File

@@ -66,7 +66,7 @@ export default class FieldsService {
const data = {
collection: column.table,
field: column.name,
type: column ? getLocalType(column.type) : 'alias',
type: column ? getLocalType(column.type, field?.special) : 'alias',
schema: column,
meta: field || null,
};
@@ -167,7 +167,7 @@ export default class FieldsService {
const data = {
collection,
field,
type: column ? getLocalType(column.type) : 'alias',
type: column ? getLocalType(column.type, fieldInfo?.special) : 'alias',
meta: fieldInfo || null,
schema: column || null,
};
@@ -335,6 +335,8 @@ export default class FieldsService {
const type = field.type as 'float' | 'decimal';
/** @todo add precision and scale support */
column = table[type](field.field /* precision, scale */);
} else if (field.type === 'csv') {
column = table.string(field.field);
} else {
column = table[field.type](field.field);
}

View File

@@ -123,6 +123,13 @@ export default class PayloadService {
if (action === 'update') return new Date();
return value;
},
async csv(action, value) {
if (!value) return;
if (action === 'read') return value.split(',');
if (Array.isArray(value)) return value.join(',');
return value;
},
};
processValues(action: Action, payloads: Partial<Item>[]): Promise<Partial<Item>[]>;

View File

@@ -15,6 +15,7 @@ export const types = [
'timestamp',
'binary',
'uuid',
'csv',
] as const;
export type FieldMeta = {

View File

@@ -78,9 +78,21 @@ const localTypeMap: Record<string, { type: typeof types[number]; useTimezone?: b
float8: { type: 'float' },
};
export default function getLocalType(databaseType: string): typeof types[number] | 'unknown' {
export default function getLocalType(
databaseType: string,
special?: string | null
): typeof types[number] | 'unknown' {
const type = localTypeMap[databaseType.toLowerCase().split('(')[0]];
switch (special) {
case 'json':
return 'json';
case 'csv':
return 'csv';
case 'uuid':
return 'uuid';
}
if (type) {
return type.type;
}