$ open source · GPL-3.0 — need a commercial license? imqueue.com →

BaseModel.syncIndex() method

Builds and runs the statements that create one column index.

Signature:

static syncIndex(column: string, options: ColumnIndexOptions, position: number): Promise<any>;

Parameters

Parameter

Type

Description

column

string

Column the index is declared on.

options

ColumnIndexOptions

The declared index options.

position

number

Ordinal used to name the index when name is not given, which is what keeps two indices on one column from colliding.

Returns:

Promise<any>

Resolves once the index exists.

Remarks

Each ColumnIndexOptions field maps to a clause of the CREATE INDEX. The index is dropped first unless safe is set, so the declaration in the code always wins over what is in the database; with safe, an existing index is left exactly as it is and only a missing one is created.

Five of those options used to be placed where Postgres does not accept them — USING ahead of ON, and COLLATE, the operator class, the sort order and the nulls position after the closing parenthesis rather than inside it — so method, collation, opClass, order and nullsFirst could not be used at all. include was declared and never emitted. Both are fixed, and the reason neither was noticed is the promise bug below: the statement failed, and the rejection went nowhere.

The two statements used to be attached to the same already-resolved promise rather than chained onto each other, so they were issued together and this returned before either had run — the create could reach the server ahead of the drop, and a failure of either became an unhandled rejection rather than rejecting here. They now run in order, and the returned promise waits for them.