@tanglemedia/directus-collection-flex-editor
Version:
Creates the flex table and some basic related nodes like image, resource, embed, and section
57 lines (50 loc) • 1.71 kB
JavaScript
const COLLECTION_M2M_FILE = "tngl_flex_content_nodes";
const RELATED_COLLECTION = "tngl_flex_content";
const RELATED_COLLECTION_ID = "tngl_flex_content_id";
const SUBCOLLECTIONS = [
"tngl_node_resource",
"tngl_node_image",
"tngl_node_section",
"tngl_node_embed",
];
export async function up(knex) {
let allowedCollectionsPromises = SUBCOLLECTIONS.map((subCollection) => {
return knex.schema.hasTable(subCollection);
});
allowedCollectionsPromises = await Promise.all(allowedCollectionsPromises);
const finalAllowedCollections = SUBCOLLECTIONS.filter(
(subCollection, index) => allowedCollectionsPromises[index]
);
// register relations between tngl_menu and tngl_menu_item to `directus_relations` table
// for full list of directus_relations properties, refer to: https://docs.directus.io/reference/system/relations.html
await knex("directus_relations").insert([
{
many_collection: COLLECTION_M2M_FILE,
many_field: RELATED_COLLECTION_ID,
one_collection: RELATED_COLLECTION,
one_field: "editor_nodes",
one_collection_field: null,
one_allowed_collections: null,
junction_field: "item",
sort_field: "sort",
one_deselect_action: "delete",
},
{
many_collection: COLLECTION_M2M_FILE,
many_field: "item",
one_collection: null,
one_field: null,
one_collection_field: "collection",
one_allowed_collections: [...finalAllowedCollections].join(","),
junction_field: RELATED_COLLECTION_ID,
sort_field: null,
one_deselect_action: "nullify",
},
]);
}
export async function down(knex) {
//remove associated data from directus_relations table
await knex("directus_relations")
.where("many_collection", COLLECTION_M2M_FILE)
.del();
}