UNPKG

@tanglemedia/directus-collection-embed

Version:

Installs the Embed Collection to your Directus project.

120 lines (110 loc) 2.68 kB
const COLLECTION_NAME = "tngl_embed"; const newOptions = { choices: [ { text: "Custom Code", value: "custom", }, { text: "Vimeo", value: "vimeo", }, { text: "Youtube", value: "youtube", }, ], }; const prevOptions = { choices: [ { text: "Custom Code", value: "custom", }, { text: "Youtube", value: "youtube", }, ], }; export async function up(knex) { // add a vimeo id column to the tngl_embed table await knex.schema.alterTable(COLLECTION_NAME, function (table) { table.string("vimeo_id", 255).nullable(); }); // add vimeo as an option await knex("directus_fields") .where("collection", COLLECTION_NAME) .andWhere("field", "type") .update({ options: JSON.stringify(newOptions), display_options: JSON.stringify(newOptions), }); // register fields to directus_fields table await knex("directus_fields").insert([ { collection: COLLECTION_NAME, field: "vimeo", special: "alias,no-data,group", interface: "group-raw", conditions: JSON.stringify([ { name: "visibility", rule: { _and: [{ type: { _neq: "vimeo" } }] }, hidden: true, options: {}, }, ]), }, { collection: COLLECTION_NAME, field: "vimeo_id", special: null, interface: "input", group: "vimeo", }, ]); // sort fields await knex("directus_fields") .where("collection", COLLECTION_NAME) .andWhere("field", "youtube") .update({ sort: 5, }); await knex("directus_fields") .where("collection", COLLECTION_NAME) .andWhere("field", "vimeo") .update({ sort: 6, }); await knex("directus_fields") .where("collection", COLLECTION_NAME) .andWhere("field", "custom") .update({ sort: 7, }); } export async function down(knex) { // remove vimeo fields from directus_fields table await knex("directus_fields") .where("collection", COLLECTION_NAME) .andWhere("field", "vimeo") .del(); // remove vimeo fields from directus_fields table await knex("directus_fields") .where("collection", COLLECTION_NAME) .andWhere("field", "vimeo_id") .del(); // remove vimeo as an option await knex("directus_fields") .where("collection", COLLECTION_NAME) .andWhere("field", "type") .update({ options: JSON.stringify(prevOptions), display_options: JSON.stringify(prevOptions), }); // drop the vimeo_id column await knex.schema.alterTable(COLLECTION_NAME, function (table) { table.dropColumn("vimeo_id"); }); }