Finish create collection

This commit is contained in:
rijkvanzanten
2020-07-01 12:35:22 -04:00
parent db90fddbcd
commit 2680092dc9
3 changed files with 19 additions and 5 deletions

View File

@@ -1,6 +1,5 @@
import database, { schemaInspector } from '../database';
import * as ItemsService from '../services/items';
import { Table } from '../knex-schema-inspector/lib/types/table';
import { Collection } from '../types/collection';
import { Query } from '../types/query';
import { ColumnBuilder } from 'knex';
@@ -44,18 +43,31 @@ export const create = async (payload: any) => {
translation: payload.translation || null,
});
/** @TODO insert all fields to directus_fields */
/**
* @TODO make this flexible and based on payload
*/
await database('directus_fields').insert(
payload.fields.map((field: any) => ({
collection: payload.collection,
field: field.field,
locked: false,
required: false,
readonly: false,
hidden_detail: false,
hidden_browse: false,
}))
);
return collection;
};
export const readAll = async (query?: Query) => {
const [tables, collections] = await Promise.all([
schemaInspector.tables(),
schemaInspector.tableInfo(),
ItemsService.readItems<Collection>('directus_collections', query),
]);
const data = (tables as Table[]).map((table) => {
const data = tables.map((table) => {
const collectionInfo = collections.find((collection) => {
return collection.collection === table.name;
});
@@ -75,7 +87,7 @@ export const readAll = async (query?: Query) => {
export const readOne = async (collection: string, query?: Query) => {
const [table, collectionInfo] = await Promise.all([
schemaInspector.table(collection),
schemaInspector.tableInfo(collection),
ItemsService.readItem<Collection>('directus_collections', collection, query),
]);

0
src/services/fields.ts Normal file
View File

View File

@@ -1,5 +1,7 @@
import database from '../database';
/** @TODO replace this with schema inspector */
export const hasCollection = async (collection: string) => {
return await database.schema.hasTable(collection);
};