Skip to content

feat: add @Index, createIndexDecorator - #15482

Merged
ephys merged 1 commit into
mainfrom
ephys/index-decorator
Dec 19, 2022
Merged

ephys merged 1 commit into
mainfrom
ephys/index-decorator

Conversation

@ephys

@ephys ephys commented Dec 19, 2022

Copy link
Copy Markdown
Member

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • If a documentation update is necessary, have you opened a PR to the documentation repository?
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Does the name of your PR follow our conventions?

Description Of Change

This PR adds @Index & createIndexDecorator from sequelize-typescript (with some tweaks)

It also fixes sequelize/sequelize-typescript#725 in the new implementation

It also extends the capabilities of @Index to the "regular" way of defining attributes

@Index

Makes it possible to define either a complicated non-composite index, or a simple composite index:

class User extends Model {
  // This declares a very simple index on firstName
  // Equivalent to setting { index: true } when using User.init
  @Index
  declare firstName: string;

  // This declares a complex index on lastName
  @Index({
    concurrently: true,
    // the options specific to this column in the index
    attribute: {
      order: 'ASC',
    },
  })
  declare lastName: string;

  // This declares a simple composite index on country & city
  // Equivalent to setting { index: 'country-city' } when using User.init
  @Index('country-city')
  declare country: string;
  
  @Index('country-city')
  declare city: string;
}

You can also declare complex composite indexes using this decorator, but createIndexDecorator makes that much easier as you don't need to repeat the shared options on each attribute.

createIndexDecorator

Makes it easy to declare complex composite indexes:

// creates a composite index called "my_composite_index"
const MyCompositeIndex = createIndexDecorator('MyCompositeIndex', {
  concurrently: true,
});

class User extends Model {
  // Adds firstName & lastName to the composite index

  @MyCompositeIndex
  declare firstName: string;

  // This configures options specific to this column in the index
  @MyCompositeIndex({
    order: 'ASC',
  })
  declare lastName: string;
}

@ephys
ephys merged commit ab9aaba into main Dec 19, 2022
@ephys
ephys deleted the ephys/index-decorator branch December 19, 2022 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

createIndexDecorator has problems with "underscored" or "field" params

2 participants