@tanglemedia/directus-collection-modal
Version:
Installs the Modal Collection to your Directus project.
213 lines (201 loc) • 4.95 kB
JavaScript
const MODAL_TABLE_NAME = "tngl_modal";
const MODAL_COLLECTION_INSERTS = [
{
collection: MODAL_TABLE_NAME,
icon: "web_asset",
note: null,
display_template: null,
hidden: true,
singleton: true,
translations: null,
archive_field: null,
archive_app_filter: true,
archive_value: null,
unarchive_value: null,
sort_field: null,
accountability: "all",
color: null,
item_duplication_fields: null,
sort: null,
group: null,
collapse: "open",
preview_url: null,
versioning: false,
},
];
const MODAL_FIELDS_INSERTS = [
{
collection: MODAL_TABLE_NAME,
field: "id",
special: null,
interface: "input",
options: null,
display: null,
display_options: null,
readonly: true,
hidden: true,
sort: 1,
width: "full",
translations: null,
note: null,
conditions: null,
required: false,
group: null,
validation: null,
validation_message: null,
},
{
collection: MODAL_TABLE_NAME,
field: "enable",
special: ["cast-boolean"],
interface: "boolean",
options: null,
display: null,
display_options: null,
readonly: false,
hidden: false,
sort: 2,
width: "full",
translations: null,
note: null,
conditions: null,
required: false,
group: null,
validation: null,
validation_message: null,
},
{
collection: MODAL_TABLE_NAME,
field: "heading",
special: null,
interface: "input",
options: null,
display: null,
display_options: null,
readonly: false,
hidden: false,
sort: 3,
width: "full",
translations: null,
note: null,
conditions: null,
required: false,
group: null,
validation: null,
validation_message: null,
},
{
collection: MODAL_TABLE_NAME,
field: "content",
special: null,
interface: "input-rich-text-html",
options: {
toolbar: [
"bold",
"italic",
"underline",
"h1",
"h2",
"h3",
"numlist",
"bullist",
"removeformat",
"customLink",
"hr",
"code",
"fullscreen",
],
},
display: null,
display_options: null,
readonly: false,
hidden: false,
sort: 4,
width: "full",
translations: null,
note: null,
conditions: null,
required: false,
group: null,
validation: null,
validation_message: null,
},
{
collection: MODAL_TABLE_NAME,
field: "image",
special: ["file"],
interface: "file-image",
options: {
crop: false,
},
display: null,
display_options: null,
readonly: false,
hidden: false,
sort: 5,
width: "full",
translations: null,
note: null,
conditions: null,
required: false,
group: null,
validation: null,
validation_message: null,
},
];
const MODAL_RELATIONS_INSERTS = [
{
junction_field: null,
many_collection: MODAL_TABLE_NAME,
many_field: "image",
one_allowed_collections: null,
one_collection: "directus_files",
one_collection_field: null,
one_deselect_action: "nullify",
one_field: null,
sort_field: null,
},
];
export async function up(knex) {
// if modal table exists, do not proceed with migration
if (await knex.schema.hasTable(MODAL_TABLE_NAME)) {
return;
}
// create modal table
await knex.schema.createTable(MODAL_TABLE_NAME, (table) => {
table.increments("id").primary().unsigned().notNullable();
table.string("heading", 255).nullable().defaultTo(null);
table.text("content").nullable().defaultTo(null);
table.specificType("image", "char(36)").nullable().defaultTo(null);
table.boolean("enable").nullable().defaultTo(0);
// Define foreign key for `image` referencing `directus_files.id`
table
.foreign("image")
.references("id")
.inTable("directus_files")
.onDelete("SET NULL"); // FOREIGN KEY constraint with ON DELETE SET NULL
});
// register modal table into `directus_collections` table
// for full list of directus_collections properties, refer to: https://docs.directus.io/reference/system/collections.html
await knex("directus_collections").insert(MODAL_COLLECTION_INSERTS);
// register modal table fields into `directus_fields` table
// for full list of directus_fields properties, refer to: https://docs.directus.io/reference/system/fields.html
await knex("directus_fields").insert(MODAL_FIELDS_INSERTS);
// register relations between modal and image 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(MODAL_RELATIONS_INSERTS);
}
export async function down(knex) {
// remove associated data from directus_relations table
await knex("directus_relations")
.where("many_collection", MODAL_TABLE_NAME)
.del();
// remove associated data from directus_fields table
await knex("directus_fields").where("collection", MODAL_TABLE_NAME).del();
// remove associated data from directus_collections table
await knex("directus_collections")
.where("collection", MODAL_TABLE_NAME)
.del();
// drop table
await knex.schema.dropTableIfExists(MODAL_TABLE_NAME);
}