@tanglemedia/directus-collection-flex-editor
Version:
Creates the flex table and some basic related nodes like image, resource, embed, and section
64 lines (53 loc) • 1.93 kB
JavaScript
const COLLECTION_NAME = "tngl_flex_content_nodes";
export async function up(knex) {
// if table exists, do not proceed with table creation
if (await knex.schema.hasTable(COLLECTION_NAME)) {
// check if ID field exists
const idExists = await knex.schema.hasColumn(COLLECTION_NAME, "id");
if (!idExists) table.uuid("id").primary().notNullable(); // Primary key
// check if content field exists
const sortExists = await knex.schema.hasColumn(COLLECTION_NAME, "sort");
if (!sortExists) table.integer("sort").nullable(); // Sort field
// check if plain field exists
const itemExists = await knex.schema.hasColumn(COLLECTION_NAME, "item");
if (!itemExists) table.string("item", 255).nullable(); // Item field
// check if plain field exists
const collectionExists = await knex.schema.hasColumn(
COLLECTION_NAME,
"collection"
);
if (!collectionExists) table.string("collection", 255).nullable(); // Collection field
// check tngl_flex_content_id
const flexibleContentIdExists = await knex.schema.hasColumn(
COLLECTION_NAME,
"tngl_flex_content_id"
);
if (!flexibleContentIdExists) {
table.uuid("tngl_flex_content_id").nullable(); // Foreign key
// Foreign key constraint
table
.foreign("tngl_flex_content_id")
.references("id")
.inTable("tngl_flex_content")
.onDelete("CASCADE");
}
return;
}
// if table does not exist, create table
await knex.schema.createTable(COLLECTION_NAME, (table) => {
table.uuid("id").primary().notNullable();
table.integer("sort").nullable();
table.uuid("tngl_flex_content_id").nullable();
table.string("item", 255).nullable();
table.string("collection", 255).nullable();
table
.foreign("tngl_flex_content_id")
.references("id")
.inTable("tngl_flex_content")
.onDelete("CASCADE");
});
}
export async function down(knex) {
// drop table
await knex.schema.dropTableIfExists(COLLECTION_NAME);
}