不要怂,就是干,撸起袖子干!

Commit 8662fc8b by Erik Seliger Committed by Simon Schick

fix(typings): use correct set of allowed options for addIndex (#10885)

1 parent 15354c7b
...@@ -16,7 +16,7 @@ import { ValidationOptions } from './instance-validator'; ...@@ -16,7 +16,7 @@ import { ValidationOptions } from './instance-validator';
import { ModelManager } from './model-manager'; import { ModelManager } from './model-manager';
import Op = require('./operators'); import Op = require('./operators');
import { Promise } from './promise'; import { Promise } from './promise';
import { QueryOptions } from './query-interface'; import { QueryOptions, IndexesOptions } from './query-interface';
import { Config, Options, Sequelize, SyncOptions } from './sequelize'; import { Config, Options, Sequelize, SyncOptions } from './sequelize';
import { Transaction } from './transaction'; import { Transaction } from './transaction';
import { Col, Fn, Literal, Where } from './utils'; import { Col, Fn, Literal, Where } from './utils';
...@@ -1134,55 +1134,7 @@ export interface ModelValidateOptions { ...@@ -1134,55 +1134,7 @@ export interface ModelValidateOptions {
/** /**
* Interface for indexes property in InitOptions * Interface for indexes property in InitOptions
*/ */
export interface ModelIndexesOptions { export type ModelIndexesOptions = IndexesOptions
/**
* The name of the index. Defaults to model name + _ + fields concatenated
*/
name?: string;
/**
* Index type. Only used by mysql. One of `UNIQUE`, `FULLTEXT` and `SPATIAL`
*/
index?: string;
/**
* The method to create the index by (`USING` statement in SQL). BTREE and HASH are supported by mysql and
* postgres, and postgres additionally supports GIST and GIN.
*/
method?: string;
/**
* Should the index by unique? Can also be triggered by setting type to `UNIQUE`
*
* @default false
*/
unique?: boolean;
/**
* PostgreSQL will build the index without taking any write locks. Postgres only
*
* @default false
*/
concurrently?: boolean;
/**
* An array of the fields to index. Each field can either be a string containing the name of the field,
* a sequelize object (e.g `sequelize.fn`), or an object with the following attributes: `attribute`
* (field name), `length` (create a prefix index of length chars), `order` (the direction the column
* should be sorted in), `collate` (the collation (sort order) for the column)
*/
fields?: (string | { attribute: string; length: number; order: string; collate: string })[];
/**
* Type of search index. Postgres only
*/
using?: string;
/**
* Index operator type. Postgres only
*/
operator?: string;
}
/** /**
* Interface for name property in InitOptions * Interface for name property in InitOptions
......
...@@ -123,19 +123,69 @@ export interface QueryInterfaceDropAllTablesOptions extends QueryInterfaceOption ...@@ -123,19 +123,69 @@ export interface QueryInterfaceDropAllTablesOptions extends QueryInterfaceOption
skip?: string[]; skip?: string[];
} }
export interface QueryInterfaceIndexOptions extends QueryInterfaceOptions { export type IndexType = 'UNIQUE' | 'FULLTEXT' | 'SPATIAL';
type?: 'UNIQUE' | 'FULLTEXT' | 'SPATIAL'; export type IndexMethod = 'BTREE' | 'HASH' | 'GIST' | 'SPGIST' | 'GIN' | 'BRIN' | string;
/** The name of the index. Default is __ */ export interface IndexesOptions {
/**
* The name of the index. Defaults to model name + _ + fields concatenated
*/
name?: string; name?: string;
/** For FULLTEXT columns set your parser */ /** For FULLTEXT columns set your parser */
parser?: string; parser?: string | null;
/**
* Index type. Only used by mysql. One of `UNIQUE`, `FULLTEXT` and `SPATIAL`
*/
type?: IndexType;
/**
* Should the index by unique? Can also be triggered by setting type to `UNIQUE`
*
* @default false
*/
unique?: boolean;
/**
* PostgreSQL will build the index without taking any write locks. Postgres only
*
* @default false
*/
concurrently?: boolean;
/** Set a type for the index, e.g. BTREE. See the documentation of the used dialect */ /**
using?: string; * An array of the fields to index. Each field can either be a string containing the name of the field,
* a sequelize object (e.g `sequelize.fn`), or an object with the following attributes: `name`
* (field name), `length` (create a prefix index of length chars), `order` (the direction the column
* should be sorted in), `collate` (the collation (sort order) for the column)
*/
fields?: (string | { name: string; length?: number; order?: 'ASC' | 'DESC'; collate?: string })[];
/**
* The method to create the index by (`USING` statement in SQL). BTREE and HASH are supported by mysql and
* postgres, and postgres additionally supports GIST, SPGIST, BRIN and GIN.
*/
using?: IndexMethod;
/**
* Index operator type. Postgres only
*/
operator?: string;
/**
* Optional where parameter for index. Can be used to limit the index to certain rows.
*/
where?: WhereOptions;
/**
* Prefix to append to the index name.
*/
prefix?: string;
} }
export interface QueryInterfaceIndexOptions extends IndexesOptions, QueryInterfaceOptions {}
export interface AddUniqueConstraintOptions { export interface AddUniqueConstraintOptions {
type: 'unique'; type: 'unique';
name?: string; name?: string;
......
...@@ -61,10 +61,25 @@ User.init( ...@@ -61,10 +61,25 @@ User.init(
return {} return {}
} }
}, },
indexes: [{
fields: ['firstName'],
using: 'BTREE',
name: 'firstNameIdx',
concurrently: true,
}],
sequelize, sequelize,
} }
); );
User.afterSync(() => {
sequelize.getQueryInterface().addIndex(User.tableName, {
fields: ['lastName'],
using: 'BTREE',
name: 'lastNameIdx',
concurrently: true,
})
})
// Hooks // Hooks
User.afterFind((users, options) => { User.afterFind((users, options) => {
console.log('found'); console.log('found');
......
...@@ -126,7 +126,6 @@ queryInterface.addIndex('Person', ['firstname', 'lastname']); ...@@ -126,7 +126,6 @@ queryInterface.addIndex('Person', ['firstname', 'lastname']);
// This example will create a unique index with the name SuperDuperIndex using the optional 'options' field. // This example will create a unique index with the name SuperDuperIndex using the optional 'options' field.
// Possible options: // Possible options:
// - indicesType: UNIQUE|FULLTEXT|SPATIAL
// - indexName: The name of the index. Default is __ // - indexName: The name of the index. Default is __
// - parser: For FULLTEXT columns set your parser // - parser: For FULLTEXT columns set your parser
// - indexType: Set a type for the index, e.g. BTREE. See the documentation of the used dialect // - indexType: Set a type for the index, e.g. BTREE. See the documentation of the used dialect
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!