UNPKG

@tanglemedia/directus-collection-flex-editor

Version:

Creates the flex table and some basic related nodes like image, resource, embed, and section

55 lines (47 loc) 1.71 kB
const COLLECTION_NAME = "tngl_flex_content_nodes"; const SUBCOLLECTIONS = [ "tngl_node_resource", "tngl_node_image", "tngl_node_section", "tngl_node_embed", ]; export async function up(knex) { // if row exists in directus_collections table, do not proceed with migration const collectionExists = await knex("directus_collections") .select("collection") .where("collection", COLLECTION_NAME) .first(); // register table into `directus_collections` table // for full list of directus_collections properties, refer to: https://docs.directus.io/reference/system/collections.html if (collectionExists) { // prepare the item duplication fields based on the subcomponent collections let allowedCollectionsPromises = SUBCOLLECTIONS.map((subCollection) => { return knex.schema.hasTable(subCollection); }); allowedCollectionsPromises = await Promise.all(allowedCollectionsPromises); const finalAllowedCollections = SUBCOLLECTIONS.filter( (subCollection, index) => allowedCollectionsPromises[index] ); const itemDuplicationFields = finalAllowedCollections.map( (subCollection) => { return `item:${subCollection}.id`; } ); // update the directus_collections table await knex("directus_collections") .where({ collection: COLLECTION_NAME }) .update({ item_duplication_fields: JSON.stringify(itemDuplicationFields), }) .then((res) => console.log("result", res)) .catch((err) => console.error(err)); } } export async function down(knex) { // reset the item_duplication_fields fields of the directus_collections table await knex("directus_collections") .where("collection", COLLECTION_NAME) .update({ item_duplication_fields: null, }); }