-function addMethodsToModel (model: any, classMethods: Function[], instanceMethods: Function[] = []) {
- classMethods.forEach(m => model[m.name] = m)
- instanceMethods.forEach(m => model.prototype[m.name] = m)
+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'
+ }
+}
+
+function createSimilarityAttribute (col: string, value: string) {
+ return Sequelize.fn(
+ 'similarity',
+
+ searchTrigramNormalizeCol(col),
+
+ searchTrigramNormalizeValue(value)
+ )