+ // Alias
+ if (field.toLowerCase() === 'match') field = Sequelize.col('similarity')
+
+ return [ [ field, direction ], lastSort ]
+}
+
+function getSortOnModel (model: any, value: string, lastSort: string[] = [ 'id', 'ASC' ]) {
+ let [ firstSort ] = getSort(value)
+
+ if (model) return [ [ model, firstSort[0], firstSort[1] ], lastSort ]
+ return [ firstSort, lastSort ]
+}
+
+function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value') {
+ if (validator(value) === false) {
+ throw new Error(`"${value}" is not a valid ${fieldName}.`)
+ }
+}
+
+function buildTrigramSearchIndex (indexName: string, attribute: string) {
+ return {
+ name: indexName,
+ fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + '))') as any ],
+ using: 'gin',
+ operator: 'gin_trgm_ops'
+ }