Rename database/system to schema/meta in api

This commit is contained in:
rijkvanzanten
2020-08-06 13:40:12 -04:00
parent c50bb811cb
commit d8584ffad8
6 changed files with 54 additions and 57 deletions

View File

@@ -59,14 +59,14 @@ const newFieldSchema = Joi.object({
collection: Joi.string().optional(),
field: Joi.string().required(),
type: Joi.string().valid(...types),
database: Joi.object({
schema: Joi.object({
comment: Joi.string(),
default_value: Joi.any(),
max_length: [Joi.number(), Joi.string()],
is_nullable: Joi.bool(),
}),
/** @todo base this on default validation */
system: Joi.any(),
meta: Joi.any(),
});
router.post(

View File

@@ -27,9 +27,9 @@ export default class CollectionsService {
if (!collection.fields) collection.fields = [];
collection.fields = collection.fields.map((field) => {
if (field.system) {
field.system = {
...field.system,
if (field.meta) {
field.meta = {
...field.meta,
field: field.field,
collection: collection.collection!,
};
@@ -76,8 +76,8 @@ export default class CollectionsService {
await collectionItemsService.create(collectionInfo);
const fieldPayloads = payload
.fields!.filter((field) => field.system)
.map((field) => field.system);
.fields!.filter((field) => field.meta)
.map((field) => field.meta);
await fieldItemsService.create(fieldPayloads);
@@ -122,7 +122,7 @@ export default class CollectionsService {
const tablesInDatabase = await schemaInspector.tableInfo();
const tables = tablesInDatabase.filter((table) => collectionKeys.includes(table.name));
const system: any[] = await collectionItemsService.readByQuery({
const meta: any[] = await collectionItemsService.readByQuery({
filter: { collection: { _in: collectionKeys } },
});
@@ -131,8 +131,8 @@ export default class CollectionsService {
for (const table of tables) {
const collection: Collection = {
collection: table.name,
system: system.find((systemInfo) => systemInfo.collection === table.name) || null,
database: table,
meta: meta.find((systemInfo) => systemInfo.collection === table.name) || null,
schema: table,
};
collections.push(collection);
@@ -160,7 +160,7 @@ export default class CollectionsService {
}
const tablesToFetchInfoFor = tablesInDatabase.map((table) => table.name);
const system: any[] = await collectionItemsService.readByQuery({
const meta: any[] = await collectionItemsService.readByQuery({
filter: { collection: { _in: tablesToFetchInfoFor } },
});
@@ -169,8 +169,8 @@ export default class CollectionsService {
for (const table of tablesInDatabase) {
const collection: Collection = {
collection: table.name,
system: system.find((systemInfo) => systemInfo.collection === table.name) || null,
database: table,
meta: meta.find((systemInfo) => systemInfo.collection === table.name) || null,
schema: table,
};
collections.push(collection);
@@ -198,11 +198,11 @@ export default class CollectionsService {
if (data && key) {
const payload = data as Partial<Collection>;
if (!payload.system) {
if (!payload.meta) {
throw new InvalidPayloadException(`"system" key is required`);
}
return (await collectionItemsService.update(payload.system!, key as any)) as
return (await collectionItemsService.update(payload.meta!, key as any)) as
| string
| string[];
}
@@ -211,7 +211,7 @@ export default class CollectionsService {
const collectionUpdates = payloads.map((collection) => {
return {
...collection.system,
...collection.meta,
collection: collection.collection,
};
});

View File

@@ -1,7 +1,7 @@
import database, { schemaInspector } from '../database';
import { Field } from '../types/field';
import { uniq } from 'lodash';
import { Accountability, AbstractServiceOptions, System } from '../types';
import { Accountability, AbstractServiceOptions, FieldMeta } from '../types';
import ItemsService from '../services/items';
import { ColumnBuilder } from 'knex';
import getLocalType from '../utils/get-local-type';
@@ -44,17 +44,17 @@ export default class FieldsService {
}
async readAll(collection?: string) {
let fields: System[];
let fields: FieldMeta[];
if (collection) {
fields = (await this.itemsService.readByQuery({
filter: { collection: { _eq: collection } },
})) as System[];
})) as FieldMeta[];
} else {
fields = (await this.itemsService.readByQuery({})) as System[];
fields = (await this.itemsService.readByQuery({})) as FieldMeta[];
}
fields = (await this.payloadService.processValues('read', fields)) as System[];
fields = (await this.payloadService.processValues('read', fields)) as FieldMeta[];
let columns = await schemaInspector.columnInfo(collection);
@@ -74,27 +74,27 @@ export default class FieldsService {
collection: column.table,
field: column.name,
type: column ? getLocalType(column.type) : 'alias',
database: column,
system: field || null,
schema: column,
meta: field || null,
};
return data as Field;
});
let aliasFields = await this.knex
.select<System[]>('*')
.select<FieldMeta[]>('*')
.from('directus_fields')
.whereIn('special', ['alias', 'o2m']);
aliasFields = (await this.payloadService.processValues('read', aliasFields)) as System[];
aliasFields = (await this.payloadService.processValues('read', aliasFields)) as FieldMeta[];
const aliasFieldsAsField = aliasFields.map((field) => {
const data = {
collection: field.collection,
field: field.field,
type: field.special,
database: null,
system: field,
schema: null,
meta: field,
};
return data;
@@ -112,7 +112,7 @@ export default class FieldsService {
.where({ collection, field })
.first();
fieldInfo = (await this.payloadService.processValues('read', fieldInfo)) as System[];
fieldInfo = (await this.payloadService.processValues('read', fieldInfo)) as FieldMeta[];
try {
column = await schemaInspector.columnInfo(collection, field);
@@ -140,7 +140,7 @@ export default class FieldsService {
* Check if table / directus_fields row already exists
*/
if (field.database) {
if (field.schema) {
if (table) {
this.addColumnToTable(table, field as Field);
} else {
@@ -150,9 +150,9 @@ export default class FieldsService {
}
}
if (field.system) {
if (field.meta) {
await this.itemsService.create({
...field.system,
...field.meta,
collection: collection,
field: field.field,
});
@@ -162,16 +162,16 @@ export default class FieldsService {
/** @todo research how to make this happen in SQLite / Redshift */
async updateField(collection: string, field: RawField, accountability?: Accountability) {
if (field.database) {
await database.schema.alterTable(collection, (table) => {
if (field.schema) {
await this.knex.schema.alterTable(collection, (table) => {
let column: ColumnBuilder;
if (!field.database) return;
if (!field.schema) return;
if (field.type === 'string') {
column = table.string(
field.field,
field.database.max_length !== null ? field.database.max_length : undefined
field.schema.max_length !== null ? field.schema.max_length : undefined
);
} else if (['float', 'decimal'].includes(field.type)) {
const type = field.type as 'float' | 'decimal';
@@ -181,14 +181,11 @@ export default class FieldsService {
column = table[field.type](field.field);
}
if (field.database.default_value) {
column.defaultTo(field.database.default_value);
if (field.schema.default_value) {
column.defaultTo(field.schema.default_value);
}
if (
field.database.is_nullable !== undefined &&
field.database.is_nullable === false
) {
if (field.schema.is_nullable !== undefined && field.schema.is_nullable === false) {
column.notNullable();
} else {
column.nullable();
@@ -198,7 +195,7 @@ export default class FieldsService {
});
}
if (field.system) {
if (field.meta) {
const record = await database
.select<{ id: number }>('id')
.from('directus_fields')
@@ -206,7 +203,7 @@ export default class FieldsService {
.first();
if (!record) throw new FieldNotFoundException(collection, field.field);
await database('directus_fields')
.update(field.system)
.update(field.meta)
.where({ collection, field: field.field });
}
@@ -225,10 +222,10 @@ export default class FieldsService {
public addColumnToTable(table: CreateTableBuilder, field: Field) {
let column: ColumnBuilder;
if (field.database?.has_auto_increment) {
if (field.schema?.has_auto_increment) {
column = table.increments(field.field);
} else if (field.type === 'string') {
column = table.string(field.field, field.database?.max_length || undefined);
column = table.string(field.field, field.schema?.max_length || undefined);
} else if (['float', 'decimal'].includes(field.type)) {
const type = field.type as 'float' | 'decimal';
/** @todo add precision and scale support */
@@ -237,17 +234,17 @@ export default class FieldsService {
column = table[field.type](field.field);
}
if (field.database?.default_value) {
column.defaultTo(field.database.default_value);
if (field.schema?.default_value) {
column.defaultTo(field.schema.default_value);
}
if (field.database.is_nullable !== undefined && field.database.is_nullable === false) {
if (field.schema.is_nullable !== undefined && field.schema.is_nullable === false) {
column.notNullable();
} else {
column.nullable();
}
if (field.database?.is_primary_key) {
if (field.schema?.is_primary_key) {
column.primary();
}
}

View File

@@ -3,7 +3,7 @@
* handled correctly.
*/
import { System } from '../types/field';
import { FieldMeta } from '../types/field';
import argon2 from 'argon2';
import { v4 as uuidv4 } from 'uuid';
import database from '../database';
@@ -104,7 +104,7 @@ export default class PayloadService {
const specialFieldsQuery = this.knex
.select('field', 'special')
.from<System>('directus_fields')
.from<FieldMeta>('directus_fields')
.where({ collection: this.collection })
.whereNotNull('special');
@@ -143,7 +143,7 @@ export default class PayloadService {
}
async processField(
field: Pick<System, 'field' | 'special'>,
field: Pick<FieldMeta, 'field' | 'special'>,
payload: Partial<Item>,
operation: Operation
) {

View File

@@ -4,7 +4,7 @@ import { Table } from 'knex-schema-inspector/lib/types/table';
export type Collection = {
collection: string;
fields?: Field[];
system: {
meta: {
collection: string;
note: string | null;
hidden: boolean;
@@ -12,5 +12,5 @@ export type Collection = {
icon: string | null;
translation: Record<string, string>;
} | null;
database: Table;
schema: Table;
};

View File

@@ -17,7 +17,7 @@ export const types = [
'uuid',
] as const;
export type System = {
export type FieldMeta = {
id: number;
collection: string;
field: string;
@@ -40,6 +40,6 @@ export type Field = {
collection: string;
field: string;
type: typeof types[number];
database: Column;
system: System | null;
schema: Column;
meta: FieldMeta | null;
};