Как установить триггер postgresql в knexfile - PullRequest
0 голосов
/ 30 марта 2020

У меня есть группы таблиц с tsvector столбцом fulltext.

Мне нужно обновлять этот столбец и индекс всякий раз, когда запись добавляется или обновляется с помощью такой команды:

UPDATE groups
SET FULLTEXT = to_tsvector('english', coalesce(name, '') || ' ' || coalesce(description, '') || ' ' || coalesce(address, ''));

Я предполагаю, что мне нужно включить что-то в мой knexfile, но я ищу пример.

Вот схема таблицы:

Table "public.groups"                                        |
+----------------------------------------------------------------------------------------------------------------------------------------------+
|      Column     |           Type           | Collation | Nullable |              Default                                                     |
| ----------------+--------------------------+-----------+----------+------------------------------------                                      |
|  id             | integer                  |           | not null | nextval('groups_id_seq'::regclass)                                       |
|  name           | character varying(255)   |           | not null |                                                                          |
|  description    | text                     |           |          |                                                                          |
|  created_at     | timestamp with time zone |           |          |                                                                          |
|  updated_at     | timestamp with time zone |           |          |                                                                          |
|  owners_id      | integer                  |           |          |                                                                          |
|  max_members    | integer                  |           |          | 10                                                                       |
|  private_group  | boolean                  |           |          | false                                                                    |
|  address        | character varying(255)   |           |          |                                                                          |
|  latitude       | real                     |           |          |                                                                          |
|  longitude      | real                     |           |          |                                                                          |
|  points_horizon | integer                  |           |          | 14                                                                       |
|  fulltext       | tsvector                 |           |          |                                                                          |
| Indexes:                                                                                                                                     |
|     "groups_pkey" PRIMARY KEY, btree (id)                                                                                                    |
|     "groups_name_unique" UNIQUE CONSTRAINT, btree (name)                                                                                     |
|     "groups_fulltext_index" gin (fulltext)                                                                                                   |
| Foreign-key constraints:                                                                                                                     |
|     "groups_owners_id_foreign" FOREIGN KEY (owners_id) REFERENCES users(id) ON DELETE CASCADE                                                |
| Referenced by:                                                                                                                               |
|     TABLE "events" CONSTRAINT "events_groups_id_foreign" FOREIGN KEY (groups_id) REFERENCES groups(id) ON DELETE CASCADE                     |
|     TABLE "group_invitations" CONSTRAINT "group_invitations_group_id_foreign" FOREIGN KEY (group_id) REFERENCES groups(id) ON DELETE CASCADE |
|     TABLE "memberships" CONSTRAINT "memberships_groups_id_foreign" FOREIGN KEY (groups_id) REFERENCES groups(id) ON DELETE CASCADE           |
|     TABLE "posts" CONSTRAINT "posts_groups_id_foreign" FOREIGN KEY (groups_id) REFERENCES groups(id) ON DELETE CASCADE                       |
|     TABLE "site_invitations" CONSTRAINT "site_invitations_group_id_foreign" FOREIGN KEY (group_id) REFERENCES groups(id) ON DELETE CASCADE   |
|     TABLE "uploads" CONSTRAINT "uploads_groups_id_foreign" FOREIGN KEY (groups_id) REFERENCES groups(id)                                     |
+----------------------------------------------------------------------------------------------------------------------------------------------+

Вот мой текущий knexfile :

// Update with your config settings.

const dotenv = require('dotenv');
dotenv.config({ path: "./.env" });

module.exports = {

  development: {
    client: 'postgresql',
    connection: { 
      database: process.env.DB_NAME, // update with env var
      user: process.env.DB_USER, // update with env var 
      password: process.env.DB_PASSWORD
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations',
      directory: './db/migrations'
    },
    seeds: {
      directory: './db/seeds'
    }
  },

  production: {
    client: 'postgresql',
    connection: process.env.DATABASE_URL,
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations',
      directory: './db/migrations'
    },
    seeds: {
      directory: './db/seeds'
    },
    ssl: true
  },

  staging: {
    client: 'postgresql',
    connection: process.env.DATABASE_URL,
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations',
      directory: './db/migrations'
    },
    seeds: {
      directory: './db/seeds'
    },
    ssl: true
  }

};
...