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

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';
import { ModelManager } from './model-manager';
import Op = require('./operators');
import { Promise } from './promise';
import { QueryOptions } from './query-interface';
import { QueryOptions, IndexesOptions } from './query-interface';
import { Config, Options, Sequelize, SyncOptions } from './sequelize';
import { Transaction } from './transaction';
import { Col, Fn, Literal, Where } from './utils';
......@@ -1134,55 +1134,7 @@ export interface ModelValidateOptions {
/**
* Interface for indexes property in InitOptions
*/
export interface ModelIndexesOptions {
/**
* 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;
}
export type ModelIndexesOptions = IndexesOptions
/**
* Interface for name property in InitOptions
......
......@@ -123,19 +123,69 @@ export interface QueryInterfaceDropAllTablesOptions extends QueryInterfaceOption
skip?: string[];
}
export interface QueryInterfaceIndexOptions extends QueryInterfaceOptions {
type?: 'UNIQUE' | 'FULLTEXT' | 'SPATIAL';
export type IndexType = '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;
/** 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 {
type: 'unique';
name?: string;
......
......@@ -61,10 +61,25 @@ User.init(
return {}
}
},
indexes: [{
fields: ['firstName'],
using: 'BTREE',
name: 'firstNameIdx',
concurrently: true,
}],
sequelize,
}
);
User.afterSync(() => {
sequelize.getQueryInterface().addIndex(User.tableName, {
fields: ['lastName'],
using: 'BTREE',
name: 'lastNameIdx',
concurrently: true,
})
})
// Hooks
User.afterFind((users, options) => {
console.log('found');
......
......@@ -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.
// Possible options:
// - indicesType: UNIQUE|FULLTEXT|SPATIAL
// - indexName: The name of the index. Default is __
// - parser: For FULLTEXT columns set your parser
// - 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!