Custom indexes in the schema
planned
D
Derrickmehaffy
Basically the ability to define indexes in a standard way via the schema file (and maybe in the future via the CTB)
module.exports = {
collectionName: 'user',
info: {
singularName: 'user',
pluralName: 'users',
displayName: 'User',
},
options: {},
pluginOptions: {
},
attributes: {
fullName: {
type: 'string',
min: 1,
required: true,
},
},
// experimental feature:
indexes: [
{
name: 'upload_folders_path_id_index', // this should be optional
columns: ['full_name'], // This should be the name of the attribute not the db column name
type: 'unique',
},
],
};
Stan
It seems the feature already supported but not documented in v4.
Commented as // experimental feature here
https://github.com/strapi/strapi/blob/f8af92b375dc730ba47ed2117f25df893aae696c/packages/core/upload/server/content-types/folder/schema.js#L57
I tried with my schema.json in my content type and it worked. Maybe the full feature would allow to add custom indexes on other content types via plugins or merge schema.
Overall the current experimental solution looks good to me and recalls Magento 2 Declarative Schema approach that is quite handy in my opinion https://developer.adobe.com/commerce/php/development/components/declarative-schema/configuration/.
Maybe we can just document and ship it as it is?
R
Reenan Campos
We also need this feature in strapi to proceed with the creation of index customs, in order to optimize strapi. It was marked as "planned" in March 2023, is there any estimate of when it will arrive?
Hasan BAŞUSTA
const { Kusta } = require("strapi_kusta/ks");
async function DbIndexed() {
const knex = strapi.db.connection.schema;
const contentTypes = strapi.contentTypes;
const liste = [];
for (const uid of Object.keys(contentTypes)) {
const ct = contentTypes[uid];
const collectionName = ct.collectionName;
const attributes = ct.attributes;
const colums = [];
for (const columName of Object.keys(attributes)) {
// userTatilde bunu user_tatilde yap
const newColumName = columName.replace(/([A-Z])/g, "_$1").toLowerCase();
const attribute = attributes[columName];
if (attribute.indexed) {
console.log("Indexed: ", collectionName, newColumName);
colums.push(newColumName);
}
}
liste.push({ collectionName, colums });
}
const newListe = liste.filter((item) => item.colums.length);
for (const shem of newListe) {
const colums = shem.colums;
for (const colum of colums)
{
await strapi.db.connection.context.raw(
CREATE INDEX IF NOT EXISTS \
${colum}\ ON \
${shem.collectionName}\ (\
${colum}\);
);
}
}
}
module.exports = DbIndexed;
Robin Louis Van Komen
Is there any timeline for this?
We really need the feature, as we are currently manually maintaining our indexes, and they are overridden every time we do any schema changes.
Hasan BAŞUSTA
Robin Louis Van Komen: I added the feature to the top
Derrick Mehaffy
planned
Hasan BAŞUSTA
Derrick Mehaffy: Do you know when it will arrive?