diff --git a/lib/pg.js b/lib/pg.js index 2af7051..ab0d9c4 100644 --- a/lib/pg.js +++ b/lib/pg.js @@ -48,6 +48,20 @@ const createIndex = (entityName, indexName, def, entity) => { const fieldNames = []; const fieldList = unique || index; const indexFields = typeof fieldList === 'boolean' ? [indexName] : fieldList; + const indexMethods = ['btree', 'hash', 'gin', 'gist', 'spgist', 'brin']; + const maxMethodLength = indexMethods.reduce( + (prev, curr) => (prev.length > curr.length ? prev : curr), + '', + ).length; + const idxName = `"${prefix}${entityName}${toUpperCamel(indexName)}"`; + if (typeof indexFields === 'string') { + const hasMethod = indexMethods.some((method) => + indexFields.substring(0, maxMethodLength).includes(method), + ); + if (hasMethod) + return `CREATE ${uni}INDEX ${idxName} ON "${entityName}" USING ${indexFields};`; + return `CREATE ${uni}INDEX ${idxName} ON "${entityName}" ${indexFields};`; + } fieldNames.push(...indexFields); const names = []; for (const name of fieldNames) { @@ -56,7 +70,6 @@ const createIndex = (entityName, indexName, def, entity) => { names.push(isFirstUpper(field.type) ? name + 'Id' : name); } const fields = '("' + names.join('", "') + '")'; - const idxName = `"${prefix}${entityName}${toUpperCamel(indexName)}"`; return `CREATE ${uni}INDEX ${idxName} ON "${entityName}" ${fields};`; };