@tanglemedia/directus-collection-flex-editor
Version:
Creates the flex table and some basic related nodes like image, resource, embed, and section
66 lines (57 loc) • 2 kB
JavaScript
const FLEX_CONTENT_FIELD = "tngl_flx_cntnt";
const COLLECTION_NAME = "tngl_flex_content";
let flexCollection = process.env.FLEX_COLLECTION;
if (!flexCollection) {
flexCollection = [];
} else {
flexCollection = flexCollection.split(",");
}
// add flex content to some basic tables aside from the ones specified in the FLEX_COLLECTION
let basicFlexCollection =
"tngl_article,tngl_event,tngl_job,tngl_people,tngl_page";
basicFlexCollection = basicFlexCollection.split(",");
flexCollection = [...flexCollection, ...basicFlexCollection];
flexCollection = [...new Set(flexCollection)];
export async function up(knex) {
// check if the tables in the FLEX_COLLECTION exist
let flexCollectionExistsPromises = flexCollection.map((collection) => {
return knex.schema.hasTable(collection);
});
flexCollectionExistsPromises = await Promise.all(
flexCollectionExistsPromises
);
const finalFlexCollection = flexCollection.filter(
(collection, index) => flexCollectionExistsPromises[index]
);
// check if the tables in the FLEX_COLLECTION has the tngl_flex_content field
let flexCollectionFieldExistsPromises = finalFlexCollection.map(
(collection) => {
return knex.schema.hasColumn(collection, FLEX_CONTENT_FIELD);
}
);
flexCollectionFieldExistsPromises = await Promise.all(
flexCollectionFieldExistsPromises
);
// add tngl_flex_content editor to the collections in the FLEX_COLLECTION
let flexCollectionUpdatePromises = finalFlexCollection.forEach(
async (collection, index) => {
return await knex.transaction(async (trcx) => {
if (!flexCollectionFieldExistsPromises[index]) {
await knex.schema
.alterTable(collection, (table) => {
table.uuid(FLEX_CONTENT_FIELD).nullable();
table
.foreign(FLEX_CONTENT_FIELD)
.references("id")
.inTable(COLLECTION_NAME)
.onDelete("SET NULL");
})
.then(trcx.commit);
} else {
Promise.resolve();
}
});
}
);
}
export async function down(knex) {}